From c5f47f09170b107d3c37dbd219bdb8405a26f05a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Thuret?= Date: Mon, 25 Oct 2021 11:46:49 +0200 Subject: [PATCH] add scheduler to remove files after 30 minutes instead of after download --- app/app.py | 14 ++++++++++++-- app/remove_translated_files.py | 26 ++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 2 deletions(-) create mode 100644 app/remove_translated_files.py diff --git a/app/app.py b/app/app.py index 0e53a88..f39ec76 100644 --- a/app/app.py +++ b/app/app.py @@ -14,13 +14,23 @@ from translatehtml import translate_html from werkzeug.utils import secure_filename from app import flood +from app import remove_translated_files from app.language import detect_languages, transliterate from .api_keys import Database from .suggestions import Database as SuggestionsDatabase def get_upload_dir(): - return os.path.join(tempfile.gettempdir(), "libretranslate-files-translate") + upload_dir = os.path.join(tempfile.gettempdir(), "libretranslate-files-translate") + + if not os.path.isdir(upload_dir): + os.mkdir(upload_dir) + + return upload_dir + + +remove_translated_files.setup(get_upload_dir()) + def get_json_dict(request): d = request.get_json() @@ -610,7 +620,7 @@ def create_app(args): return_data.write(fo.read()) return_data.seek(0) - os.remove(filepath) + print(filename) return send_file(return_data, attachment_filename=filename) diff --git a/app/remove_translated_files.py b/app/remove_translated_files.py new file mode 100644 index 0000000..c354b1a --- /dev/null +++ b/app/remove_translated_files.py @@ -0,0 +1,26 @@ +import atexit +import os +import time +from datetime import datetime + +from apscheduler.schedulers.background import BackgroundScheduler + + +def remove_translated_files(upload_dir: str): + now = time.mktime(datetime.now().timetuple()) + + for f in os.listdir(upload_dir): + f = os.path.join(upload_dir, f) + if os.path.isfile(f): + f_time = os.path.getmtime(f) + if (now - f_time) > 1800: # 30 minutes + os.remove(f) + + +def setup(upload_dir): + scheduler = BackgroundScheduler(daemon=True) + scheduler.add_job(remove_translated_files, "interval", minutes=30, kwargs={'upload_dir': upload_dir}) + scheduler.start() + + # Shut down the scheduler when exiting the app + atexit.register(lambda: scheduler.shutdown())