diff options
| author | jfriedli | 2020-03-27 10:59:16 -0700 |
|---|---|---|
| committer | jvoisin | 2020-03-27 10:59:16 -0700 |
| commit | 7104b2107db592756f4090ba772869eb86d6a44f (patch) | |
| tree | 4864a5c07d3f47f5952ef7164507881918ce77e5 /main.py | |
| parent | d50f68ae446df413fc6459be9933f373e9f7cdda (diff) | |
file removal background jobv0.2.0
Diffstat (limited to '')
| -rw-r--r-- | main.py | 20 |
1 files changed, 14 insertions, 6 deletions
| @@ -10,6 +10,7 @@ import zipfile | |||
| 10 | 10 | ||
| 11 | from cerberus import Validator | 11 | from cerberus import Validator |
| 12 | import utils | 12 | import utils |
| 13 | import file_removal_scheduler | ||
| 13 | from libmat2 import parser_factory | 14 | from libmat2 import parser_factory |
| 14 | from flask import Flask, flash, request, redirect, url_for, render_template, send_from_directory, after_this_request | 15 | from flask import Flask, flash, request, redirect, url_for, render_template, send_from_directory, after_this_request |
| 15 | from flask_restful import Resource, Api, reqparse, abort | 16 | from flask_restful import Resource, Api, reqparse, abort |
| @@ -25,31 +26,36 @@ def create_app(test_config=None): | |||
| 25 | app.config['UPLOAD_FOLDER'] = './uploads/' | 26 | app.config['UPLOAD_FOLDER'] = './uploads/' |
| 26 | app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024 # 16MB | 27 | app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024 # 16MB |
| 27 | app.config['CUSTOM_TEMPLATES_DIR'] = 'custom_templates' | 28 | app.config['CUSTOM_TEMPLATES_DIR'] = 'custom_templates' |
| 28 | app.config.from_object('config') # optionally load settings from config.py | 29 | # optionally load settings from config.py |
| 30 | app.config.from_object('config') | ||
| 31 | |||
| 32 | if test_config is not None: | ||
| 33 | app.config.update(test_config) | ||
| 29 | 34 | ||
| 30 | app.jinja_loader = jinja2.ChoiceLoader([ # type: ignore | 35 | app.jinja_loader = jinja2.ChoiceLoader([ # type: ignore |
| 31 | jinja2.FileSystemLoader(app.config['CUSTOM_TEMPLATES_DIR']), | 36 | jinja2.FileSystemLoader(app.config['CUSTOM_TEMPLATES_DIR']), |
| 32 | app.jinja_loader, | 37 | app.jinja_loader, |
| 33 | ]) | 38 | ]) |
| 34 | 39 | ||
| 35 | api = Api(app) | 40 | api = Api(app) |
| 36 | CORS(app, resources={r"/api/*": {"origins": utils.get_allow_origin_header_value()}}) | 41 | CORS(app, resources={r"/api/*": {"origins": utils.get_allow_origin_header_value()}}) |
| 37 | 42 | ||
| 38 | @app.route('/download/<string:key>/<string:filename>') | 43 | @app.route('/download/<string:key>/<string:filename>') |
| 39 | def download_file(key: str, filename:str): | 44 | def download_file(key: str, filename: str): |
| 40 | if filename != secure_filename(filename): | 45 | if filename != secure_filename(filename): |
| 41 | return redirect(url_for('upload_file')) | 46 | return redirect(url_for('upload_file')) |
| 42 | 47 | ||
| 43 | complete_path, filepath = get_file_paths(filename) | 48 | complete_path, filepath = get_file_paths(filename) |
| 49 | file_removal_scheduler.run_file_removal_job(app.config['UPLOAD_FOLDER']) | ||
| 44 | 50 | ||
| 45 | if not os.path.exists(complete_path): | 51 | if not os.path.exists(complete_path): |
| 46 | return redirect(url_for('upload_file')) | 52 | return redirect(url_for('upload_file')) |
| 47 | if hmac.compare_digest(utils.hash_file(complete_path), key) is False: | 53 | if hmac.compare_digest(utils.hash_file(complete_path), key) is False: |
| 48 | return redirect(url_for('upload_file')) | 54 | return redirect(url_for('upload_file')) |
| 49 | |||
| 50 | @after_this_request | 55 | @after_this_request |
| 51 | def remove_file(response): | 56 | def remove_file(response): |
| 52 | os.remove(complete_path) | 57 | if os.path.exists(complete_path): |
| 58 | os.remove(complete_path) | ||
| 53 | return response | 59 | return response |
| 54 | return send_from_directory(app.config['UPLOAD_FOLDER'], filepath, as_attachment=True) | 60 | return send_from_directory(app.config['UPLOAD_FOLDER'], filepath, as_attachment=True) |
| 55 | 61 | ||
| @@ -176,9 +182,11 @@ def create_app(test_config=None): | |||
| 176 | complete_path, filepath = is_valid_api_download_file(filename, key) | 182 | complete_path, filepath = is_valid_api_download_file(filename, key) |
| 177 | # Make sure the file is NOT deleted on HEAD requests | 183 | # Make sure the file is NOT deleted on HEAD requests |
| 178 | if request.method == 'GET': | 184 | if request.method == 'GET': |
| 185 | file_removal_scheduler.run_file_removal_job(app.config['UPLOAD_FOLDER']) | ||
| 179 | @after_this_request | 186 | @after_this_request |
| 180 | def remove_file(response): | 187 | def remove_file(response): |
| 181 | os.remove(complete_path) | 188 | if os.path.exists(complete_path): |
| 189 | os.remove(complete_path) | ||
| 182 | return response | 190 | return response |
| 183 | 191 | ||
| 184 | return send_from_directory(app.config['UPLOAD_FOLDER'], filepath, as_attachment=True) | 192 | return send_from_directory(app.config['UPLOAD_FOLDER'], filepath, as_attachment=True) |
