annotate 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
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
abc5d545dbaa pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
5 from sat.core.i18n import _
abc5d545dbaa pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
6 from twisted.internet import defer
abc5d545dbaa pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
7 from twisted.web import static
abc5d545dbaa pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
8 from libervia.server.utils import ProgressHandler
abc5d545dbaa pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
9 import tempfile
abc5d545dbaa pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
10 import os
abc5d545dbaa pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
11 import os.path
abc5d545dbaa pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
12 from sat.core.log import getLogger
abc5d545dbaa pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
13 log = getLogger('pages/file')
abc5d545dbaa pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
14 """files handling pages"""
abc5d545dbaa pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
15
abc5d545dbaa pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
16 name = u'files_view'
abc5d545dbaa pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
17 access = C.PAGES_ACCESS_PROFILE
abc5d545dbaa pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
18
abc5d545dbaa pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
19 def parse_url(self, request):
abc5d545dbaa pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
20 self.getPathArgs(request, ['service', '*path'], min_args=2, service='jid', path='')
abc5d545dbaa pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
21
abc5d545dbaa pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
22 def cleanup(dummy, tmp_dir, dest_path):
abc5d545dbaa pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
23 try:
abc5d545dbaa pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
24 os.unlink(dest_path)
abc5d545dbaa pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
25 except OSError:
abc5d545dbaa pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
26 log.warning(_(u"Can't remove temporary file {path}").format(path=dest_path))
abc5d545dbaa pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
27 try:
abc5d545dbaa pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
28 os.rmdir(tmp_dir)
abc5d545dbaa pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
29 except OSError:
abc5d545dbaa pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
30 log.warning(_(u"Can't remove temporary directory {path}").format(path=tmp_dir))
abc5d545dbaa pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
31
abc5d545dbaa pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
32
abc5d545dbaa pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
33 @defer.inlineCallbacks
abc5d545dbaa pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
34 def render(self, request):
abc5d545dbaa pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
35 data = self.getRData(request)
abc5d545dbaa pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
36 profile = self.getProfile(request)
abc5d545dbaa pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
37 service, path_elts = data[u'service'], data[u'path']
abc5d545dbaa pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
38 basename = path_elts[-1]
abc5d545dbaa pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
39 dir_elts = path_elts[:-1]
abc5d545dbaa pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
40 dir_path = u'/'.join(dir_elts)
abc5d545dbaa pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
41 tmp_dir = tempfile.mkdtemp()
abc5d545dbaa pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
42 dest_path = os.path.join(tmp_dir, basename)
abc5d545dbaa pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
43 request.notifyFinish().addCallback(cleanup, tmp_dir, dest_path)
abc5d545dbaa pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
44 progress_id = yield self.host.bridgeCall('fileJingleRequest',
abc5d545dbaa pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
45 service.full(),
abc5d545dbaa pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
46 dest_path,
abc5d545dbaa pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
47 basename,
abc5d545dbaa pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
48 u'',
abc5d545dbaa pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
49 u'',
abc5d545dbaa pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
50 {u'path': dir_path},
abc5d545dbaa pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
51 profile)
abc5d545dbaa pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
52 log.debug(u'file requested')
abc5d545dbaa pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
53 yield ProgressHandler(self.host, progress_id, profile).register()
abc5d545dbaa pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
54 log.debug(u'file downloaded')
abc5d545dbaa pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
55 self.delegateToResource(request, static.File(dest_path))