Skip to content

meringue.thumbnail.images ¤

get_thumbnail_cache ¤

get_thumbnail_cache() -> BaseCache | None

Returns the cache for thumbnail marks and locks or None if it is disabled (see THUMBNAIL_CACHE).

Source code in meringue/thumbnail/images.py
def get_thumbnail_cache() -> BaseCache | None:
    """
    Returns the cache for thumbnail marks and locks or `None` if it is disabled
    (see `THUMBNAIL_CACHE`).
    """

    alias = m_settings.THUMBNAIL_CACHE

    if not alias:
        return None

    return caches[alias]

is_operation_aborted ¤

is_operation_aborted(error: BaseException) -> bool

Checks the S3 OperationAborted error (botocore ClientError) without importing botocore.

Source code in meringue/thumbnail/images.py
def is_operation_aborted(error: BaseException) -> bool:
    """
    Checks the S3 `OperationAborted` error (botocore `ClientError`) without importing botocore.
    """

    return get_client_error_code(error) == "OperationAborted"

ThumbnailImage ¤

ThumbnailImage(
    image_path: Path | ThumbnailSource | Any,
    job_chain: JobChainType,
    out_format: str,
    image: Image,
    storage: Storage | None = None,
)

Thumbnail Image class.

Attributes:

  • image_path –

    Source image: a model FieldFile (any storage, including S3) or an absolute local path.

  • job_chain –

    Thumbnail job chain.

  • out_format –

    Output image format.

  • image –

    Thumbnail PIL image (usually lazy, it is made only when the thumbnail is saved).

  • storage –

    Thumbnail file storage.

Source code in meringue/thumbnail/images.py
def __init__(
    self,
    image_path: Path | ThumbnailSource | Any,
    job_chain: JobChainType,  # str  = "extra@1",
    out_format: str,
    image: Image,
    storage: Storage | None = None,
):
    """
    Attributes:
        image_path: Source image: a model `FieldFile` (any storage, including S3) or an
            absolute local path.
        job_chain: Thumbnail job chain.
        out_format: Output image format.
        image: Thumbnail PIL image (usually lazy, it is made only when the thumbnail is saved).
        storage: Thumbnail file storage.
    """

    self.source = ThumbnailSource.from_value(image_path)
    self.source_image_path = self.source.path
    self.job_chain = job_chain
    self.out_format = out_format
    self.image = image
    self.storage = storage or default_storage
    self._saved = None
    self.locked = False
    """
    The thumbnail is being made or written by another process (the lock in `THUMBNAIL_CACHE`
    is taken or S3 returned `OperationAborted`): the file may not exist yet.
    """

locked instance-attribute ¤

locked = False

The thumbnail is being made or written by another process (the lock in THUMBNAIL_CACHE is taken or S3 returned OperationAborted): the file may not exist yet.

name property ¤

name: Path

Relative path to thumbnail.

We split the path into separate directories so as not to dump everything in one directory, which can be a problem with a large count of images.

Returns:

  • Path –

    Thumbnail path.

thumbnail_hash property ¤

thumbnail_hash: str

Calculates the thumbnail hash based on the job chain and source path.

Returns:

  • str –

    Thumbnail hash.

file_extension property ¤

file_extension: str

Returns the file extension for the thumbnail.

Returns:

  • str –

    Thumbnail file extension.

filename property ¤

filename: str

Returns the file name for the thumbnail.

Returns:

  • str –

    Thumbnail file name.

path property ¤

path: Path

Absolute path to thumbnail.

url property ¤

url: str

Url to thumbnail.

is_supports_alpha property ¤

is_supports_alpha: bool

Returns the alpha channel support flag.

mimetype property ¤

mimetype: str

Return thumbnail mimetype.

Python mimetypes library doesn't know webp =(.

Returns:

  • str –

    Thumbnail mimetype.

cache_key property ¤

cache_key: str

Cache key of the "thumbnail is saved" mark.

lock_key property ¤

lock_key: str

Cache key of the thumbnail generation lock.

saved property ¤

saved: bool

Check saved thumbnail or not.

With THUMBNAIL_CACHE the storage is checked only if there is no mark in the cache.

Returns:

  • bool –

    Saved flag.

save ¤

save(force=False, **kwargs)

Save image to storage.

With THUMBNAIL_CACHE only one process makes a thumbnail: the lock is taken with cache.add, and the others return without making it (the thumbnail URL is the same, but the file may not exist yet, see locked).

Several processes can write the same new thumbnail at the same time. S3 rejects parallel PUT of the same key with OperationAborted: the other process is already writing the same file, so it is logged as a warning.

Attributes:

  • force –

    Save even if the thumbnail is already saved.

  • **kwargs –

    Pillow imamge save params.

Source code in meringue/thumbnail/images.py
def save(self, force=False, **kwargs):
    """
    Save image to storage.

    With `THUMBNAIL_CACHE` only one process makes a thumbnail: the lock is taken with
    `cache.add`, and the others return without making it (the thumbnail URL is the same, but
    the file may not exist yet, see [locked][meringue.thumbnail.images.ThumbnailImage.locked]).

    Several processes can write the same new thumbnail at the same time. S3 rejects parallel
    PUT of the same key with `OperationAborted`: the other process is already writing the same
    file, so it is logged as a warning.

    Attributes:
        force: Save even if the thumbnail is already saved.
        **kwargs: Pillow imamge save params.
    """

    if (not force) and self.saved:
        return

    cache = get_thumbnail_cache()

    lock_timeout = m_settings.THUMBNAIL_CACHE_LOCK_TIMEOUT
    if cache is not None and not cache.add(self.lock_key, True, lock_timeout):
        logger.debug("Thumbnail `%s` is being made by another process", self.name)
        self.locked = True
        return

    try:
        self._save(**kwargs)
    except Exception as error:
        if not is_operation_aborted(error):
            raise

        logger.warning("Thumbnail `%s` is being written by another process", self.name)
        self.locked = True
        return
    finally:
        if cache is not None:
            cache.delete(self.lock_key)

    self._saved = True
    self.mark_saved(cache)