misc: clean old thumbnails (#52528)

This commit is contained in:
Lauréline Guérin 2021-04-09 15:02:30 +02:00
parent d38db142c6
commit d49c9b15bc
No known key found for this signature in database
GPG Key ID: 1FAB9B9B4F93D473
2 changed files with 31 additions and 5 deletions

View File

@ -319,3 +319,21 @@ def test_clean_tempfiles():
pub.clean_tempfiles()
assert os.listdir(dirname) == ['a']
def test_clean_thumbnails():
pub = create_temporary_pub()
pub.clean_thumbnails()
dirname = os.path.join(pub.app_dir, 'thumbs')
if not os.path.exists(dirname):
os.mkdir(dirname)
with open(os.path.join(dirname, 'a'), 'w') as fd:
fd.write('a')
with open(os.path.join(dirname, 'b'), 'w') as fd:
os.utime(fd.fileno(), times=(time.time() - 40 * 86400, time.time() - 40 * 86400))
pub.clean_thumbnails()
assert os.listdir(dirname) == ['a']

View File

@ -619,25 +619,33 @@ class QommonPublisher(Publisher):
# started more than two days ago, probably aborted job
job.remove_self()
def clean_tempfiles(self):
now = time.time()
one_month_ago = now - 30 * 86400
dirname = os.path.join(self.app_dir, 'tempfiles')
def _clean_files(self, limit, dirname):
if not os.path.exists(dirname):
return
for filename in os.listdir(dirname):
if os.stat(os.path.join(dirname, filename))[8] < one_month_ago:
if os.stat(os.path.join(dirname, filename))[8] < limit:
try:
os.unlink(os.path.join(dirname, filename))
except OSError:
pass
def clean_tempfiles(self):
now = time.time()
one_month_ago = now - 30 * 86400
self._clean_files(one_month_ago, os.path.join(self.app_dir, 'tempfiles'))
def clean_thumbnails(self):
now = time.time()
one_month_ago = now - 30 * 86400
self._clean_files(one_month_ago, os.path.join(self.app_dir, 'thumbs'))
@classmethod
def register_cronjobs(cls):
cls.register_cronjob(CronJob(cls.clean_sessions, minutes=range(0, 60, 5), name='clean_sessions'))
cls.register_cronjob(CronJob(cls.clean_nonces, minutes=range(0, 60, 5), name='clean_nonces'))
cls.register_cronjob(CronJob(cls.clean_afterjobs, minutes=[0], name='clean_afterjobs'))
cls.register_cronjob(CronJob(cls.clean_tempfiles, minutes=[0], name='clean_tempfiles'))
cls.register_cronjob(CronJob(cls.clean_thumbnails, minutes=[0], name='clean_thumbnails'))
_initialized = False