don't download image in db session
This commit is contained in:
parent
d58f5bd61b
commit
5b4a58f2db
1 changed files with 41 additions and 30 deletions
|
@ -437,30 +437,6 @@ class Item(db.Model):
|
||||||
if state.nodes.download_preview(u.id, self.id):
|
if state.nodes.download_preview(u.id, self.id):
|
||||||
break
|
break
|
||||||
|
|
||||||
def update_cover(self):
|
|
||||||
logger.debug('%s update cover', self.id)
|
|
||||||
key = 'cover:%s' % self.id
|
|
||||||
cover = None
|
|
||||||
if 'cover' in self.meta and self.meta['cover']:
|
|
||||||
logger.debug('download cover %s %s', self.id, self.meta['cover'])
|
|
||||||
try:
|
|
||||||
cover = ox.cache.read_url(self.meta['cover'])
|
|
||||||
except:
|
|
||||||
logger.debug('unable to read cover url %s', self.meta['cover'])
|
|
||||||
cover = None
|
|
||||||
if cover:
|
|
||||||
icons[key] = cover
|
|
||||||
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)
|
|
||||||
logger.debug('%s update_cover done', self.id)
|
|
||||||
|
|
||||||
def get_preview(self):
|
def get_preview(self):
|
||||||
key = 'preview:%s' % self.id
|
key = 'preview:%s' % self.id
|
||||||
data = icons[key]
|
data = icons[key]
|
||||||
|
@ -490,11 +466,14 @@ class Item(db.Model):
|
||||||
icons.clear('preview:%s:' % self.id)
|
icons.clear('preview:%s:' % self.id)
|
||||||
logger.debug('%s update_preview done', self.id)
|
logger.debug('%s update_preview done', self.id)
|
||||||
|
|
||||||
def update_icons(self):
|
def update_cover(self):
|
||||||
if state.online:
|
if state.online:
|
||||||
self.update_cover()
|
download_cover(self.id)
|
||||||
elif state.tasks:
|
elif state.tasks:
|
||||||
state.tasks.queue('getcover', self.id)
|
state.tasks.queue('getcover', self.id)
|
||||||
|
|
||||||
|
def update_icons(self):
|
||||||
|
self.update_cover()
|
||||||
self.update_preview()
|
self.update_preview()
|
||||||
|
|
||||||
def load_metadata(self):
|
def load_metadata(self):
|
||||||
|
@ -856,15 +835,47 @@ def update_sort_table():
|
||||||
s.connection().execute(q)
|
s.connection().execute(q)
|
||||||
s.commit()
|
s.commit()
|
||||||
|
|
||||||
|
def download_cover(id):
|
||||||
|
key = 'cover:%s' % id
|
||||||
|
with db.session():
|
||||||
|
i = Item.get(id)
|
||||||
|
if i:
|
||||||
|
url = i.meta.get('cover')
|
||||||
|
else:
|
||||||
|
url = None
|
||||||
|
|
||||||
|
logger.debug('download cover %s %s', self.id, url)
|
||||||
|
ratio = None
|
||||||
|
try:
|
||||||
|
cover = ox.net.read_url(url)
|
||||||
|
ratio = get_ratio(cover)
|
||||||
|
except:
|
||||||
|
logger.debug('unable to read cover url %s', url)
|
||||||
|
cover = None
|
||||||
|
with db.session():
|
||||||
|
i = Item.get(id, for_update=True)
|
||||||
|
if i:
|
||||||
|
if cover:
|
||||||
|
i.info['coverRatio'] = get_ratio(cover)
|
||||||
|
else:
|
||||||
|
if 'previewRatio' in i.info:
|
||||||
|
i.info['coverRatio'] = i.info['previewRatio']
|
||||||
|
elif 'coverRatio' in i.info:
|
||||||
|
del i.info['coverRatio']
|
||||||
|
i.save()
|
||||||
|
|
||||||
|
if cover:
|
||||||
|
icons[key] = cover
|
||||||
|
else:
|
||||||
|
del icons[key]
|
||||||
|
icons.clear('cover:%s:' % id)
|
||||||
|
logger.debug('%s update_cover done', id)
|
||||||
|
|
||||||
def get_cover(id):
|
def get_cover(id):
|
||||||
delay = 60
|
delay = 60
|
||||||
if state.online:
|
if state.online:
|
||||||
# logger.debug('get_cover(%s)', id)
|
# logger.debug('get_cover(%s)', id)
|
||||||
with db.session():
|
download_cover(id)
|
||||||
i = Item.get(id)
|
|
||||||
if i:
|
|
||||||
i.update_cover()
|
|
||||||
else:
|
else:
|
||||||
state.main.call_later(delay, lambda: state.tasks.queue('getcover', id))
|
state.main.call_later(delay, lambda: state.tasks.queue('getcover', id))
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue