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 #733

Closed
wants to merge 5 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
14 changes: 8 additions & 6 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,18 @@ jobs:
- python-version: '3.8'
target: 'qa'
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3

- name: Start Redis
uses: supercharge/redis-github-action@1.1.0
uses: supercharge/redis-github-action@1.5.0

- name: Install system dependencies
run: sudo apt-get install libgraphicsmagick1-dev graphicsmagick libjpeg62 zlib1g-dev
run: |
sudo apt-get update
sudo apt-get install libgraphicsmagick1-dev graphicsmagick libjpeg62 zlib1g-dev

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}

Expand All @@ -33,7 +35,7 @@ jobs:
echo "::set-output name=dir::$(pip cache dir)"

- name: Cache
uses: actions/cache@v2
uses: actions/cache@v3
with:
path: ${{ steps.pip-cache.outputs.dir }}
key:
Expand All @@ -53,6 +55,6 @@ jobs:
TARGET: ${{ matrix.target }}

- name: Upload coverage
uses: codecov/codecov-action@v1
uses: codecov/codecov-action@v3
with:
name: Python ${{ matrix.python-version }}
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 @@ 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 +127,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
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 @@ class Command(BaseCommand):
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 @@ def handle(self, *labels, **options):

return

if label == 'clear_delete_referenced':
elif label == 'cleanup_delete_timeout' and not settings.THUMBNAIL_CLEANUP_DELETE_TIMEOUT:
self.stdout.write(
"THUMBNAIL_CLEANUP_DELETE_TIMEOUT is empty. No action taken",
ending=' ... '
)
return

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

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

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

return

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