mirror of
https://github.com/LibreTranslate/LibreTranslate.git
synced 2024-11-21 23:41:01 +00:00
add scheduler to remove files after 30 minutes instead of after download
This commit is contained in:
parent
738dba7476
commit
c5f47f0917
2 changed files with 38 additions and 2 deletions
14
app/app.py
14
app/app.py
|
@ -14,13 +14,23 @@ from translatehtml import translate_html
|
||||||
from werkzeug.utils import secure_filename
|
from werkzeug.utils import secure_filename
|
||||||
|
|
||||||
from app import flood
|
from app import flood
|
||||||
|
from app import remove_translated_files
|
||||||
from app.language import detect_languages, transliterate
|
from app.language import detect_languages, transliterate
|
||||||
from .api_keys import Database
|
from .api_keys import Database
|
||||||
from .suggestions import Database as SuggestionsDatabase
|
from .suggestions import Database as SuggestionsDatabase
|
||||||
|
|
||||||
|
|
||||||
def get_upload_dir():
|
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):
|
def get_json_dict(request):
|
||||||
d = request.get_json()
|
d = request.get_json()
|
||||||
|
@ -610,7 +620,7 @@ def create_app(args):
|
||||||
return_data.write(fo.read())
|
return_data.write(fo.read())
|
||||||
return_data.seek(0)
|
return_data.seek(0)
|
||||||
|
|
||||||
os.remove(filepath)
|
print(filename)
|
||||||
|
|
||||||
return send_file(return_data, attachment_filename=filename)
|
return send_file(return_data, attachment_filename=filename)
|
||||||
|
|
||||||
|
|
26
app/remove_translated_files.py
Normal file
26
app/remove_translated_files.py
Normal file
|
@ -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())
|
Loading…
Reference in a new issue