33
44from easy_thumbnails .files import Thumbnailer
55
6-
7- # match the source filename using `__` as the seperator. ``opts_and_ext`` is non
8- # greedy so it should match the last occurence of `__`.
9- # in ``ThumbnailerNameMixin.get_thumbnail_name`` we ensure that there is no `__`
10- # in the opts part.
11- RE_ORIGINAL_FILENAME = re .compile (r"^(?P<source_filename>.*)__(?P<opts_and_ext>.*?)$" )
6+ # easy-thumbnails default pattern
7+ # e.g: source.jpg.100x100_q80_crop_upscale.jpg
8+ RE_ORIGINAL_FILENAME = re .compile (
9+ r"^(?P<source_filename>.*?)\.(?P<opts_and_ext>[^.]+\.[^.]+)$"
10+ )
1211
1312
1413def thumbnail_to_original_filename (thumbnail_name ):
@@ -19,69 +18,42 @@ def thumbnail_to_original_filename(thumbnail_name):
1918
2019
2120class ThumbnailerNameMixin :
22- thumbnail_basedir = ''
23- thumbnail_subdir = ''
24- thumbnail_prefix = ''
21+ thumbnail_basedir = ""
22+ thumbnail_subdir = ""
23+ thumbnail_prefix = ""
2524
2625 def get_thumbnail_name (self , thumbnail_options , transparent = False ):
2726 """
28- A version of ``Thumbnailer.get_thumbnail_name`` that produces a
29- reproducible thumbnail name that can be converted back to the original
30- filename. For public files, it uses easy_thumbnails default naming.
27+ Get thumbnail name using easy-thumbnails pattern.
28+ For public files: Uses configurable naming via THUMBNAIL_NAMER
29+ For private files: Uses easy-thumbnails default naming pattern regardless of THUMBNAIL_NAMER
3130 """
3231 is_public = False
3332 if hasattr (self , "thumbnail_storage" ):
3433 is_public = "PrivateFileSystemStorage" not in str (
3534 self .thumbnail_storage .__class__
3635 )
3736
38- if is_public :
39- return super (ThumbnailerNameMixin , self ).get_thumbnail_name (
40- thumbnail_options , transparent
41- )
42-
4337 path , source_filename = os .path .split (self .name )
44- source_extension = os .path .splitext (source_filename )[1 ][1 :].lower ()
45- preserve_extensions = self .thumbnail_preserve_extensions
46- if preserve_extensions is True or source_extension == 'svg' or \
47- isinstance (preserve_extensions , (list , tuple )) and source_extension in preserve_extensions :
48- extension = source_extension
49- elif transparent :
50- extension = self .thumbnail_transparency_extension
51- else :
52- extension = self .thumbnail_extension
53- extension = extension or 'jpg'
54-
55- thumbnail_options = thumbnail_options .copy ()
56- size = tuple (thumbnail_options .pop ('size' ))
57- initial_opts = ['{}x{}' .format (* size )]
58- quality = thumbnail_options .pop ('quality' , self .thumbnail_quality )
59- if extension == 'jpg' :
60- initial_opts .append (f'q{ quality } ' )
61- elif extension == 'svg' :
62- thumbnail_options .pop ('subsampling' , None )
63- thumbnail_options .pop ('upscale' , None )
64-
65- opts = list (thumbnail_options .items ())
66- opts .sort () # Sort the options so the file name is consistent.
67- opts = ['{}' .format (v is not True and f'{ k } -{ v } ' or k )
68- for k , v in opts if v ]
69- all_opts = '_' .join (initial_opts + opts )
70-
71- basedir = self .thumbnail_basedir
72- subdir = self .thumbnail_subdir
73-
74- # make sure our magic delimiter is not used in all_opts
75- all_opts = all_opts .replace ('__' , '_' )
76- filename = f'{ source_filename } __{ all_opts } .{ extension } '
38+ thumbnail_name = super (ThumbnailerNameMixin , self ).get_thumbnail_name (
39+ thumbnail_options , transparent
40+ )
41+ if is_public :
42+ return thumbnail_name
7743
78- return os .path .join (basedir , path , subdir , filename )
44+ base_thumb_name = os .path .basename (thumbnail_name )
45+ return os .path .join (
46+ self .thumbnail_basedir ,
47+ path ,
48+ self .thumbnail_subdir ,
49+ f"{ source_filename } .{ base_thumb_name } " ,
50+ )
7951
8052
8153class ActionThumbnailerMixin :
82- thumbnail_basedir = ''
83- thumbnail_subdir = ''
84- thumbnail_prefix = ''
54+ thumbnail_basedir = ""
55+ thumbnail_subdir = ""
56+ thumbnail_prefix = ""
8557
8658 def get_thumbnail_name (self , thumbnail_options , transparent = False ):
8759 """
@@ -101,7 +73,7 @@ def thumbnail_exists(self, thumbnail_name):
10173
10274class FilerThumbnailer (ThumbnailerNameMixin , Thumbnailer ):
10375 def __init__ (self , * args , ** kwargs ):
104- self .thumbnail_basedir = kwargs .pop (' thumbnail_basedir' , '' )
76+ self .thumbnail_basedir = kwargs .pop (" thumbnail_basedir" , "" )
10577 super ().__init__ (* args , ** kwargs )
10678
10779
0 commit comments