don't download image in db session

This commit is contained in:
j 2019-01-31 15:03:54 +05:30
parent d58f5bd61b
commit 5b4a58f2db

View file

@ -437,30 +437,6 @@ class Item(db.Model):
if state.nodes.download_preview(u.id, self.id):
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):
key = 'preview:%s' % self.id
data = icons[key]
@ -490,11 +466,14 @@ class Item(db.Model):
icons.clear('preview:%s:' % self.id)
logger.debug('%s update_preview done', self.id)
def update_icons(self):
def update_cover(self):
if state.online:
self.update_cover()
download_cover(self.id)
elif state.tasks:
state.tasks.queue('getcover', self.id)
def update_icons(self):
self.update_cover()
self.update_preview()
def load_metadata(self):
@ -856,15 +835,47 @@ def update_sort_table():
s.connection().execute(q)
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):
delay = 60
if state.online:
# logger.debug('get_cover(%s)', id)
with db.session():
i = Item.get(id)
if i:
i.update_cover()
download_cover(id)
else:
state.main.call_later(delay, lambda: state.tasks.queue('getcover', id))