Mercurial > libervia-backend
comparison libervia/backend/plugins/plugin_comp_file_sharing_management.py @ 4270:0d7bb4df2343
Reformatted code base using black.
author | Goffi <goffi@goffi.org> |
---|---|
date | Wed, 19 Jun 2024 18:44:57 +0200 |
parents | 4b842c1fb686 |
children | e9971a4b0627 |
comparison
equal
deleted
inserted
replaced
4269:64a85ce8be70 | 4270:0d7bb4df2343 |
---|---|
83 async_=True, | 83 async_=True, |
84 ) | 84 ) |
85 | 85 |
86 def profile_connected(self, client): | 86 def profile_connected(self, client): |
87 self._c.add_ad_hoc_command( | 87 self._c.add_ad_hoc_command( |
88 client, self._on_change_file, "Change Permissions of File(s)", | 88 client, |
89 self._on_change_file, | |
90 "Change Permissions of File(s)", | |
89 node=NS_FILE_MANAGEMENT_PERM, | 91 node=NS_FILE_MANAGEMENT_PERM, |
90 allowed_magics=C.ENTITY_ALL, | 92 allowed_magics=C.ENTITY_ALL, |
91 ) | 93 ) |
92 self._c.add_ad_hoc_command( | 94 self._c.add_ad_hoc_command( |
93 client, self._on_delete_file, "Delete File(s)", | 95 client, |
96 self._on_delete_file, | |
97 "Delete File(s)", | |
94 node=NS_FILE_MANAGEMENT_DELETE, | 98 node=NS_FILE_MANAGEMENT_DELETE, |
95 allowed_magics=C.ENTITY_ALL, | 99 allowed_magics=C.ENTITY_ALL, |
96 ) | 100 ) |
97 self._c.add_ad_hoc_command( | 101 self._c.add_ad_hoc_command( |
98 client, self._on_gen_thumbnails, "Generate Thumbnails", | 102 client, |
103 self._on_gen_thumbnails, | |
104 "Generate Thumbnails", | |
99 node=NS_FILE_MANAGEMENT_THUMB, | 105 node=NS_FILE_MANAGEMENT_THUMB, |
100 allowed_magics=C.ENTITY_ALL, | 106 allowed_magics=C.ENTITY_ALL, |
101 ) | 107 ) |
102 self._c.add_ad_hoc_command( | 108 self._c.add_ad_hoc_command( |
103 client, self._on_quota, "Get Quota", | 109 client, |
110 self._on_quota, | |
111 "Get Quota", | |
104 node=NS_FILE_MANAGEMENT_QUOTA, | 112 node=NS_FILE_MANAGEMENT_QUOTA, |
105 allowed_magics=C.ENTITY_ALL, | 113 allowed_magics=C.ENTITY_ALL, |
106 ) | 114 ) |
107 | 115 |
108 def _delete(self, service_jid_s, path, namespace, profile): | 116 def _delete(self, service_jid_s, path, namespace, profile): |
109 client = self.host.get_client(profile) | 117 client = self.host.get_client(profile) |
110 service_jid = jid.JID(service_jid_s) if service_jid_s else None | 118 service_jid = jid.JID(service_jid_s) if service_jid_s else None |
111 return defer.ensureDeferred(self._c.sequence( | 119 return defer.ensureDeferred( |
112 client, | 120 self._c.sequence( |
113 [{"path": path, "namespace": namespace}, {"confirm": True}], | 121 client, |
114 NS_FILE_MANAGEMENT_DELETE, | 122 [{"path": path, "namespace": namespace}, {"confirm": True}], |
115 service_jid, | 123 NS_FILE_MANAGEMENT_DELETE, |
116 )) | 124 service_jid, |
125 ) | |
126 ) | |
117 | 127 |
118 def _err(self, reason): | 128 def _err(self, reason): |
119 """Helper method to get argument to return for error | 129 """Helper method to get argument to return for error |
120 | 130 |
121 workflow will be interrupted with an error note | 131 workflow will be interrupted with an error note |
131 """Create the form to select the file to use | 141 """Create the form to select the file to use |
132 | 142 |
133 @return (tuple): arguments to use in defer.returnValue | 143 @return (tuple): arguments to use in defer.returnValue |
134 """ | 144 """ |
135 status = self._c.STATUS.EXECUTING | 145 status = self._c.STATUS.EXECUTING |
136 form = data_form.Form("form", title="File Management", | 146 form = data_form.Form( |
137 formNamespace=NS_FILE_MANAGEMENT) | 147 "form", title="File Management", formNamespace=NS_FILE_MANAGEMENT |
138 | 148 ) |
139 field = data_form.Field( | 149 |
140 "text-single", "path", required=True | 150 field = data_form.Field("text-single", "path", required=True) |
141 ) | |
142 form.addField(field) | 151 form.addField(field) |
143 | 152 |
144 field = data_form.Field( | 153 field = data_form.Field("text-single", "namespace", required=False) |
145 "text-single", "namespace", required=False | |
146 ) | |
147 form.addField(field) | 154 form.addField(field) |
148 | 155 |
149 payload = form.toElement() | 156 payload = form.toElement() |
150 return payload, status, None, None | 157 return payload, status, None, None |
151 | 158 |
157 @return (D(dict)): found file data | 164 @return (D(dict)): found file data |
158 @raise WorkflowError: something is wrong | 165 @raise WorkflowError: something is wrong |
159 """ | 166 """ |
160 fields = command_form.fields | 167 fields = command_form.fields |
161 try: | 168 try: |
162 path = fields['path'].value.strip() | 169 path = fields["path"].value.strip() |
163 namespace = fields['namespace'].value or None | 170 namespace = fields["namespace"].value or None |
164 except KeyError: | 171 except KeyError: |
165 self._c.ad_hoc_error(self._c.ERROR.BAD_PAYLOAD) | 172 self._c.ad_hoc_error(self._c.ERROR.BAD_PAYLOAD) |
166 | 173 |
167 if not path: | 174 if not path: |
168 self._c.ad_hoc_error(self._c.ERROR.BAD_PAYLOAD) | 175 self._c.ad_hoc_error(self._c.ERROR.BAD_PAYLOAD) |
169 | 176 |
170 requestor = session_data['requestor'] | 177 requestor = session_data["requestor"] |
171 requestor_bare = requestor.userhostJID() | 178 requestor_bare = requestor.userhostJID() |
172 path = path.rstrip('/') | 179 path = path.rstrip("/") |
173 parent_path, basename = os.path.split(path) | 180 parent_path, basename = os.path.split(path) |
174 | 181 |
175 # TODO: if parent_path and basename are empty, we ask for root directory | 182 # TODO: if parent_path and basename are empty, we ask for root directory |
176 # this must be managed | 183 # this must be managed |
177 | 184 |
178 try: | 185 try: |
179 found_files = await self.host.memory.get_files( | 186 found_files = await self.host.memory.get_files( |
180 client, requestor_bare, path=parent_path, name=basename, | 187 client, |
181 namespace=namespace) | 188 requestor_bare, |
189 path=parent_path, | |
190 name=basename, | |
191 namespace=namespace, | |
192 ) | |
182 found_file = found_files[0] | 193 found_file = found_files[0] |
183 except (exceptions.NotFound, IndexError): | 194 except (exceptions.NotFound, IndexError): |
184 raise WorkflowError(self._err(_("file not found"))) | 195 raise WorkflowError(self._err(_("file not found"))) |
185 except exceptions.PermissionError: | 196 except exceptions.PermissionError: |
186 raise WorkflowError(self._err(_("forbidden"))) | 197 raise WorkflowError(self._err(_("forbidden"))) |
187 | 198 |
188 if found_file['owner'] != requestor_bare: | 199 if found_file["owner"] != requestor_bare: |
189 # only owner can manage files | 200 # only owner can manage files |
190 log.warning(_("Only owner can manage files")) | 201 log.warning(_("Only owner can manage files")) |
191 raise WorkflowError(self._err(_("forbidden"))) | 202 raise WorkflowError(self._err(_("forbidden"))) |
192 | 203 |
193 session_data['found_file'] = found_file | 204 session_data["found_file"] = found_file |
194 session_data['namespace'] = namespace | 205 session_data["namespace"] = namespace |
195 return found_file | 206 return found_file |
196 | 207 |
197 def _update_read_permission(self, access, allowed_jids): | 208 def _update_read_permission(self, access, allowed_jids): |
198 if not allowed_jids: | 209 if not allowed_jids: |
199 if C.ACCESS_PERM_READ in access: | 210 if C.ACCESS_PERM_READ in access: |
200 del access[C.ACCESS_PERM_READ] | 211 del access[C.ACCESS_PERM_READ] |
201 elif allowed_jids == 'PUBLIC': | 212 elif allowed_jids == "PUBLIC": |
202 access[C.ACCESS_PERM_READ] = { | 213 access[C.ACCESS_PERM_READ] = {"type": C.ACCESS_TYPE_PUBLIC} |
203 "type": C.ACCESS_TYPE_PUBLIC | |
204 } | |
205 else: | 214 else: |
206 access[C.ACCESS_PERM_READ] = { | 215 access[C.ACCESS_PERM_READ] = { |
207 "type": C.ACCESS_TYPE_WHITELIST, | 216 "type": C.ACCESS_TYPE_WHITELIST, |
208 "jids": [j.full() for j in allowed_jids] | 217 "jids": [j.full() for j in allowed_jids], |
209 } | 218 } |
210 | 219 |
211 async def _update_dir(self, client, requestor, namespace, file_data, allowed_jids): | 220 async def _update_dir(self, client, requestor, namespace, file_data, allowed_jids): |
212 """Recursively update permission of a directory and all subdirectories | 221 """Recursively update permission of a directory and all subdirectories |
213 | 222 |
214 @param file_data(dict): metadata of the file | 223 @param file_data(dict): metadata of the file |
215 @param allowed_jids(list[jid.JID]): list of entities allowed to read the file | 224 @param allowed_jids(list[jid.JID]): list of entities allowed to read the file |
216 """ | 225 """ |
217 assert file_data['type'] == C.FILE_TYPE_DIRECTORY | 226 assert file_data["type"] == C.FILE_TYPE_DIRECTORY |
218 files_data = await self.host.memory.get_files( | 227 files_data = await self.host.memory.get_files( |
219 client, requestor, parent=file_data['id'], namespace=namespace) | 228 client, requestor, parent=file_data["id"], namespace=namespace |
229 ) | |
220 | 230 |
221 for file_data in files_data: | 231 for file_data in files_data: |
222 if not file_data['access'].get(C.ACCESS_PERM_READ, {}): | 232 if not file_data["access"].get(C.ACCESS_PERM_READ, {}): |
223 log.debug("setting {perm} read permission for {name}".format( | 233 log.debug( |
224 perm=allowed_jids, name=file_data['name'])) | 234 "setting {perm} read permission for {name}".format( |
235 perm=allowed_jids, name=file_data["name"] | |
236 ) | |
237 ) | |
225 await self.host.memory.file_update( | 238 await self.host.memory.file_update( |
226 file_data['id'], 'access', | 239 file_data["id"], |
227 partial(self._update_read_permission, allowed_jids=allowed_jids)) | 240 "access", |
228 if file_data['type'] == C.FILE_TYPE_DIRECTORY: | 241 partial(self._update_read_permission, allowed_jids=allowed_jids), |
229 await self._update_dir(client, requestor, namespace, file_data, 'PUBLIC') | 242 ) |
243 if file_data["type"] == C.FILE_TYPE_DIRECTORY: | |
244 await self._update_dir(client, requestor, namespace, file_data, "PUBLIC") | |
230 | 245 |
231 async def _on_change_file(self, client, command_elt, session_data, action, node): | 246 async def _on_change_file(self, client, command_elt, session_data, action, node): |
232 try: | 247 try: |
233 x_elt = next(command_elt.elements(data_form.NS_X_DATA, "x")) | 248 x_elt = next(command_elt.elements(data_form.NS_X_DATA, "x")) |
234 command_form = data_form.Form.fromElement(x_elt) | 249 command_form = data_form.Form.fromElement(x_elt) |
235 except StopIteration: | 250 except StopIteration: |
236 command_form = None | 251 command_form = None |
237 | 252 |
238 found_file = session_data.get('found_file') | 253 found_file = session_data.get("found_file") |
239 requestor = session_data['requestor'] | 254 requestor = session_data["requestor"] |
240 requestor_bare = requestor.userhostJID() | 255 requestor_bare = requestor.userhostJID() |
241 | 256 |
242 if command_form is None or len(command_form.fields) == 0: | 257 if command_form is None or len(command_form.fields) == 0: |
243 # root request | 258 # root request |
244 return self._get_root_args() | 259 return self._get_root_args() |
249 found_file = await self._get_file_data(client, session_data, command_form) | 264 found_file = await self._get_file_data(client, session_data, command_form) |
250 except WorkflowError as e: | 265 except WorkflowError as e: |
251 return e.err_args | 266 return e.err_args |
252 | 267 |
253 # management request | 268 # management request |
254 if found_file['type'] == C.FILE_TYPE_DIRECTORY: | 269 if found_file["type"] == C.FILE_TYPE_DIRECTORY: |
255 instructions = D_("Please select permissions for this directory") | 270 instructions = D_("Please select permissions for this directory") |
256 else: | 271 else: |
257 instructions = D_("Please select permissions for this file") | 272 instructions = D_("Please select permissions for this file") |
258 | 273 |
259 form = data_form.Form("form", title="File Management", | 274 form = data_form.Form( |
260 instructions=[instructions], | 275 "form", |
261 formNamespace=NS_FILE_MANAGEMENT) | 276 title="File Management", |
277 instructions=[instructions], | |
278 formNamespace=NS_FILE_MANAGEMENT, | |
279 ) | |
262 field = data_form.Field( | 280 field = data_form.Field( |
263 "text-multi", "read_allowed", required=False, | 281 "text-multi", |
264 desc='list of jids allowed to read this file (beside yourself), or ' | 282 "read_allowed", |
265 '"PUBLIC" to let a public access' | 283 required=False, |
284 desc="list of jids allowed to read this file (beside yourself), or " | |
285 '"PUBLIC" to let a public access', | |
266 ) | 286 ) |
267 read_access = found_file["access"].get(C.ACCESS_PERM_READ, {}) | 287 read_access = found_file["access"].get(C.ACCESS_PERM_READ, {}) |
268 access_type = read_access.get('type', C.ACCESS_TYPE_WHITELIST) | 288 access_type = read_access.get("type", C.ACCESS_TYPE_WHITELIST) |
269 if access_type == C.ACCESS_TYPE_PUBLIC: | 289 if access_type == C.ACCESS_TYPE_PUBLIC: |
270 field.values = ['PUBLIC'] | 290 field.values = ["PUBLIC"] |
271 else: | 291 else: |
272 field.values = read_access.get('jids', []) | 292 field.values = read_access.get("jids", []) |
273 form.addField(field) | 293 form.addField(field) |
274 if found_file['type'] == C.FILE_TYPE_DIRECTORY: | 294 if found_file["type"] == C.FILE_TYPE_DIRECTORY: |
275 field = data_form.Field( | 295 field = data_form.Field( |
276 "boolean", "recursive", value=False, required=False, | 296 "boolean", |
297 "recursive", | |
298 value=False, | |
299 required=False, | |
277 desc="Files under it will be made public to follow this dir " | 300 desc="Files under it will be made public to follow this dir " |
278 "permission (only if they don't have already a permission set)." | 301 "permission (only if they don't have already a permission set).", |
279 ) | 302 ) |
280 form.addField(field) | 303 form.addField(field) |
281 | 304 |
282 status = self._c.STATUS.EXECUTING | 305 status = self._c.STATUS.EXECUTING |
283 payload = form.toElement() | 306 payload = form.toElement() |
284 return (payload, status, None, None) | 307 return (payload, status, None, None) |
285 | 308 |
286 else: | 309 else: |
287 # final phase, we'll do permission change here | 310 # final phase, we'll do permission change here |
288 try: | 311 try: |
289 read_allowed = command_form.fields['read_allowed'] | 312 read_allowed = command_form.fields["read_allowed"] |
290 except KeyError: | 313 except KeyError: |
291 self._c.ad_hoc_error(self._c.ERROR.BAD_PAYLOAD) | 314 self._c.ad_hoc_error(self._c.ERROR.BAD_PAYLOAD) |
292 | 315 |
293 if read_allowed.value == 'PUBLIC': | 316 if read_allowed.value == "PUBLIC": |
294 allowed_jids = 'PUBLIC' | 317 allowed_jids = "PUBLIC" |
295 elif read_allowed.value.strip() == '': | 318 elif read_allowed.value.strip() == "": |
296 allowed_jids = None | 319 allowed_jids = None |
297 else: | 320 else: |
298 try: | 321 try: |
299 allowed_jids = [jid.JID(v.strip()) for v in read_allowed.values | 322 allowed_jids = [ |
300 if v.strip()] | 323 jid.JID(v.strip()) for v in read_allowed.values if v.strip() |
324 ] | |
301 except RuntimeError as e: | 325 except RuntimeError as e: |
302 log.warning(_("Can't use read_allowed values: {reason}").format( | 326 log.warning( |
303 reason=e)) | 327 _("Can't use read_allowed values: {reason}").format(reason=e) |
328 ) | |
304 self._c.ad_hoc_error(self._c.ERROR.BAD_PAYLOAD) | 329 self._c.ad_hoc_error(self._c.ERROR.BAD_PAYLOAD) |
305 | 330 |
306 if found_file['type'] == C.FILE_TYPE_FILE: | 331 if found_file["type"] == C.FILE_TYPE_FILE: |
307 await self.host.memory.file_update( | 332 await self.host.memory.file_update( |
308 found_file['id'], 'access', | 333 found_file["id"], |
309 partial(self._update_read_permission, allowed_jids=allowed_jids)) | 334 "access", |
335 partial(self._update_read_permission, allowed_jids=allowed_jids), | |
336 ) | |
310 else: | 337 else: |
311 try: | 338 try: |
312 recursive = command_form.fields['recursive'] | 339 recursive = command_form.fields["recursive"] |
313 except KeyError: | 340 except KeyError: |
314 self._c.ad_hoc_error(self._c.ERROR.BAD_PAYLOAD) | 341 self._c.ad_hoc_error(self._c.ERROR.BAD_PAYLOAD) |
315 await self.host.memory.file_update( | 342 await self.host.memory.file_update( |
316 found_file['id'], 'access', | 343 found_file["id"], |
317 partial(self._update_read_permission, allowed_jids=allowed_jids)) | 344 "access", |
345 partial(self._update_read_permission, allowed_jids=allowed_jids), | |
346 ) | |
318 if recursive: | 347 if recursive: |
319 # we set all file under the directory as public (if they haven't | 348 # we set all file under the directory as public (if they haven't |
320 # already a permission set), so allowed entities of root directory | 349 # already a permission set), so allowed entities of root directory |
321 # can read them. | 350 # can read them. |
322 namespace = session_data['namespace'] | 351 namespace = session_data["namespace"] |
323 await self._update_dir( | 352 await self._update_dir( |
324 client, requestor_bare, namespace, found_file, 'PUBLIC') | 353 client, requestor_bare, namespace, found_file, "PUBLIC" |
354 ) | |
325 | 355 |
326 # job done, we can end the session | 356 # job done, we can end the session |
327 status = self._c.STATUS.COMPLETED | 357 status = self._c.STATUS.COMPLETED |
328 payload = None | 358 payload = None |
329 note = (self._c.NOTE.INFO, _("management session done")) | 359 note = (self._c.NOTE.INFO, _("management session done")) |
334 x_elt = next(command_elt.elements(data_form.NS_X_DATA, "x")) | 364 x_elt = next(command_elt.elements(data_form.NS_X_DATA, "x")) |
335 command_form = data_form.Form.fromElement(x_elt) | 365 command_form = data_form.Form.fromElement(x_elt) |
336 except StopIteration: | 366 except StopIteration: |
337 command_form = None | 367 command_form = None |
338 | 368 |
339 found_file = session_data.get('found_file') | 369 found_file = session_data.get("found_file") |
340 requestor = session_data['requestor'] | 370 requestor = session_data["requestor"] |
341 requestor_bare = requestor.userhostJID() | 371 requestor_bare = requestor.userhostJID() |
342 | 372 |
343 if command_form is None or len(command_form.fields) == 0: | 373 if command_form is None or len(command_form.fields) == 0: |
344 # root request | 374 # root request |
345 return self._get_root_args() | 375 return self._get_root_args() |
348 # file selected, we need confirmation before actually deleting | 378 # file selected, we need confirmation before actually deleting |
349 try: | 379 try: |
350 found_file = await self._get_file_data(client, session_data, command_form) | 380 found_file = await self._get_file_data(client, session_data, command_form) |
351 except WorkflowError as e: | 381 except WorkflowError as e: |
352 return e.err_args | 382 return e.err_args |
353 if found_file['type'] == C.FILE_TYPE_DIRECTORY: | 383 if found_file["type"] == C.FILE_TYPE_DIRECTORY: |
354 msg = D_("Are you sure to delete directory {name} and all files and " | 384 msg = D_( |
355 "directories under it?").format(name=found_file['name']) | 385 "Are you sure to delete directory {name} and all files and " |
386 "directories under it?" | |
387 ).format(name=found_file["name"]) | |
356 else: | 388 else: |
357 msg = D_("Are you sure to delete file {name}?" | 389 msg = D_( |
358 .format(name=found_file['name'])) | 390 "Are you sure to delete file {name}?".format(name=found_file["name"]) |
359 form = data_form.Form("form", title="File Management", | 391 ) |
360 instructions = [msg], | 392 form = data_form.Form( |
361 formNamespace=NS_FILE_MANAGEMENT) | 393 "form", |
394 title="File Management", | |
395 instructions=[msg], | |
396 formNamespace=NS_FILE_MANAGEMENT, | |
397 ) | |
362 field = data_form.Field( | 398 field = data_form.Field( |
363 "boolean", "confirm", value=False, required=True, | 399 "boolean", |
364 desc="check this box to confirm" | 400 "confirm", |
401 value=False, | |
402 required=True, | |
403 desc="check this box to confirm", | |
365 ) | 404 ) |
366 form.addField(field) | 405 form.addField(field) |
367 status = self._c.STATUS.EXECUTING | 406 status = self._c.STATUS.EXECUTING |
368 payload = form.toElement() | 407 payload = form.toElement() |
369 return (payload, status, None, None) | 408 return (payload, status, None, None) |
370 | 409 |
371 else: | 410 else: |
372 # final phase, we'll do deletion here | 411 # final phase, we'll do deletion here |
373 try: | 412 try: |
374 confirmed = C.bool(command_form.fields['confirm'].value) | 413 confirmed = C.bool(command_form.fields["confirm"].value) |
375 except KeyError: | 414 except KeyError: |
376 self._c.ad_hoc_error(self._c.ERROR.BAD_PAYLOAD) | 415 self._c.ad_hoc_error(self._c.ERROR.BAD_PAYLOAD) |
377 if not confirmed: | 416 if not confirmed: |
378 note = None | 417 note = None |
379 else: | 418 else: |
380 recursive = found_file['type'] == C.FILE_TYPE_DIRECTORY | 419 recursive = found_file["type"] == C.FILE_TYPE_DIRECTORY |
381 await self.host.memory.file_delete( | 420 await self.host.memory.file_delete( |
382 client, requestor_bare, found_file['id'], recursive) | 421 client, requestor_bare, found_file["id"], recursive |
422 ) | |
383 note = (self._c.NOTE.INFO, _("file deleted")) | 423 note = (self._c.NOTE.INFO, _("file deleted")) |
384 status = self._c.STATUS.COMPLETED | 424 status = self._c.STATUS.COMPLETED |
385 payload = None | 425 payload = None |
386 return (payload, status, None, note) | 426 return (payload, status, None, note) |
387 | 427 |
391 async def _gen_thumbs(self, client, requestor, namespace, file_data): | 431 async def _gen_thumbs(self, client, requestor, namespace, file_data): |
392 """Recursively generate thumbnails | 432 """Recursively generate thumbnails |
393 | 433 |
394 @param file_data(dict): metadata of the file | 434 @param file_data(dict): metadata of the file |
395 """ | 435 """ |
396 if file_data['type'] == C.FILE_TYPE_DIRECTORY: | 436 if file_data["type"] == C.FILE_TYPE_DIRECTORY: |
397 sub_files_data = await self.host.memory.get_files( | 437 sub_files_data = await self.host.memory.get_files( |
398 client, requestor, parent=file_data['id'], namespace=namespace) | 438 client, requestor, parent=file_data["id"], namespace=namespace |
439 ) | |
399 for sub_file_data in sub_files_data: | 440 for sub_file_data in sub_files_data: |
400 await self._gen_thumbs(client, requestor, namespace, sub_file_data) | 441 await self._gen_thumbs(client, requestor, namespace, sub_file_data) |
401 | 442 |
402 elif file_data['type'] == C.FILE_TYPE_FILE: | 443 elif file_data["type"] == C.FILE_TYPE_FILE: |
403 media_type = file_data['media_type'] | 444 media_type = file_data["media_type"] |
404 file_path = os.path.join(self.files_path, file_data['file_hash']) | 445 file_path = os.path.join(self.files_path, file_data["file_hash"]) |
405 if media_type == 'image': | 446 if media_type == "image": |
406 thumbnails = [] | 447 thumbnails = [] |
407 | 448 |
408 for max_thumb_size in self._t.SIZES: | 449 for max_thumb_size in self._t.SIZES: |
409 try: | 450 try: |
410 thumb_size, thumb_id = await self._t.generate_thumbnail( | 451 thumb_size, thumb_id = await self._t.generate_thumbnail( |
412 max_thumb_size, | 453 max_thumb_size, |
413 # we keep thumbnails for 6 months | 454 # we keep thumbnails for 6 months |
414 60 * 60 * 24 * 31 * 6, | 455 60 * 60 * 24 * 31 * 6, |
415 ) | 456 ) |
416 except Exception as e: | 457 except Exception as e: |
417 log.warning(_("Can't create thumbnail: {reason}") | 458 log.warning( |
418 .format(reason=e)) | 459 _("Can't create thumbnail: {reason}").format(reason=e) |
460 ) | |
419 break | 461 break |
420 thumbnails.append({"id": thumb_id, "size": thumb_size}) | 462 thumbnails.append({"id": thumb_id, "size": thumb_size}) |
421 | 463 |
422 await self.host.memory.file_update( | 464 await self.host.memory.file_update( |
423 file_data['id'], 'extra', | 465 file_data["id"], |
424 partial(self._update_thumbs, thumbnails=thumbnails)) | 466 "extra", |
425 | 467 partial(self._update_thumbs, thumbnails=thumbnails), |
426 log.info("thumbnails for [{file_name}] generated" | 468 ) |
427 .format(file_name=file_data['name'])) | 469 |
470 log.info( | |
471 "thumbnails for [{file_name}] generated".format( | |
472 file_name=file_data["name"] | |
473 ) | |
474 ) | |
428 | 475 |
429 else: | 476 else: |
430 log.warning("unmanaged file type: {type_}".format(type_=file_data['type'])) | 477 log.warning("unmanaged file type: {type_}".format(type_=file_data["type"])) |
431 | 478 |
432 async def _on_gen_thumbnails(self, client, command_elt, session_data, action, node): | 479 async def _on_gen_thumbnails(self, client, command_elt, session_data, action, node): |
433 try: | 480 try: |
434 x_elt = next(command_elt.elements(data_form.NS_X_DATA, "x")) | 481 x_elt = next(command_elt.elements(data_form.NS_X_DATA, "x")) |
435 command_form = data_form.Form.fromElement(x_elt) | 482 command_form = data_form.Form.fromElement(x_elt) |
436 except StopIteration: | 483 except StopIteration: |
437 command_form = None | 484 command_form = None |
438 | 485 |
439 found_file = session_data.get('found_file') | 486 found_file = session_data.get("found_file") |
440 requestor = session_data['requestor'] | 487 requestor = session_data["requestor"] |
441 | 488 |
442 if command_form is None or len(command_form.fields) == 0: | 489 if command_form is None or len(command_form.fields) == 0: |
443 # root request | 490 # root request |
444 return self._get_root_args() | 491 return self._get_root_args() |
445 | 492 |
449 found_file = await self._get_file_data(client, session_data, command_form) | 496 found_file = await self._get_file_data(client, session_data, command_form) |
450 except WorkflowError as e: | 497 except WorkflowError as e: |
451 return e.err_args | 498 return e.err_args |
452 | 499 |
453 log.info("Generating thumbnails as requested") | 500 log.info("Generating thumbnails as requested") |
454 await self._gen_thumbs(client, requestor, found_file['namespace'], found_file) | 501 await self._gen_thumbs(client, requestor, found_file["namespace"], found_file) |
455 | 502 |
456 # job done, we can end the session | 503 # job done, we can end the session |
457 status = self._c.STATUS.COMPLETED | 504 status = self._c.STATUS.COMPLETED |
458 payload = None | 505 payload = None |
459 note = (self._c.NOTE.INFO, _("thumbnails generated")) | 506 note = (self._c.NOTE.INFO, _("thumbnails generated")) |
460 return (payload, status, None, note) | 507 return (payload, status, None, note) |
461 | 508 |
462 async def _on_quota(self, client, command_elt, session_data, action, node): | 509 async def _on_quota(self, client, command_elt, session_data, action, node): |
463 requestor = session_data['requestor'] | 510 requestor = session_data["requestor"] |
464 quota = self.host.plugins["file_sharing"].get_quota(client, requestor) | 511 quota = self.host.plugins["file_sharing"].get_quota(client, requestor) |
465 try: | 512 try: |
466 size_used = await self.host.memory.file_get_used_space(client, requestor) | 513 size_used = await self.host.memory.file_get_used_space(client, requestor) |
467 except exceptions.PermissionError: | 514 except exceptions.PermissionError: |
468 raise WorkflowError(self._err(_("forbidden"))) | 515 raise WorkflowError(self._err(_("forbidden"))) |
471 form.makeFields({"quota": quota, "user": size_used}) | 518 form.makeFields({"quota": quota, "user": size_used}) |
472 payload = form.toElement() | 519 payload = form.toElement() |
473 note = ( | 520 note = ( |
474 self._c.NOTE.INFO, | 521 self._c.NOTE.INFO, |
475 _("You are currently using {size_used} on {size_quota}").format( | 522 _("You are currently using {size_used} on {size_quota}").format( |
476 size_used = utils.get_human_size(size_used), | 523 size_used=utils.get_human_size(size_used), |
477 size_quota = ( | 524 size_quota=( |
478 _("unlimited quota") if quota is None | 525 _("unlimited quota") if quota is None else utils.get_human_size(quota) |
479 else utils.get_human_size(quota) | 526 ), |
480 ) | 527 ), |
481 ) | |
482 ) | 528 ) |
483 return (payload, status, None, note) | 529 return (payload, status, None, note) |