Skip to content
Snippets Groups Projects
Commit 27fcf5ea authored by felixwelter's avatar felixwelter
Browse files

Add multi file upload and index purging

parent 356bbd5b
No related branches found
No related tags found
No related merge requests found
import glob
import os import os
from pathlib import Path from pathlib import Path
...@@ -11,6 +12,7 @@ app = Flask(__name__) ...@@ -11,6 +12,7 @@ app = Flask(__name__)
SLIDE_DIR = "slides" SLIDE_DIR = "slides"
IMAGE_DIR = "img_cache" IMAGE_DIR = "img_cache"
INDEX_DIR = "index"
Index = TitleFocusSearchIndex Index = TitleFocusSearchIndex
...@@ -18,6 +20,7 @@ Index = TitleFocusSearchIndex ...@@ -18,6 +20,7 @@ Index = TitleFocusSearchIndex
@app.route('/') @app.route('/')
def index(): def index():
ll = os.listdir(SLIDE_DIR) ll = os.listdir(SLIDE_DIR)
ll.sort()
return render_template('index.html', ll=ll) return render_template('index.html', ll=ll)
...@@ -27,15 +30,17 @@ def allowed_file(filename): ...@@ -27,15 +30,17 @@ def allowed_file(filename):
@app.route('/upload', methods=['POST']) @app.route('/upload', methods=['POST'])
def upload(): def upload():
if 'file' in request.files: if 'files' in request.files:
file = request.files['file'] files = request.files.getlist('files')
for i, file in enumerate(files):
print(i, file)
if file.filename != '': if file.filename != '':
if file and allowed_file(file.filename): if file and allowed_file(file.filename):
filename = secure_filename(file.filename) filename = secure_filename(file.filename)
file_path = os.path.join(Path(SLIDE_DIR), filename) file_path = os.path.join(Path(SLIDE_DIR), filename)
file.save(file_path) file.save(file_path)
pdf = pdfplumber.open(file_path) pdf = pdfplumber.open(file_path)
index = Index() index = Index(index_dir=INDEX_DIR)
for i, page in enumerate(pdf.pages): for i, page in enumerate(pdf.pages):
text = page.extract_text() text = page.extract_text()
index.add(str(file_path), i, text, text.split("\n")[0]) # Assumes title in the first line index.add(str(file_path), i, text, text.split("\n")[0]) # Assumes title in the first line
...@@ -49,7 +54,7 @@ def upload(): ...@@ -49,7 +54,7 @@ def upload():
@app.route("/search", methods=['POST']) @app.route("/search", methods=['POST'])
def query(): def query():
try: try:
index = Index() index = Index(index_dir=INDEX_DIR)
query = request.form.get("term") query = request.form.get("term")
context = request.form.get("context") context = request.form.get("context")
result = index.search(query, context) result = index.search(query, context)
...@@ -64,6 +69,15 @@ def query(): ...@@ -64,6 +69,15 @@ def query():
}) })
@app.route("/reset_index", methods=['POST'])
def reset_index():
for folder in [IMAGE_DIR, SLIDE_DIR, INDEX_DIR]:
files = glob.glob(folder + '/*')
for f in files:
os.remove(f)
return redirect('/')
@app.route("/slide/<img_name>") @app.route("/slide/<img_name>")
def slide(img_name): def slide(img_name):
path = os.path.join(IMAGE_DIR, img_name) path = os.path.join(IMAGE_DIR, img_name)
......
...@@ -5,13 +5,18 @@ ...@@ -5,13 +5,18 @@
<body> <body>
<h1>Upload new slide</h1> <h1>Upload new slide</h1>
<form action="upload" enctype="multipart/form-data" method="post"> <form action="upload" enctype="multipart/form-data" method="post">
<input type="file" name="file"> <input type="file" name="files" multiple="">
<input type="submit" value="Upload"> <input type="submit" value="Upload">
</form> </form>
{% for item in ll %}
<ul> <ul>
{% for item in ll %}
<li>{{ item }}</li> <li>{{ item }}</li>
</ul>{% endfor %} {% endfor %}
</ul>
<form action="reset_index" method="post">
<input type="submit" value="Reset Index">
</form>
<h1>Query</h1> <h1>Query</h1>
<form action="search" method="post"> <form action="search" method="post">
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment