Commits

George Notaras committed 8e952b1

Early fix for the filename extension issue when thumbnails are different format than the source

Comments (0)

Files changed (3)

src/thumbnail_works/fields.py

         if self._options_are_valid(options):
             self.options = self._get_options(options)
         self.name = self._get_name(name)
+        self.ext = self._get_filename_extension_from_format()
         self.width, self.height = get_width_height_from_string(options['size'])
         self.url = make_thumbnail_path(source.url, self.name)
     
     def _get_name(self, name):
         return name.replace(' ', '_')
     
+    def _get_filename_extension_from_format(self):
+        """Returns an extension according to the format.
+        
+        """
+        ext = self.options['format'].lower()
+        if ext == 'jpeg':
+            return 'jpg'
+        return ext
+    
     def _get_options(self, options):
         """This method ensures that all the available options are set to a
         value.
           
         class MyModel(models.Model):
             photo = EnhancedImageField(
-                process_source = '512x384',
+                process_source = dict(size='512x384'),
                 thumbnails = {
                     'avatar': dict(size='80x60', sharpen=True),
                     'large': dict(size='200x150'),
                 setattr(self, thumb_name, thumb_spec)
     
     def process_image(self, content, options):
+        pass
         
     
     def save(self, name, content, save=True):
             thumb_spec = ThumbnailSpec('dummy', source_img_opts, self)
             # This also saves the source image using the THUMBNAILS_FORMAT
             processed_content = process_content_as_image(content, thumb_spec.options)
-        elif settings.THUMBNAILS_FORCE_SOURCE_FORMAT:
-            source_img_opts = dict(format=settings.THUMBNAILS_FORCE_SOURCE_FORMAT)
-            thumb_spec = ThumbnailSpec('dummy', source_img_opts, self)
-            processed_content = process_content_as_image(content, thumb_spec.options)
         else:
             processed_content = content
         
+        # TODO: set correct extension according to the format
+        
         super(EnhancedImageFieldFile, self).save(source_filename, processed_content, save)
         
         # self.name has been re-set in the save() above
         # Generate thumbnails
         if self.field.thumbnails:
             for thumb_name, thumb_options in self.field.thumbnails.items():
-                #new_content = copy.deepcopy(content)
-                print "Doing: ", thumb_name, thumb_size
-                
                 thumb_spec = ThumbnailSpec(thumb_name, thumb_options, self)
-                
-                processed_content = process_content_as_image(
-                    content, thumb_spec.options)
-                thumb_path = make_thumbnail_path(source_path, thumb_name)
-                print thumb_path
+                processed_content = process_content_as_image(content, thumb_spec.options)
+                thumb_path = make_thumbnail_path(source_path, thumb_name, force_ext=thumb_spec.ext)
                 thumb_path_saved = self.storage.save(thumb_path, processed_content)
                 
                 assert thumb_path == thumb_path_saved, 'The calculated \
             
             class MyModel(models.Model):
                 photo = EnhancedImageField(
-                    process_source = '512x384',
+                    process_source = dict(size='512x384'),
                     thumbnails = {
                         'avatar': dict(size='80x60', sharpen=True),
                         'large': dict(size='200x150'),

src/thumbnail_works/settings.py

 from django.conf import settings
 
 
-# If THUMBNAILS_FORCE_FORMAT is set to True, then the uploaded image is 
-THUMBNAILS_FORCE_SOURCE_FORMAT = getattr(settings, 'DTW_THUMBNAILS_FORCE_SOURCE_FORMAT', None)
-
 # JPEG, PNG
 THUMBNAILS_FORMAT = getattr(settings, 'DTW_THUMBNAILS_FORMAT', 'JPEG')
 
 
 
 # This is the name of the directory where the thumbnails will be stored
-THUMBNAILS_DIRNAME = getattr(settings, 'DTW_THUMBNAILS_DIRNAME', '')
+THUMBNAILS_DIRNAME = getattr(settings, 'DTW_THUMBNAILS_DIRNAME', 'thumbs')
 
+THUMBNAILS_DELAYED_GENERATION = getattr(settings, 'DTW_THUMBNAILS_DELAYED_GENERATION', True)

src/thumbnail_works/utils.py

     return size_x, size_y
 
 
-def make_thumbnail_path(source_path, thumbnail_name):
+def make_thumbnail_path(source_path, thumbnail_name, force_ext=None):
     """
     THUMBNAILS_DIRNAME
     For urls and filesystem paths
     root_dir = os.path.dirname(source_path)  # /media/images
     filename = os.path.basename(source_path)
     base_filename, ext = os.path.splitext(filename)
-    return os.path.join(root_dir, '%s.%s.%s' % (base_filename, thumbnail_name, ext))
+    if force_ext:
+        ext = force_ext
+    thumb_filename = '%s.%s%s' % (base_filename, thumbnail_name, ext)
+    if settings.THUMBNAILS_DIRNAME:
+        return os.path.join(root_dir, settings.THUMBNAILS_DIRNAME, thumb_filename)
+    return os.path.join(root_dir, '%s.%s%s' % (base_filename, thumbnail_name, ext))
 
 
 def process_content_as_image(content, options):