Sorl-thumbnail will not delete thumbnails

Problems with SORL Thumbnail and deleting thumbnail files or updating thumbnails when overwriting a file. The scenario is that I have a file that is always the same for each record, but can be overwritten. It is necessary that the sketch be recreated when a new file is uploaded and the old file is overwritten.

This is at the model + form level, so I use the low level API to generate thumbs.

Tried to use:

from sorl.thumbnail import delete

delete(filename)

But without success, the thumbnail will never be deleted or overwritten.

I even tried:

from sorl.thumbnail.images import ImageFile
from sorl.thumbnail import default

image_file = ImageFile(filename)
default.kvstore.delete_thumbnails(image_file)

Again without success.

Please, help!

Update:

I found work by creating an alternative ThumbnailBackend and a new _get_thumbnail_filename method. The new method uses the SHA-1 hash file to always have a thumbnail specific to the current file.

, .

class HashThumbnailBackend(ThumbnailBackend):

  def _get_thumbnail_filename(self, source, geometry_string, options):
    """
    Computes the destination filename.
    """
    import hashlib

    # hash object
    hash = hashlib.sha1()

    # open file and read it in as chunks to save memory
    f = source.storage.open(u'%s' % source, 'rb')
    while True:
      chunk = f.read(128)
      if not chunk:
        break
      hash.update(hashlib.sha1(chunk).hexdigest())

    # close file
    f.close()

    hash.update(geometry_string)
    hash.update(serialize(options))
    key = hash.hexdigest()

    # make some subdirs
    path = '%s/%s/%s' % (key[:2], key[2:4], key)
    return '%s%s.%s' % (settings.THUMBNAIL_PREFIX, path,
                        self.extensions[options['format']])
+3
4

, . , , X. - , KV .

| Command | Original | Thumbnails | KV Original | KV Thumbnails |
| #1      | X        | X          | X           | X             |
| #2      |          | X          |             | X             |
| #3      |          | X          | X           | X             |
  • sorl.thumbnail.delete(filename)
  • sorl.thumbnail.default.kvstore.delete_thumbnails(image_file)
  • sorl.thumbnail.delete(filename, delete_file=False)

, # 3. ... , filename MEDIA_ROOT ( , ). , , , , , ImageFields FileFields , , django 1.2.5, . .

: , , , , , , - .

+16

, , , .

FileField, :

material = models.FileField(upload_to='materials')

get_thumbnail() , FileField vs python . :

thumb = get_thumbnail(modelinstance.material, '%dx%d' % (thumb_width, thumb_height))

, , , sorl . !

sorl FileField. python FileField, , , ? :

sorl.thumbnail.delete(modelinstance.material.file)

:

sorl.thumbnail.delete(modelinstance.material)

, KV Store -, . !

: http://sorl-thumbnail.readthedocs.org/en/latest/operation.html

, . /manage.py . /manage.py Django . Django ( memcached). :

import os

# Set the DJANGO_SETTINGS_MODULE environment variable.
os.environ['DJANGO_SETTINGS_MODULE'] = "yourproject.settings"

from django.core.cache import cache

# Flush!
cache._cache.flush_all()

SO. , -:)

+6

, delete(file) File , get_thumbnail() {% thumbnail ...%} templatetag.

, ImageFile, , (ImageFile.key) delete(), , .

, , Python File, , , Django File, Django, FileField ( ) File, .

, :

{% load thumbnail %}
{% thumbnail image "100" as im %}
<img src="{{ im.url }}" width="{{ im.width }}" height="{{ im.height }}">
{% endthumbnail %}

image - models.ImageField, File:

{% load thumbnail %}
{% thumbnail image.file "100" as im %}

Python ( Storage , ):

from django.core.files.storage import FileSystemStorage
from django.core.files import File
from sorl.thumbnail import delete


class OverwriteStorage(FileSystemStorage):
    def _save(self, name, content):
        if self.exists(name):
            img = File(open(os.path.join(self.location, name), "w"))
            delete(img)
        return super(OverwriteStorage, self)._save(name, content)

, sorl .

+1

. , Sorl .

:

sorl.thumbnail.get_thumbnail(self.picture.url, geometry_string, **options)
# picture being a FieldFile

( ) :

sorl.thumbnail.delete(self.picture.name, delete_files=False)

URL , . Sorl , KV Store FS .

The fix is ​​to simply change the get_thumbnailname argument to self.picture.name.

+1
source

Source: https://habr.com/ru/post/1793724/


All Articles