1
- import uuid
2
1
import os
3
- import shutil
2
+ import sys
4
3
import urlparse
5
4
import re
6
5
import hashlib
6
+ import logging
7
7
8
8
from lxml import html
9
9
from PIL import Image , ImageFile
16
16
17
17
ImageFile .MAXBLOCKS = 10000000
18
18
19
+ logger = logging .getLogger (__name__ )
20
+
21
+ class ImageResizeError (Exception ):
22
+ pass
23
+
19
24
20
25
def match_or_none (string , rx ):
21
26
"""
@@ -174,6 +179,9 @@ def transcode_to_jpeg(image, path, width, height):
174
179
new_image .save (new_path , quality = 80 , optimize = 1 )
175
180
return new_path
176
181
182
+ def _get_resize_error_message (path ):
183
+ return 'There was a problem resizing this image: {0}' .format (os .path .split (path )[1 ])
184
+
177
185
def re_render (path , width , height ):
178
186
"""
179
187
Given an original image, width, and height, creates a thumbnailed image
@@ -193,6 +201,7 @@ def re_render(path, width, height):
193
201
@return: Path to the 'rendered' image.
194
202
@rtype: "/path/to/image"
195
203
"""
204
+
196
205
try :
197
206
image = Image .open (path )
198
207
except IOError :
@@ -230,15 +239,21 @@ def re_render(path, width, height):
230
239
return new_path
231
240
232
241
# Re-render the image, optimizing for filesize
233
- new_image = image .resize ((width , height ), Image .ANTIALIAS )
242
+ try :
243
+ new_image = image .resize ((width , height ), Image .ANTIALIAS )
244
+ except TypeError :
245
+ raise ImageResizeError (_get_resize_error_message (path )), None , sys .exc_info ()[2 ]
234
246
235
247
image_params = {}
236
248
if image .format != "GIF" :
237
249
image_params = dict (
238
250
quality = 80 ,
239
251
optimize = 1 ,
240
252
)
241
- new_image .save (new_path , ** image_params )
253
+ try :
254
+ new_image .save (new_path , ** image_params )
255
+ except IOError :
256
+ raise ImageResizeError (_get_resize_error_message (path )), None , sys .exc_info ()[2 ]
242
257
return new_path
243
258
244
259
def get_html_tree (content ):
@@ -258,17 +273,27 @@ def resize_images(post_content):
258
273
@return: Modified contents.
259
274
@rtype: basestring
260
275
"""
276
+
261
277
# Get tree
262
278
tree = get_html_tree (post_content )
263
-
264
279
# Get images
265
280
imgs = tree .xpath ('//img[starts-with(@src, "%s")]' % settings .STATIC_URL )
266
281
for img in imgs :
267
282
orig_url = img .attrib ['src' ]
268
283
orig_path = get_local_path (orig_url )
269
284
270
285
width , height = get_dimensions (img )
271
- rendered_path = re_render (orig_path , width , height )
286
+ try :
287
+ rendered_path = re_render (orig_path , width , height )
288
+ except ImageResizeError :
289
+ raise
290
+ except Exception as e :
291
+ # If something goes wrong, just use the original path so as not to
292
+ # interrupt the user. However, log it, because it's still a real problem.
293
+ logger .error (e , exc_info = True , extra = {
294
+ 'stack' : True ,
295
+ })
296
+ rendered_path = orig_path
272
297
273
298
# If we haven't changed the image, move along.
274
299
if rendered_path == orig_path :
0 commit comments