def thumbs(request, crop=True):
"""
Return an image/jpeg image that's a thumbnail of the encoded request.
"""
encoded = request.match_info['encoded']
cached_thumb = os.path.join(args.cache, encoded)
if not os.path.isfile(cached_thumb):
try:
__, w_x_h, path = decode(CIPHER_KEY, encoded).split(':', 3)
except (binascii.Error, UnicodeDecodeError, ValueError):
return web.HTTPNotFound()
# WISHLIST add as extra context to the aiohttp.access logger
logger.info('Decoded as %s %s', path, w_x_h)
abspath = args.STORAGE_DIR + path
try:
im = Image.open(abspath)
except (FileNotFoundError, IsADirectoryError):
return web.HTTPNotFound()
thumb_dimension = [int(x) for x in w_x_h.split('x')]
with open(cached_thumb, 'wb') as fh:
if crop:
cropped_im = crop_1(im)
cropped_im.thumbnail(thumb_dimension)
cropped_im.save(fh, 'jpeg')
else:
im.thumbnail(thumb_dimension)
im.save(fh, 'jpeg')
with open(cached_thumb, 'rb') as fh:
return web.Response(
status=200, body=fh.read(), content_type='image/jpeg',
headers={
'Cache-Control': 'max-age=86400',
})
评论列表
文章目录