Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Overriding filename generation #204

Closed
artscoop opened this Issue · 12 comments

3 participants

@artscoop

Hi,
Is there a (quite) simple way to override thumbnail file names ?
I prefer using a hash as a suffix instead of a string containing every thumbnail option.

I've taken a look at get_thumbnail_name but don't really know what to do from there.

@volksman

Hello! I am new to easy_thumbnails, as a convert from imagekit. So far easy-thumbnails is just that. However I have one major problem. I wrote my own processor to apply a watermark. In order to do this I pass a path to the watermark itself to the processor. That now ends up as part of the filename and in fact breaks things into sub directories due to / being in the file name.

I really don't want to expose the absolute path to a private file to the general public through an image filename. I should be able to pass a filename or use a setting to set a convention for file naming.

SmileyChris, any thoughts or plans to expand this side of the app?

@fdemmer fdemmer referenced this issue from a commit in fdemmer/easy-thumbnails
@fdemmer fdemmer + add THUMBNAIL_FILENAME_PATTERN (#204) 136c4e9
@fdemmer

i'd like to propose this patch to allow more fine grained customization of the filename. please review if the changes break naming of the path, i am not 100% sure i understood everything in there...

@fdemmer fdemmer referenced this issue from a commit in fdemmer/easy-thumbnails
@fdemmer fdemmer + readd all_opts key (#204) 3e67173
@artscoop

That is some neat thing to start with, thanks. But !
It would be perfect in this case to have a setting pointing to a function which processes the options string. (for example, you could have a function that hashes the string, another that removes some characters and so on)
Because my problem is that the options string can become very long.

@fdemmer

You could use a pattern with just the resolution: "%(name)s_%(size_x)sx%(size_y)s.%(extension)s"
(That's what i wanted, so i made the changes...)

I don't see the point in hashing the information to make the name shorter. When it's hashed you cannot read it, it's basically lost. So, why not leave it out from the start?

Also I would not want to have a function in my settings.

However you could probably do that pretty easily based on my patch. Just replace line 363 of files.py with this:

filename = settings.THUMBNAIL_FILENAME_FUNC(filename_parts)

... and set THUMBNAIL_FILENAME_FUNC in settings to patch in your hashing function returning a filename (without path).

@artscoop

The problem with this pattern is that you can have different presets with the same size.
Hashing the opts is useful because the options string in the filename can be very long (40 chars for example). In my case I wanted to have a hash of the options string because a filename like

http://www.domain.sub/media/.../filename.jpg_200x200_q85_crop-smart_radius-30_filter-noise_tilt-15.png

is very long to me. Whereas a name like

http://www.domain.sub/media/.../filename.jpg_a75f2438.png

or

http://www.domain.sub/media/.../filename.jpg_85x85_a75f2438.png

is shorter, has no need to be readable, but can be retrieved easily with the use of a hash function (or any function);

The idea is not to have a function in the settings file, but something in the form

THUMBNAIL_FILENAME_FUNCTION = "app.module.function"

with a sane default to a function with the original behaviour.

@fdemmer

ah, ok, i have not thought about same size, different filters and good point about the settings.
another idea would be using the "alias" in the name to have a readable short name for all the settings.

might be i can come back to this and implement it. for now i have to work on other stuff though...

@artscoop

Oh yes, the alias idea is good ! Even better !

@volksman

I'm not sure why it can't just be

original_filename__filter_name.ext

Doesn't that work? Each spec/filter has a unique name.

@artscoop

What do you call "filter" here ?
Is this an alias or a processor name ?

@volksman

We have this type of info in our settings:

THUMBNAIL_ALIASES = {
'': {
'admin_thumb': {'size': (150, 150), 'crop': True},
},
'listing_photos.Photo': {
'thumbnail': {'size': (252,167), 'crop': 'smart'},
'listing_gallery_thumbnail': {'size': (160,106), 'crop': 'smart'},
'listing_list_thumbnail': {'size': (600,400), 'crop': 'smart'},
'display': {'size': (850,None), 'crop': 'scale'},
'lander_thumbnail': {'size': (300,203), 'crop': 'smart'},
},

Why not just use that to name the files? Like

myimage_display.jpg

or with the app name:

myimage_listing_photos_display.jpg

OR using the app name as a dir:

CACHE/listing_photos/myimage_display.jpg

I actually prefer the last one personally. But anything is better than what it currently does.

@artscoop

Oh sorry, yes, that was what were were talking about ultimately, using aliases. Your last proposal is good too.

@fdemmer

that's nice, thanks for the patch!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.