From f7d9c0e87ccd8d416ae0c41d492207cc22eea8c4 Mon Sep 17 00:00:00 2001 From: "Dominik V. Salonen" Date: Wed, 23 Dec 2015 23:27:12 +0100 Subject: [PATCH] I think this is production ready now. --- .gitignore | 1 + conf.py.sample | 1 + run.py | 25 ++++++++++++++++++++----- 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index d4c209b..a1304cb 100644 --- a/.gitignore +++ b/.gitignore @@ -4,5 +4,6 @@ conf.py # Data data/* +legacy/* auth_data.json files.db \ No newline at end of file diff --git a/conf.py.sample b/conf.py.sample index ae12ba9..90440bb 100644 --- a/conf.py.sample +++ b/conf.py.sample @@ -21,6 +21,7 @@ config["ALLOWED_EXTENSIONS"] = set(['txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif']) # If this is set to true, old files will be deleted. TIME is how far behind (in seconds) the last accessed time can be before files get deleted config["DELETE_FILES"] = True config["TIME"] = 30 +config["CLEAN_INTERVAL"] = 120 # Site info displayed to the user config["SITE_DATA"] = { diff --git a/run.py b/run.py index 931d769..1d66919 100755 --- a/run.py +++ b/run.py @@ -28,16 +28,24 @@ log = logging.getLogger('werkzeug') log.setLevel(logging.ERROR) +def cleaner_thread(): + # This is horrid + while True: + print_log('Notice', 'Cleaner started') + delete_old() + time.sleep(config["CLEAN_INTERVAL"]) + + def delete_old(): targetTime = time.time() - config["TIME"] old = db.get_old_files(targetTime) for file in old: + print_log('Notice', 'Removing old file "' + file["file"] + '"') try: os.remove(os.path.join(config["UPLOAD_FOLDER"], file["file"])) except Exception: - pass + print_log('Warning', 'Failed to delete old file "' + file["file"] + '"') db.delete_entry(file["file"]) - print_log('Notice', 'Removed old file "' + file["file"] + '"') def auth(key): @@ -56,6 +64,7 @@ def allowed_file(filename): @app.route('/', methods=['GET', 'POST']) def upload_file(): if request.method == 'POST': + print_log('Web', 'New file Relieved') if not auth(request.headers.get('X-Hyozan-Auth')): abort(403) data = dict() @@ -75,7 +84,7 @@ def upload_file(): data["file"] = filename data["url"] = config["DOMAIN"] + "/" + filename - print_log('Main', 'New file processed') + print_log('Main', 'New file processed "' + filename + '"') if request.form["source"] == "web": return redirect(url_for('get_file', filename=filename), code=302) @@ -89,15 +98,21 @@ def upload_file(): @app.route('/', methods=['GET']) def get_file(filename): - print_log('Main', 'Hit "' + filename + '" - ' + time_to_string(time.time())) + print_log('Web', 'Hit "' + filename + '" - ' + time_to_string(time.time())) db.update_file(filename) return send_from_directory(config['UPLOAD_FOLDER'], filename) -delete_old() +@app.route('/share/') +@app.route('/file/') +def serve_legacy(filename): + return send_from_directory('legacy', filename) +cleaner = Thread(target = cleaner_thread, ) +cleaner.start() if __name__ == '__main__': app.run( port=config["PORT"], host=config["HOST"], debug=config["DEBUG"] ) +cleaner.join(timeout=15) \ No newline at end of file