diff --git a/apps/files/cron.py b/apps/files/cron.py index 78aa0878ee0..a76c8b370f9 100644 --- a/apps/files/cron.py +++ b/apps/files/cron.py @@ -1,9 +1,11 @@ +import hashlib import os import shutil import stat import time from django.conf import settings +from django.core.cache import cache import cronjobs import commonware.log @@ -17,8 +19,22 @@ def cleanup_extracted_file(): log.info('Removing extracted files for file viewer.') root = os.path.join(settings.TMP_PATH, 'file_viewer') for path in os.listdir(root): - path = os.path.join(root, path) - age = time.time() - os.stat(path)[stat.ST_ATIME] + full = os.path.join(root, path) + age = time.time() - os.stat(full)[stat.ST_ATIME] if (age) > (60 * 60): - log.info('Removing extracted files: %s, %dsecs old.' % (path, age)) - shutil.rmtree(path) + log.info('Removing extracted files: %s, %dsecs old.' % (full, age)) + shutil.rmtree(full) + # Nuke out the file and diff caches when the file gets removed. + id = os.path.basename(path) + try: + int(id) + except ValueError: + continue + for prefix in ['file-viewer-get-deleted-files', + 'file-viewer-get-files', + 'file-viewer']: + key = hashlib.md5() + key.update(str(id)) + cache.delete('%s:memoize:%s:%s' % (settings.CACHE_PREFIX, + prefix, key.hexdigest())) + log.info('Removing cache file-viewer cache entries for: %s' % id) diff --git a/apps/files/helpers.py b/apps/files/helpers.py index 27bd508cc0c..28b1234f39e 100644 --- a/apps/files/helpers.py +++ b/apps/files/helpers.py @@ -189,12 +189,16 @@ def get_files(self): addon-file. Full of all the useful information you'll need to serve this file, build templates etc. """ + if self._files: + return self._files + if not self.is_extracted(): return {} # In case a cron job comes along and deletes the files # mid tree building. try: - return self._get_files() + self._files = self._get_files() + return self._files except (OSError, IOError): return {} @@ -281,7 +285,7 @@ def __init__(self, left, right): self.key = None def __str__(self): - return '%s:%s' % (self.left, self.right) + return str(self.left) def extract(self): self.left.extract(), self.right.extract()