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

Delete old thumbnails from the media/cache directory and the database #740

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
11 changes: 11 additions & 0 deletions docs/management.rst
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,17 @@ useful if your Key Value Store has garbage data not dealt with by cleanup or
you're switching Key Value Store backend.


.. _thumbnail-cleanup-delete-timeout:

thumbnail cleanup_delete_timeout
=================================
``python manage.py thumbnail cleanup_delete_timeout``

Deletes thumbnails in the cache, kvstore (database) and storage (filesystem),
if the file's created time is before THUMBNAIL_CLEANUP_DELETE_TIMEOUT seconds ago.
No action will be taken if THUMBNAIL_CLEANUP_DELETE_TIMEOUT is as ``None``


.. _thumbnail-clear-delete-referenced:

thumbnail clear_delete_referenced
Expand Down
9 changes: 9 additions & 0 deletions docs/reference/settings.rst
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,15 @@ at maximum or ``None`` if your caching backend can handle that as infinite.
Only applicable for the Cached DB Key Value Store.


``THUMBNAIL_CLEANUP_DELETE_TIMEOUT``
===========================

- Default: ``3600 * 24 * 365 * 10``

Timeout to deletes thumbnails in the cache, kvstore (database) and storage (filesystem),
based on file's created time. If set as ``None`` then no action will be taken.


``THUMBNAIL_CACHE``
===================

Expand Down
1 change: 1 addition & 0 deletions sorl/thumbnail/conf/defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
# Cache timeout for ``cached_db`` store. You should probably keep this at
# maximum or ``0`` if your caching backend can handle that as infinite.
THUMBNAIL_CACHE_TIMEOUT = 3600 * 24 * 365 * 10 # 10 years
THUMBNAIL_CLEANUP_DELETE_TIMEOUT = 3600 * 24 * 365 * 10 # 10 years

# The cache configuration to use for storing thumbnail data
THUMBNAIL_CACHE = 'default'
Expand Down
30 changes: 28 additions & 2 deletions sorl/thumbnail/kvstores/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,22 @@
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)

Check warning on line 109 in sorl/thumbnail/kvstores/base.py

View check run for this annotation

Codecov / codecov/patch

sorl/thumbnail/kvstores/base.py#L109

Added line #L109 was not covered by tests

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 +127,20 @@
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

Check warning on line 138 in sorl/thumbnail/kvstores/base.py

View check run for this annotation

Codecov / codecov/patch

sorl/thumbnail/kvstores/base.py#L135-L138

Added lines #L135 - L138 were not covered by tests
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

Check warning on line 143 in sorl/thumbnail/kvstores/base.py

View check run for this annotation

Codecov / codecov/patch

sorl/thumbnail/kvstores/base.py#L140-L143

Added lines #L140 - L143 were not covered by tests

thumbnail_keys = list(thumbnail_keys_set)

Expand Down
42 changes: 40 additions & 2 deletions 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 @@ -16,6 +26,7 @@
def add_arguments(self, parser):
parser.add_argument('args', choices=VALID_LABELS, nargs=1)

# flake8: noqa: C901
def handle(self, *labels, **options):
verbosity = int(options.get('verbosity'))
label = labels[0]
Expand All @@ -31,7 +42,34 @@

return

if label == 'clear_delete_referenced':
elif label == 'cleanup_delete_timeout':
if settings.THUMBNAIL_CLEANUP_DELETE_TIMEOUT:
if verbosity >= 1:
self.stdout.write(

Check warning on line 48 in sorl/thumbnail/management/commands/thumbnail.py

View check run for this annotation

Codecov / codecov/patch

sorl/thumbnail/management/commands/thumbnail.py#L46-L48

Added lines #L46 - L48 were not covered by tests
"""
Cleanup thumbnails and delete if created time before
THUMBNAIL_CLEANUP_DELETE_TIMEOUT seconds ago
""",
ending=' ... '
)

thumbnail_cache_timeout_dt = timezone.now() - timedelta(

Check warning on line 56 in sorl/thumbnail/management/commands/thumbnail.py

View check run for this annotation

Codecov / codecov/patch

sorl/thumbnail/management/commands/thumbnail.py#L56

Added line #L56 was not covered by tests
seconds=settings.THUMBNAIL_CLEANUP_DELETE_TIMEOUT
)
default.kvstore.cleanup_and_delete_if_created_time_before_dt(thumbnail_cache_timeout_dt)

Check warning on line 59 in sorl/thumbnail/management/commands/thumbnail.py

View check run for this annotation

Codecov / codecov/patch

sorl/thumbnail/management/commands/thumbnail.py#L59

Added line #L59 was not covered by tests

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

Check warning on line 62 in sorl/thumbnail/management/commands/thumbnail.py

View check run for this annotation

Codecov / codecov/patch

sorl/thumbnail/management/commands/thumbnail.py#L61-L62

Added lines #L61 - L62 were not covered by tests

else:
self.stdout.write(

Check warning on line 65 in sorl/thumbnail/management/commands/thumbnail.py

View check run for this annotation

Codecov / codecov/patch

sorl/thumbnail/management/commands/thumbnail.py#L65

Added line #L65 was not covered by tests
"THUMBNAIL_CLEANUP_DELETE_TIMEOUT is empty. No action taken",
ending=' ... '
)

return

Check warning on line 70 in sorl/thumbnail/management/commands/thumbnail.py

View check run for this annotation

Codecov / codecov/patch

sorl/thumbnail/management/commands/thumbnail.py#L70

Added line #L70 was not covered by tests

elif label == 'clear_delete_referenced':
if verbosity >= 1:
self.stdout.write(
"Delete all thumbnail files referenced in Key Value Store",
Expand Down
Loading