Sorl-thumbnail: Serving Static Files

Created on 20 Sep 2011  ·  3Comments  ·  Source: jazzband/sorl-thumbnail

I can't find any way to thumbnail a static (in STATIC_ROOT) file. It only seems to work on external (http://....) and media (in MEDIA_ROOT) files.

This is very helpful when the output size of a static image is not always the same (be it through user preferences, theming, etc)

Most helpful comment

Here is an example of a patch https://github.com/marcinn/sorl-thumbnail/commit/61557b41169f0656063c557735ef62908a5fcaab

EDIT: sorry, it seems that output from thumbnail.url is valid, but when retrieved from cached.url it is prefixed with /media/ instead of /static/. There is somethin' more broken inside.

EDIT (2): there is a design issue in deserializing storage code - the storage class is loaded and instantiated with defaults instead of using existing instance. In the other words - sorl.thumbnail does not reuse storage instance but creates own (configured differently). As a workaround we can inherit from FileSystemStorage and define custom initializer.

After applying it anyone can easily generate thumbnails for anything.
It is possible to create a custom template tag like this one:

from django.core.files.storage import FileSystemStorage
from django.conf import settings
from django import template
from sorl.thumbnail.templatetags.thumbnail import ThumbnailNode


register = template.Library()

class StaticThumbnailStorage(FileSystemStorage):
    def __init__(self, *args, **kw):
        super(StaticThumbnailStorage, self).__init__(
            *args, location='/path/to/custom/static/'
            base_url=settings.STATIC_URL, **kw)

storage = StaticThumbnaikStorage()

class StaticThumbnailNode(ThumbnailNode):
    def _get_thumbnail(self, file_, geometry, **options):
        options['storage'] = storage
        return super(StaticThumbnailNode, self)._get_thumbnail(
                file_, geometry, **options)


@register.tag
def static_thumbnail(parser, token):
    return StaticThumbnailNode(parser, token)

Please note that settings do not need to be changed.

All 3 comments

You need to use some storage that uses STATIC_ROOT, for example you can use 'django.core.files.storage.FileSystemStorage' and instantiate with location=settings.STATIC_ROOT and base_url=settings.STATIC_URL, please continue on stackoverflow as this is not a bug.

After five years still there is no possibility to generate thumbs from static files without changing the DEFAULT storage, because of https://github.com/mariocesar/sorl-thumbnail/blob/master/sorl/thumbnail/base.py#L100
StackOverflow will not help due to implementation.

Any chances to FIX that? Passing custom storage instance via get_thumbnail() will be enough.

Here is an example of a patch https://github.com/marcinn/sorl-thumbnail/commit/61557b41169f0656063c557735ef62908a5fcaab

EDIT: sorry, it seems that output from thumbnail.url is valid, but when retrieved from cached.url it is prefixed with /media/ instead of /static/. There is somethin' more broken inside.

EDIT (2): there is a design issue in deserializing storage code - the storage class is loaded and instantiated with defaults instead of using existing instance. In the other words - sorl.thumbnail does not reuse storage instance but creates own (configured differently). As a workaround we can inherit from FileSystemStorage and define custom initializer.

After applying it anyone can easily generate thumbnails for anything.
It is possible to create a custom template tag like this one:

from django.core.files.storage import FileSystemStorage
from django.conf import settings
from django import template
from sorl.thumbnail.templatetags.thumbnail import ThumbnailNode


register = template.Library()

class StaticThumbnailStorage(FileSystemStorage):
    def __init__(self, *args, **kw):
        super(StaticThumbnailStorage, self).__init__(
            *args, location='/path/to/custom/static/'
            base_url=settings.STATIC_URL, **kw)

storage = StaticThumbnaikStorage()

class StaticThumbnailNode(ThumbnailNode):
    def _get_thumbnail(self, file_, geometry, **options):
        options['storage'] = storage
        return super(StaticThumbnailNode, self)._get_thumbnail(
                file_, geometry, **options)


@register.tag
def static_thumbnail(parser, token):
    return StaticThumbnailNode(parser, token)

Please note that settings do not need to be changed.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

nuschk picture nuschk  ·  7Comments

Flimm picture Flimm  ·  10Comments

tagmetag picture tagmetag  ·  7Comments

devova picture devova  ·  8Comments

awidgery picture awidgery  ·  5Comments