Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add management command to delete based on THUMBNAIL_CACHE_TIMEOUT #724

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 30 additions & 2 deletions sorl/thumbnail/kvstores/base.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from django.core.files.storage import FileSystemStorage

from sorl.thumbnail.conf import settings
from sorl.thumbnail.helpers import serialize, deserialize, ThumbnailError
from sorl.thumbnail.images import serialize_image_file, deserialize_image_file
Expand Down Expand Up @@ -93,8 +95,22 @@ def cleanup(self):
Cleans up the key value store. In detail:
1. Deletes all key store references for image_files that do not exist
and all key references for its thumbnails *and* their image_files.
2. Deletes or updates all invalid thumbnail keys
2. Deletes or updates all invalid thumbnail keys.
"""
self._cleanup()

def cleanup_and_delete_if_created_time_before_dt(self, dt):
"""
Cleans up the key value store. In detail:
1. Deletes all key store references for image_files that:
- do not exist, or
- created time before ``dt``
and all key references for its thumbnails *and* their image_files.
2. Deletes or updates all invalid thumbnail keys.
"""
self._cleanup(delete_if_created_time_before_dt=dt)

def _cleanup(self, delete_if_created_time_before_dt=None):
for key in self._find_keys(identity='image'):
image_file = self._get(key)

Expand All @@ -113,8 +129,20 @@ def cleanup(self):
thumbnail_keys_set = set(thumbnail_keys)

for thumbnail_key in thumbnail_keys:
if not self._get(thumbnail_key):
thumbnail = self._get(thumbnail_key)
if not thumbnail:
thumbnail_keys_set.remove(thumbnail_key)
else:
if delete_if_created_time_before_dt is not None:
try:
created_time = thumbnail.storage.get_created_time(thumbnail.name)
except NotImplementedError:
pass
else:
if created_time < delete_if_created_time_before_dt:
thumbnail_keys_set.remove(thumbnail_key)
self.delete(thumbnail, False)
thumbnail.delete() # delete the actual file

thumbnail_keys = list(thumbnail_keys_set)

Expand Down
27 changes: 26 additions & 1 deletion sorl/thumbnail/management/commands/thumbnail.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
from datetime import timedelta

from django.core.management.base import BaseCommand
from django.utils import timezone

from sorl.thumbnail import default
from sorl.thumbnail.conf import settings
from sorl.thumbnail.images import delete_all_thumbnails


VALID_LABELS = ['cleanup', 'clear', 'clear_delete_referenced', 'clear_delete_all']
VALID_LABELS = [
'cleanup',
'cleanup_delete_timeout',
'clear',
'clear_delete_referenced',
'clear_delete_all',
]


class Command(BaseCommand):
Expand All @@ -31,6 +41,21 @@ def handle(self, *labels, **options):

return

if label == 'cleanup_delete_timeout':
if verbosity >= 1:
self.stdout.write(
"Cleanup thumbnails and delete if created time before THUMBNAIL_CACHE_TIMEOUT seconds ago",
ending=' ... '
)

thumbnail_cache_timeout_dt = timezone.now() - timedelta(seconds=settings.THUMBNAIL_CACHE_TIMEOUT)
default.kvstore.cleanup_and_delete_if_created_time_before_dt(thumbnail_cache_timeout_dt)

if verbosity >= 1:
self.stdout.write('[Done]')

return

if label == 'clear_delete_referenced':
if verbosity >= 1:
self.stdout.write(
Expand Down