changeset 1067:808ec98de8b3

pages (files/list): retrieve thumbnail: when thumbnail url is not available (only id is available), thumbnail are retrieved using BoB. The thumbnail closest to 300px witdh is used.
author Goffi <goffi@goffi.org>
date Wed, 14 Mar 2018 09:23:23 +0100
parents d1324e43d448
children 5e809a49971c
files src/pages/files/list/page_meta.py src/server/utils.py
diffstat 2 files changed, 36 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/src/pages/files/list/page_meta.py	Wed Mar 14 09:20:38 2018 +0100
+++ b/src/pages/files/list/page_meta.py	Wed Mar 14 09:23:23 2018 +0100
@@ -2,8 +2,12 @@
 # -*- coding: utf-8 -*-
 
 from libervia.server.constants import Const as C
+from sat.core.i18n import _
 from twisted.internet import defer
+from libervia.server import session_iface
 from sat.core.log import getLogger
+import json
+import os
 log = getLogger('pages/file')
 """files handling pages"""
 
@@ -24,11 +28,41 @@
 
     files_data = yield self.host.bridgeCall('FISList', service.full(), path, {}, profile)
     for file_data in files_data:
+        try:
+            extra_raw = file_data[u'extra']
+        except KeyError:
+            pass
+        else:
+            file_data[u'extra'] = json.loads(extra_raw) if extra_raw else {}
         dir_path = path_elts + [file_data['name']]
         if file_data[u'type'] == C.FILE_TYPE_DIRECTORY:
             page = self
         elif file_data[u'type'] == C.FILE_TYPE_FILE:
             page = self.getPageByName('files_view')
+            try:
+                thumbnails = file_data[u'extra']['thumbnails']
+                if not thumbnails:
+                    raise KeyError
+            except KeyError:
+                pass
+            else:
+                thumbnails.sort(key = lambda t: t['size'])
+                thumb = thumbnails[0]
+                for thumb_data in thumbnails:
+                    if thumb_data['size'][0] > 300:
+                        break
+                    thumb = thumb_data
+                if u'url' in thumb:
+                    file_data['thumb_url'] = thumb['url']
+                elif u'id' in thumb:
+                    try:
+                        thumb_path = yield self.host.bridgeCall('bobGetFile', service.full(), thumb[u'id'], profile)
+                    except Exception as e:
+                        log.warning(_(u"Can't retrieve thumbnail: {reason}").format(reason=e))
+                    else:
+                        filename = os.path.basename(thumb_path)
+                        session_data = self.host.getSessionData(request, session_iface.ISATSession)
+                        file_data['thumb_url'] = os.path.join(session_data.cache_dir, filename)
         else:
             raise ValueError(u'unexpected file type: {file_type}'.format(file_type=file_data[u'type']))
         file_data[u'url'] = page.getURL(service.full(), *dir_path)
--- a/src/server/utils.py	Wed Mar 14 09:20:38 2018 +0100
+++ b/src/server/utils.py	Wed Mar 14 09:23:23 2018 +0100
@@ -58,6 +58,8 @@
             elif name == u'error':
                 handler_data[u'deferred'].errback(Exception(data))
                 handler_data[u'instance'].unregister_handler()
+            else:
+                log.error(u'unexpected signal: {name}'.format(name=name))
 
     def _timeout(self):
         log.warning(_(u"No progress received, cancelling handler: {progress_id} [{profile}]").format(