avoid rewriting cover cache if one resize fails

This commit is contained in:
j 2016-02-18 19:23:49 +05:30
parent 0f1cd9662d
commit 84259fb0c4
2 changed files with 31 additions and 10 deletions

View File

@ -11,7 +11,7 @@ import tornado.web
from oxtornado import run_async
from settings import icons_db_path, static_path
from utils import resize_image
from utils import resize_image, is_svg
import db
@ -130,6 +130,8 @@ def get_icon_sync(id, type_, size):
return bytes(data)
key = '%s:%s' % (type_, id)
data = icons[key]
if is_svg(data):
return bytes(data)
if not data:
type_ = 'preview' if type_ == 'cover' else 'cover'
key = '%s:%s' % (type_, id)
@ -150,13 +152,26 @@ def get_icon_sync(id, type_, size):
if not data:
data = icons.default_cover()
if size:
data = resize_image(data, size=size)
icons[skey] = data
try:
data = resize_image(data, size=size)
except:
logger.debug('failed to resize default cover %s %s %s', id, size, skey)
data = None
if data:
icons[skey] = data
size = None
if size:
data = resize_image(data, size=size)
icons[skey] = data
data = bytes(data) or ''
try:
data = resize_image(data, size=size)
except:
logger.debug('failed to resize %s %s %s', id, size, skey)
data = None
if data:
icons[skey] = data
if data:
data = bytes(data)
else:
data = ''
return data
@run_async
@ -192,8 +207,11 @@ def get_icon_app(id, type_, size, callback):
data = icons.default_cover()
size = None
if size:
data = resize_image(data, size=size)
icons[skey] = data
try:
data = resize_image(data, size=size)
icons[skey] = data
except:
pass
data = bytes(data) or ''
callback(data)

View File

@ -66,14 +66,17 @@ def get_by_key(objects, key, value):
def get_by_id(objects, id):
return get_by_key(objects, 'id', id)
def is_svg(data):
return data and b'<svg' in data[:256]
def resize_image(data, width=None, size=None):
if isinstance(data, bytes):
data = BytesIO(data)
else:
data = StringIO(data)
source = Image.open(data)
if source.mode not in ('1', 'CMYK', 'L', 'RGB', 'RGBA', 'RGBX', 'YCbCr'):
source = source.convert('RGB')
#if source.mode not in ('1', 'CMYK', 'L', 'RGB', 'RGBA', 'RGBX', 'YCbCr'):
source = source.convert('RGB')
source_width = source.size[0]
source_height = source.size[1]
if size: