Fix cleanup.py

master
HidUser0 2023-01-16 15:36:48 +03:00
parent 8056976568
commit 052dc32de2
Signed by: HidUser0
GPG Key ID: 7E3251A2FA52F006
1 changed files with 11 additions and 6 deletions

View File

@ -1,8 +1,9 @@
from os import chdir, path, listdir, stat, remove, sep
from datetime import timedelta
from time import time
from binascii import unhexlify
from fhost import app
from fhost import app, db, File
chdir(path.dirname(path.abspath(__file__)) + sep + app.config["FHOST_STORAGE_PATH"])
@ -12,13 +13,17 @@ min_days = app.config['MIN_DAYS']
max_days = app.config['MAX_DAYS']
current_file = time()
for file in listdir('.'):
file_stat = stat(file)
for filename in listdir('.'):
file_stat = stat(filename)
age = timedelta(seconds=current_file - file_stat.st_mtime).days
maxage = min_days + (-max_days + min_days) * (file_stat.st_size / max_content_len - 1) ** 3
if age >= maxage:
remove(file)
print('Deleting file: {}, age: {}'.format(file, age))
if True:
file_obj = File.query.filter_by(sha256=unhexlify(filename)).first()
file_obj.removed = True
db.session.add(file_obj)
db.session.commit()
remove(filename)
print('Deleting file: {}, age: {}'.format(filename, age))