diff --git a/README.md b/README.md index a2dd7d5..697f602 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,6 @@ Note: I switched to Gunicorn at some point because Bjoern was somehow annoying. Dependencies: -```passlib argon2_cffi flask gunicorn flask-paranoid``` +```passlib argon2_cffi flask gunicorn flask-paranoid apscheduler``` This application makes use of the `secrets` module (a cryptographically strong version of `random`) from the standard library, which is only available in Python 3.6+. If you really can't be bothered use the latest version of python3, or just don't want cryptographically strong random character filenames/prefixes for some reason, you can directly replace all instances of `secrets` with `random`. diff --git a/fileHost.py b/fileHost.py index fc98741..2d3b3d6 100755 --- a/fileHost.py +++ b/fileHost.py @@ -17,6 +17,7 @@ from flask import Flask, session, request, abort, redirect, url_for, g, \ render_template from werkzeug.utils import secure_filename from flask_paranoid import Paranoid +from apscheduler.schedulers.background import BackgroundScheduler class ReverseProxied(object): """ @@ -53,22 +54,6 @@ class ReverseProxied(object): return self.app(environ, start_response) -class CronThread(threading.Thread): - def __init__(self): - threading.Thread.__init__(self) - self.stop = threading.Event() - - def run(self): - while not self.stop.is_set(): - records = db_execute( - "SELECT filename, delete_date FROM uploads WHERE delete_date" - ).fetchall() - for filename, delete_date in records: - if time.time() >= delete_date: - delete_file(filename) - self.stop.wait(60) - - app = Flask(__name__) app.wsgi_app = ReverseProxied(app.wsgi_app) app.config['MAX_CONTENT_LENGTH'] = 128 * 1024 * 1024 @@ -80,6 +65,9 @@ app.config["DB_LOCK"] = threading.Lock() paranoid = Paranoid(app) paranoid.redirect_view = 'login' +scheduler = BackgroundScheduler(timezone="America/New_York") +scheduler.start() + def db_execute(*args, **kwargs): """ @@ -94,6 +82,19 @@ def db_execute(*args, **kwargs): return res +@scheduler.scheduled_job("interval", minutes=1) +def delete_this(): + """ + Removes files that are past the expiration date. + """ + records = db_execute( + "SELECT filename, delete_date FROM uploads WHERE delete_date" + ).fetchall() + for filename, delete_date in records: + if time.time() >= delete_date: + delete_file(filename) + + def init(): """ Initializes the application. @@ -127,11 +128,6 @@ def init(): "uploaded_date INTEGER DEFAULT (STRFTIME('%s', 'now'))," "delete_date INTEGER)") - # init cron thread - t = CronThread() - t.start() - app.config["CRON_THREAD"] = t - def add_user(username, password, admin="FALSE"): """ @@ -429,7 +425,7 @@ def get_rand_chars(n): init() -atexit.register(app.config["CRON_THREAD"].stop.set) +atexit.register(scheduler.shutdown) if __name__ == "__main__": import sys if len(sys.argv) > 1: