annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1064
abc5d545dbaa pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/env python2.7
abc5d545dbaa pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
abc5d545dbaa pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
3
abc5d545dbaa pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
4 from libervia.server.constants import Const as C
1067
808ec98de8b3 pages (files/list): retrieve thumbnail:
Goffi <goffi@goffi.org>
parents: 1064
diff changeset
5 from sat.core.i18n import _
1064
abc5d545dbaa pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
6 from twisted.internet import defer
1067
808ec98de8b3 pages (files/list): retrieve thumbnail:
Goffi <goffi@goffi.org>
parents: 1064
diff changeset
7 from libervia.server import session_iface
1064
abc5d545dbaa pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
8 from sat.core.log import getLogger
1067
808ec98de8b3 pages (files/list): retrieve thumbnail:
Goffi <goffi@goffi.org>
parents: 1064
diff changeset
9 import json
808ec98de8b3 pages (files/list): retrieve thumbnail:
Goffi <goffi@goffi.org>
parents: 1064
diff changeset
10 import os
1064
abc5d545dbaa pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
11 log = getLogger('pages/file')
abc5d545dbaa pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
12 """files handling pages"""
abc5d545dbaa pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
13
abc5d545dbaa pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
14 name = u'files'
abc5d545dbaa pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
15 access = C.PAGES_ACCESS_PROFILE
abc5d545dbaa pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
16 template = u"file/overview.html"
abc5d545dbaa pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
17
abc5d545dbaa pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
18 def parse_url(self, request):
abc5d545dbaa pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
19 self.getPathArgs(request, ['service', '*path'], min_args=1, service='jid', path='')
abc5d545dbaa pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
20
abc5d545dbaa pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
21 @defer.inlineCallbacks
abc5d545dbaa pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
22 def prepare_render(self, request):
abc5d545dbaa pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
23 data = self.getRData(request)
abc5d545dbaa pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
24 template_data = request.template_data
abc5d545dbaa pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
25 service, path_elts = data[u'service'], data[u'path']
abc5d545dbaa pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
26 path = u'/'.join(path_elts)
abc5d545dbaa pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
27 profile = self.getProfile(request) or C.SERVICE_PROFILE
abc5d545dbaa pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
28
abc5d545dbaa pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
29 files_data = yield self.host.bridgeCall('FISList', service.full(), path, {}, profile)
abc5d545dbaa pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
30 for file_data in files_data:
1067
808ec98de8b3 pages (files/list): retrieve thumbnail:
Goffi <goffi@goffi.org>
parents: 1064
diff changeset
31 try:
808ec98de8b3 pages (files/list): retrieve thumbnail:
Goffi <goffi@goffi.org>
parents: 1064
diff changeset
32 extra_raw = file_data[u'extra']
808ec98de8b3 pages (files/list): retrieve thumbnail:
Goffi <goffi@goffi.org>
parents: 1064
diff changeset
33 except KeyError:
808ec98de8b3 pages (files/list): retrieve thumbnail:
Goffi <goffi@goffi.org>
parents: 1064
diff changeset
34 pass
808ec98de8b3 pages (files/list): retrieve thumbnail:
Goffi <goffi@goffi.org>
parents: 1064
diff changeset
35 else:
808ec98de8b3 pages (files/list): retrieve thumbnail:
Goffi <goffi@goffi.org>
parents: 1064
diff changeset
36 file_data[u'extra'] = json.loads(extra_raw) if extra_raw else {}
1064
abc5d545dbaa pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
37 dir_path = path_elts + [file_data['name']]
abc5d545dbaa pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
38 if file_data[u'type'] == C.FILE_TYPE_DIRECTORY:
abc5d545dbaa pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
39 page = self
abc5d545dbaa pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
40 elif file_data[u'type'] == C.FILE_TYPE_FILE:
abc5d545dbaa pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
41 page = self.getPageByName('files_view')
1067
808ec98de8b3 pages (files/list): retrieve thumbnail:
Goffi <goffi@goffi.org>
parents: 1064
diff changeset
42 try:
808ec98de8b3 pages (files/list): retrieve thumbnail:
Goffi <goffi@goffi.org>
parents: 1064
diff changeset
43 thumbnails = file_data[u'extra']['thumbnails']
808ec98de8b3 pages (files/list): retrieve thumbnail:
Goffi <goffi@goffi.org>
parents: 1064
diff changeset
44 if not thumbnails:
808ec98de8b3 pages (files/list): retrieve thumbnail:
Goffi <goffi@goffi.org>
parents: 1064
diff changeset
45 raise KeyError
808ec98de8b3 pages (files/list): retrieve thumbnail:
Goffi <goffi@goffi.org>
parents: 1064
diff changeset
46 except KeyError:
808ec98de8b3 pages (files/list): retrieve thumbnail:
Goffi <goffi@goffi.org>
parents: 1064
diff changeset
47 pass
808ec98de8b3 pages (files/list): retrieve thumbnail:
Goffi <goffi@goffi.org>
parents: 1064
diff changeset
48 else:
808ec98de8b3 pages (files/list): retrieve thumbnail:
Goffi <goffi@goffi.org>
parents: 1064
diff changeset
49 thumbnails.sort(key = lambda t: t['size'])
808ec98de8b3 pages (files/list): retrieve thumbnail:
Goffi <goffi@goffi.org>
parents: 1064
diff changeset
50 thumb = thumbnails[0]
808ec98de8b3 pages (files/list): retrieve thumbnail:
Goffi <goffi@goffi.org>
parents: 1064
diff changeset
51 for thumb_data in thumbnails:
808ec98de8b3 pages (files/list): retrieve thumbnail:
Goffi <goffi@goffi.org>
parents: 1064
diff changeset
52 if thumb_data['size'][0] > 300:
808ec98de8b3 pages (files/list): retrieve thumbnail:
Goffi <goffi@goffi.org>
parents: 1064
diff changeset
53 break
808ec98de8b3 pages (files/list): retrieve thumbnail:
Goffi <goffi@goffi.org>
parents: 1064
diff changeset
54 thumb = thumb_data
808ec98de8b3 pages (files/list): retrieve thumbnail:
Goffi <goffi@goffi.org>
parents: 1064
diff changeset
55 if u'url' in thumb:
808ec98de8b3 pages (files/list): retrieve thumbnail:
Goffi <goffi@goffi.org>
parents: 1064
diff changeset
56 file_data['thumb_url'] = thumb['url']
808ec98de8b3 pages (files/list): retrieve thumbnail:
Goffi <goffi@goffi.org>
parents: 1064
diff changeset
57 elif u'id' in thumb:
808ec98de8b3 pages (files/list): retrieve thumbnail:
Goffi <goffi@goffi.org>
parents: 1064
diff changeset
58 try:
808ec98de8b3 pages (files/list): retrieve thumbnail:
Goffi <goffi@goffi.org>
parents: 1064
diff changeset
59 thumb_path = yield self.host.bridgeCall('bobGetFile', service.full(), thumb[u'id'], profile)
808ec98de8b3 pages (files/list): retrieve thumbnail:
Goffi <goffi@goffi.org>
parents: 1064
diff changeset
60 except Exception as e:
808ec98de8b3 pages (files/list): retrieve thumbnail:
Goffi <goffi@goffi.org>
parents: 1064
diff changeset
61 log.warning(_(u"Can't retrieve thumbnail: {reason}").format(reason=e))
808ec98de8b3 pages (files/list): retrieve thumbnail:
Goffi <goffi@goffi.org>
parents: 1064
diff changeset
62 else:
808ec98de8b3 pages (files/list): retrieve thumbnail:
Goffi <goffi@goffi.org>
parents: 1064
diff changeset
63 filename = os.path.basename(thumb_path)
808ec98de8b3 pages (files/list): retrieve thumbnail:
Goffi <goffi@goffi.org>
parents: 1064
diff changeset
64 session_data = self.host.getSessionData(request, session_iface.ISATSession)
808ec98de8b3 pages (files/list): retrieve thumbnail:
Goffi <goffi@goffi.org>
parents: 1064
diff changeset
65 file_data['thumb_url'] = os.path.join(session_data.cache_dir, filename)
1064
abc5d545dbaa pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
66 else:
abc5d545dbaa pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
67 raise ValueError(u'unexpected file type: {file_type}'.format(file_type=file_data[u'type']))
abc5d545dbaa pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
68 file_data[u'url'] = page.getURL(service.full(), *dir_path)
abc5d545dbaa pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
69 template_data[u'files_data'] = files_data
abc5d545dbaa pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
70 template_data[u'path'] = path
abc5d545dbaa pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
71 if path_elts:
abc5d545dbaa pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
72 template_data[u'parent_url'] = self.getURL(service.full(), *path_elts[:-1])
abc5d545dbaa pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
73