2
2
from future .builtins import int , open , str
3
3
4
4
from hashlib import md5
5
+ import io
5
6
import os
6
7
try :
7
8
from urllib .parse import quote , unquote
@@ -312,11 +313,14 @@ def thumbnail(image_url, width, height, upscale=True, quality=95, left=.5,
312
313
# image, which is something we do in filebrowser when a new image
313
314
# is written, allowing us to purge any previously generated
314
315
# thumbnails that may match a new image name.
315
- thumb_dir = os .path .join (settings .MEDIA_ROOT , image_dir ,
316
- settings .THUMBNAILS_DIR_NAME , image_name )
317
- if not os .path .exists (thumb_dir ):
316
+ thumb_dir = os .path .join (
317
+ image_dir ,
318
+ settings .THUMBNAILS_DIR_NAME ,
319
+ image_name
320
+ )
321
+ if not default_storage .exists (thumb_dir ):
318
322
try :
319
- os .makedirs (thumb_dir )
323
+ default_storage .makedirs (thumb_dir )
320
324
except OSError :
321
325
pass
322
326
@@ -329,7 +333,7 @@ def thumbnail(image_url, width, height, upscale=True, quality=95, left=.5,
329
333
thumb_url = "%s/%s" % (image_url_path , thumb_url )
330
334
331
335
try :
332
- thumb_exists = os . path .exists (thumb_path )
336
+ thumb_exists = default_storage .exists (thumb_path )
333
337
except UnicodeEncodeError :
334
338
# The image that was saved to a filesystem with utf-8 support,
335
339
# but somehow the locale has changed and the filesystem does not
@@ -421,21 +425,12 @@ def thumbnail(image_url, width, height, upscale=True, quality=95, left=.5,
421
425
to_size = (to_width , to_height )
422
426
to_pos = (left , top )
423
427
try :
428
+ print ('GEN [%s].' % thumb_url )
429
+ output = io .BytesIO ()
424
430
image = ImageOps .fit (image , to_size , Image .ANTIALIAS , 0 , to_pos )
425
- image = image .save (thumb_path , filetype , quality = quality , ** image_info )
426
- # Push a remote copy of the thumbnail if MEDIA_URL is
427
- # absolute.
428
- if "://" in settings .MEDIA_URL :
429
- with open (thumb_path , "rb" ) as f :
430
- default_storage .save (unquote (thumb_url ), File (f ))
431
+ image = image .save (output , filetype , quality = quality , ** image_info )
432
+ default_storage .save (unquote (thumb_url ), File (output ))
431
433
except Exception :
432
- # If an error occurred, a corrupted image may have been saved,
433
- # so remove it, otherwise the check for it existing will just
434
- # return the corrupted image next time it's requested.
435
- try :
436
- os .remove (thumb_path )
437
- except Exception :
438
- pass
439
434
return image_url
440
435
return thumb_url
441
436
0 commit comments