Mercurial > libervia-backend
annotate src/plugins/plugin_misc_file.py @ 1575:833bdb227b16
plugins XEP-0234, file: moved human file size conversion to file plugi
author | Goffi <goffi@goffi.org> |
---|---|
date | Wed, 11 Nov 2015 18:19:49 +0100 |
parents | babd97d80049 |
children | 846a39900fa6 |
rev | line source |
---|---|
1525 | 1 #!/usr/bin/python |
2 # -*- coding: utf-8 -*- | |
3 | |
4 # SAT plugin for file tansfer | |
5 # Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014, 2015 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 | |
1574
babd97d80049
plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents:
1568
diff
changeset
|
20 from sat.core.i18n import _, D_ |
1525 | 21 from sat.core.constants import Const as C |
22 from sat.core.log import getLogger | |
23 log = getLogger(__name__) | |
1574
babd97d80049
plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents:
1568
diff
changeset
|
24 from sat.tools import xml_tools |
babd97d80049
plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents:
1568
diff
changeset
|
25 from twisted.internet import defer |
1525 | 26 import os |
27 import uuid | |
28 | |
29 | |
30 PLUGIN_INFO = { | |
31 "name": "File Tansfer", | |
32 "import_name": "FILE", | |
33 "type": C.PLUG_TYPE_MISC, | |
34 "main": "FilePlugin", | |
35 "handler": "no", | |
36 "description": _("""File Tansfer Management: | |
37 This plugin manage the various ways of sending a file, and choose the best one.""") | |
38 } | |
39 | |
40 | |
1574
babd97d80049
plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents:
1568
diff
changeset
|
41 CONFIRM = D_(u'{peer} wants to send the file "{name}" to you:\n{desc}\n\nThe file has a size of {size_human}\n\nDo you accept ?') |
babd97d80049
plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents:
1568
diff
changeset
|
42 CONFIRM_TITLE = D_(u'Confirm file transfer') |
babd97d80049
plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents:
1568
diff
changeset
|
43 CONFIRM_OVERWRITE = D_(u'File {} already exists, are you sure you want to overwrite ?') |
babd97d80049
plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents:
1568
diff
changeset
|
44 CONFIRM_OVERWRITE_TITLE = D_(u'File exists') |
babd97d80049
plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents:
1568
diff
changeset
|
45 |
babd97d80049
plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents:
1568
diff
changeset
|
46 |
1553 | 47 class SatFile(object): |
1525 | 48 """A file-like object to have high level files manipulation""" |
49 # TODO: manage "with" statement | |
50 | |
51 def __init__(self, host, path, mode='r', uid=None, size=None, profile=C.PROF_KEY_NONE): | |
52 """ | |
53 @param host: %(doc_host)s | |
54 @param path(str): path of the file to get | |
55 @param mode(str): same as for built-in "open" function | |
56 @param uid(unicode, None): unique id identifing this progressing element | |
57 will be automaticaly generated if None | |
58 @param size(None, int): size of the file | |
59 """ | |
60 self.host = host | |
61 self.uid = uid or unicode(uuid.uuid4()) | |
62 self._file = open(path, mode) | |
63 self.size = None | |
64 self.profile = profile | |
65 self.eof = defer.Deferred() | |
66 self.host.registerProgressCb(self.uid, self.getProgress, profile) | |
67 self.host.bridge.progressStarted(self.uid, self.profile) | |
68 self.eof.addCallback(lambda ignore: self.host.bridge.progressFinished(self.uid, self.profile)) | |
69 self.eof.addErrback(lambda failure: self.host.bridge.progressError(self.uid, unicode(failure), self.profile)) | |
70 | |
71 def close(self): | |
72 self._file.close() | |
73 self.host.removeProgressCb(self.uid, self.profile) | |
74 | |
1568
1f7a34d499e0
plugins XEP-0234, file: use of SatFile for writing too
Goffi <goffi@goffi.org>
parents:
1553
diff
changeset
|
75 def flush(self): |
1f7a34d499e0
plugins XEP-0234, file: use of SatFile for writing too
Goffi <goffi@goffi.org>
parents:
1553
diff
changeset
|
76 self._file.flush() |
1f7a34d499e0
plugins XEP-0234, file: use of SatFile for writing too
Goffi <goffi@goffi.org>
parents:
1553
diff
changeset
|
77 |
1f7a34d499e0
plugins XEP-0234, file: use of SatFile for writing too
Goffi <goffi@goffi.org>
parents:
1553
diff
changeset
|
78 def write(self, buf): |
1f7a34d499e0
plugins XEP-0234, file: use of SatFile for writing too
Goffi <goffi@goffi.org>
parents:
1553
diff
changeset
|
79 self._file.write(buf) |
1f7a34d499e0
plugins XEP-0234, file: use of SatFile for writing too
Goffi <goffi@goffi.org>
parents:
1553
diff
changeset
|
80 |
1525 | 81 def read(self, size=-1): |
82 read = self._file.read(size) | |
83 if not read: | |
84 self.eof.callback(None) | |
85 return read | |
86 | |
87 def seek(self, offset, whence=os.SEEK_SET): | |
88 self._file.seek(offset, whence) | |
89 | |
90 def tell(self): | |
91 return self._file.tell() | |
92 | |
93 def getProgress(self, progress_id, data, profile): | |
94 return {'position': self._file.tell(), 'size': self.size or 0} | |
95 | |
96 | |
97 class FilePlugin(object): | |
98 File=SatFile | |
99 | |
100 def __init__(self, host): | |
101 log.info(_("plugin File initialization")) | |
102 self.host = host | |
103 | |
1574
babd97d80049
plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents:
1568
diff
changeset
|
104 # Dialogs with user |
babd97d80049
plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents:
1568
diff
changeset
|
105 # the overwrite check is done here |
1525 | 106 |
1574
babd97d80049
plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents:
1568
diff
changeset
|
107 def _openFileWrite(self, file_path, transfer_data, file_data, profile): |
babd97d80049
plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents:
1568
diff
changeset
|
108 assert 'file_obj' not in transfer_data |
babd97d80049
plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents:
1568
diff
changeset
|
109 transfer_data['file_obj'] = SatFile( |
babd97d80049
plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents:
1568
diff
changeset
|
110 self.host, |
babd97d80049
plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents:
1568
diff
changeset
|
111 file_path, |
babd97d80049
plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents:
1568
diff
changeset
|
112 'w', |
babd97d80049
plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents:
1568
diff
changeset
|
113 size=file_data['size'], |
babd97d80049
plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents:
1568
diff
changeset
|
114 profile=profile, |
babd97d80049
plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents:
1568
diff
changeset
|
115 ) |
babd97d80049
plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents:
1568
diff
changeset
|
116 |
babd97d80049
plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents:
1568
diff
changeset
|
117 def _gotConfirmation(self, data, peer_jid, transfer_data, file_data, profile): |
babd97d80049
plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents:
1568
diff
changeset
|
118 """Called when the permission and dest path have been received |
babd97d80049
plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents:
1568
diff
changeset
|
119 |
babd97d80049
plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents:
1568
diff
changeset
|
120 @param peer_jid(jid.JID): jid of the file sender |
babd97d80049
plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents:
1568
diff
changeset
|
121 @param transfer_data(dict): same as for [self.getDestDir] |
babd97d80049
plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents:
1568
diff
changeset
|
122 @param file_data(dict): same as for [self.getDestDir] |
babd97d80049
plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents:
1568
diff
changeset
|
123 @param profile: %(doc_profile)s |
babd97d80049
plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents:
1568
diff
changeset
|
124 return (bool): True if copy is wanted and OK |
babd97d80049
plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents:
1568
diff
changeset
|
125 False if user wants to cancel |
babd97d80049
plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents:
1568
diff
changeset
|
126 if file exists ask confirmation and call again self._getDestDir if needed |
babd97d80049
plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents:
1568
diff
changeset
|
127 """ |
babd97d80049
plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents:
1568
diff
changeset
|
128 if data.get('cancelled', False): |
babd97d80049
plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents:
1568
diff
changeset
|
129 return False |
babd97d80049
plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents:
1568
diff
changeset
|
130 path = data['path'] |
babd97d80049
plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents:
1568
diff
changeset
|
131 file_data['file_path'] = file_path = os.path.join(path, file_data['name']) |
babd97d80049
plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents:
1568
diff
changeset
|
132 log.debug(u'destination file path set to {}'.format(file_path)) |
babd97d80049
plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents:
1568
diff
changeset
|
133 |
babd97d80049
plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents:
1568
diff
changeset
|
134 # we manage case where file already exists |
babd97d80049
plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents:
1568
diff
changeset
|
135 if os.path.exists(file_path): |
babd97d80049
plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents:
1568
diff
changeset
|
136 def check_overwrite(overwrite): |
babd97d80049
plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents:
1568
diff
changeset
|
137 if overwrite: |
babd97d80049
plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents:
1568
diff
changeset
|
138 self._openFileWrite(file_path, transfer_data, file_data, profile) |
babd97d80049
plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents:
1568
diff
changeset
|
139 return True |
babd97d80049
plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents:
1568
diff
changeset
|
140 else: |
babd97d80049
plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents:
1568
diff
changeset
|
141 return self.getDestDir(peer_jid, transfer_data, file_data, profile) |
babd97d80049
plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents:
1568
diff
changeset
|
142 |
babd97d80049
plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents:
1568
diff
changeset
|
143 exists_d = xml_tools.deferConfirm( |
babd97d80049
plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents:
1568
diff
changeset
|
144 self.host, |
babd97d80049
plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents:
1568
diff
changeset
|
145 _(CONFIRM_OVERWRITE).format(file_path), |
babd97d80049
plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents:
1568
diff
changeset
|
146 _(CONFIRM_OVERWRITE_TITLE), |
babd97d80049
plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents:
1568
diff
changeset
|
147 profile=profile) |
babd97d80049
plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents:
1568
diff
changeset
|
148 exists_d.addCallback(check_overwrite) |
babd97d80049
plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents:
1568
diff
changeset
|
149 return exists_d |
babd97d80049
plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents:
1568
diff
changeset
|
150 |
babd97d80049
plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents:
1568
diff
changeset
|
151 self._openFileWrite(file_path, transfer_data, file_data, profile) |
babd97d80049
plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents:
1568
diff
changeset
|
152 return True |
babd97d80049
plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents:
1568
diff
changeset
|
153 |
babd97d80049
plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents:
1568
diff
changeset
|
154 def getDestDir(self, peer_jid, transfer_data, file_data, profile): |
babd97d80049
plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents:
1568
diff
changeset
|
155 """Request confirmation and destination dir to user |
babd97d80049
plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents:
1568
diff
changeset
|
156 |
babd97d80049
plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents:
1568
diff
changeset
|
157 Overwrite confirmation is managed. |
babd97d80049
plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents:
1568
diff
changeset
|
158 if transfer is confirmed, 'file_obj' is added to transfer_data |
babd97d80049
plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents:
1568
diff
changeset
|
159 @param peer_jid(jid.JID): jid of the file sender |
babd97d80049
plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents:
1568
diff
changeset
|
160 @param filename(unicode): name of the file |
babd97d80049
plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents:
1568
diff
changeset
|
161 @param transfer_data(dict): data of the transfer session, |
babd97d80049
plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents:
1568
diff
changeset
|
162 it will be only used to store the file_obj. |
babd97d80049
plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents:
1568
diff
changeset
|
163 "file_obj" key *MUST NOT* exist before using getDestDir |
babd97d80049
plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents:
1568
diff
changeset
|
164 @param file_data(dict): information about the file to be transfered |
babd97d80049
plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents:
1568
diff
changeset
|
165 It MUST contain the following keys: |
babd97d80049
plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents:
1568
diff
changeset
|
166 - peer_jid (jid.JID): other peer jid |
babd97d80049
plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents:
1568
diff
changeset
|
167 - name (unicode): name of the file to trasnsfer |
babd97d80049
plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents:
1568
diff
changeset
|
168 the name must not be empty or contain a "/" character |
babd97d80049
plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents:
1568
diff
changeset
|
169 - size (int): size of the file |
babd97d80049
plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents:
1568
diff
changeset
|
170 It may content the key used in CONFIRM constant |
babd97d80049
plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents:
1568
diff
changeset
|
171 It *MUST NOT* contain the "peer" key |
babd97d80049
plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents:
1568
diff
changeset
|
172 "file_path" will be added to this dict once destination selected |
1575
833bdb227b16
plugins XEP-0234, file: moved human file size conversion to file plugi
Goffi <goffi@goffi.org>
parents:
1574
diff
changeset
|
173 "size_human" will also be added with human readable file size |
1574
babd97d80049
plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents:
1568
diff
changeset
|
174 @param profile: %(doc_profile)s |
babd97d80049
plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents:
1568
diff
changeset
|
175 return (defer.Deferred): True if transfer is accepted |
babd97d80049
plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents:
1568
diff
changeset
|
176 """ |
babd97d80049
plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents:
1568
diff
changeset
|
177 filename = file_data['name'] |
babd97d80049
plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents:
1568
diff
changeset
|
178 assert filename and not '/' in filename |
1575
833bdb227b16
plugins XEP-0234, file: moved human file size conversion to file plugi
Goffi <goffi@goffi.org>
parents:
1574
diff
changeset
|
179 # human readable size |
833bdb227b16
plugins XEP-0234, file: moved human file size conversion to file plugi
Goffi <goffi@goffi.org>
parents:
1574
diff
changeset
|
180 file_data['size_human'] = u'{:.6n} Mio'.format(float(file_data['size'])/(1024**2)) |
1574
babd97d80049
plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents:
1568
diff
changeset
|
181 d = xml_tools.deferDialog(self.host, |
babd97d80049
plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents:
1568
diff
changeset
|
182 _(CONFIRM).format(peer=peer_jid.full(), **file_data), |
babd97d80049
plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents:
1568
diff
changeset
|
183 _(CONFIRM_TITLE), |
babd97d80049
plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents:
1568
diff
changeset
|
184 type_=C.XMLUI_DIALOG_FILE, |
babd97d80049
plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents:
1568
diff
changeset
|
185 options={C.XMLUI_DATA_FILETYPE: C.XMLUI_DATA_FILETYPE_DIR}, |
babd97d80049
plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents:
1568
diff
changeset
|
186 profile=profile) |
babd97d80049
plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents:
1568
diff
changeset
|
187 d.addCallback(self._gotConfirmation, peer_jid, transfer_data, file_data, profile) |
babd97d80049
plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents:
1568
diff
changeset
|
188 return d |