try to get preview from peers, fixes #79

This commit is contained in:
j 2016-01-21 12:35:49 +05:30
parent 0ff3b9ee3c
commit 5f8094bba3
5 changed files with 33 additions and 12 deletions

View file

@ -170,6 +170,7 @@ class Changelog(db.Model):
i = Item.get(itemid) i = Item.get(itemid)
if i: if i:
i.sync_metadata() i.sync_metadata()
i.get_preview()
return True return True
def action_removeitem(self, user, timestamp, itemid): def action_removeitem(self, user, timestamp, itemid):

View file

@ -93,14 +93,12 @@ class Icons(dict):
icons = Icons(icons_db_path) icons = Icons(icons_db_path)
@run_async def get_icon_sync(id, type_, size):
def get_icon(id, type_, size, callback):
if size: if size:
skey = '%s:%s:%s' % (type_, id, size) skey = '%s:%s:%s' % (type_, id, size)
data = icons[skey] data = icons[skey]
if data: if data:
callback(bytes(data)) return bytes(data)
return
key = '%s:%s' % (type_, id) key = '%s:%s' % (type_, id)
data = icons[key] data = icons[key]
if not data: if not data:
@ -128,7 +126,11 @@ def get_icon(id, type_, size, callback):
if size: if size:
data = icons[skey] = resize_image(data, size=size) data = icons[skey] = resize_image(data, size=size)
data = bytes(data) or '' data = bytes(data) or ''
callback(data) return data
@run_async
def get_icon(id, type_, size, callback):
callback(get_icon_sync(id, type_, size))
def clear_default_cover_cache(): def clear_default_cover_cache():
icons.clear('default:cover:') icons.clear('default:cover:')

View file

@ -366,6 +366,11 @@ class Item(db.Model):
path = self.get_path() path = self.get_path()
if path: if path:
return getattr(media, self.info['extension']).cover(path) return getattr(media, self.info['extension']).cover(path)
else:
for u in self.users:
if u.id != settings.USER_ID:
if state.nodes.download_preview(u.id, self.id):
break
def update_cover(self): def update_cover(self):
key = 'cover:%s'%self.id key = 'cover:%s'%self.id
@ -388,6 +393,14 @@ class Item(db.Model):
del self.info['coverRatio'] del self.info['coverRatio']
icons.clear('cover:%s:' % self.id) icons.clear('cover:%s:' % self.id)
def get_preview(self):
key = 'preview:%s'%self.id
data = icons[key]
if not data:
preview = self.extract_preview()
if preview:
icons[key] = preview
def update_preview(self): def update_preview(self):
key = 'preview:%s'%self.id key = 'preview:%s'%self.id
preview = self.extract_preview() preview = self.extract_preview()

View file

@ -127,9 +127,8 @@ class Handler(http.server.SimpleHTTPRequestHandler):
self.wfile.write(b'404 - Not Found') self.wfile.write(b'404 - Not Found')
return return
if preview: if preview:
from item.icons import icons from item.icons import get_icon_sync
key = 'preview:' + id data = get_icon_sync(id, 'preview', 512)
data = icons[key]
if data: if data:
self.send_response(200, 'ok') self.send_response(200, 'ok')
self.send_header('Content-type', 'image/jpg') self.send_header('Content-type', 'image/jpg')

View file

@ -359,10 +359,11 @@ class Node(Thread):
logger.debug('FAILED %s', url) logger.debug('FAILED %s', url)
return False return False
def download_preview(self, item): def download_preview(self, item_id):
logger.debug('trying to download preview from %s for %s', self.url, item_id)
from item.icons import icons from item.icons import icons
self.resolve() self.resolve()
url = '%s/preview/%s' % (self.url, item.id) url = '%s/preview/%s' % (self.url, item_id)
self._opener.addheaders = list(zip(self.headers.keys(), self.headers.values())) self._opener.addheaders = list(zip(self.headers.keys(), self.headers.values()))
try: try:
r = self._opener.open(url, timeout=self.TIMEOUT*2) r = self._opener.open(url, timeout=self.TIMEOUT*2)
@ -376,9 +377,9 @@ class Node(Thread):
if r.headers.get('content-encoding', None) == 'gzip': if r.headers.get('content-encoding', None) == 'gzip':
fileobj = gzip.GzipFile(fileobj=r) fileobj = gzip.GzipFile(fileobj=r)
content = fileobj.read() content = fileobj.read()
key = 'preview:' + item.id key = 'preview:' + item_id
icons[key] = content icons[key] = content
icons.clear('preview:%s:'%item.id) icons.clear(key+':')
return True return True
except: except:
logger.debug('preview download failed %s', url, exc_info=1) logger.debug('preview download failed %s', url, exc_info=1)
@ -446,6 +447,11 @@ class Nodes(Thread):
def download(self, id, item): def download(self, id, item):
return id in self._nodes and self._nodes[id].download(item) return id in self._nodes and self._nodes[id].download(item)
def download_preview(self, id, item):
return id in self._nodes and \
self._nodes[id].is_online() and \
self._nodes[id].download_preview(item)
def _call(self, target, action, *args): def _call(self, target, action, *args):
if target == 'all': if target == 'all':
nodes = list(self._nodes.values()) nodes = list(self._nodes.values())