diff --git a/.gitignore b/.gitignore index 5db59f5..f85a7d2 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ __pycache__ config.toml +venv/ diff --git a/src/__init__.py b/src/__init__.py index 0346241..f19ee53 100644 --- a/src/__init__.py +++ b/src/__init__.py @@ -1,11 +1,11 @@ #!/usr/bin/env python3 import os -import tomllib -from flask import Flask, render_template, send_file -with open("config.toml", "rb") as f: - config = tomllib.load(f) +from flask import Flask, render_template, send_file, request + +from . import file +from . import config app = Flask( __name__, @@ -18,13 +18,11 @@ app.jinja_env.globals.update( path_join=os.path.join, ) -# Catch all - @app.route('/', defaults={'path': ''}) @app.route('/') def index(path): - internal_path = os.path.join(config['base_path'], path) + internal_path = os.path.join(config.get('base_path'), path) path = '/' + path # Checks ################################################### @@ -61,3 +59,29 @@ def index(path): # Path exists, not a folder, must be a file, send return send_file(internal_path) + + +@app.route('/search') +def search(): + q = request.args.get('q', '') + + if q == '': + return ( + render_template( + "error.html", + code=400, + msg='No search string provided.', + ), + 400, + ) + + ret = file.search(q) + + return ( + render_template( + "search.html", + list=ret, + q=q, + ), + 200, + ) diff --git a/src/config.py b/src/config.py new file mode 100644 index 0000000..8d4b981 --- /dev/null +++ b/src/config.py @@ -0,0 +1,8 @@ +import tomllib + +with open("config.toml", "rb") as f: + config = tomllib.load(f) + + +def get(q: str): + return config.get(q, None) diff --git a/src/file.py b/src/file.py new file mode 100644 index 0000000..11c7a4a --- /dev/null +++ b/src/file.py @@ -0,0 +1,46 @@ +import os +import threading + +from unidecode import unidecode + +from . import config + +filelist = None +filelist_lock = threading.Lock() + + +def filter_strings(q, lst, processor): + q = unidecode(q).lower().split() + + ret = [] + for entry in lst: + processed_entry = unidecode(processor(entry)).lower() + + if all(word in processed_entry for word in q): + ret.append(entry) + + return ret + + +def search(q: str): + + with filelist_lock: + global filelist + if filelist is None: + filelist = [] + for root, dirs, files in os.walk(config.get('base_path'), topdown=True): + # We want the root relative to the base path + rel_root = root[len(config.get('base_path')):] + + # Don't search search hidden stuff + [dirs.remove(x) for x in list(dirs) if x.startswith('.')] + [files.remove(x) for x in list(files) if x.startswith('.')] + + filelist += map(lambda x: [rel_root, x], dirs + files) + + copy_filelist = filelist + + ret = filter_strings(q, copy_filelist, lambda x: x[1]) + + return ret + return [x[0] for x in ret] diff --git a/static/style/main.css b/static/style/main.css index 5c11865..dfae75e 100644 --- a/static/style/main.css +++ b/static/style/main.css @@ -8,10 +8,16 @@ a { text-decoration: none; color: lightgrey; } - a:visited { - color: grey; - } a:hover { color: #191919; background-color: white; } + +input { + border: 1px solid black; + background-color: white; +} + +.base_path { + color: grey; +} diff --git a/templates/base.html b/templates/base.html index a536865..6d5b5d6 100644 --- a/templates/base.html +++ b/templates/base.html @@ -6,13 +6,13 @@ - {% block title %}{% endblock %} - {{ config["name"] }} + {% block title %}{% endblock %} - {{ config.get("name") }} - + {# #} diff --git a/templates/directory.html b/templates/directory.html index a085587..fc7f28a 100644 --- a/templates/directory.html +++ b/templates/directory.html @@ -4,21 +4,26 @@ {% block body %} -{% if path != "/" %} -<-- -

-{% endif %} +
+ + +
-{% for dir in dirs %} -{{ dir }}
-{% endfor %} + {% if path != "/" %} + <-- +

+ {% endif %} -

+ {% for dir in dirs %} + {{ dir }}
+ {% endfor %} -

Archivos

+

-{% for file in files %} -{{ file }}
-{% endfor %} +

Archivos

+ + {% for file in files %} + {{ file }}
+ {% endfor %} {% endblock %} diff --git a/templates/search.html b/templates/search.html new file mode 100644 index 0000000..f65b67b --- /dev/null +++ b/templates/search.html @@ -0,0 +1,21 @@ +{% extends 'base.html' %} + +{% block title %}{{ path }}{% endblock %} + +{% block body %} + +
+ + +
+ + {% if path != "/" %} + <-- +

+ {% endif %} + + {% for entry in list %} + {{ entry[0] }}/{{ entry[1] }}
+ {% endfor %} + +{% endblock %}