comparison src/plugins/plugin_comp_file_sharing.py @ 2504:67cc54b01a12

plugin file sharing component: first draft: this component act as an endpoint where a user can store and retrieve its files using Jingle.
author Goffi <goffi@goffi.org>
date Wed, 28 Feb 2018 18:28:39 +0100
parents
children 4440ea7047bd
comparison
equal deleted inserted replaced
2503:c0bec8bac2b5 2504:67cc54b01a12
1 #!/usr/bin/env python2
2 # -*- coding: utf-8 -*-
3
4 # SAT plugin for parrot mode (experimental)
5 # Copyright (C) 2009-2018 Jérôme Poisson (goffi@goffi.org)
6
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Affero General Public License for more details.
16
17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19
20 from sat.core.i18n import _
21 from sat.core.constants import Const as C
22 from sat.core import exceptions
23 from sat.core.log import getLogger
24 log = getLogger(__name__)
25 from sat.tools.common import regex
26 from sat.tools import stream
27 from twisted.internet import defer
28 import os
29 import os.path
30
31
32 PLUGIN_INFO = {
33 C.PI_NAME: "File sharing component",
34 C.PI_IMPORT_NAME: "file_sharing",
35 C.PI_MODES: [C.PLUG_MODE_COMPONENT],
36 C.PI_TYPE: C.PLUG_TYPE_ENTRY_POINT,
37 C.PI_PROTOCOLS: [],
38 C.PI_DEPENDENCIES: ["FILE", "XEP-0234", "XEP-0260", "XEP-0261", "XEP-0329"],
39 C.PI_RECOMMENDATIONS: [],
40 C.PI_MAIN: "FileSharing",
41 C.PI_HANDLER: "no",
42 C.PI_DESCRIPTION: _(u"""Component hosting and sharing files""")
43 }
44
45 PROGRESS_ID_KEY = 'progress_id'
46 HASH_ALGO = u'sha-256'
47
48
49 class FileSharing(object):
50
51 def __init__(self, host):
52 log.info(_(u"File Sharing initialization"))
53 self.host = host
54 self._f = host.plugins['FILE']
55 self._jf = host.plugins['XEP-0234']
56 self._h = host.plugins['XEP-0300']
57 host.trigger.add("FILE_getDestDir", self._getDestDirTrigger)
58 host.trigger.add("XEP-0234_fileSendingRequest", self._fileSendingRequestTrigger, priority=1000)
59 self.files_path = host.getLocalPath(None, C.FILES_DIR, profile=False)
60
61 def profileConnected(self, client):
62 path = client.file_tmp_dir = os.path.join(
63 self.host.memory.getConfig('', 'local_dir'),
64 C.FILES_TMP_DIR,
65 regex.pathEscape(client.profile))
66 if not os.path.exists(path):
67 os.makedirs(path)
68
69 @defer.inlineCallbacks
70 def _fileTransferedCb(self, dummy, client, peer_jid, file_data, file_path):
71 if file_data[u'hash_algo'] == HASH_ALGO:
72 log.debug(_(u"Reusing already generated hash"))
73 file_hash = file_data[u'hash_hasher'].hexdigest()
74 else:
75 hasher = self._h.getHasher(HASH_ALGO)
76 with open('file_path') as f:
77 file_hash = yield self._h.calculateHash(f, hasher)
78 final_path = os.path.join(self.files_path, file_hash)
79 if os.path.isfile(final_path):
80 log.debug(u"file [{file_hash}] already exists, we can remove temporary one".format(file_hash = file_hash))
81 os.unlink(file_path)
82 else:
83 os.rename(file_path, final_path)
84 log.debug(u"file [{file_hash}] moved to {files_path}".format(file_hash=file_hash, files_path=self.files_path))
85 self.host.memory.setFile(client,
86 name=file_data[u'name'],
87 version=u'',
88 file_hash=file_hash,
89 hash_algo=HASH_ALGO,
90 size=file_data[u'size'],
91 path=file_data.get(u'path'),
92 namespace=file_data.get(u'namespace'),
93 owner=peer_jid)
94
95 def _getDestDirTrigger(self, client, peer_jid, transfer_data, file_data, stream_object):
96 if not client.is_component:
97 return True, None
98 assert stream_object
99 assert 'stream_object' not in transfer_data
100 assert PROGRESS_ID_KEY in file_data
101 filename = file_data['name']
102 assert filename and not '/' in filename
103 file_tmp_dir = self.host.getLocalPath(client, C.FILES_TMP_DIR, peer_jid.userhost(), component=True, profile=False)
104 file_tmp_path = file_data['file_path'] = os.path.join(file_tmp_dir, file_data['name'])
105
106 transfer_data['finished_d'].addCallback(self._fileTransferedCb, client, peer_jid, file_data, file_tmp_path)
107
108 self._f.openFileWrite(client, file_tmp_path, transfer_data, file_data, stream_object)
109 return False, defer.succeed(True)
110
111 @defer.inlineCallbacks
112 def _retrieveFiles(self, client, session, content_data, content_name, file_data, file_elt):
113 peer_jid = session[u'peer_jid']
114 try:
115 found_files = yield self.host.memory.getFiles(client,
116 peer_jid=peer_jid,
117 name=file_data.get(u'name'),
118 file_hash=file_data.get(u'hash'),
119 hash_algo=file_data.get(u'hash_algo'),
120 path=file_data.get(u'path'),
121 namespace=file_data.get(u'namespace'))
122 except exceptions.NotFound:
123 found_files = None
124 except exceptions.PermissionError:
125 log.warning(_(u"{peer_jid} is trying to access an unauthorized file: {name}").format(
126 peer_jid=peer_jid, name=file_data.get(u'name')))
127 defer.returnValue(False)
128
129 if not found_files:
130 log.warning(_(u"no matching file found ({file_data})").format(file_data=file_data))
131 defer.returnValue(False)
132
133 # we only use the first found file
134 found_file = found_files[0]
135 file_hash = found_file[u'hash']
136 file_path = os.path.join(self.files_path, file_hash)
137 file_data[u'hash_hasher'] = hasher = self._h.getHasher(found_file[u'hash_algo'])
138 size = file_data[u'size'] = found_file[u'size']
139 file_data[u'file_hash'] = file_hash
140 file_data[u'hash_algo'] = found_file[u'hash_algo']
141
142 # we complete file_elt so peer can have some details on the file
143 if u'name' not in file_data:
144 file_elt.addElement(u'name', content=found_file[u'name'])
145 file_elt.addElement(u'size', content=unicode(size))
146 content_data['stream_object'] = stream.FileStreamObject(
147 self.host,
148 client,
149 file_path,
150 uid=self._jf.getProgressId(session, content_name),
151 size=size,
152 data_cb=lambda data: hasher.update(data),
153 )
154 defer.returnValue(True)
155
156 def _fileSendingRequestTrigger(self, client, session, content_data, content_name, file_data, file_elt):
157 if not client.is_component:
158 return True, None
159 else:
160 return False, self._retrieveFiles(client, session, content_data, content_name, file_data, file_elt)