annotate sat/plugins/plugin_comp_file_sharing_management.py @ 3568:04283582966f

core, frontends: fix invalid translatable strings. Some f-strings where used in translatable text, this has been fixed by using explicit `format()` call (using a script based on `tokenize`). As tokenize messes with spaces, a reformating tool (`black`) has been applied to some files afterwards.
author Goffi <goffi@goffi.org>
date Mon, 14 Jun 2021 18:35:12 +0200
parents a8259a1f89b2
children 888109774673
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
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
24 from sat.core.i18n import _, D_
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
25 from sat.core import exceptions
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
26 from sat.core.constants import Const as C
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
27 from sat.core.log import getLogger
3529
698579bedd6f component file sharing (plugin management): new command to get available quota/used space.
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
28 from sat.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"]
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
76 self.files_path = host.getLocalPath(None, C.FILES_DIR, profile=False)
3305
04a7d77ed86f plugin file sharing management: added a fileSharingDelete method
Goffi <goffi@goffi.org>
parents: 3298
diff changeset
77 host.bridge.addMethod(
04a7d77ed86f plugin file sharing management: added a fileSharingDelete method
Goffi <goffi@goffi.org>
parents: 3298
diff changeset
78 "fileSharingDelete",
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
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
86 def profileConnected(self, client):
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
87 self._c.addAdHocCommand(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2934
diff changeset
88 client, self._onChangeFile, "Change Permissions of File(s)",
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
89 node=NS_FILE_MANAGEMENT_PERM,
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
90 allowed_magics=C.ENTITY_ALL,
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
91 )
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
92 self._c.addAdHocCommand(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2934
diff changeset
93 client, self._onDeleteFile, "Delete File(s)",
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
94 node=NS_FILE_MANAGEMENT_DELETE,
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
95 allowed_magics=C.ENTITY_ALL,
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
96 )
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
97 self._c.addAdHocCommand(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2934
diff changeset
98 client, self._onGenThumbnails, "Generate Thumbnails",
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
99 node=NS_FILE_MANAGEMENT_THUMB,
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
100 allowed_magics=C.ENTITY_ALL,
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
101 )
3529
698579bedd6f component file sharing (plugin management): new command to get available quota/used space.
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
102 self._c.addAdHocCommand(
698579bedd6f component file sharing (plugin management): new command to get available quota/used space.
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
103 client, self._onQuota, "Get Quota",
698579bedd6f component file sharing (plugin management): new command to get available quota/used space.
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
104 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
105 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
106 )
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
107
3305
04a7d77ed86f plugin file sharing management: added a fileSharingDelete method
Goffi <goffi@goffi.org>
parents: 3298
diff changeset
108 def _delete(self, service_jid_s, path, namespace, profile):
04a7d77ed86f plugin file sharing management: added a fileSharingDelete method
Goffi <goffi@goffi.org>
parents: 3298
diff changeset
109 client = self.host.getClient(profile)
04a7d77ed86f plugin file sharing management: added a fileSharingDelete method
Goffi <goffi@goffi.org>
parents: 3298
diff changeset
110 service_jid = jid.JID(service_jid_s) if service_jid_s else None
04a7d77ed86f plugin file sharing management: added a fileSharingDelete method
Goffi <goffi@goffi.org>
parents: 3298
diff changeset
111 return defer.ensureDeferred(self._c.sequence(
04a7d77ed86f plugin file sharing management: added a fileSharingDelete method
Goffi <goffi@goffi.org>
parents: 3298
diff changeset
112 client,
04a7d77ed86f plugin file sharing management: added a fileSharingDelete method
Goffi <goffi@goffi.org>
parents: 3298
diff changeset
113 [{"path": path, "namespace": namespace}, {"confirm": True}],
04a7d77ed86f plugin file sharing management: added a fileSharingDelete method
Goffi <goffi@goffi.org>
parents: 3298
diff changeset
114 NS_FILE_MANAGEMENT_DELETE,
04a7d77ed86f plugin file sharing management: added a fileSharingDelete method
Goffi <goffi@goffi.org>
parents: 3298
diff changeset
115 service_jid,
04a7d77ed86f plugin file sharing management: added a fileSharingDelete method
Goffi <goffi@goffi.org>
parents: 3298
diff changeset
116 ))
04a7d77ed86f plugin file sharing management: added a fileSharingDelete method
Goffi <goffi@goffi.org>
parents: 3298
diff changeset
117
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
118 def _err(self, reason):
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
119 """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
120
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
121 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
122 @param reason(unicode): reason of the error
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
123 @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
124 """
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
125 status = self._c.STATUS.COMPLETED
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
126 payload = None
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
127 note = (self._c.NOTE.ERROR, reason)
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
128 return payload, status, None, note
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
129
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
130 def _getRootArgs(self):
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
131 """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
132
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.EXECUTING
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2934
diff changeset
136 form = data_form.Form("form", title="File Management",
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
137 formNamespace=NS_FILE_MANAGEMENT)
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
138
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
139 field = data_form.Field(
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
140 "text-single", "path", required=True
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
141 )
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
142 form.addField(field)
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
143
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
144 field = data_form.Field(
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
145 "text-single", "namespace", required=False
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
146 )
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
147 form.addField(field)
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
148
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
149 payload = form.toElement()
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
150 return payload, status, None, None
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
151
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
152 @defer.inlineCallbacks
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
153 def _getFileData(self, client, session_data, command_form):
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
154 """Retrieve field requested in root form
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 "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
157 @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
158 @return (D(dict)): found file data
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
159 @raise WorkflowError: something is wrong
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
160 """
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
161 fields = command_form.fields
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
162 try:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2934
diff changeset
163 path = fields['path'].value.strip()
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2934
diff changeset
164 namespace = fields['namespace'].value or None
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
165 except KeyError:
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
166 self._c.adHocError(self._c.ERROR.BAD_PAYLOAD)
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
167
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
168 if not path:
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
169 self._c.adHocError(self._c.ERROR.BAD_PAYLOAD)
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
170
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2934
diff changeset
171 requestor = session_data['requestor']
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
172 requestor_bare = requestor.userhostJID()
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2934
diff changeset
173 path = path.rstrip('/')
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
174 parent_path, basename = os.path.split(path)
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
175
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
176 # 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
177 # this must be managed
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
178
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
179 try:
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
180 found_files = yield self.host.memory.getFiles(
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
181 client, requestor_bare, path=parent_path, name=basename,
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
182 namespace=namespace)
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
183 found_file = found_files[0]
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
184 except (exceptions.NotFound, IndexError):
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2934
diff changeset
185 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
186 except exceptions.PermissionError:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2934
diff changeset
187 raise WorkflowError(self._err(_("forbidden")))
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
188
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
189 if found_file['owner'] != requestor_bare:
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
190 # only owner can manage files
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2934
diff changeset
191 log.warning(_("Only owner can manage files"))
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2934
diff changeset
192 raise WorkflowError(self._err(_("forbidden")))
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
193
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2934
diff changeset
194 session_data['found_file'] = found_file
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2934
diff changeset
195 session_data['namespace'] = namespace
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
196 defer.returnValue(found_file)
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
197
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
198 def _updateReadPermission(self, access, allowed_jids):
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
199 if not allowed_jids:
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
200 if C.ACCESS_PERM_READ in access:
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
201 del access[C.ACCESS_PERM_READ]
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2934
diff changeset
202 elif allowed_jids == 'PUBLIC':
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
203 access[C.ACCESS_PERM_READ] = {
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2934
diff changeset
204 "type": C.ACCESS_TYPE_PUBLIC
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
205 }
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
206 else:
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
207 access[C.ACCESS_PERM_READ] = {
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2934
diff changeset
208 "type": C.ACCESS_TYPE_WHITELIST,
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2934
diff changeset
209 "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
210 }
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
211
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
212 @defer.inlineCallbacks
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
213 def _updateDir(self, client, requestor, namespace, file_data, allowed_jids):
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
214 """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
215
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
216 @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
217 @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
218 """
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2934
diff changeset
219 assert file_data['type'] == C.FILE_TYPE_DIRECTORY
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
220 files_data = yield self.host.memory.getFiles(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2934
diff changeset
221 client, requestor, parent=file_data['id'], namespace=namespace)
2929
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 for file_data in files_data:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2934
diff changeset
224 if not file_data['access'].get(C.ACCESS_PERM_READ, {}):
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2934
diff changeset
225 log.debug("setting {perm} read permission for {name}".format(
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2934
diff changeset
226 perm=allowed_jids, name=file_data['name']))
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
227 yield self.host.memory.fileUpdate(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2934
diff changeset
228 file_data['id'], 'access',
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
229 partial(self._updateReadPermission, allowed_jids=allowed_jids))
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2934
diff changeset
230 if file_data['type'] == C.FILE_TYPE_DIRECTORY:
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2934
diff changeset
231 yield self._updateDir(client, requestor, namespace, file_data, 'PUBLIC')
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
232
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
233 @defer.inlineCallbacks
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
234 def _onChangeFile(self, client, command_elt, session_data, action, node):
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
235 try:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2934
diff changeset
236 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
237 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
238 except StopIteration:
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
239 command_form = None
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
240
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
241 found_file = session_data.get('found_file')
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2934
diff changeset
242 requestor = session_data['requestor']
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
243 requestor_bare = requestor.userhostJID()
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
244
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
245 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
246 # root request
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
247 defer.returnValue(self._getRootArgs())
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
248
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
249 elif found_file is None:
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
250 # 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
251 try:
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
252 found_file = yield self._getFileData(client, session_data, command_form)
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
253 except WorkflowError as e:
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
254 defer.returnValue(e.err_args)
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
255
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
256 # management request
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2934
diff changeset
257 if found_file['type'] == C.FILE_TYPE_DIRECTORY:
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2934
diff changeset
258 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
259 else:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2934
diff changeset
260 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
261
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2934
diff changeset
262 form = data_form.Form("form", title="File Management",
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
263 instructions=[instructions],
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
264 formNamespace=NS_FILE_MANAGEMENT)
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
265 field = data_form.Field(
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
266 "text-multi", "read_allowed", required=False,
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2934
diff changeset
267 desc='list of jids allowed to read this file (beside yourself), or '
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2934
diff changeset
268 '"PUBLIC" to let a public access'
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
269 )
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2934
diff changeset
270 read_access = found_file["access"].get(C.ACCESS_PERM_READ, {})
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2934
diff changeset
271 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
272 if access_type == C.ACCESS_TYPE_PUBLIC:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2934
diff changeset
273 field.values = ['PUBLIC']
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
274 else:
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
275 field.values = read_access.get('jids', [])
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
276 form.addField(field)
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2934
diff changeset
277 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
278 field = data_form.Field(
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
279 "boolean", "recursive", value=False, required=False,
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2934
diff changeset
280 desc="Files under it will be made public to follow this dir "
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2934
diff changeset
281 "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
282 )
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
283 form.addField(field)
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
284
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
285 status = self._c.STATUS.EXECUTING
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
286 payload = form.toElement()
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
287 defer.returnValue((payload, status, None, None))
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
288
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
289 else:
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
290 # 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
291 try:
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
292 read_allowed = command_form.fields['read_allowed']
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
293 except KeyError:
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
294 self._c.adHocError(self._c.ERROR.BAD_PAYLOAD)
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
295
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2934
diff changeset
296 if read_allowed.value == 'PUBLIC':
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2934
diff changeset
297 allowed_jids = 'PUBLIC'
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2934
diff changeset
298 elif read_allowed.value.strip() == '':
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
299 allowed_jids = None
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
300 else:
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
301 try:
2934
0bafdbda7f5f plugin file sharing management: filter out empty values in allowed_jids
Goffi <goffi@goffi.org>
parents: 2929
diff changeset
302 allowed_jids = [jid.JID(v.strip()) for v in read_allowed.values
0bafdbda7f5f plugin file sharing management: filter out empty values in allowed_jids
Goffi <goffi@goffi.org>
parents: 2929
diff changeset
303 if v.strip()]
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
304 except RuntimeError as e:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2934
diff changeset
305 log.warning(_("Can't use read_allowed values: {reason}").format(
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
306 reason=e))
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
307 self._c.adHocError(self._c.ERROR.BAD_PAYLOAD)
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
308
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2934
diff changeset
309 if found_file['type'] == C.FILE_TYPE_FILE:
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
310 yield self.host.memory.fileUpdate(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2934
diff changeset
311 found_file['id'], 'access',
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
312 partial(self._updateReadPermission, allowed_jids=allowed_jids))
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
313 else:
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
314 try:
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
315 recursive = command_form.fields['recursive']
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
316 except KeyError:
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
317 self._c.adHocError(self._c.ERROR.BAD_PAYLOAD)
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
318 yield self.host.memory.fileUpdate(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2934
diff changeset
319 found_file['id'], 'access',
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
320 partial(self._updateReadPermission, allowed_jids=allowed_jids))
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
321 if recursive:
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
322 # 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
323 # 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
324 # can read them.
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2934
diff changeset
325 namespace = session_data['namespace']
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
326 yield self._updateDir(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2934
diff changeset
327 client, requestor_bare, namespace, found_file, 'PUBLIC')
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
328
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
329 # job done, we can end the session
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
330 status = self._c.STATUS.COMPLETED
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
331 payload = None
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2934
diff changeset
332 note = (self._c.NOTE.INFO, _("management session done"))
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
333 defer.returnValue((payload, status, None, note))
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
334
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
335 @defer.inlineCallbacks
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
336 def _onDeleteFile(self, client, command_elt, session_data, action, node):
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
337 try:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2934
diff changeset
338 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
339 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
340 except StopIteration:
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
341 command_form = None
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
342
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
343 found_file = session_data.get('found_file')
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2934
diff changeset
344 requestor = session_data['requestor']
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
345 requestor_bare = requestor.userhostJID()
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
346
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
347 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
348 # root request
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
349 defer.returnValue(self._getRootArgs())
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
350
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
351 elif found_file is None:
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
352 # 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
353 try:
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
354 found_file = yield self._getFileData(client, session_data, command_form)
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
355 except WorkflowError as e:
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
356 defer.returnValue(e.err_args)
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2934
diff changeset
357 if found_file['type'] == C.FILE_TYPE_DIRECTORY:
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2934
diff changeset
358 msg = D_("Are you sure to delete directory {name} and all files and "
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2934
diff changeset
359 "directories under it?").format(name=found_file['name'])
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
360 else:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2934
diff changeset
361 msg = D_("Are you sure to delete file {name}?"
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2934
diff changeset
362 .format(name=found_file['name']))
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2934
diff changeset
363 form = data_form.Form("form", title="File Management",
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
364 instructions = [msg],
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
365 formNamespace=NS_FILE_MANAGEMENT)
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
366 field = data_form.Field(
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
367 "boolean", "confirm", value=False, required=True,
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2934
diff changeset
368 desc="check this box to confirm"
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
369 )
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
370 form.addField(field)
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
371 status = self._c.STATUS.EXECUTING
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
372 payload = form.toElement()
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
373 defer.returnValue((payload, status, None, None))
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
374
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
375 else:
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
376 # final phase, we'll do deletion here
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
377 try:
3298
d0e8b0a3ef9c comp file sharing management: fixed confirmation check
Goffi <goffi@goffi.org>
parents: 3288
diff changeset
378 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
379 except KeyError:
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
380 self._c.adHocError(self._c.ERROR.BAD_PAYLOAD)
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
381 if not confirmed:
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
382 note = None
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
383 else:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2934
diff changeset
384 recursive = 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
385 yield self.host.memory.fileDelete(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2934
diff changeset
386 client, requestor_bare, found_file['id'], recursive)
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2934
diff changeset
387 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
388 status = self._c.STATUS.COMPLETED
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
389 payload = None
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
390 defer.returnValue((payload, status, None, note))
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
391
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
392 def _updateThumbs(self, extra, thumbnails):
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
393 extra[C.KEY_THUMBNAILS] = thumbnails
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
394
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
395 @defer.inlineCallbacks
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
396 def _genThumbs(self, client, requestor, namespace, file_data):
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
397 """Recursively generate thumbnails
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
398
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
399 @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
400 """
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2934
diff changeset
401 if file_data['type'] == C.FILE_TYPE_DIRECTORY:
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
402 sub_files_data = yield self.host.memory.getFiles(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2934
diff changeset
403 client, requestor, parent=file_data['id'], namespace=namespace)
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
404 for sub_file_data in sub_files_data:
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
405 yield self._genThumbs(client, requestor, namespace, sub_file_data)
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
406
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2934
diff changeset
407 elif file_data['type'] == C.FILE_TYPE_FILE:
3288
780fb8dd07ef core (memory/sqlite): new database schema (v9):
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
408 media_type = file_data['media_type']
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2934
diff changeset
409 file_path = os.path.join(self.files_path, file_data['file_hash'])
3288
780fb8dd07ef core (memory/sqlite): new database schema (v9):
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
410 if media_type == 'image':
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
411 thumbnails = []
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
412
3329
15612c0fb421 plugin XEP-0264: updated size to get values closest to standard one:
Goffi <goffi@goffi.org>
parents: 3305
diff changeset
413 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
414 try:
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
415 thumb_size, thumb_id = yield self._t.generateThumbnail(
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
416 file_path,
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
417 max_thumb_size,
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
418 #  we keep thumbnails for 6 months
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
419 60 * 60 * 24 * 31 * 6,
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
420 )
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
421 except Exception as e:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2934
diff changeset
422 log.warning(_("Can't create thumbnail: {reason}")
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
423 .format(reason=e))
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
424 break
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2934
diff changeset
425 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
426
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
427 yield self.host.memory.fileUpdate(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2934
diff changeset
428 file_data['id'], 'extra',
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
429 partial(self._updateThumbs, thumbnails=thumbnails))
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
430
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2934
diff changeset
431 log.info("thumbnails for [{file_name}] generated"
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2934
diff changeset
432 .format(file_name=file_data['name']))
2929
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 else:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2934
diff changeset
435 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
436
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
437 @defer.inlineCallbacks
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
438 def _onGenThumbnails(self, client, command_elt, session_data, action, node):
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
439 try:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2934
diff changeset
440 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
441 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
442 except StopIteration:
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
443 command_form = None
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
444
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
445 found_file = session_data.get('found_file')
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2934
diff changeset
446 requestor = session_data['requestor']
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
447
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
448 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
449 # root request
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
450 defer.returnValue(self._getRootArgs())
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
451
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
452 elif found_file is None:
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
453 # 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
454 try:
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
455 found_file = yield self._getFileData(client, session_data, command_form)
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
456 except WorkflowError as e:
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
457 defer.returnValue(e.err_args)
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
458
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2934
diff changeset
459 log.info("Generating thumbnails as requested")
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2934
diff changeset
460 yield self._genThumbs(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
461
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
462 # job done, we can end the session
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
463 status = self._c.STATUS.COMPLETED
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
464 payload = None
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2934
diff changeset
465 note = (self._c.NOTE.INFO, _("thumbnails generated"))
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
466 defer.returnValue((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
467
698579bedd6f component file sharing (plugin management): new command to get available quota/used space.
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
468 async def _onQuota(self, client, command_elt, session_data, action, node):
698579bedd6f component file sharing (plugin management): new command to get available quota/used space.
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
469 requestor = session_data['requestor']
698579bedd6f component file sharing (plugin management): new command to get available quota/used space.
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
470 quota = self.host.plugins["file_sharing"].getQuota(client, requestor)
698579bedd6f component file sharing (plugin management): new command to get available quota/used space.
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
471 try:
698579bedd6f component file sharing (plugin management): new command to get available quota/used space.
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
472 size_used = await self.host.memory.fileGetUsedSpace(client, requestor)
698579bedd6f component file sharing (plugin management): new command to get available quota/used space.
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
473 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
474 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
475 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
476 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
477 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
478 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
479 note = (
698579bedd6f component file sharing (plugin management): new command to get available quota/used space.
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
480 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
481 _("You are currently using {size_used} on {size_quota}").format(
698579bedd6f component file sharing (plugin management): new command to get available quota/used space.
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
482 size_used = utils.getHumanSize(size_used),
698579bedd6f component file sharing (plugin management): new command to get available quota/used space.
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
483 size_quota = (
698579bedd6f component file sharing (plugin management): new command to get available quota/used space.
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
484 _("unlimited quota") if quota is None
698579bedd6f component file sharing (plugin management): new command to get available quota/used space.
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
485 else utils.getHumanSize(quota)
698579bedd6f component file sharing (plugin management): new command to get available quota/used space.
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
486 )
698579bedd6f component file sharing (plugin management): new command to get available quota/used space.
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
487 )
698579bedd6f component file sharing (plugin management): new command to get available quota/used space.
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
488 )
698579bedd6f component file sharing (plugin management): new command to get available quota/used space.
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
489 return (payload, status, None, note)