annotate src/plugins/plugin_comp_file_sharing.py @ 2514:4440ea7047bd

file sharing component: thumbnails integration first draft: if an uploaded file is detected as an image, 2 thumbnails (small and medium size) are generated automatically.
author Goffi <goffi@goffi.org>
date Fri, 02 Mar 2018 17:53:31 +0100
parents 67cc54b01a12
children 353880a5c363
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
26 from sat.tools import stream
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
27 from twisted.internet import defer
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
28 import os
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
29 import os.path
2514
4440ea7047bd file sharing component: thumbnails integration first draft:
Goffi <goffi@goffi.org>
parents: 2504
diff changeset
30 import mimetypes
2504
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
31
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
32
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
33 PLUGIN_INFO = {
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
34 C.PI_NAME: "File sharing component",
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
35 C.PI_IMPORT_NAME: "file_sharing",
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
36 C.PI_MODES: [C.PLUG_MODE_COMPONENT],
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
37 C.PI_TYPE: C.PLUG_TYPE_ENTRY_POINT,
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
38 C.PI_PROTOCOLS: [],
2514
4440ea7047bd file sharing component: thumbnails integration first draft:
Goffi <goffi@goffi.org>
parents: 2504
diff changeset
39 C.PI_DEPENDENCIES: ["FILE", "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
40 C.PI_RECOMMENDATIONS: [],
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
41 C.PI_MAIN: "FileSharing",
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
42 C.PI_HANDLER: "no",
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
43 C.PI_DESCRIPTION: _(u"""Component hosting and sharing files""")
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
44 }
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
45
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
46 HASH_ALGO = u'sha-256'
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
47
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
48
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
49 class FileSharing(object):
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 def __init__(self, host):
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
52 log.info(_(u"File Sharing initialization"))
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
53 self.host = host
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
54 self._f = host.plugins['FILE']
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
55 self._jf = host.plugins['XEP-0234']
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
56 self._h = host.plugins['XEP-0300']
2514
4440ea7047bd file sharing component: thumbnails integration first draft:
Goffi <goffi@goffi.org>
parents: 2504
diff changeset
57 self._t = host.plugins['XEP-0264']
2504
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
58 host.trigger.add("FILE_getDestDir", self._getDestDirTrigger)
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
59 host.trigger.add("XEP-0234_fileSendingRequest", self._fileSendingRequestTrigger, priority=1000)
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
60 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
61
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
62 def profileConnected(self, client):
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
63 path = client.file_tmp_dir = os.path.join(
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
64 self.host.memory.getConfig('', 'local_dir'),
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
65 C.FILES_TMP_DIR,
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
66 regex.pathEscape(client.profile))
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
67 if not os.path.exists(path):
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
68 os.makedirs(path)
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
69
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
70 @defer.inlineCallbacks
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
71 def _fileTransferedCb(self, dummy, client, peer_jid, file_data, file_path):
2514
4440ea7047bd file sharing component: thumbnails integration first draft:
Goffi <goffi@goffi.org>
parents: 2504
diff changeset
72 name = file_data[u'name']
4440ea7047bd file sharing component: thumbnails integration first draft:
Goffi <goffi@goffi.org>
parents: 2504
diff changeset
73 extra = {}
4440ea7047bd file sharing component: thumbnails integration first draft:
Goffi <goffi@goffi.org>
parents: 2504
diff changeset
74
2504
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
75 if file_data[u'hash_algo'] == HASH_ALGO:
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
76 log.debug(_(u"Reusing already generated hash"))
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
77 file_hash = file_data[u'hash_hasher'].hexdigest()
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
78 else:
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
79 hasher = self._h.getHasher(HASH_ALGO)
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
80 with open('file_path') as f:
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
81 file_hash = yield self._h.calculateHash(f, hasher)
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
82 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
83
2504
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
84 if os.path.isfile(final_path):
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
85 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
86 os.unlink(file_path)
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
87 else:
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
88 os.rename(file_path, final_path)
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
89 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
90
4440ea7047bd file sharing component: thumbnails integration first draft:
Goffi <goffi@goffi.org>
parents: 2504
diff changeset
91 mime_type = file_data.get(u'mime_type')
4440ea7047bd file sharing component: thumbnails integration first draft:
Goffi <goffi@goffi.org>
parents: 2504
diff changeset
92 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
93 mime_type = mimetypes.guess_type(name)[0]
4440ea7047bd file sharing component: thumbnails integration first draft:
Goffi <goffi@goffi.org>
parents: 2504
diff changeset
94
4440ea7047bd file sharing component: thumbnails integration first draft:
Goffi <goffi@goffi.org>
parents: 2504
diff changeset
95 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
96 thumbnails = extra.setdefault(C.KEY_THUMBNAILS, [])
4440ea7047bd file sharing component: thumbnails integration first draft:
Goffi <goffi@goffi.org>
parents: 2504
diff changeset
97 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
98 try:
4440ea7047bd file sharing component: thumbnails integration first draft:
Goffi <goffi@goffi.org>
parents: 2504
diff changeset
99 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
100 max_thumb_size,
4440ea7047bd file sharing component: thumbnails integration first draft:
Goffi <goffi@goffi.org>
parents: 2504
diff changeset
101 # we keep thumbnails for 6 months
4440ea7047bd file sharing component: thumbnails integration first draft:
Goffi <goffi@goffi.org>
parents: 2504
diff changeset
102 60*60*24*31*6)
4440ea7047bd file sharing component: thumbnails integration first draft:
Goffi <goffi@goffi.org>
parents: 2504
diff changeset
103 except Exception as e:
4440ea7047bd file sharing component: thumbnails integration first draft:
Goffi <goffi@goffi.org>
parents: 2504
diff changeset
104 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
105 break
4440ea7047bd file sharing component: thumbnails integration first draft:
Goffi <goffi@goffi.org>
parents: 2504
diff changeset
106 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
107
2504
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
108 self.host.memory.setFile(client,
2514
4440ea7047bd file sharing component: thumbnails integration first draft:
Goffi <goffi@goffi.org>
parents: 2504
diff changeset
109 name=name,
2504
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
110 version=u'',
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
111 file_hash=file_hash,
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
112 hash_algo=HASH_ALGO,
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
113 size=file_data[u'size'],
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
114 path=file_data.get(u'path'),
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
115 namespace=file_data.get(u'namespace'),
2514
4440ea7047bd file sharing component: thumbnails integration first draft:
Goffi <goffi@goffi.org>
parents: 2504
diff changeset
116 mime_type=mime_type,
4440ea7047bd file sharing component: thumbnails integration first draft:
Goffi <goffi@goffi.org>
parents: 2504
diff changeset
117 owner=peer_jid,
4440ea7047bd file sharing component: thumbnails integration first draft:
Goffi <goffi@goffi.org>
parents: 2504
diff changeset
118 extra=extra)
2504
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
119
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
120 def _getDestDirTrigger(self, client, peer_jid, transfer_data, file_data, stream_object):
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
121 if not client.is_component:
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
122 return True, None
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
123 assert stream_object
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
124 assert 'stream_object' not in transfer_data
2514
4440ea7047bd file sharing component: thumbnails integration first draft:
Goffi <goffi@goffi.org>
parents: 2504
diff changeset
125 assert C.KEY_PROGRESS_ID in file_data
2504
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
126 filename = file_data['name']
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
127 assert filename and not '/' in filename
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
128 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
129 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
130
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
131 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
132
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
133 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
134 return False, defer.succeed(True)
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
135
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
136 @defer.inlineCallbacks
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
137 def _retrieveFiles(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
138 peer_jid = session[u'peer_jid']
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
139 try:
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
140 found_files = yield self.host.memory.getFiles(client,
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
141 peer_jid=peer_jid,
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
142 name=file_data.get(u'name'),
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
143 file_hash=file_data.get(u'hash'),
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
144 hash_algo=file_data.get(u'hash_algo'),
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
145 path=file_data.get(u'path'),
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
146 namespace=file_data.get(u'namespace'))
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
147 except exceptions.NotFound:
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
148 found_files = None
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
149 except exceptions.PermissionError:
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
150 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
151 peer_jid=peer_jid, name=file_data.get(u'name')))
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
152 defer.returnValue(False)
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
153
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
154 if not found_files:
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
155 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
156 defer.returnValue(False)
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
157
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
158 # we only use the first found file
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
159 found_file = found_files[0]
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
160 file_hash = found_file[u'hash']
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
161 file_path = os.path.join(self.files_path, file_hash)
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
162 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
163 size = file_data[u'size'] = found_file[u'size']
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
164 file_data[u'file_hash'] = file_hash
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
165 file_data[u'hash_algo'] = found_file[u'hash_algo']
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
166
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
167 # 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
168 if u'name' not in file_data:
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
169 file_elt.addElement(u'name', content=found_file[u'name'])
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
170 file_elt.addElement(u'size', content=unicode(size))
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
171 content_data['stream_object'] = stream.FileStreamObject(
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
172 self.host,
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
173 client,
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
174 file_path,
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
175 uid=self._jf.getProgressId(session, content_name),
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
176 size=size,
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
177 data_cb=lambda data: hasher.update(data),
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
178 )
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
179 defer.returnValue(True)
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
180
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
181 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
182 if not client.is_component:
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
183 return True, None
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
184 else:
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
185 return False, self._retrieveFiles(client, session, content_data, content_name, file_data, file_elt)