From 83618ff529dbd8e8b89a51e54a7d81eab7dd823b Mon Sep 17 00:00:00 2001 From: Jan Friedli Date: Fri, 15 May 2020 14:38:13 +0200 Subject: added new property which indicates the remaining download time in secs --- matweb/file_removal_scheduler.py | 4 +++- matweb/rest_api.py | 2 ++ matweb/utils.py | 6 ++++++ 3 files changed, 11 insertions(+), 1 deletion(-) (limited to 'matweb') diff --git a/matweb/file_removal_scheduler.py b/matweb/file_removal_scheduler.py index 2ce7912..59bb9ca 100644 --- a/matweb/file_removal_scheduler.py +++ b/matweb/file_removal_scheduler.py @@ -4,6 +4,8 @@ import sys import os import random +from matweb import utils + def run_file_removal_job(upload_folder_path): if random.randint(0, 10) == 0: @@ -18,7 +20,7 @@ def delete_file_when_too_old(filepath): last_time = time.time() - file_mod_time # if file is older than our configured max timeframe, delete it - if last_time > int(os.environ.get('MAT2_MAX_FILE_AGE_FOR_REMOVAL', 15 * 60)): + if last_time > utils.get_file_removal_max_age_sec(): try: os.remove(filepath) except OSError: diff --git a/matweb/rest_api.py b/matweb/rest_api.py index a07d2d2..f893fca 100644 --- a/matweb/rest_api.py +++ b/matweb/rest_api.py @@ -48,6 +48,7 @@ class APIUpload(Resource): key, secret, meta_after, output_filename = utils.cleanup(parser, filepath, self.upload_folder) return utils.return_file_created_response( + utils.get_file_removal_max_age_sec(), output_filename, mime, key, @@ -133,6 +134,7 @@ class APIBulkDownloadCreator(Resource): abort(500, message='Unable to clean %s' % mime) key, secret, meta_after, output_filename = utils.cleanup(parser, zip_path, self.upload_folder) return { + 'inactive_after_sec': utils.get_file_removal_max_age_sec(), 'output_filename': output_filename, 'mime': mime, 'key': key, diff --git a/matweb/utils.py b/matweb/utils.py index 20c213d..915d735 100644 --- a/matweb/utils.py +++ b/matweb/utils.py @@ -35,6 +35,7 @@ def check_upload_folder(upload_folder): def return_file_created_response( + inactive_after_sec: int, output_filename: str, mime: str, key: str, @@ -44,6 +45,7 @@ def return_file_created_response( download_link: str ) -> dict: return { + 'inactive_after_sec': inactive_after_sec, 'output_filename': output_filename, 'mime': mime, 'key': key, @@ -106,3 +108,7 @@ def is_valid_api_download_file(filename: str, key: str, secret: str, upload_fold if hmac.compare_digest(hash_file(complete_path, secret), key) is False: abort(400, message='The file hash does not match') return complete_path, filepath + + +def get_file_removal_max_age_sec() -> int: + return int(os.environ.get('MAT2_MAX_FILE_AGE_FOR_REMOVAL', 15 * 60)) -- cgit v1.3