Mercurial > libervia-backend
annotate sat/plugins/plugin_comp_file_sharing.py @ 3216:8418e0c83ed7
plugin text commands: handles coroutines + better command parsing:
- async coroutine can now be used for text commands (in addition to Deferred and regular
callbacks)
- isidentifier is now used to detect a command, allowing to use "_" in command names
- if message starts with a single `/`, it is never sent (previously it could be sent is
some corner cases)
- don't use OrderedDict anymore as dict now keep insertion order by default
author | Goffi <goffi@goffi.org> |
---|---|
date | Fri, 13 Mar 2020 17:50:08 +0100 |
parents | 9d0df638c8b4 |
children | 9057713ab124 |
rev | line source |
---|---|
3028 | 1 #!/usr/bin/env python3 |
2504
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
2 |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
3 # SAT plugin for parrot mode (experimental) |
3136 | 4 # Copyright (C) 2009-2020 Jérôme Poisson (goffi@goffi.org) |
2504
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
5 |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
6 # This program is free software: you can redistribute it and/or modify |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
7 # it under the terms of the GNU Affero General Public License as published by |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
8 # the Free Software Foundation, either version 3 of the License, or |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
9 # (at your option) any later version. |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
10 |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
11 # This program is distributed in the hope that it will be useful, |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
14 # GNU Affero General Public License for more details. |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
15 |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
16 # You should have received a copy of the GNU Affero General Public License |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
17 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
18 |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
19 from sat.core.i18n import _ |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
20 from sat.core.constants import Const as C |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
21 from sat.core import exceptions |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
22 from sat.core.log import getLogger |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
23 from sat.tools.common import regex |
2528
65e278997715
component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents:
2527
diff
changeset
|
24 from sat.tools.common import uri |
2504
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
25 from sat.tools import stream |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
26 from twisted.internet import defer |
2527
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
27 from twisted.words.protocols.jabber import error |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
28 from wokkel import pubsub |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
29 from wokkel import generic |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
30 from functools import partial |
2504
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
31 import os |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
32 import os.path |
2514
4440ea7047bd
file sharing component: thumbnails integration first draft:
Goffi <goffi@goffi.org>
parents:
2504
diff
changeset
|
33 import mimetypes |
2504
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
34 |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
35 |
3136 | 36 log = getLogger(__name__) |
37 | |
38 | |
2504
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
39 PLUGIN_INFO = { |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
40 C.PI_NAME: "File sharing component", |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
41 C.PI_IMPORT_NAME: "file_sharing", |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
42 C.PI_MODES: [C.PLUG_MODE_COMPONENT], |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
43 C.PI_TYPE: C.PLUG_TYPE_ENTRY_POINT, |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
44 C.PI_PROTOCOLS: [], |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
45 C.PI_DEPENDENCIES: [ |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
46 "FILE", |
2929
e0429ff7f6b6
plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
47 "FILE_SHARING_MANAGEMENT", |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
48 "XEP-0231", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
49 "XEP-0234", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
50 "XEP-0260", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
51 "XEP-0261", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
52 "XEP-0264", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
53 "XEP-0329", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
54 ], |
2504
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
55 C.PI_RECOMMENDATIONS: [], |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
56 C.PI_MAIN: "FileSharing", |
2527
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
57 C.PI_HANDLER: C.BOOL_TRUE, |
3028 | 58 C.PI_DESCRIPTION: _("""Component hosting and sharing files"""), |
2504
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
59 } |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
60 |
3028 | 61 HASH_ALGO = "sha-256" |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
62 NS_COMMENTS = "org.salut-a-toi.comments" |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
63 COMMENT_NODE_PREFIX = "org.salut-a-toi.file_comments/" |
2504
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
64 |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
65 |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
66 class FileSharing(object): |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
67 def __init__(self, host): |
3028 | 68 log.info(_("File Sharing initialization")) |
2504
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
69 self.host = host |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
70 self._f = host.plugins["FILE"] |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
71 self._jf = host.plugins["XEP-0234"] |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
72 self._h = host.plugins["XEP-0300"] |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
73 self._t = host.plugins["XEP-0264"] |
2504
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
74 host.trigger.add("FILE_getDestDir", self._getDestDirTrigger) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
75 host.trigger.add( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
76 "XEP-0234_fileSendingRequest", self._fileSendingRequestTrigger, priority=1000 |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
77 ) |
2528
65e278997715
component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents:
2527
diff
changeset
|
78 host.trigger.add("XEP-0234_buildFileElement", self._addFileComments) |
65e278997715
component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents:
2527
diff
changeset
|
79 host.trigger.add("XEP-0234_parseFileElement", self._getFileComments) |
65e278997715
component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents:
2527
diff
changeset
|
80 host.trigger.add("XEP-0329_compGetFilesFromNode", self._addCommentsData) |
2504
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
81 self.files_path = host.getLocalPath(None, C.FILES_DIR, profile=False) |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
82 |
2527
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
83 def getHandler(self, client): |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
84 return Comments_handler(self) |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
85 |
2504
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
86 def profileConnected(self, client): |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
87 path = client.file_tmp_dir = os.path.join( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
88 self.host.memory.getConfig("", "local_dir"), |
2504
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
89 C.FILES_TMP_DIR, |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
90 regex.pathEscape(client.profile), |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
91 ) |
2504
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
92 if not os.path.exists(path): |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
93 os.makedirs(path) |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
94 |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
95 @defer.inlineCallbacks |
2765
378188abe941
misc: replaced all "dummy" by the more conventional and readable "__" ("_" being used for gettext)
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
96 def _fileTransferedCb(self, __, client, peer_jid, file_data, file_path): |
2528
65e278997715
component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents:
2527
diff
changeset
|
97 """post file reception tasks |
65e278997715
component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents:
2527
diff
changeset
|
98 |
65e278997715
component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents:
2527
diff
changeset
|
99 on file is received, this method create hash/thumbnails if necessary |
65e278997715
component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents:
2527
diff
changeset
|
100 move the file to the right location, and create metadata entry in database |
65e278997715
component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents:
2527
diff
changeset
|
101 """ |
3028 | 102 name = file_data["name"] |
2514
4440ea7047bd
file sharing component: thumbnails integration first draft:
Goffi <goffi@goffi.org>
parents:
2504
diff
changeset
|
103 extra = {} |
4440ea7047bd
file sharing component: thumbnails integration first draft:
Goffi <goffi@goffi.org>
parents:
2504
diff
changeset
|
104 |
3028 | 105 if file_data["hash_algo"] == HASH_ALGO: |
106 log.debug(_("Reusing already generated hash")) | |
107 file_hash = file_data["hash_hasher"].hexdigest() | |
2504
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
108 else: |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
109 hasher = self._h.getHasher(HASH_ALGO) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
110 with open("file_path") as f: |
2504
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
111 file_hash = yield self._h.calculateHash(f, hasher) |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
112 final_path = os.path.join(self.files_path, file_hash) |
2514
4440ea7047bd
file sharing component: thumbnails integration first draft:
Goffi <goffi@goffi.org>
parents:
2504
diff
changeset
|
113 |
2504
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
114 if os.path.isfile(final_path): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
115 log.debug( |
3028 | 116 "file [{file_hash}] already exists, we can remove temporary one".format( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
117 file_hash=file_hash |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
118 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
119 ) |
2504
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
120 os.unlink(file_path) |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
121 else: |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
122 os.rename(file_path, final_path) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
123 log.debug( |
3028 | 124 "file [{file_hash}] moved to {files_path}".format( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
125 file_hash=file_hash, files_path=self.files_path |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
126 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
127 ) |
2514
4440ea7047bd
file sharing component: thumbnails integration first draft:
Goffi <goffi@goffi.org>
parents:
2504
diff
changeset
|
128 |
3028 | 129 mime_type = file_data.get("mime_type") |
130 if not mime_type or mime_type == "application/octet-stream": | |
2514
4440ea7047bd
file sharing component: thumbnails integration first draft:
Goffi <goffi@goffi.org>
parents:
2504
diff
changeset
|
131 mime_type = mimetypes.guess_type(name)[0] |
4440ea7047bd
file sharing component: thumbnails integration first draft:
Goffi <goffi@goffi.org>
parents:
2504
diff
changeset
|
132 |
3028 | 133 if mime_type is not None and mime_type.startswith("image"): |
2514
4440ea7047bd
file sharing component: thumbnails integration first draft:
Goffi <goffi@goffi.org>
parents:
2504
diff
changeset
|
134 thumbnails = extra.setdefault(C.KEY_THUMBNAILS, []) |
4440ea7047bd
file sharing component: thumbnails integration first draft:
Goffi <goffi@goffi.org>
parents:
2504
diff
changeset
|
135 for max_thumb_size in (self._t.SIZE_SMALL, self._t.SIZE_MEDIUM): |
4440ea7047bd
file sharing component: thumbnails integration first draft:
Goffi <goffi@goffi.org>
parents:
2504
diff
changeset
|
136 try: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
137 thumb_size, thumb_id = yield self._t.generateThumbnail( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
138 final_path, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
139 max_thumb_size, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
140 # we keep thumbnails for 6 months |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
141 60 * 60 * 24 * 31 * 6, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
142 ) |
2514
4440ea7047bd
file sharing component: thumbnails integration first draft:
Goffi <goffi@goffi.org>
parents:
2504
diff
changeset
|
143 except Exception as e: |
3028 | 144 log.warning(_("Can't create thumbnail: {reason}").format(reason=e)) |
2514
4440ea7047bd
file sharing component: thumbnails integration first draft:
Goffi <goffi@goffi.org>
parents:
2504
diff
changeset
|
145 break |
3028 | 146 thumbnails.append({"id": thumb_id, "size": thumb_size}) |
2514
4440ea7047bd
file sharing component: thumbnails integration first draft:
Goffi <goffi@goffi.org>
parents:
2504
diff
changeset
|
147 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
148 self.host.memory.setFile( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
149 client, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
150 name=name, |
3028 | 151 version="", |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
152 file_hash=file_hash, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
153 hash_algo=HASH_ALGO, |
3028 | 154 size=file_data["size"], |
155 path=file_data.get("path"), | |
156 namespace=file_data.get("namespace"), | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
157 mime_type=mime_type, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
158 owner=peer_jid, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
159 extra=extra, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
160 ) |
2504
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
161 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
162 def _getDestDirTrigger( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
163 self, client, peer_jid, transfer_data, file_data, stream_object |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
164 ): |
2528
65e278997715
component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents:
2527
diff
changeset
|
165 """This trigger accept file sending request, and store file locally""" |
2504
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
166 if not client.is_component: |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
167 return True, None |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
168 assert stream_object |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
169 assert "stream_object" not in transfer_data |
2514
4440ea7047bd
file sharing component: thumbnails integration first draft:
Goffi <goffi@goffi.org>
parents:
2504
diff
changeset
|
170 assert C.KEY_PROGRESS_ID in file_data |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
171 filename = file_data["name"] |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
172 assert filename and not "/" in filename |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
173 file_tmp_dir = self.host.getLocalPath( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
174 client, C.FILES_TMP_DIR, peer_jid.userhost(), component=True, profile=False |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
175 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
176 file_tmp_path = file_data["file_path"] = os.path.join( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
177 file_tmp_dir, file_data["name"] |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
178 ) |
2504
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
179 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
180 transfer_data["finished_d"].addCallback( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
181 self._fileTransferedCb, client, peer_jid, file_data, file_tmp_path |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
182 ) |
2504
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
183 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
184 self._f.openFileWrite( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
185 client, file_tmp_path, transfer_data, file_data, stream_object |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
186 ) |
2504
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
187 return False, defer.succeed(True) |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
188 |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
189 @defer.inlineCallbacks |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
190 def _retrieveFiles( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
191 self, client, session, content_data, content_name, file_data, file_elt |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
192 ): |
2528
65e278997715
component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents:
2527
diff
changeset
|
193 """This method retrieve a file on request, and send if after checking permissions""" |
3028 | 194 peer_jid = session["peer_jid"] |
2504
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
195 try: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
196 found_files = yield self.host.memory.getFiles( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
197 client, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
198 peer_jid=peer_jid, |
3028 | 199 name=file_data.get("name"), |
200 file_hash=file_data.get("file_hash"), | |
201 hash_algo=file_data.get("hash_algo"), | |
202 path=file_data.get("path"), | |
203 namespace=file_data.get("namespace"), | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
204 ) |
2504
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
205 except exceptions.NotFound: |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
206 found_files = None |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
207 except exceptions.PermissionError: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
208 log.warning( |
3028 | 209 _("{peer_jid} is trying to access an unauthorized file: {name}").format( |
210 peer_jid=peer_jid, name=file_data.get("name") | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
211 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
212 ) |
2504
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
213 defer.returnValue(False) |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
214 |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
215 if not found_files: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
216 log.warning( |
3028 | 217 _("no matching file found ({file_data})").format(file_data=file_data) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
218 ) |
2504
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
219 defer.returnValue(False) |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
220 |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
221 # we only use the first found file |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
222 found_file = found_files[0] |
3028 | 223 if found_file['type'] != C.FILE_TYPE_FILE: |
224 raise TypeError("a file was expected, type is {type_}".format( | |
225 type_=found_file['type'])) | |
226 file_hash = found_file["file_hash"] | |
2504
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
227 file_path = os.path.join(self.files_path, file_hash) |
3028 | 228 file_data["hash_hasher"] = hasher = self._h.getHasher(found_file["hash_algo"]) |
229 size = file_data["size"] = found_file["size"] | |
230 file_data["file_hash"] = file_hash | |
231 file_data["hash_algo"] = found_file["hash_algo"] | |
2504
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
232 |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
233 # we complete file_elt so peer can have some details on the file |
3028 | 234 if "name" not in file_data: |
235 file_elt.addElement("name", content=found_file["name"]) | |
236 file_elt.addElement("size", content=str(size)) | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
237 content_data["stream_object"] = stream.FileStreamObject( |
2504
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
238 self.host, |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
239 client, |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
240 file_path, |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
241 uid=self._jf.getProgressId(session, content_name), |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
242 size=size, |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
243 data_cb=lambda data: hasher.update(data), |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
244 ) |
2504
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
245 defer.returnValue(True) |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
246 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
247 def _fileSendingRequestTrigger( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
248 self, client, session, content_data, content_name, file_data, file_elt |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
249 ): |
2504
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
250 if not client.is_component: |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
251 return True, None |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
252 else: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
253 return ( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
254 False, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
255 self._retrieveFiles( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
256 client, session, content_data, content_name, file_data, file_elt |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
257 ), |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
258 ) |
2527
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
259 |
2528
65e278997715
component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents:
2527
diff
changeset
|
260 ## comments triggers ## |
65e278997715
component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents:
2527
diff
changeset
|
261 |
65e278997715
component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents:
2527
diff
changeset
|
262 def _addFileComments(self, file_elt, extra_args): |
65e278997715
component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents:
2527
diff
changeset
|
263 try: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
264 comments_url = extra_args.pop("comments_url") |
2528
65e278997715
component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents:
2527
diff
changeset
|
265 except KeyError: |
65e278997715
component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents:
2527
diff
changeset
|
266 return |
65e278997715
component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents:
2527
diff
changeset
|
267 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
268 comment_elt = file_elt.addElement((NS_COMMENTS, "comments"), content=comments_url) |
2528
65e278997715
component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents:
2527
diff
changeset
|
269 |
65e278997715
component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents:
2527
diff
changeset
|
270 try: |
3028 | 271 count = len(extra_args["extra"]["comments"]) |
2528
65e278997715
component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents:
2527
diff
changeset
|
272 except KeyError: |
65e278997715
component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents:
2527
diff
changeset
|
273 count = 0 |
65e278997715
component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents:
2527
diff
changeset
|
274 |
3028 | 275 comment_elt["count"] = str(count) |
2528
65e278997715
component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents:
2527
diff
changeset
|
276 return True |
65e278997715
component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents:
2527
diff
changeset
|
277 |
65e278997715
component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents:
2527
diff
changeset
|
278 def _getFileComments(self, file_elt, file_data): |
65e278997715
component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents:
2527
diff
changeset
|
279 try: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
280 comments_elt = next(file_elt.elements(NS_COMMENTS, "comments")) |
2528
65e278997715
component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents:
2527
diff
changeset
|
281 except StopIteration: |
65e278997715
component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents:
2527
diff
changeset
|
282 return |
3028 | 283 file_data["comments_url"] = str(comments_elt) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
284 file_data["comments_count"] = comments_elt["count"] |
2528
65e278997715
component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents:
2527
diff
changeset
|
285 return True |
65e278997715
component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents:
2527
diff
changeset
|
286 |
65e278997715
component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents:
2527
diff
changeset
|
287 def _addCommentsData(self, client, iq_elt, owner, node_path, files_data): |
65e278997715
component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents:
2527
diff
changeset
|
288 for file_data in files_data: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
289 file_data["comments_url"] = uri.buildXMPPUri( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
290 "pubsub", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
291 path=client.jid.full(), |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
292 node=COMMENT_NODE_PREFIX + file_data["id"], |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
293 ) |
2528
65e278997715
component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents:
2527
diff
changeset
|
294 return True |
65e278997715
component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents:
2527
diff
changeset
|
295 |
2527
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
296 |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
297 class Comments_handler(pubsub.PubSubService): |
2528
65e278997715
component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents:
2527
diff
changeset
|
298 """This class is a minimal Pubsub service handling virtual nodes for comments""" |
2527
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
299 |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
300 def __init__(self, plugin_parent): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
301 super(Comments_handler, self).__init__() # PubsubVirtualResource()) |
2527
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
302 self.host = plugin_parent.host |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
303 self.plugin_parent = plugin_parent |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
304 self.discoIdentity = { |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
305 "category": "pubsub", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
306 "type": "virtual", # FIXME: non standard, here to avoid this service being considered as main pubsub one |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
307 "name": "files commenting service", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
308 } |
2527
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
309 |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
310 def _getFileId(self, nodeIdentifier): |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
311 if not nodeIdentifier.startswith(COMMENT_NODE_PREFIX): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
312 raise error.StanzaError("item-not-found") |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
313 file_id = nodeIdentifier[len(COMMENT_NODE_PREFIX) :] |
2527
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
314 if not file_id: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
315 raise error.StanzaError("item-not-found") |
2527
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
316 return file_id |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
317 |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
318 @defer.inlineCallbacks |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
319 def getFileData(self, requestor, nodeIdentifier): |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
320 file_id = self._getFileId(nodeIdentifier) |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
321 try: |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
322 files = yield self.host.memory.getFiles(self.parent, requestor, file_id) |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
323 except (exceptions.NotFound, exceptions.PermissionError): |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
324 # we don't differenciate between NotFound and PermissionError |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
325 # to avoid leaking information on existing files |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
326 raise error.StanzaError("item-not-found") |
2527
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
327 if not files: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
328 raise error.StanzaError("item-not-found") |
2527
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
329 if len(files) > 1: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
330 raise error.InternalError("there should be only one file") |
2527
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
331 defer.returnValue(files[0]) |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
332 |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
333 def commentsUpdate(self, extra, new_comments, peer_jid): |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
334 """update comments (replace or insert new_comments) |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
335 |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
336 @param extra(dict): extra data to update |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
337 @param new_comments(list[tuple(unicode, unicode, unicode)]): comments to update or insert |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
338 @param peer_jid(unicode, None): bare jid of the requestor, or None if request is done by owner |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
339 """ |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
340 current_comments = extra.setdefault("comments", []) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
341 new_comments_by_id = {c[0]: c for c in new_comments} |
2527
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
342 updated = [] |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
343 # we now check every current comment, to see if one id in new ones |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
344 # exist, in which case we must update |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
345 for idx, comment in enumerate(current_comments): |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
346 comment_id = comment[0] |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
347 if comment_id in new_comments_by_id: |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
348 # a new comment has an existing id, update is requested |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
349 if peer_jid and comment[1] != peer_jid: |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
350 # requestor has not the right to modify the comment |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
351 raise exceptions.PermissionError |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
352 # we replace old_comment with updated one |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
353 new_comment = new_comments_by_id[comment_id] |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
354 current_comments[idx] = new_comment |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
355 updated.append(new_comment) |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
356 |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
357 # we now remove every updated comments, to only keep |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
358 # the ones to insert |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
359 for comment in updated: |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
360 new_comments.remove(comment) |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
361 |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
362 current_comments.extend(new_comments) |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
363 |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
364 def commentsDelete(self, extra, comments): |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
365 try: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
366 comments_dict = extra["comments"] |
2527
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
367 except KeyError: |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
368 return |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
369 for comment in comments: |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
370 try: |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
371 comments_dict.remove(comment) |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
372 except ValueError: |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
373 continue |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
374 |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
375 def _getFrom(self, item_elt): |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
376 """retrieve published of an item |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
377 |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
378 @param item_elt(domish.element): <item> element |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
379 @return (unicode): full jid as string |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
380 """ |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
381 iq_elt = item_elt |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
382 while iq_elt.parent != None: |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
383 iq_elt = iq_elt.parent |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
384 return iq_elt["from"] |
2527
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
385 |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
386 @defer.inlineCallbacks |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
387 def publish(self, requestor, service, nodeIdentifier, items): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
388 # we retrieve file a first time to check authorisations |
2527
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
389 file_data = yield self.getFileData(requestor, nodeIdentifier) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
390 file_id = file_data["id"] |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
391 comments = [(item["id"], self._getFrom(item), item.toXml()) for item in items] |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
392 if requestor.userhostJID() == file_data["owner"]: |
2527
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
393 peer_jid = None |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
394 else: |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
395 peer_jid = requestor.userhost() |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
396 update_cb = partial(self.commentsUpdate, new_comments=comments, peer_jid=peer_jid) |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
397 try: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
398 yield self.host.memory.fileUpdate(file_id, "extra", update_cb) |
2527
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
399 except exceptions.PermissionError: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
400 raise error.StanzaError("not-authorized") |
2527
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
401 |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
402 @defer.inlineCallbacks |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
403 def items(self, requestor, service, nodeIdentifier, maxItems, itemIdentifiers): |
2527
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
404 file_data = yield self.getFileData(requestor, nodeIdentifier) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
405 comments = file_data["extra"].get("comments", []) |
2527
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
406 if itemIdentifiers: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
407 defer.returnValue( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
408 [generic.parseXml(c[2]) for c in comments if c[0] in itemIdentifiers] |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
409 ) |
2527
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
410 else: |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
411 defer.returnValue([generic.parseXml(c[2]) for c in comments]) |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
412 |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
413 @defer.inlineCallbacks |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
414 def retract(self, requestor, service, nodeIdentifier, itemIdentifiers): |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
415 file_data = yield self.getFileData(requestor, nodeIdentifier) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
416 file_id = file_data["id"] |
2527
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
417 try: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
418 comments = file_data["extra"]["comments"] |
2527
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
419 except KeyError: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
420 raise error.StanzaError("item-not-found") |
2527
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
421 |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
422 to_remove = [] |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
423 for comment in comments: |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
424 comment_id = comment[0] |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
425 if comment_id in itemIdentifiers: |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
426 to_remove.append(comment) |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
427 itemIdentifiers.remove(comment_id) |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
428 if not itemIdentifiers: |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
429 break |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
430 |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
431 if itemIdentifiers: |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
432 # not all items have been to_remove, we can't continue |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
433 raise error.StanzaError("item-not-found") |
2527
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
434 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
435 if requestor.userhostJID() != file_data["owner"]: |
2527
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
436 if not all([c[1] == requestor.userhost() for c in to_remove]): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
437 raise error.StanzaError("not-authorized") |
2527
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
438 |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
439 remove_cb = partial(self.commentsDelete, comments=to_remove) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
440 yield self.host.memory.fileUpdate(file_id, "extra", remove_cb) |