avoid rewriting cover cache if one resize fails
This commit is contained in:
parent
0f1cd9662d
commit
84259fb0c4
2 changed files with 31 additions and 10 deletions
|
@ -11,7 +11,7 @@ import tornado.web
|
||||||
|
|
||||||
from oxtornado import run_async
|
from oxtornado import run_async
|
||||||
from settings import icons_db_path, static_path
|
from settings import icons_db_path, static_path
|
||||||
from utils import resize_image
|
from utils import resize_image, is_svg
|
||||||
import db
|
import db
|
||||||
|
|
||||||
|
|
||||||
|
@ -130,6 +130,8 @@ def get_icon_sync(id, type_, size):
|
||||||
return bytes(data)
|
return bytes(data)
|
||||||
key = '%s:%s' % (type_, id)
|
key = '%s:%s' % (type_, id)
|
||||||
data = icons[key]
|
data = icons[key]
|
||||||
|
if is_svg(data):
|
||||||
|
return bytes(data)
|
||||||
if not data:
|
if not data:
|
||||||
type_ = 'preview' if type_ == 'cover' else 'cover'
|
type_ = 'preview' if type_ == 'cover' else 'cover'
|
||||||
key = '%s:%s' % (type_, id)
|
key = '%s:%s' % (type_, id)
|
||||||
|
@ -150,13 +152,26 @@ def get_icon_sync(id, type_, size):
|
||||||
if not data:
|
if not data:
|
||||||
data = icons.default_cover()
|
data = icons.default_cover()
|
||||||
if size:
|
if size:
|
||||||
data = resize_image(data, size=size)
|
try:
|
||||||
icons[skey] = data
|
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
|
size = None
|
||||||
if size:
|
if size:
|
||||||
data = resize_image(data, size=size)
|
try:
|
||||||
icons[skey] = data
|
data = resize_image(data, size=size)
|
||||||
data = bytes(data) or ''
|
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
|
return data
|
||||||
|
|
||||||
@run_async
|
@run_async
|
||||||
|
@ -192,8 +207,11 @@ def get_icon_app(id, type_, size, callback):
|
||||||
data = icons.default_cover()
|
data = icons.default_cover()
|
||||||
size = None
|
size = None
|
||||||
if size:
|
if size:
|
||||||
data = resize_image(data, size=size)
|
try:
|
||||||
icons[skey] = data
|
data = resize_image(data, size=size)
|
||||||
|
icons[skey] = data
|
||||||
|
except:
|
||||||
|
pass
|
||||||
data = bytes(data) or ''
|
data = bytes(data) or ''
|
||||||
callback(data)
|
callback(data)
|
||||||
|
|
||||||
|
|
|
@ -66,14 +66,17 @@ def get_by_key(objects, key, value):
|
||||||
def get_by_id(objects, id):
|
def get_by_id(objects, id):
|
||||||
return get_by_key(objects, 'id', 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):
|
def resize_image(data, width=None, size=None):
|
||||||
if isinstance(data, bytes):
|
if isinstance(data, bytes):
|
||||||
data = BytesIO(data)
|
data = BytesIO(data)
|
||||||
else:
|
else:
|
||||||
data = StringIO(data)
|
data = StringIO(data)
|
||||||
source = Image.open(data)
|
source = Image.open(data)
|
||||||
if source.mode not in ('1', 'CMYK', 'L', 'RGB', 'RGBA', 'RGBX', 'YCbCr'):
|
#if source.mode not in ('1', 'CMYK', 'L', 'RGB', 'RGBA', 'RGBX', 'YCbCr'):
|
||||||
source = source.convert('RGB')
|
source = source.convert('RGB')
|
||||||
source_width = source.size[0]
|
source_width = source.size[0]
|
||||||
source_height = source.size[1]
|
source_height = source.size[1]
|
||||||
if size:
|
if size:
|
||||||
|
|
Loading…
Reference in a new issue