Skip to content

meringue.thumbnail.storage ¤

get_default_storage_params ¤

get_default_storage_params() -> (
    tuple[type[Storage], dict[str, Any]]
)

Returns the default storage class and its options (a copy, safe to change).

Source code in meringue/thumbnail/storage.py
def get_default_storage_params() -> tuple[type[Storage], dict[str, Any]]:
    """
    Returns the default storage class and its options (a copy, safe to change).
    """

    if django.VERSION < (4, 2):
        return import_string(settings.DEFAULT_FILE_STORAGE), {}

    from django.conf import DEFAULT_STORAGE_ALIAS  # noqa: PLC0415

    # `settings.STORAGES` and not `storages.backends`: on Django 4.2 the latter drops `OPTIONS`
    # when `STORAGES` is changed with `override_settings`.
    params = settings.STORAGES[DEFAULT_STORAGE_ALIAS]
    return import_string(params["BACKEND"]), dict(params.get("OPTIONS", {}))

get_thumbnail_subdir ¤

get_thumbnail_subdir() -> str

Thumbnail directory relative to the media root (THUMBNAIL_DIR relative to MEDIA_ROOT).

Source code in meringue/thumbnail/storage.py
def get_thumbnail_subdir() -> str:
    """
    Thumbnail directory relative to the media root (`THUMBNAIL_DIR` relative to `MEDIA_ROOT`).
    """

    try:
        subdir = Path(m_settings.THUMBNAIL_DIR).relative_to(settings.MEDIA_ROOT)
    except ValueError:
        return DEFAULT_THUMBNAIL_SUBDIR

    return subdir.as_posix().strip("/") or DEFAULT_THUMBNAIL_SUBDIR

get_s3_storage ¤

get_s3_storage(
    storage_cls: type[Storage], options: dict[str, Any]
) -> Storage

S3 storage for thumbnails.

django-storages S3 storages have no base_url option (Invalid setting 'base_url') and do not accept a location with a leading slash, so THUMBNAIL_DIR and THUMBNAIL_URL are not used. Thumbnails are saved to the same bucket in <storage location>/m/thumbnail, and URLs are built by the storage itself (custom_domain, etc.).

Source code in meringue/thumbnail/storage.py
def get_s3_storage(storage_cls: type[Storage], options: dict[str, Any]) -> Storage:
    """
    S3 storage for thumbnails.

    django-storages S3 storages have no `base_url` option (`Invalid setting 'base_url'`) and do
    not accept a location with a leading slash, so `THUMBNAIL_DIR` and `THUMBNAIL_URL` are not
    used. Thumbnails are saved to the same bucket in `<storage location>/m/thumbnail`, and URLs
    are built by the storage itself (`custom_domain`, etc.).
    """

    base_location = options.get("location")

    if base_location is None:
        base_location = storage_cls(**options).location

    parts = (base_location or "", get_thumbnail_subdir())
    options["location"] = "/".join(part.strip("/") for part in parts if part.strip("/"))
    options.pop("base_url", None)
    return storage_cls(**options)

get_storage ¤

get_storage() -> Storage

Finds the default storage class and returns its instance for use with thumbnails.

For django-storages S3 storages the thumbnails are saved to <storage location>/m/thumbnail of the same bucket.

Warning

This method has not been tested with custom stores and complex parameter sets.

Returns:

Source code in meringue/thumbnail/storage.py
def get_storage() -> Storage:
    """
    Finds the default storage class and returns its instance for use with thumbnails.

    For django-storages S3 storages the thumbnails are saved to `<storage location>/m/thumbnail`
    of the same bucket.

    !!! warning
        This method has not been tested with custom stores and complex parameter sets.

    Returns:
        Storage instance.
    """

    storage_cls, options = get_default_storage_params()

    if is_s3_storage_class(storage_cls):
        return get_s3_storage(storage_cls, options)

    if django.VERSION < (4, 2):
        return storage_cls(
            location=m_settings.THUMBNAIL_DIR,
            base_url=m_settings.THUMBNAIL_URL,
        )

    from django.conf import DEFAULT_STORAGE_ALIAS  # noqa: PLC0415
    from django.core.files.storage import storages  # noqa: PLC0415

    params = storages.backends[DEFAULT_STORAGE_ALIAS]
    params["location"] = m_settings.THUMBNAIL_DIR
    params["base_url"] = m_settings.THUMBNAIL_URL
    return storages.create_storage(params)