comparison src/pages/files/view/page_meta.py @ 1064:abc5d545dbaa

pages (files): files sharing first draft: 2 subpages are available at the moment: - "list" to list shared files from an entity - "view" to request the file and send it via HTTP
author Goffi <goffi@goffi.org>
date Sun, 11 Mar 2018 19:33:38 +0100
parents
children 2dab7692eae7
comparison
equal deleted inserted replaced
1063:4b69f69c6ffd 1064:abc5d545dbaa
1 #!/usr/bin/env python2.7
2 # -*- coding: utf-8 -*-
3
4 from libervia.server.constants import Const as C
5 from sat.core.i18n import _
6 from twisted.internet import defer
7 from twisted.web import static
8 from libervia.server.utils import ProgressHandler
9 import tempfile
10 import os
11 import os.path
12 from sat.core.log import getLogger
13 log = getLogger('pages/file')
14 """files handling pages"""
15
16 name = u'files_view'
17 access = C.PAGES_ACCESS_PROFILE
18
19 def parse_url(self, request):
20 self.getPathArgs(request, ['service', '*path'], min_args=2, service='jid', path='')
21
22 def cleanup(dummy, tmp_dir, dest_path):
23 try:
24 os.unlink(dest_path)
25 except OSError:
26 log.warning(_(u"Can't remove temporary file {path}").format(path=dest_path))
27 try:
28 os.rmdir(tmp_dir)
29 except OSError:
30 log.warning(_(u"Can't remove temporary directory {path}").format(path=tmp_dir))
31
32
33 @defer.inlineCallbacks
34 def render(self, request):
35 data = self.getRData(request)
36 profile = self.getProfile(request)
37 service, path_elts = data[u'service'], data[u'path']
38 basename = path_elts[-1]
39 dir_elts = path_elts[:-1]
40 dir_path = u'/'.join(dir_elts)
41 tmp_dir = tempfile.mkdtemp()
42 dest_path = os.path.join(tmp_dir, basename)
43 request.notifyFinish().addCallback(cleanup, tmp_dir, dest_path)
44 progress_id = yield self.host.bridgeCall('fileJingleRequest',
45 service.full(),
46 dest_path,
47 basename,
48 u'',
49 u'',
50 {u'path': dir_path},
51 profile)
52 log.debug(u'file requested')
53 yield ProgressHandler(self.host, progress_id, profile).register()
54 log.debug(u'file downloaded')
55 self.delegateToResource(request, static.File(dest_path))