From b89976813019cb5909c7d8c09a2024e1ce54d08d Mon Sep 17 00:00:00 2001 From: j Date: Sat, 16 Jan 2016 10:47:52 +0530 Subject: [PATCH] clear all cached sizes --- oml/item/icons.py | 13 ++++++++++--- oml/item/models.py | 38 +++++++++++++++++++++----------------- oml/nodes.py | 3 +-- oml/utils.py | 7 +++++++ 4 files changed, 39 insertions(+), 22 deletions(-) diff --git a/oml/item/icons.py b/oml/item/icons.py index d4228bc..a1f021f 100644 --- a/oml/item/icons.py +++ b/oml/item/icons.py @@ -82,6 +82,15 @@ class Icons(dict): c.close() conn.close() + def clear(self, prefix): + sql = 'DELETE FROM icon WHERE id LIKE ?' + conn = self.connect() + c = conn.cursor() + c.execute(sql, (prefix + '%', )) + conn.commit() + c.close() + conn.close() + icons = Icons(icons_db_path) @run_async @@ -122,9 +131,7 @@ def get_icon(id, type_, size, callback): callback(data) def clear_default_cover_cache(): - for resolution in (64, 128, 256, 512, 1024): - key = '%s:%s:%s' % ('default', 'cover', resolution) - del icons[key] + icons.clear('default:cover:') @run_async def get_icon_app(id, type_, size, callback): diff --git a/oml/item/models.py b/oml/item/models.py index f82ee77..1ea5e61 100644 --- a/oml/item/models.py +++ b/oml/item/models.py @@ -21,7 +21,7 @@ from .icons import icons from .person import get_sort_name, Person from queryparser import Parser from settings import config -from utils import remove_empty_folders +from utils import remove_empty_folders, get_ratio from websocket import trigger_event import db import media @@ -310,13 +310,7 @@ class Item(db.Model): if path: return getattr(media, self.info['extension']).cover(path) - def update_icons(self): - def get_ratio(data): - try: - img = Image.open(BytesIO(data)) - return img.size[0]/img.size[1] - except: - return 1 + def update_cover(self): key = 'cover:%s'%self.id cover = None if 'cover' in self.meta and self.meta['cover']: @@ -330,25 +324,35 @@ class Item(db.Model): self.info['coverRatio'] = get_ratio(cover) else: del icons[key] + if not cover: + if 'previewRatio' in self.info: + self.info['coverRatio'] = self.info['previewRatio'] + elif 'coverRatio' in self.info: + del self.info['coverRatio'] + icons.clear('cover:%s:'%self.id) + + def update_preview(self): key = 'preview:%s'%self.id preview = self.extract_preview() if preview: icons[key] = preview self.info['previewRatio'] = get_ratio(preview) - if not cover: + if not 'coverRatio' in self.info: self.info['coverRatio'] = self.info['previewRatio'] else: del icons[key] if 'previewRatio' in self.info: del self.info['previewRatio'] - if not cover and 'coverRatio' in self.info: - del self.info['coverRatio'] - if cover and not preview: - self.info['previewRatio'] = self.info['coverRatio'] - for key in ('cover', 'preview'): - key = '%s:%s' % (key, self.id) - for resolution in (128, 256, 512): - del icons['%s:%s' % (key, resolution)] + if not preview: + if 'coverRatio' in self.info: + self.info['previewRatio'] = self.info['coverRatio'] + elif 'previewRatio' in self.info: + del self.info['previewRatio'] + icons.clear('preview:%s:'%self.id) + + def update_icons(self): + self.update_cover() + self.update_preview() def load_metadata(self): ''' diff --git a/oml/nodes.py b/oml/nodes.py index c9768af..98c1cf2 100644 --- a/oml/nodes.py +++ b/oml/nodes.py @@ -384,8 +384,7 @@ class Node(Thread): content = fileobj.read() key = 'preview:' + item.id icons[key] = content - for resolution in (128, 256, 512): - del icons['preview:%s' % resolution] + icons.clear('preview:%s:'%item.id) return True except: logger.debug('preview download failed %s', url, exc_info=1) diff --git a/oml/utils.py b/oml/utils.py index f669146..bdb8ad2 100644 --- a/oml/utils.py +++ b/oml/utils.py @@ -391,3 +391,10 @@ def _to_json(python_object): return '%d-%02d-%02dT%02d:%02d%02dZ' % tuple(list(tt)[:6]) return python_object.strftime('%Y-%m-%dT%H:%M:%SZ') raise TypeError(u'%s %s is not JSON serializable' % (repr(python_object), type(python_object))) + +def get_ratio(data): + try: + img = Image.open(BytesIO(data)) + return img.size[0]/img.size[1] + except: + return 1