changeset 981:ef1558941bc9

Additional tweaks to ssl_images.
author Brian Neal <bgneal@gmail.com>
date Sat, 24 Oct 2015 21:29:07 -0500
parents 3ebde23a59d0
children 7db9037915c4
files core/download.py core/management/commands/ssl_images.py
diffstat 2 files changed, 19 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/core/download.py	Thu Oct 15 18:35:56 2015 -0500
+++ b/core/download.py	Sat Oct 24 21:29:07 2015 -0500
@@ -5,6 +5,7 @@
 import os
 import shutil
 import tempfile
+from urlparse import urlparse
 
 import requests
 
@@ -38,10 +39,15 @@
     if not path:
         content_type = r.headers.get('content-type')
         suffix = mimetypes.guess_extension(content_type) if content_type else ''
+
+        # mimetypes currently returns '.jpe' for jpeg; so fix that up here...
         if suffix == '.jpe':
             suffix = '.jpg'
-        elif suffix is None:
-            suffix = ''
+        elif not suffix:
+            # No content-type so guess based on extension if we can
+            p = urlparse(url)
+            suffix = os.path.splitext(p.path)[1]
+
         fd, path = tempfile.mkstemp(suffix=suffix)
         os.close(fd)
 
--- a/core/management/commands/ssl_images.py	Thu Oct 15 18:35:56 2015 -0500
+++ b/core/management/commands/ssl_images.py	Sat Oct 24 21:29:07 2015 -0500
@@ -70,6 +70,11 @@
     handler.setFormatter(formatter)
     logger.addHandler(handler)
 
+    requests_log = logging.getLogger("requests.packages.urllib3")
+    requests_log.setLevel(logging.INFO)
+    requests_log.propagate = True
+    requests_log.addHandler(handler)
+
 
 def resize_image(img_path):
     """Resizes the image found at img_path if necessary.
@@ -85,8 +90,12 @@
 
     if image.size > PHOTO_MAX_SIZE:
         logger.info('Resizing from %s to %s', image.size, PHOTO_MAX_SIZE)
-        image.thumbnail(PHOTO_MAX_SIZE, Image.ANTIALIAS)
-        image.save(img_path)
+        try:
+            image.thumbnail(PHOTO_MAX_SIZE, Image.ANTIALIAS)
+            image.save(img_path)
+        except IOError as ex:
+            logger.error("Error resizing image from %s: %s", img_path, ex)
+            return False
 
     return True