Mercurial > libervia-web
comparison libervia/web/pages/files/list/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/list/page_meta.py@106bae41f5c8 |
children |
comparison
equal
deleted
inserted
replaced
1517:b8ed9726525b | 1518:eb00d593801d |
---|---|
1 #!/usr/bin/env python3 | |
2 | |
3 import json | |
4 import os | |
5 from pathlib import Path | |
6 from libervia.backend.core.log import getLogger | |
7 from libervia.backend.tools.common import uri | |
8 from libervia.frontends.bridge.bridge_frontend import BridgeException | |
9 from libervia.web.server.constants import Const as C | |
10 from libervia.web.server import session_iface | |
11 from libervia.web.server import pages_tools | |
12 | |
13 log = getLogger(__name__) | |
14 """files handling pages""" | |
15 | |
16 name = "files_list" | |
17 access = C.PAGES_ACCESS_PROFILE | |
18 template = "file/overview.html" | |
19 | |
20 | |
21 def parse_url(self, request): | |
22 self.get_path_args(request, ["service", "*path"], min_args=1, service="jid", path="") | |
23 | |
24 | |
25 def add_breadcrumb(self, request, breadcrumbs): | |
26 data = self.get_r_data(request) | |
27 breadcrumbs.append({ | |
28 "label": data["service"], | |
29 "url": self.get_url(data["service"].full()), | |
30 "icon": "server", | |
31 }) | |
32 for idx, p in enumerate(data["path"]): | |
33 breadcrumbs.append({ | |
34 "label": p, | |
35 "url": self.get_url(data["service"].full(), *data["path"][:idx+1]), | |
36 "icon": "folder-open-empty", | |
37 }) | |
38 | |
39 | |
40 async def prepare_render(self, request): | |
41 data = self.get_r_data(request) | |
42 thumb_limit = data.get("thumb_limit", 400) | |
43 template_data = request.template_data | |
44 service, path_elts = data["service"], data["path"] | |
45 path = Path('/', *path_elts) | |
46 profile = self.get_profile(request) or C.SERVICE_PROFILE | |
47 session_data = self.host.get_session_data( | |
48 request, session_iface.IWebSession | |
49 ) | |
50 | |
51 try: | |
52 files_data = await self.host.bridge_call( | |
53 "fis_list", service.full(), str(path), {}, profile) | |
54 except BridgeException as e: | |
55 if e.condition == 'item-not-found': | |
56 log.debug( | |
57 f'"item-not-found" received for {path} at {service}, this may indicate ' | |
58 f'that the location is new') | |
59 files_data = [] | |
60 else: | |
61 raise e | |
62 for file_data in files_data: | |
63 try: | |
64 extra_raw = file_data["extra"] | |
65 except KeyError: | |
66 pass | |
67 else: | |
68 file_data["extra"] = json.loads(extra_raw) if extra_raw else {} | |
69 dir_path = path_elts + [file_data["name"]] | |
70 if file_data["type"] == C.FILE_TYPE_DIRECTORY: | |
71 page = self | |
72 elif file_data["type"] == C.FILE_TYPE_FILE: | |
73 page = self.get_page_by_name("files_view") | |
74 | |
75 # we set URL for the last thumbnail which has a size below thumb_limit | |
76 try: | |
77 thumbnails = file_data["extra"]["thumbnails"] | |
78 thumb = thumbnails[0] | |
79 for thumb_data in thumbnails: | |
80 if thumb_data["size"][0] > thumb_limit: | |
81 break | |
82 thumb = thumb_data | |
83 file_data["thumb_url"] = ( | |
84 thumb.get("url") | |
85 or os.path.join(session_data.cache_dir, thumb["filename"]) | |
86 ) | |
87 except (KeyError, IndexError): | |
88 pass | |
89 else: | |
90 raise ValueError( | |
91 "unexpected file type: {file_type}".format(file_type=file_data["type"]) | |
92 ) | |
93 file_data["url"] = page.get_url(service.full(), *dir_path) | |
94 | |
95 ## comments ## | |
96 comments_url = file_data.get("comments_url") | |
97 if comments_url: | |
98 parsed_url = uri.parse_xmpp_uri(comments_url) | |
99 comments_service = file_data["comments_service"] = parsed_url["path"] | |
100 comments_node = file_data["comments_node"] = parsed_url["node"] | |
101 try: | |
102 comments_count = file_data["comments_count"] = int( | |
103 file_data["comments_count"] | |
104 ) | |
105 except KeyError: | |
106 comments_count = None | |
107 if comments_count and data.get("retrieve_comments", False): | |
108 file_data["comments"] = await pages_tools.retrieve_comments( | |
109 self, comments_service, comments_node, profile=profile | |
110 ) | |
111 | |
112 # parent dir affiliation | |
113 # TODO: some caching? What if affiliation changes? | |
114 | |
115 try: | |
116 affiliations = await self.host.bridge_call( | |
117 "fis_affiliations_get", service.full(), "", str(path), profile | |
118 ) | |
119 except BridgeException as e: | |
120 if e.condition == 'item-not-found': | |
121 log.debug( | |
122 f'"item-not-found" received for {path} at {service}, this may indicate ' | |
123 f'that the location is new') | |
124 # FIXME: Q&D handling of empty dir (e.g. new directory/photos album) | |
125 affiliations = { | |
126 session_data.jid.userhost(): "owner" | |
127 } | |
128 if e.condition == "service-unavailable": | |
129 affiliations = {} | |
130 else: | |
131 raise e | |
132 | |
133 directory_affiliation = affiliations.get(session_data.jid.userhost()) | |
134 if directory_affiliation == "owner": | |
135 # we need to transtype dict items to str because with some bridges (D-Bus) | |
136 # we have a specific type which can't be exposed | |
137 self.expose_to_scripts( | |
138 request, | |
139 affiliations={str(e): str(a) for e, a in affiliations.items()} | |
140 ) | |
141 | |
142 template_data["directory_affiliation"] = directory_affiliation | |
143 template_data["files_data"] = files_data | |
144 template_data["path"] = path | |
145 self.expose_to_scripts( | |
146 request, | |
147 directory_affiliation=str(directory_affiliation), | |
148 files_service=service.full(), | |
149 files_path=str(path), | |
150 ) | |
151 if path_elts: | |
152 template_data["parent_url"] = self.get_url(service.full(), *path_elts[:-1]) |