summaryrefslogtreecommitdiff
path: root/matweb/frontend.py
diff options
context:
space:
mode:
authorjfriedli2020-04-23 10:39:35 -0700
committerjfriedli2020-04-23 10:39:35 -0700
commite1bac8b6a7fd857f38b7bcb678398c82baaa8fd5 (patch)
treefa87e526289e455f2f17b86973d08eb6850e721f /matweb/frontend.py
parentd14988fa3fa97f549fb8eaf601cb2c687cdce143 (diff)
Refactoring
Diffstat (limited to '')
-rw-r--r--matweb/frontend.py77
1 files changed, 77 insertions, 0 deletions
diff --git a/matweb/frontend.py b/matweb/frontend.py
new file mode 100644
index 0000000..93432b4
--- /dev/null
+++ b/matweb/frontend.py
@@ -0,0 +1,77 @@
1import hmac
2import os
3
4from flask import Blueprint, render_template, url_for, current_app, after_this_request, send_from_directory, request, \
5 flash
6from werkzeug.utils import secure_filename, redirect
7
8from matweb import file_removal_scheduler, utils
9
10routes = Blueprint('routes', __name__)
11
12
13@routes.route('/info')
14def info():
15 utils.get_supported_extensions()
16 return render_template(
17 'info.html', extensions=utils.get_supported_extensions()
18 )
19
20
21@routes.route('/download/<string:key>/<string:filename>')
22def download_file(key: str, filename: str):
23 if filename != secure_filename(filename):
24 return redirect(url_for('routes.upload_file'))
25
26 complete_path, filepath = utils.get_file_paths(filename, current_app.config['UPLOAD_FOLDER'])
27 file_removal_scheduler.run_file_removal_job(current_app.config['UPLOAD_FOLDER'])
28
29 if not os.path.exists(complete_path):
30 return redirect(url_for('routes.upload_file'))
31 if hmac.compare_digest(utils.hash_file(complete_path), key) is False:
32 return redirect(url_for('routes.upload_file'))
33
34 @after_this_request
35 def remove_file(response):
36 if os.path.exists(complete_path):
37 os.remove(complete_path)
38 return response
39 return send_from_directory(current_app.config['UPLOAD_FOLDER'], filepath, as_attachment=True)
40
41
42@routes.route('/', methods=['GET', 'POST'])
43def upload_file():
44 utils.check_upload_folder(current_app.config['UPLOAD_FOLDER'])
45 mime_types = utils.get_supported_extensions()
46
47 if request.method == 'POST':
48 if 'file' not in request.files: # check if the post request has the file part
49 flash('No file part')
50 return redirect(request.url)
51
52 uploaded_file = request.files['file']
53 if not uploaded_file.filename:
54 flash('No selected file')
55 return redirect(request.url)
56
57 filename, filepath = utils.save_file(uploaded_file, current_app.config['UPLOAD_FOLDER'])
58 parser, mime = utils.get_file_parser(filepath)
59
60 if parser is None:
61 flash('The type %s is not supported' % mime)
62 return redirect(url_for('routes.upload_file'))
63
64 meta = parser.get_meta()
65
66 if parser.remove_all() is not True:
67 flash('Unable to clean %s' % mime)
68 return redirect(url_for('routes.upload_file'))
69
70 key, meta_after, output_filename = utils.cleanup(parser, filepath, current_app.config['UPLOAD_FOLDER'])
71
72 return render_template(
73 'download.html', mimetypes=mime_types, meta=meta, filename=output_filename, meta_after=meta_after, key=key
74 )
75
76 max_file_size = int(current_app.config['MAX_CONTENT_LENGTH'] / 1024 / 1024)
77 return render_template('index.html', max_file_size=max_file_size, mimetypes=mime_types) \ No newline at end of file