diff options
Diffstat (limited to 'file_removal_scheduler.py')
| -rw-r--r-- | file_removal_scheduler.py | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/file_removal_scheduler.py b/file_removal_scheduler.py new file mode 100644 index 0000000..2ce7912 --- /dev/null +++ b/file_removal_scheduler.py | |||
| @@ -0,0 +1,26 @@ | |||
| 1 | import glob | ||
| 2 | import time | ||
| 3 | import sys | ||
| 4 | import os | ||
| 5 | import random | ||
| 6 | |||
| 7 | |||
| 8 | def run_file_removal_job(upload_folder_path): | ||
| 9 | if random.randint(0, 10) == 0: | ||
| 10 | for file in glob.glob(upload_folder_path + '/*'): | ||
| 11 | delete_file_when_too_old(file) | ||
| 12 | |||
| 13 | |||
| 14 | def delete_file_when_too_old(filepath): | ||
| 15 | file_mod_time = os.stat(filepath).st_mtime | ||
| 16 | |||
| 17 | # time in second since last modification of file | ||
| 18 | last_time = time.time() - file_mod_time | ||
| 19 | |||
| 20 | # if file is older than our configured max timeframe, delete it | ||
| 21 | if last_time > int(os.environ.get('MAT2_MAX_FILE_AGE_FOR_REMOVAL', 15 * 60)): | ||
| 22 | try: | ||
| 23 | os.remove(filepath) | ||
| 24 | except OSError: | ||
| 25 | print('Automatic File Removal failed on file: ' + str(filepath)) | ||
| 26 | sys.exit(1) | ||
