comparison libervia/web/pages/files/view/page_meta.py @ 1518:eb00d593801d

refactoring: rename `libervia` to `libervia.web` + update imports following backend changes
author Goffi <goffi@goffi.org>
date Fri, 02 Jun 2023 16:49:28 +0200
parents libervia/pages/files/view/page_meta.py@106bae41f5c8
children ebd538cb26cb
comparison
equal deleted inserted replaced
1517:b8ed9726525b 1518:eb00d593801d
1 #!/usr/bin/env python3
2
3
4 from libervia.web.server.constants import Const as C
5 from libervia.backend.core.i18n import _
6 from twisted.web import static
7 from libervia.web.server.utils import ProgressHandler
8 import tempfile
9 import os
10 import os.path
11 from libervia.backend.core.log import getLogger
12
13 log = getLogger(__name__)
14 """files handling pages"""
15
16 name = "files_view"
17 access = C.PAGES_ACCESS_PROFILE
18
19
20 def parse_url(self, request):
21 self.get_path_args(request, ["service", "*path"], min_args=2, service="jid", path="")
22
23
24 def cleanup(__, tmp_dir, dest_path):
25 try:
26 os.unlink(dest_path)
27 except OSError:
28 log.warning(_("Can't remove temporary file {path}").format(path=dest_path))
29 try:
30 os.rmdir(tmp_dir)
31 except OSError:
32 log.warning(_("Can't remove temporary directory {path}").format(path=tmp_dir))
33
34
35 async def render(self, request):
36 data = self.get_r_data(request)
37 profile = self.get_profile(request)
38 service, path_elts = data["service"], data["path"]
39 basename = path_elts[-1]
40 dir_elts = path_elts[:-1]
41 dir_path = "/".join(dir_elts)
42 tmp_dir = tempfile.mkdtemp()
43 dest_path = os.path.join(tmp_dir, basename)
44 request.notifyFinish().addCallback(cleanup, tmp_dir, dest_path)
45 progress_id = await self.host.bridge_call(
46 "file_jingle_request",
47 service.full(),
48 dest_path,
49 basename,
50 "",
51 "",
52 {"path": dir_path},
53 profile,
54 )
55 log.debug("file requested")
56 await ProgressHandler(self.host, progress_id, profile).register()
57 log.debug("file downloaded")
58 self.delegate_to_resource(request, static.File(dest_path))