Mercurial > libervia-backend
annotate src/plugins/plugin_comp_file_sharing.py @ 2532:772447ec070f
jp: pubsub options refactoring:
There is now only "use_pubsub", and specification are set using "pubsub_flags" argument when instantiating CommandBase.
Options are more Python Zen compliant by using explicit arguments for item, draft, url instead of trying to guess with magic keyword and type detection.
Pubsub node and item are now always using respecively "-n" and "-i" even when required, this way shell history can be used to change command more easily, and it's globally less confusing for user.
if --pubsub-url is used, elements can be overwritten with individual option (e.g. change item id with --item).
New "use_draft" argument in CommandBase, to re-use current draft or open a file path as draft.
Item can now be specified when using a draft. If it already exists, its content will be added to current draft (with a separator), to avoid loosing data.
common.BaseEdit.getItemPath could be simplified thanks to those changes.
Pubsub URI handling has been moved to base.py.
author | Goffi <goffi@goffi.org> |
---|---|
date | Wed, 21 Mar 2018 19:13:22 +0100 |
parents | cbbf2ff2ef3f |
children |
rev | line source |
---|---|
2504
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1 #!/usr/bin/env python2 |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
2 # -*- coding: utf-8 -*- |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
3 |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
4 # SAT plugin for parrot mode (experimental) |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
5 # Copyright (C) 2009-2018 Jérôme Poisson (goffi@goffi.org) |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
6 |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
7 # 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
|
8 # 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
|
9 # 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
|
10 # (at your option) any later version. |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
11 |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
12 # 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
|
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
15 # GNU Affero General Public License for more details. |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
16 |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
17 # 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
|
18 # 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
|
19 |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
20 from sat.core.i18n import _ |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
21 from sat.core.constants import Const as C |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
22 from sat.core import exceptions |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
23 from sat.core.log import getLogger |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
24 log = getLogger(__name__) |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
25 from sat.tools.common import regex |
2528
65e278997715
component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents:
2527
diff
changeset
|
26 from sat.tools.common import uri |
2504
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
27 from sat.tools import stream |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
28 from twisted.internet import defer |
2527
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
29 from twisted.words.protocols.jabber import error |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
30 from wokkel import pubsub |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
31 from wokkel import generic |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
32 from functools import partial |
2504
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
33 import os |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
34 import os.path |
2514
4440ea7047bd
file sharing component: thumbnails integration first draft:
Goffi <goffi@goffi.org>
parents:
2504
diff
changeset
|
35 import mimetypes |
2504
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
36 |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
37 |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
38 PLUGIN_INFO = { |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
39 C.PI_NAME: "File sharing component", |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
40 C.PI_IMPORT_NAME: "file_sharing", |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
41 C.PI_MODES: [C.PLUG_MODE_COMPONENT], |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
42 C.PI_TYPE: C.PLUG_TYPE_ENTRY_POINT, |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
43 C.PI_PROTOCOLS: [], |
2522
95c31756944c
component file sharing, plugin XEP-0231: thumbnail are now returned by component using Bits of Binary:
Goffi <goffi@goffi.org>
parents:
2519
diff
changeset
|
44 C.PI_DEPENDENCIES: ["FILE", "XEP-0231", "XEP-0234", "XEP-0260", "XEP-0261", "XEP-0264", "XEP-0329"], |
2504
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
45 C.PI_RECOMMENDATIONS: [], |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
46 C.PI_MAIN: "FileSharing", |
2527
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
47 C.PI_HANDLER: C.BOOL_TRUE, |
2504
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
48 C.PI_DESCRIPTION: _(u"""Component hosting and sharing files""") |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
49 } |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
50 |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
51 HASH_ALGO = u'sha-256' |
2528
65e278997715
component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents:
2527
diff
changeset
|
52 NS_COMMENTS = 'org.salut-a-toi.comments' |
2527
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
53 COMMENT_NODE_PREFIX = 'org.salut-a-toi.file_comments/' |
2504
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
54 |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
55 |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
56 class FileSharing(object): |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
57 |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
58 def __init__(self, host): |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
59 log.info(_(u"File Sharing initialization")) |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
60 self.host = host |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
61 self._f = host.plugins['FILE'] |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
62 self._jf = host.plugins['XEP-0234'] |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
63 self._h = host.plugins['XEP-0300'] |
2514
4440ea7047bd
file sharing component: thumbnails integration first draft:
Goffi <goffi@goffi.org>
parents:
2504
diff
changeset
|
64 self._t = host.plugins['XEP-0264'] |
2504
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
65 host.trigger.add("FILE_getDestDir", self._getDestDirTrigger) |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
66 host.trigger.add("XEP-0234_fileSendingRequest", self._fileSendingRequestTrigger, priority=1000) |
2528
65e278997715
component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents:
2527
diff
changeset
|
67 host.trigger.add("XEP-0234_buildFileElement", self._addFileComments) |
65e278997715
component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents:
2527
diff
changeset
|
68 host.trigger.add("XEP-0234_parseFileElement", self._getFileComments) |
65e278997715
component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents:
2527
diff
changeset
|
69 host.trigger.add("XEP-0329_compGetFilesFromNode", self._addCommentsData) |
2504
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
70 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
|
71 |
2527
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
72 def getHandler(self, client): |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
73 return Comments_handler(self) |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
74 |
2504
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
75 def profileConnected(self, client): |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
76 path = client.file_tmp_dir = os.path.join( |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
77 self.host.memory.getConfig('', 'local_dir'), |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
78 C.FILES_TMP_DIR, |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
79 regex.pathEscape(client.profile)) |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
80 if not os.path.exists(path): |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
81 os.makedirs(path) |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
82 |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
83 @defer.inlineCallbacks |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
84 def _fileTransferedCb(self, dummy, client, peer_jid, file_data, file_path): |
2528
65e278997715
component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents:
2527
diff
changeset
|
85 """post file reception tasks |
65e278997715
component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents:
2527
diff
changeset
|
86 |
65e278997715
component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents:
2527
diff
changeset
|
87 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
|
88 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
|
89 """ |
2514
4440ea7047bd
file sharing component: thumbnails integration first draft:
Goffi <goffi@goffi.org>
parents:
2504
diff
changeset
|
90 name = file_data[u'name'] |
4440ea7047bd
file sharing component: thumbnails integration first draft:
Goffi <goffi@goffi.org>
parents:
2504
diff
changeset
|
91 extra = {} |
4440ea7047bd
file sharing component: thumbnails integration first draft:
Goffi <goffi@goffi.org>
parents:
2504
diff
changeset
|
92 |
2504
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
93 if file_data[u'hash_algo'] == HASH_ALGO: |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
94 log.debug(_(u"Reusing already generated hash")) |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
95 file_hash = file_data[u'hash_hasher'].hexdigest() |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
96 else: |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
97 hasher = self._h.getHasher(HASH_ALGO) |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
98 with open('file_path') as f: |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
99 file_hash = yield self._h.calculateHash(f, hasher) |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
100 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
|
101 |
2504
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
102 if os.path.isfile(final_path): |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
103 log.debug(u"file [{file_hash}] already exists, we can remove temporary one".format(file_hash = file_hash)) |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
104 os.unlink(file_path) |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
105 else: |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
106 os.rename(file_path, final_path) |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
107 log.debug(u"file [{file_hash}] moved to {files_path}".format(file_hash=file_hash, files_path=self.files_path)) |
2514
4440ea7047bd
file sharing component: thumbnails integration first draft:
Goffi <goffi@goffi.org>
parents:
2504
diff
changeset
|
108 |
4440ea7047bd
file sharing component: thumbnails integration first draft:
Goffi <goffi@goffi.org>
parents:
2504
diff
changeset
|
109 mime_type = file_data.get(u'mime_type') |
4440ea7047bd
file sharing component: thumbnails integration first draft:
Goffi <goffi@goffi.org>
parents:
2504
diff
changeset
|
110 if not mime_type or mime_type == u'application/octet-stream': |
4440ea7047bd
file sharing component: thumbnails integration first draft:
Goffi <goffi@goffi.org>
parents:
2504
diff
changeset
|
111 mime_type = mimetypes.guess_type(name)[0] |
4440ea7047bd
file sharing component: thumbnails integration first draft:
Goffi <goffi@goffi.org>
parents:
2504
diff
changeset
|
112 |
4440ea7047bd
file sharing component: thumbnails integration first draft:
Goffi <goffi@goffi.org>
parents:
2504
diff
changeset
|
113 if mime_type is not None and mime_type.startswith(u'image'): |
4440ea7047bd
file sharing component: thumbnails integration first draft:
Goffi <goffi@goffi.org>
parents:
2504
diff
changeset
|
114 thumbnails = extra.setdefault(C.KEY_THUMBNAILS, []) |
4440ea7047bd
file sharing component: thumbnails integration first draft:
Goffi <goffi@goffi.org>
parents:
2504
diff
changeset
|
115 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
|
116 try: |
4440ea7047bd
file sharing component: thumbnails integration first draft:
Goffi <goffi@goffi.org>
parents:
2504
diff
changeset
|
117 thumb_size, thumb_id = yield self._t.generateThumbnail(final_path, |
4440ea7047bd
file sharing component: thumbnails integration first draft:
Goffi <goffi@goffi.org>
parents:
2504
diff
changeset
|
118 max_thumb_size, |
4440ea7047bd
file sharing component: thumbnails integration first draft:
Goffi <goffi@goffi.org>
parents:
2504
diff
changeset
|
119 # we keep thumbnails for 6 months |
4440ea7047bd
file sharing component: thumbnails integration first draft:
Goffi <goffi@goffi.org>
parents:
2504
diff
changeset
|
120 60*60*24*31*6) |
4440ea7047bd
file sharing component: thumbnails integration first draft:
Goffi <goffi@goffi.org>
parents:
2504
diff
changeset
|
121 except Exception as e: |
4440ea7047bd
file sharing component: thumbnails integration first draft:
Goffi <goffi@goffi.org>
parents:
2504
diff
changeset
|
122 log.warning(_(u"Can't create thumbnail: {reason}").format(reason=e)) |
4440ea7047bd
file sharing component: thumbnails integration first draft:
Goffi <goffi@goffi.org>
parents:
2504
diff
changeset
|
123 break |
4440ea7047bd
file sharing component: thumbnails integration first draft:
Goffi <goffi@goffi.org>
parents:
2504
diff
changeset
|
124 thumbnails.append({u'id': thumb_id, u'size': thumb_size}) |
4440ea7047bd
file sharing component: thumbnails integration first draft:
Goffi <goffi@goffi.org>
parents:
2504
diff
changeset
|
125 |
2504
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
126 self.host.memory.setFile(client, |
2514
4440ea7047bd
file sharing component: thumbnails integration first draft:
Goffi <goffi@goffi.org>
parents:
2504
diff
changeset
|
127 name=name, |
2504
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
128 version=u'', |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
129 file_hash=file_hash, |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
130 hash_algo=HASH_ALGO, |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
131 size=file_data[u'size'], |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
132 path=file_data.get(u'path'), |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
133 namespace=file_data.get(u'namespace'), |
2514
4440ea7047bd
file sharing component: thumbnails integration first draft:
Goffi <goffi@goffi.org>
parents:
2504
diff
changeset
|
134 mime_type=mime_type, |
4440ea7047bd
file sharing component: thumbnails integration first draft:
Goffi <goffi@goffi.org>
parents:
2504
diff
changeset
|
135 owner=peer_jid, |
4440ea7047bd
file sharing component: thumbnails integration first draft:
Goffi <goffi@goffi.org>
parents:
2504
diff
changeset
|
136 extra=extra) |
2504
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
137 |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
138 def _getDestDirTrigger(self, client, peer_jid, transfer_data, file_data, stream_object): |
2528
65e278997715
component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents:
2527
diff
changeset
|
139 """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
|
140 if not client.is_component: |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
141 return True, None |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
142 assert stream_object |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
143 assert 'stream_object' not in transfer_data |
2514
4440ea7047bd
file sharing component: thumbnails integration first draft:
Goffi <goffi@goffi.org>
parents:
2504
diff
changeset
|
144 assert C.KEY_PROGRESS_ID in file_data |
2504
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
145 filename = file_data['name'] |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
146 assert filename and not '/' in filename |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
147 file_tmp_dir = self.host.getLocalPath(client, C.FILES_TMP_DIR, peer_jid.userhost(), component=True, profile=False) |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
148 file_tmp_path = file_data['file_path'] = os.path.join(file_tmp_dir, file_data['name']) |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
149 |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
150 transfer_data['finished_d'].addCallback(self._fileTransferedCb, client, peer_jid, file_data, file_tmp_path) |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
151 |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
152 self._f.openFileWrite(client, file_tmp_path, transfer_data, file_data, stream_object) |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
153 return False, defer.succeed(True) |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
154 |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
155 @defer.inlineCallbacks |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
156 def _retrieveFiles(self, client, session, content_data, content_name, file_data, file_elt): |
2528
65e278997715
component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents:
2527
diff
changeset
|
157 """This method retrieve a file on request, and send if after checking permissions""" |
2504
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
158 peer_jid = session[u'peer_jid'] |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
159 try: |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
160 found_files = yield self.host.memory.getFiles(client, |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
161 peer_jid=peer_jid, |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
162 name=file_data.get(u'name'), |
2519
353880a5c363
component file sharing: fixed hash handling (key has been renamed to "file_hash")
Goffi <goffi@goffi.org>
parents:
2514
diff
changeset
|
163 file_hash=file_data.get(u'file_hash'), |
2504
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
164 hash_algo=file_data.get(u'hash_algo'), |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
165 path=file_data.get(u'path'), |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
166 namespace=file_data.get(u'namespace')) |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
167 except exceptions.NotFound: |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
168 found_files = None |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
169 except exceptions.PermissionError: |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
170 log.warning(_(u"{peer_jid} is trying to access an unauthorized file: {name}").format( |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
171 peer_jid=peer_jid, name=file_data.get(u'name'))) |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
172 defer.returnValue(False) |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
173 |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
174 if not found_files: |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
175 log.warning(_(u"no matching file found ({file_data})").format(file_data=file_data)) |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
176 defer.returnValue(False) |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
177 |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
178 # we only use the first found file |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
179 found_file = found_files[0] |
2519
353880a5c363
component file sharing: fixed hash handling (key has been renamed to "file_hash")
Goffi <goffi@goffi.org>
parents:
2514
diff
changeset
|
180 file_hash = found_file[u'file_hash'] |
2504
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
181 file_path = os.path.join(self.files_path, file_hash) |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
182 file_data[u'hash_hasher'] = hasher = self._h.getHasher(found_file[u'hash_algo']) |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
183 size = file_data[u'size'] = found_file[u'size'] |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
184 file_data[u'file_hash'] = file_hash |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
185 file_data[u'hash_algo'] = found_file[u'hash_algo'] |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
186 |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
187 # we complete file_elt so peer can have some details on the file |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
188 if u'name' not in file_data: |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
189 file_elt.addElement(u'name', content=found_file[u'name']) |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
190 file_elt.addElement(u'size', content=unicode(size)) |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
191 content_data['stream_object'] = stream.FileStreamObject( |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
192 self.host, |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
193 client, |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
194 file_path, |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
195 uid=self._jf.getProgressId(session, content_name), |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
196 size=size, |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
197 data_cb=lambda data: hasher.update(data), |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
198 ) |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
199 defer.returnValue(True) |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
200 |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
201 def _fileSendingRequestTrigger(self, client, session, content_data, content_name, file_data, file_elt): |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
202 if not client.is_component: |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
203 return True, None |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
204 else: |
67cc54b01a12
plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
205 return False, self._retrieveFiles(client, session, content_data, content_name, file_data, file_elt) |
2527
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
206 |
2528
65e278997715
component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents:
2527
diff
changeset
|
207 ## comments triggers ## |
65e278997715
component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents:
2527
diff
changeset
|
208 |
65e278997715
component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents:
2527
diff
changeset
|
209 def _addFileComments(self, file_elt, extra_args): |
65e278997715
component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents:
2527
diff
changeset
|
210 try: |
65e278997715
component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents:
2527
diff
changeset
|
211 comments_url = extra_args.pop('comments_url') |
65e278997715
component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents:
2527
diff
changeset
|
212 except KeyError: |
65e278997715
component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents:
2527
diff
changeset
|
213 return |
65e278997715
component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents:
2527
diff
changeset
|
214 |
65e278997715
component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents:
2527
diff
changeset
|
215 comment_elt = file_elt.addElement((NS_COMMENTS, 'comments'), content=comments_url) |
65e278997715
component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents:
2527
diff
changeset
|
216 |
65e278997715
component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents:
2527
diff
changeset
|
217 try: |
65e278997715
component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents:
2527
diff
changeset
|
218 count = len(extra_args[u'extra'][u'comments']) |
65e278997715
component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents:
2527
diff
changeset
|
219 except KeyError: |
65e278997715
component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents:
2527
diff
changeset
|
220 count = 0 |
65e278997715
component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents:
2527
diff
changeset
|
221 |
65e278997715
component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents:
2527
diff
changeset
|
222 comment_elt['count'] = unicode(count) |
65e278997715
component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents:
2527
diff
changeset
|
223 return True |
65e278997715
component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents:
2527
diff
changeset
|
224 |
65e278997715
component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents:
2527
diff
changeset
|
225 def _getFileComments(self, file_elt, file_data): |
65e278997715
component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents:
2527
diff
changeset
|
226 try: |
65e278997715
component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents:
2527
diff
changeset
|
227 comments_elt = next(file_elt.elements(NS_COMMENTS, 'comments')) |
65e278997715
component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents:
2527
diff
changeset
|
228 except StopIteration: |
65e278997715
component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents:
2527
diff
changeset
|
229 return |
65e278997715
component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents:
2527
diff
changeset
|
230 file_data['comments_url'] = unicode(comments_elt) |
65e278997715
component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents:
2527
diff
changeset
|
231 file_data['comments_count'] = comments_elt['count'] |
65e278997715
component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents:
2527
diff
changeset
|
232 return True |
65e278997715
component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents:
2527
diff
changeset
|
233 |
65e278997715
component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents:
2527
diff
changeset
|
234 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
|
235 for file_data in files_data: |
65e278997715
component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents:
2527
diff
changeset
|
236 file_data['comments_url'] = uri.buildXMPPUri('pubsub', |
65e278997715
component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents:
2527
diff
changeset
|
237 path=client.jid.full(), |
65e278997715
component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents:
2527
diff
changeset
|
238 node=COMMENT_NODE_PREFIX + file_data['id']) |
65e278997715
component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents:
2527
diff
changeset
|
239 return True |
65e278997715
component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents:
2527
diff
changeset
|
240 |
2527
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
241 |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
242 class Comments_handler(pubsub.PubSubService): |
2528
65e278997715
component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents:
2527
diff
changeset
|
243 """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
|
244 |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
245 def __init__(self, plugin_parent): |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
246 super(Comments_handler, self).__init__() # PubsubVirtualResource()) |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
247 self.host = plugin_parent.host |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
248 self.plugin_parent = plugin_parent |
2529
cbbf2ff2ef3f
component file sharing: changed pubsub identity:
Goffi <goffi@goffi.org>
parents:
2528
diff
changeset
|
249 self.discoIdentity = {'category': 'pubsub', |
cbbf2ff2ef3f
component file sharing: changed pubsub identity:
Goffi <goffi@goffi.org>
parents:
2528
diff
changeset
|
250 'type': 'virtual', # FIXME: non standard, here to avoid this service being considered as main pubsub one |
cbbf2ff2ef3f
component file sharing: changed pubsub identity:
Goffi <goffi@goffi.org>
parents:
2528
diff
changeset
|
251 'name': 'files commenting service'} |
2527
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
252 |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
253 def _getFileId(self, nodeIdentifier): |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
254 if not nodeIdentifier.startswith(COMMENT_NODE_PREFIX): |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
255 raise error.StanzaError('item-not-found') |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
256 file_id = nodeIdentifier[len(COMMENT_NODE_PREFIX):] |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
257 if not file_id: |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
258 raise error.StanzaError('item-not-found') |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
259 return file_id |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
260 |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
261 @defer.inlineCallbacks |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
262 def getFileData(self, requestor, nodeIdentifier): |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
263 file_id = self._getFileId(nodeIdentifier) |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
264 try: |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
265 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
|
266 except (exceptions.NotFound, exceptions.PermissionError): |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
267 # we don't differenciate between NotFound and PermissionError |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
268 # to avoid leaking information on existing files |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
269 raise error.StanzaError('item-not-found') |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
270 if not files: |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
271 raise error.StanzaError('item-not-found') |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
272 if len(files) > 1: |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
273 raise error.InternalError('there should be only one file') |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
274 defer.returnValue(files[0]) |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
275 |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
276 def commentsUpdate(self, extra, new_comments, peer_jid): |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
277 """update comments (replace or insert new_comments) |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
278 |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
279 @param extra(dict): extra data to update |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
280 @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
|
281 @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
|
282 """ |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
283 current_comments = extra.setdefault('comments', []) |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
284 new_comments_by_id = {c[0]:c for c in new_comments} |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
285 updated = [] |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
286 # 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
|
287 # exist, in which case we must update |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
288 for idx, comment in enumerate(current_comments): |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
289 comment_id = comment[0] |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
290 if comment_id in new_comments_by_id: |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
291 # 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
|
292 if peer_jid and comment[1] != peer_jid: |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
293 # 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
|
294 raise exceptions.PermissionError |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
295 # we replace old_comment with updated one |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
296 new_comment = new_comments_by_id[comment_id] |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
297 current_comments[idx] = new_comment |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
298 updated.append(new_comment) |
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 # 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
|
301 # the ones to insert |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
302 for comment in updated: |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
303 new_comments.remove(comment) |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
304 |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
305 current_comments.extend(new_comments) |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
306 |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
307 def commentsDelete(self, extra, comments): |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
308 try: |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
309 comments_dict = extra['comments'] |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
310 except KeyError: |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
311 return |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
312 for comment in comments: |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
313 try: |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
314 comments_dict.remove(comment) |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
315 except ValueError: |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
316 continue |
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 def _getFrom(self, item_elt): |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
319 """retrieve published of an item |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
320 |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
321 @param item_elt(domish.element): <item> element |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
322 @return (unicode): full jid as string |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
323 """ |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
324 iq_elt = item_elt |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
325 while iq_elt.parent != None: |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
326 iq_elt = iq_elt.parent |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
327 return iq_elt['from'] |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
328 |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
329 @defer.inlineCallbacks |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
330 def publish(self, requestor, service, nodeIdentifier, items): |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
331 # we retrieve file a first time to check authorisations |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
332 file_data = yield self.getFileData(requestor, nodeIdentifier) |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
333 file_id = file_data['id'] |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
334 comments = [(item['id'], self._getFrom(item), item.toXml()) for item in items] |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
335 if requestor.userhostJID() == file_data['owner']: |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
336 peer_jid = None |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
337 else: |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
338 peer_jid = requestor.userhost() |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
339 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
|
340 try: |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
341 yield self.host.memory.fileUpdate(file_id, 'extra', update_cb) |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
342 except exceptions.PermissionError: |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
343 raise error.StanzaError('not-authorized') |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
344 |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
345 @defer.inlineCallbacks |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
346 def items(self, requestor, service, nodeIdentifier, maxItems, |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
347 itemIdentifiers): |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
348 file_data = yield self.getFileData(requestor, nodeIdentifier) |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
349 comments = file_data['extra'].get('comments', []) |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
350 if itemIdentifiers: |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
351 defer.returnValue([generic.parseXml(c[2]) for c in comments if c[0] in itemIdentifiers]) |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
352 else: |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
353 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
|
354 |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
355 @defer.inlineCallbacks |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
356 def retract(self, requestor, service, nodeIdentifier, itemIdentifiers): |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
357 file_data = yield self.getFileData(requestor, nodeIdentifier) |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
358 file_id = file_data['id'] |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
359 try: |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
360 comments = file_data['extra']['comments'] |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
361 except KeyError: |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
362 raise error.StanzaError('item-not-found') |
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 to_remove = [] |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
365 for comment in comments: |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
366 comment_id = comment[0] |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
367 if comment_id in itemIdentifiers: |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
368 to_remove.append(comment) |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
369 itemIdentifiers.remove(comment_id) |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
370 if not itemIdentifiers: |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
371 break |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
372 |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
373 if itemIdentifiers: |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
374 # not all items have been to_remove, we can't continue |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
375 raise error.StanzaError('item-not-found') |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
376 |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
377 if requestor.userhostJID() != file_data['owner']: |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
378 if not all([c[1] == requestor.userhost() for c in to_remove]): |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
379 raise error.StanzaError('not-authorized') |
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 remove_cb = partial(self.commentsDelete, comments=to_remove) |
a201194fc461
component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents:
2522
diff
changeset
|
382 yield self.host.memory.fileUpdate(file_id, 'extra', remove_cb) |