comparison src/pages/files/list/page_meta.py @ 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 abc5d545dbaa
children 2dab7692eae7
comparison
equal deleted inserted replaced
1066:d1324e43d448 1067:808ec98de8b3
1 #!/usr/bin/env python2.7 1 #!/usr/bin/env python2.7
2 # -*- coding: utf-8 -*- 2 # -*- coding: utf-8 -*-
3 3
4 from libervia.server.constants import Const as C 4 from libervia.server.constants import Const as C
5 from sat.core.i18n import _
5 from twisted.internet import defer 6 from twisted.internet import defer
7 from libervia.server import session_iface
6 from sat.core.log import getLogger 8 from sat.core.log import getLogger
9 import json
10 import os
7 log = getLogger('pages/file') 11 log = getLogger('pages/file')
8 """files handling pages""" 12 """files handling pages"""
9 13
10 name = u'files' 14 name = u'files'
11 access = C.PAGES_ACCESS_PROFILE 15 access = C.PAGES_ACCESS_PROFILE
22 path = u'/'.join(path_elts) 26 path = u'/'.join(path_elts)
23 profile = self.getProfile(request) or C.SERVICE_PROFILE 27 profile = self.getProfile(request) or C.SERVICE_PROFILE
24 28
25 files_data = yield self.host.bridgeCall('FISList', service.full(), path, {}, profile) 29 files_data = yield self.host.bridgeCall('FISList', service.full(), path, {}, profile)
26 for file_data in files_data: 30 for file_data in files_data:
31 try:
32 extra_raw = file_data[u'extra']
33 except KeyError:
34 pass
35 else:
36 file_data[u'extra'] = json.loads(extra_raw) if extra_raw else {}
27 dir_path = path_elts + [file_data['name']] 37 dir_path = path_elts + [file_data['name']]
28 if file_data[u'type'] == C.FILE_TYPE_DIRECTORY: 38 if file_data[u'type'] == C.FILE_TYPE_DIRECTORY:
29 page = self 39 page = self
30 elif file_data[u'type'] == C.FILE_TYPE_FILE: 40 elif file_data[u'type'] == C.FILE_TYPE_FILE:
31 page = self.getPageByName('files_view') 41 page = self.getPageByName('files_view')
42 try:
43 thumbnails = file_data[u'extra']['thumbnails']
44 if not thumbnails:
45 raise KeyError
46 except KeyError:
47 pass
48 else:
49 thumbnails.sort(key = lambda t: t['size'])
50 thumb = thumbnails[0]
51 for thumb_data in thumbnails:
52 if thumb_data['size'][0] > 300:
53 break
54 thumb = thumb_data
55 if u'url' in thumb:
56 file_data['thumb_url'] = thumb['url']
57 elif u'id' in thumb:
58 try:
59 thumb_path = yield self.host.bridgeCall('bobGetFile', service.full(), thumb[u'id'], profile)
60 except Exception as e:
61 log.warning(_(u"Can't retrieve thumbnail: {reason}").format(reason=e))
62 else:
63 filename = os.path.basename(thumb_path)
64 session_data = self.host.getSessionData(request, session_iface.ISATSession)
65 file_data['thumb_url'] = os.path.join(session_data.cache_dir, filename)
32 else: 66 else:
33 raise ValueError(u'unexpected file type: {file_type}'.format(file_type=file_data[u'type'])) 67 raise ValueError(u'unexpected file type: {file_type}'.format(file_type=file_data[u'type']))
34 file_data[u'url'] = page.getURL(service.full(), *dir_path) 68 file_data[u'url'] = page.getURL(service.full(), *dir_path)
35 template_data[u'files_data'] = files_data 69 template_data[u'files_data'] = files_data
36 template_data[u'path'] = path 70 template_data[u'path'] = path