소스 검색

Merge pull request #434 from eupharis/master

Fix bug in Thumbnailer When IMG_PATH has trailing slash
Justin Mayer 10 년 전
부모
커밋
768f8fbd70
2개의 변경된 파일24개의 추가작업 그리고 14개의 파일을 삭제
  1. 6 1
      thumbnailer/test_thumbnails.py
  2. 18 13
      thumbnailer/thumbnailer.py

+ 6 - 1
thumbnailer/test_thumbnails.py

@@ -1,7 +1,7 @@
 from thumbnailer import _resizer
 from unittest import TestCase, main
 import os.path as path
-from PIL import Image, ImageChops
+from PIL import Image
 
 class ThumbnailerTests(TestCase):
 
@@ -47,6 +47,11 @@ class ThumbnailerFilenameTest(TestCase):
         new_name = r.get_thumbnail_name(self.path('sample_image.jpg'))
         self.assertEqual('sample_image_square.jpg', new_name)
 
+    def testRootWithSlash(self):
+        r = _resizer('square', '100', self.img_path + '/')
+        new_name = r.get_thumbnail_name(self.path('sample_image.jpg'))
+        self.assertEqual('sample_image_square.jpg', new_name)
+
     def testSubdir(self):
         """Test a file that is in a sub-directory of img_path."""
 

+ 18 - 13
thumbnailer/thumbnailer.py

@@ -131,8 +131,6 @@ def resize_thumbnails(pelican):
         return
 
     in_path = _image_path(pelican)
-    out_path = path.join(pelican.settings['OUTPUT_PATH'],
-                         pelican.settings.get('THUMBNAIL_DIR', DEFAULT_THUMBNAIL_DIR))
 
     sizes = pelican.settings.get('THUMBNAIL_SIZES', DEFAULT_THUMBNAIL_SIZES)
     resizers = dict((k, _resizer(k, v, in_path)) for k,v in sizes.items())
@@ -142,21 +140,28 @@ def resize_thumbnails(pelican):
             if not filename.startswith('.'):
                 for name, resizer in resizers.items():
                     in_filename = path.join(dirpath, filename)
-                    logger.debug("Processing thumbnail {0}=>{1}".format(filename, name))
-                    if pelican.settings.get('THUMBNAIL_KEEP_NAME', False):
-                        if pelican.settings.get('THUMBNAIL_KEEP_TREE', False):
-                            resizer.resize_file_to(
-                                in_filename, 
-                                path.join(out_path, name, path.dirname(path.relpath(in_filename, in_path))), True)
-                        else:
-                            resizer.resize_file_to(in_filename, path.join(out_path, name), True)
-                    else:
-                        resizer.resize_file_to(in_filename, out_path)
+                    out_path = get_out_path(pelican, in_path, in_filename, name)
+                    resizer.resize_file_to(
+                        in_filename,
+                        out_path, pelican.settings.get('THUMBNAIL_KEEP_NAME'))
+
+
+def get_out_path(pelican, in_path, in_filename, name):
+    base_out_path = path.join(pelican.settings['OUTPUT_PATH'],
+                         pelican.settings.get('THUMBNAIL_DIR', DEFAULT_THUMBNAIL_DIR))
+    logger.debug("Processing thumbnail {0}=>{1}".format(in_filename, name))
+    if pelican.settings.get('THUMBNAIL_KEEP_NAME', False):
+        if pelican.settings.get('THUMBNAIL_KEEP_TREE', False):
+            return path.join(base_out_path, name, path.dirname(path.relpath(in_filename, in_path)))
+        else:
+            return path.join(base_out_path, name)
+    else:
+        return base_out_path
 
 
 def _image_path(pelican):
     return path.join(pelican.settings['PATH'],
-        pelican.settings.get("IMAGE_PATH", DEFAULT_IMAGE_DIR))
+        pelican.settings.get("IMAGE_PATH", DEFAULT_IMAGE_DIR)).rstrip('/')
 
 
 def expand_gallery(generator, metadata):