annotate libervia/backend/plugins/plugin_comp_file_sharing_management.py @ 4332:71c939e34ca6

XEP-0373 (OX): Adjust to gpgme updates: generate with explicit algorithm and subkeys
author Syndace <me@syndace.dev>
date Sat, 13 Jul 2024 18:28:28 +0200
parents 0d7bb4df2343
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2934
diff changeset
1 #!/usr/bin/env python3
3137
559a625a236b fixed shebangs
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
2
3531
a8259a1f89b2 component file sharing (management): add values of `quota` and `used` size on result form
Goffi <goffi@goffi.org>
parents: 3529
diff changeset
3 # Libervia plugin to manage file sharing component through ad-hoc commands
3479
be6d91572633 date update
Goffi <goffi@goffi.org>
parents: 3329
diff changeset
4 # Copyright (C) 2009-2021 Jérôme Poisson (goffi@goffi.org)
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
5
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
6 # This program is free software: you can redistribute it and/or modify
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
7 # it under the terms of the GNU Affero General Public License as published by
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
8 # the Free Software Foundation, either version 3 of the License, or
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
9 # (at your option) any later version.
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
10
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
11 # This program is distributed in the hope that it will be useful,
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
14 # GNU Affero General Public License for more details.
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
15
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
16 # You should have received a copy of the GNU Affero General Public License
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
18
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
19 import os.path
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
20 from functools import partial
3531
a8259a1f89b2 component file sharing (management): add values of `quota` and `used` size on result form
Goffi <goffi@goffi.org>
parents: 3529
diff changeset
21 from wokkel import data_form
a8259a1f89b2 component file sharing (management): add values of `quota` and `used` size on result form
Goffi <goffi@goffi.org>
parents: 3529
diff changeset
22 from twisted.internet import defer
a8259a1f89b2 component file sharing (management): add values of `quota` and `used` size on result form
Goffi <goffi@goffi.org>
parents: 3529
diff changeset
23 from twisted.words.protocols.jabber import jid
4071
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
24 from libervia.backend.core.i18n import _, D_
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
25 from libervia.backend.core import exceptions
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
26 from libervia.backend.core.constants import Const as C
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
27 from libervia.backend.core.log import getLogger
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
28 from libervia.backend.tools.common import utils
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
29
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
30 log = getLogger(__name__)
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
31
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
32
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
33 PLUGIN_INFO = {
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2934
diff changeset
34 C.PI_NAME: "File Sharing Management",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2934
diff changeset
35 C.PI_IMPORT_NAME: "FILE_SHARING_MANAGEMENT",
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
36 C.PI_MODES: [C.PLUG_MODE_COMPONENT],
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2934
diff changeset
37 C.PI_TYPE: "EXP",
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
38 C.PI_PROTOCOLS: [],
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2934
diff changeset
39 C.PI_DEPENDENCIES: ["XEP-0050", "XEP-0264"],
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
40 C.PI_RECOMMENDATIONS: [],
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2934
diff changeset
41 C.PI_MAIN: "FileSharingManagement",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2934
diff changeset
42 C.PI_HANDLER: "no",
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
43 C.PI_DESCRIPTION: _(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2934
diff changeset
44 "Experimental handling of file management for file sharing. This plugins allows "
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2934
diff changeset
45 "to change permissions of stored files/directories or remove them."
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
46 ),
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
47 }
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
48
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2934
diff changeset
49 NS_FILE_MANAGEMENT = "https://salut-a-toi.org/protocol/file-management:0"
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2934
diff changeset
50 NS_FILE_MANAGEMENT_PERM = "https://salut-a-toi.org/protocol/file-management:0#perm"
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2934
diff changeset
51 NS_FILE_MANAGEMENT_DELETE = "https://salut-a-toi.org/protocol/file-management:0#delete"
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2934
diff changeset
52 NS_FILE_MANAGEMENT_THUMB = "https://salut-a-toi.org/protocol/file-management:0#thumb"
3529
698579bedd6f component file sharing (plugin management): new command to get available quota/used space.
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
53 NS_FILE_MANAGEMENT_QUOTA = "https://salut-a-toi.org/protocol/file-management:0#quota"
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
54
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
55
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
56 class WorkflowError(Exception):
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
57 """Raised when workflow can't be completed"""
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
58
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
59 def __init__(self, err_args):
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
60 """
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
61 @param err_args(tuple): arguments to return to finish the command workflow
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
62 """
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
63 Exception.__init__(self)
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
64 self.err_args = err_args
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
65
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
66
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
67 class FileSharingManagement(object):
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
68 # This is a temporary way (Q&D) to handle stored files, a better way (using pubsub
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
69 # syntax?) should be elaborated and proposed as a standard.
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
70
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
71 def __init__(self, host):
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2934
diff changeset
72 log.info(_("File Sharing Management plugin initialization"))
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
73 self.host = host
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
74 self._c = host.plugins["XEP-0050"]
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
75 self._t = host.plugins["XEP-0264"]
3932
7af29260ecb8 core: fix and renamed getLocalPath -> get_local_path:
Goffi <goffi@goffi.org>
parents: 3541
diff changeset
76 self.files_path = host.get_local_path(None, C.FILES_DIR)
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3932
diff changeset
77 host.bridge.add_method(
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3932
diff changeset
78 "file_sharing_delete",
3305
04a7d77ed86f plugin file sharing management: added a fileSharingDelete method
Goffi <goffi@goffi.org>
parents: 3298
diff changeset
79 ".plugin",
04a7d77ed86f plugin file sharing management: added a fileSharingDelete method
Goffi <goffi@goffi.org>
parents: 3298
diff changeset
80 in_sign="ssss",
04a7d77ed86f plugin file sharing management: added a fileSharingDelete method
Goffi <goffi@goffi.org>
parents: 3298
diff changeset
81 out_sign="",
04a7d77ed86f plugin file sharing management: added a fileSharingDelete method
Goffi <goffi@goffi.org>
parents: 3298
diff changeset
82 method=self._delete,
04a7d77ed86f plugin file sharing management: added a fileSharingDelete method
Goffi <goffi@goffi.org>
parents: 3298
diff changeset
83 async_=True,
04a7d77ed86f plugin file sharing management: added a fileSharingDelete method
Goffi <goffi@goffi.org>
parents: 3298
diff changeset
84 )
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
85
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3932
diff changeset
86 def profile_connected(self, client):
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3932
diff changeset
87 self._c.add_ad_hoc_command(
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
88 client,
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
89 self._on_change_file,
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
90 "Change Permissions of File(s)",
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
91 node=NS_FILE_MANAGEMENT_PERM,
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
92 allowed_magics=C.ENTITY_ALL,
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
93 )
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3932
diff changeset
94 self._c.add_ad_hoc_command(
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
95 client,
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
96 self._on_delete_file,
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
97 "Delete File(s)",
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
98 node=NS_FILE_MANAGEMENT_DELETE,
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
99 allowed_magics=C.ENTITY_ALL,
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
100 )
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3932
diff changeset
101 self._c.add_ad_hoc_command(
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
102 client,
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
103 self._on_gen_thumbnails,
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
104 "Generate Thumbnails",
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
105 node=NS_FILE_MANAGEMENT_THUMB,
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
106 allowed_magics=C.ENTITY_ALL,
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
107 )
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3932
diff changeset
108 self._c.add_ad_hoc_command(
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
109 client,
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
110 self._on_quota,
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
111 "Get Quota",
3529
698579bedd6f component file sharing (plugin management): new command to get available quota/used space.
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
112 node=NS_FILE_MANAGEMENT_QUOTA,
698579bedd6f component file sharing (plugin management): new command to get available quota/used space.
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
113 allowed_magics=C.ENTITY_ALL,
698579bedd6f component file sharing (plugin management): new command to get available quota/used space.
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
114 )
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
115
3305
04a7d77ed86f plugin file sharing management: added a fileSharingDelete method
Goffi <goffi@goffi.org>
parents: 3298
diff changeset
116 def _delete(self, service_jid_s, path, namespace, profile):
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3932
diff changeset
117 client = self.host.get_client(profile)
3305
04a7d77ed86f plugin file sharing management: added a fileSharingDelete method
Goffi <goffi@goffi.org>
parents: 3298
diff changeset
118 service_jid = jid.JID(service_jid_s) if service_jid_s else None
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
119 return defer.ensureDeferred(
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
120 self._c.sequence(
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
121 client,
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
122 [{"path": path, "namespace": namespace}, {"confirm": True}],
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
123 NS_FILE_MANAGEMENT_DELETE,
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
124 service_jid,
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
125 )
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
126 )
3305
04a7d77ed86f plugin file sharing management: added a fileSharingDelete method
Goffi <goffi@goffi.org>
parents: 3298
diff changeset
127
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
128 def _err(self, reason):
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
129 """Helper method to get argument to return for error
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
130
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
131 workflow will be interrupted with an error note
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
132 @param reason(unicode): reason of the error
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
133 @return (tuple): arguments to use in defer.returnValue
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
134 """
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
135 status = self._c.STATUS.COMPLETED
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
136 payload = None
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
137 note = (self._c.NOTE.ERROR, reason)
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
138 return payload, status, None, note
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
139
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3932
diff changeset
140 def _get_root_args(self):
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
141 """Create the form to select the file to use
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
142
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
143 @return (tuple): arguments to use in defer.returnValue
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
144 """
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
145 status = self._c.STATUS.EXECUTING
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
146 form = data_form.Form(
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
147 "form", title="File Management", formNamespace=NS_FILE_MANAGEMENT
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
148 )
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
149
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
150 field = data_form.Field("text-single", "path", required=True)
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
151 form.addField(field)
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
152
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
153 field = data_form.Field("text-single", "namespace", required=False)
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
154 form.addField(field)
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
155
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
156 payload = form.toElement()
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
157 return payload, status, None, None
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
158
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3932
diff changeset
159 async def _get_file_data(self, client, session_data, command_form):
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
160 """Retrieve field requested in root form
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
161
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
162 "found_file" will also be set in session_data
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
163 @param command_form(data_form.Form): response to root form
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
164 @return (D(dict)): found file data
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
165 @raise WorkflowError: something is wrong
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
166 """
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
167 fields = command_form.fields
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
168 try:
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
169 path = fields["path"].value.strip()
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
170 namespace = fields["namespace"].value or None
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
171 except KeyError:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3932
diff changeset
172 self._c.ad_hoc_error(self._c.ERROR.BAD_PAYLOAD)
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
173
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
174 if not path:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3932
diff changeset
175 self._c.ad_hoc_error(self._c.ERROR.BAD_PAYLOAD)
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
176
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
177 requestor = session_data["requestor"]
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
178 requestor_bare = requestor.userhostJID()
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
179 path = path.rstrip("/")
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
180 parent_path, basename = os.path.split(path)
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
181
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
182 # TODO: if parent_path and basename are empty, we ask for root directory
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
183 # this must be managed
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
184
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
185 try:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3932
diff changeset
186 found_files = await self.host.memory.get_files(
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
187 client,
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
188 requestor_bare,
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
189 path=parent_path,
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
190 name=basename,
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
191 namespace=namespace,
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
192 )
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
193 found_file = found_files[0]
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
194 except (exceptions.NotFound, IndexError):
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2934
diff changeset
195 raise WorkflowError(self._err(_("file not found")))
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
196 except exceptions.PermissionError:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2934
diff changeset
197 raise WorkflowError(self._err(_("forbidden")))
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
198
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
199 if found_file["owner"] != requestor_bare:
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
200 # only owner can manage files
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2934
diff changeset
201 log.warning(_("Only owner can manage files"))
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2934
diff changeset
202 raise WorkflowError(self._err(_("forbidden")))
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
203
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
204 session_data["found_file"] = found_file
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
205 session_data["namespace"] = namespace
3541
888109774673 core: various changes and fixes to work with new storage and D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 3531
diff changeset
206 return found_file
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
207
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3932
diff changeset
208 def _update_read_permission(self, access, allowed_jids):
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
209 if not allowed_jids:
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
210 if C.ACCESS_PERM_READ in access:
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
211 del access[C.ACCESS_PERM_READ]
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
212 elif allowed_jids == "PUBLIC":
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
213 access[C.ACCESS_PERM_READ] = {"type": C.ACCESS_TYPE_PUBLIC}
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
214 else:
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
215 access[C.ACCESS_PERM_READ] = {
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2934
diff changeset
216 "type": C.ACCESS_TYPE_WHITELIST,
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
217 "jids": [j.full() for j in allowed_jids],
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
218 }
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
219
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3932
diff changeset
220 async def _update_dir(self, client, requestor, namespace, file_data, allowed_jids):
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
221 """Recursively update permission of a directory and all subdirectories
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
222
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
223 @param file_data(dict): metadata of the file
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
224 @param allowed_jids(list[jid.JID]): list of entities allowed to read the file
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
225 """
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
226 assert file_data["type"] == C.FILE_TYPE_DIRECTORY
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3932
diff changeset
227 files_data = await self.host.memory.get_files(
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
228 client, requestor, parent=file_data["id"], namespace=namespace
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
229 )
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
230
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
231 for file_data in files_data:
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
232 if not file_data["access"].get(C.ACCESS_PERM_READ, {}):
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
233 log.debug(
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
234 "setting {perm} read permission for {name}".format(
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
235 perm=allowed_jids, name=file_data["name"]
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
236 )
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
237 )
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3932
diff changeset
238 await self.host.memory.file_update(
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
239 file_data["id"],
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
240 "access",
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
241 partial(self._update_read_permission, allowed_jids=allowed_jids),
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
242 )
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
243 if file_data["type"] == C.FILE_TYPE_DIRECTORY:
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
244 await self._update_dir(client, requestor, namespace, file_data, "PUBLIC")
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
245
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3932
diff changeset
246 async def _on_change_file(self, client, command_elt, session_data, action, node):
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
247 try:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2934
diff changeset
248 x_elt = next(command_elt.elements(data_form.NS_X_DATA, "x"))
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
249 command_form = data_form.Form.fromElement(x_elt)
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
250 except StopIteration:
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
251 command_form = None
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
252
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
253 found_file = session_data.get("found_file")
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
254 requestor = session_data["requestor"]
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
255 requestor_bare = requestor.userhostJID()
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
256
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
257 if command_form is None or len(command_form.fields) == 0:
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
258 # root request
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3932
diff changeset
259 return self._get_root_args()
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
260
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
261 elif found_file is None:
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
262 # file selected, we retrieve it and ask for permissions
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
263 try:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3932
diff changeset
264 found_file = await self._get_file_data(client, session_data, command_form)
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
265 except WorkflowError as e:
3541
888109774673 core: various changes and fixes to work with new storage and D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 3531
diff changeset
266 return e.err_args
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
267
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
268 # management request
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
269 if found_file["type"] == C.FILE_TYPE_DIRECTORY:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2934
diff changeset
270 instructions = D_("Please select permissions for this directory")
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
271 else:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2934
diff changeset
272 instructions = D_("Please select permissions for this file")
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
273
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
274 form = data_form.Form(
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
275 "form",
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
276 title="File Management",
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
277 instructions=[instructions],
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
278 formNamespace=NS_FILE_MANAGEMENT,
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
279 )
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
280 field = data_form.Field(
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
281 "text-multi",
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
282 "read_allowed",
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
283 required=False,
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
284 desc="list of jids allowed to read this file (beside yourself), or "
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
285 '"PUBLIC" to let a public access',
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
286 )
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2934
diff changeset
287 read_access = found_file["access"].get(C.ACCESS_PERM_READ, {})
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
288 access_type = read_access.get("type", C.ACCESS_TYPE_WHITELIST)
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
289 if access_type == C.ACCESS_TYPE_PUBLIC:
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
290 field.values = ["PUBLIC"]
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
291 else:
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
292 field.values = read_access.get("jids", [])
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
293 form.addField(field)
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
294 if found_file["type"] == C.FILE_TYPE_DIRECTORY:
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
295 field = data_form.Field(
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
296 "boolean",
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
297 "recursive",
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
298 value=False,
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
299 required=False,
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2934
diff changeset
300 desc="Files under it will be made public to follow this dir "
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
301 "permission (only if they don't have already a permission set).",
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
302 )
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
303 form.addField(field)
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
304
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
305 status = self._c.STATUS.EXECUTING
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
306 payload = form.toElement()
3541
888109774673 core: various changes and fixes to work with new storage and D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 3531
diff changeset
307 return (payload, status, None, None)
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
308
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
309 else:
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
310 # final phase, we'll do permission change here
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
311 try:
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
312 read_allowed = command_form.fields["read_allowed"]
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
313 except KeyError:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3932
diff changeset
314 self._c.ad_hoc_error(self._c.ERROR.BAD_PAYLOAD)
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
315
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
316 if read_allowed.value == "PUBLIC":
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
317 allowed_jids = "PUBLIC"
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
318 elif read_allowed.value.strip() == "":
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
319 allowed_jids = None
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
320 else:
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
321 try:
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
322 allowed_jids = [
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
323 jid.JID(v.strip()) for v in read_allowed.values if v.strip()
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
324 ]
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
325 except RuntimeError as e:
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
326 log.warning(
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
327 _("Can't use read_allowed values: {reason}").format(reason=e)
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
328 )
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3932
diff changeset
329 self._c.ad_hoc_error(self._c.ERROR.BAD_PAYLOAD)
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
330
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
331 if found_file["type"] == C.FILE_TYPE_FILE:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3932
diff changeset
332 await self.host.memory.file_update(
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
333 found_file["id"],
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
334 "access",
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
335 partial(self._update_read_permission, allowed_jids=allowed_jids),
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
336 )
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
337 else:
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
338 try:
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
339 recursive = command_form.fields["recursive"]
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
340 except KeyError:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3932
diff changeset
341 self._c.ad_hoc_error(self._c.ERROR.BAD_PAYLOAD)
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3932
diff changeset
342 await self.host.memory.file_update(
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
343 found_file["id"],
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
344 "access",
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
345 partial(self._update_read_permission, allowed_jids=allowed_jids),
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
346 )
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
347 if recursive:
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
348 # we set all file under the directory as public (if they haven't
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
349 # already a permission set), so allowed entities of root directory
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
350 # can read them.
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
351 namespace = session_data["namespace"]
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3932
diff changeset
352 await self._update_dir(
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
353 client, requestor_bare, namespace, found_file, "PUBLIC"
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
354 )
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
355
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
356 # job done, we can end the session
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
357 status = self._c.STATUS.COMPLETED
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
358 payload = None
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2934
diff changeset
359 note = (self._c.NOTE.INFO, _("management session done"))
3541
888109774673 core: various changes and fixes to work with new storage and D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 3531
diff changeset
360 return (payload, status, None, note)
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
361
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3932
diff changeset
362 async def _on_delete_file(self, client, command_elt, session_data, action, node):
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
363 try:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2934
diff changeset
364 x_elt = next(command_elt.elements(data_form.NS_X_DATA, "x"))
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
365 command_form = data_form.Form.fromElement(x_elt)
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
366 except StopIteration:
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
367 command_form = None
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
368
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
369 found_file = session_data.get("found_file")
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
370 requestor = session_data["requestor"]
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
371 requestor_bare = requestor.userhostJID()
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
372
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
373 if command_form is None or len(command_form.fields) == 0:
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
374 # root request
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3932
diff changeset
375 return self._get_root_args()
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
376
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
377 elif found_file is None:
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
378 # file selected, we need confirmation before actually deleting
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
379 try:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3932
diff changeset
380 found_file = await self._get_file_data(client, session_data, command_form)
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
381 except WorkflowError as e:
3541
888109774673 core: various changes and fixes to work with new storage and D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 3531
diff changeset
382 return e.err_args
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
383 if found_file["type"] == C.FILE_TYPE_DIRECTORY:
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
384 msg = D_(
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
385 "Are you sure to delete directory {name} and all files and "
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
386 "directories under it?"
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
387 ).format(name=found_file["name"])
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
388 else:
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
389 msg = D_(
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
390 "Are you sure to delete file {name}?".format(name=found_file["name"])
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
391 )
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
392 form = data_form.Form(
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
393 "form",
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
394 title="File Management",
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
395 instructions=[msg],
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
396 formNamespace=NS_FILE_MANAGEMENT,
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
397 )
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
398 field = data_form.Field(
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
399 "boolean",
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
400 "confirm",
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
401 value=False,
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
402 required=True,
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
403 desc="check this box to confirm",
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
404 )
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
405 form.addField(field)
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
406 status = self._c.STATUS.EXECUTING
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
407 payload = form.toElement()
3541
888109774673 core: various changes and fixes to work with new storage and D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 3531
diff changeset
408 return (payload, status, None, None)
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
409
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
410 else:
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
411 # final phase, we'll do deletion here
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
412 try:
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
413 confirmed = C.bool(command_form.fields["confirm"].value)
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
414 except KeyError:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3932
diff changeset
415 self._c.ad_hoc_error(self._c.ERROR.BAD_PAYLOAD)
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
416 if not confirmed:
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
417 note = None
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
418 else:
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
419 recursive = found_file["type"] == C.FILE_TYPE_DIRECTORY
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3932
diff changeset
420 await self.host.memory.file_delete(
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
421 client, requestor_bare, found_file["id"], recursive
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
422 )
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2934
diff changeset
423 note = (self._c.NOTE.INFO, _("file deleted"))
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
424 status = self._c.STATUS.COMPLETED
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
425 payload = None
3541
888109774673 core: various changes and fixes to work with new storage and D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 3531
diff changeset
426 return (payload, status, None, note)
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
427
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3932
diff changeset
428 def _update_thumbs(self, extra, thumbnails):
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
429 extra[C.KEY_THUMBNAILS] = thumbnails
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
430
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3932
diff changeset
431 async def _gen_thumbs(self, client, requestor, namespace, file_data):
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
432 """Recursively generate thumbnails
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
433
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
434 @param file_data(dict): metadata of the file
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
435 """
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
436 if file_data["type"] == C.FILE_TYPE_DIRECTORY:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3932
diff changeset
437 sub_files_data = await self.host.memory.get_files(
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
438 client, requestor, parent=file_data["id"], namespace=namespace
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
439 )
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
440 for sub_file_data in sub_files_data:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3932
diff changeset
441 await self._gen_thumbs(client, requestor, namespace, sub_file_data)
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
442
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
443 elif file_data["type"] == C.FILE_TYPE_FILE:
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
444 media_type = file_data["media_type"]
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
445 file_path = os.path.join(self.files_path, file_data["file_hash"])
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
446 if media_type == "image":
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
447 thumbnails = []
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
448
3329
15612c0fb421 plugin XEP-0264: updated size to get values closest to standard one:
Goffi <goffi@goffi.org>
parents: 3305
diff changeset
449 for max_thumb_size in self._t.SIZES:
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
450 try:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3932
diff changeset
451 thumb_size, thumb_id = await self._t.generate_thumbnail(
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
452 file_path,
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
453 max_thumb_size,
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
454 #  we keep thumbnails for 6 months
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
455 60 * 60 * 24 * 31 * 6,
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
456 )
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
457 except Exception as e:
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
458 log.warning(
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
459 _("Can't create thumbnail: {reason}").format(reason=e)
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
460 )
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
461 break
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2934
diff changeset
462 thumbnails.append({"id": thumb_id, "size": thumb_size})
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
463
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3932
diff changeset
464 await self.host.memory.file_update(
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
465 file_data["id"],
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
466 "extra",
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
467 partial(self._update_thumbs, thumbnails=thumbnails),
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
468 )
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
469
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
470 log.info(
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
471 "thumbnails for [{file_name}] generated".format(
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
472 file_name=file_data["name"]
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
473 )
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
474 )
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
475
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
476 else:
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
477 log.warning("unmanaged file type: {type_}".format(type_=file_data["type"]))
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
478
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3932
diff changeset
479 async def _on_gen_thumbnails(self, client, command_elt, session_data, action, node):
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
480 try:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2934
diff changeset
481 x_elt = next(command_elt.elements(data_form.NS_X_DATA, "x"))
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
482 command_form = data_form.Form.fromElement(x_elt)
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
483 except StopIteration:
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
484 command_form = None
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
485
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
486 found_file = session_data.get("found_file")
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
487 requestor = session_data["requestor"]
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
488
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
489 if command_form is None or len(command_form.fields) == 0:
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
490 # root request
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3932
diff changeset
491 return self._get_root_args()
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
492
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
493 elif found_file is None:
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
494 # file selected, we retrieve it and ask for permissions
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
495 try:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3932
diff changeset
496 found_file = await self._get_file_data(client, session_data, command_form)
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
497 except WorkflowError as e:
3541
888109774673 core: various changes and fixes to work with new storage and D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 3531
diff changeset
498 return e.err_args
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
499
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2934
diff changeset
500 log.info("Generating thumbnails as requested")
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
501 await self._gen_thumbs(client, requestor, found_file["namespace"], found_file)
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
502
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
503 # job done, we can end the session
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
504 status = self._c.STATUS.COMPLETED
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
505 payload = None
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2934
diff changeset
506 note = (self._c.NOTE.INFO, _("thumbnails generated"))
3541
888109774673 core: various changes and fixes to work with new storage and D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 3531
diff changeset
507 return (payload, status, None, note)
3529
698579bedd6f component file sharing (plugin management): new command to get available quota/used space.
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
508
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3932
diff changeset
509 async def _on_quota(self, client, command_elt, session_data, action, node):
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
510 requestor = session_data["requestor"]
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3932
diff changeset
511 quota = self.host.plugins["file_sharing"].get_quota(client, requestor)
3529
698579bedd6f component file sharing (plugin management): new command to get available quota/used space.
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
512 try:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3932
diff changeset
513 size_used = await self.host.memory.file_get_used_space(client, requestor)
3529
698579bedd6f component file sharing (plugin management): new command to get available quota/used space.
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
514 except exceptions.PermissionError:
698579bedd6f component file sharing (plugin management): new command to get available quota/used space.
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
515 raise WorkflowError(self._err(_("forbidden")))
698579bedd6f component file sharing (plugin management): new command to get available quota/used space.
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
516 status = self._c.STATUS.COMPLETED
3531
a8259a1f89b2 component file sharing (management): add values of `quota` and `used` size on result form
Goffi <goffi@goffi.org>
parents: 3529
diff changeset
517 form = data_form.Form("result")
a8259a1f89b2 component file sharing (management): add values of `quota` and `used` size on result form
Goffi <goffi@goffi.org>
parents: 3529
diff changeset
518 form.makeFields({"quota": quota, "user": size_used})
a8259a1f89b2 component file sharing (management): add values of `quota` and `used` size on result form
Goffi <goffi@goffi.org>
parents: 3529
diff changeset
519 payload = form.toElement()
3529
698579bedd6f component file sharing (plugin management): new command to get available quota/used space.
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
520 note = (
698579bedd6f component file sharing (plugin management): new command to get available quota/used space.
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
521 self._c.NOTE.INFO,
698579bedd6f component file sharing (plugin management): new command to get available quota/used space.
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
522 _("You are currently using {size_used} on {size_quota}").format(
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
523 size_used=utils.get_human_size(size_used),
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
524 size_quota=(
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
525 _("unlimited quota") if quota is None else utils.get_human_size(quota)
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
526 ),
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
527 ),
3529
698579bedd6f component file sharing (plugin management): new command to get available quota/used space.
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
528 )
698579bedd6f component file sharing (plugin management): new command to get available quota/used space.
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
529 return (payload, status, None, note)