Mercurial > libervia-backend
annotate sat/plugins/plugin_xep_0234.py @ 3330:7b47f48d31f3
plugin XEP-0264: fix orientation of thumbnails:
Rotation of thumbnailis is now modified according to EXIF orientation data. Original image
is not modified as user may want to keep it, and orientation transformation can modify
encoding parameters.
Update `pillow` minimum version to the first one with `exif_transpose` implementation.
author | Goffi <goffi@goffi.org> |
---|---|
date | Thu, 13 Aug 2020 23:46:18 +0200 |
parents | 5887fb414758 |
children | ac9342f359e9 |
rev | line source |
---|---|
3028 | 1 #!/usr/bin/env python3 |
3137 | 2 |
1528
1c71d7335d02
plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
3 # SAT plugin for Jingle File Transfer (XEP-0234) |
3136 | 4 # Copyright (C) 2009-2020 Jérôme Poisson (goffi@goffi.org) |
1528
1c71d7335d02
plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
5 |
1c71d7335d02
plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
6 # This program is free software: you can redistribute it and/or modify |
1c71d7335d02
plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
7 # it under the terms of the GNU Affero General Public License as published by |
1c71d7335d02
plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
8 # the Free Software Foundation, either version 3 of the License, or |
1c71d7335d02
plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
9 # (at your option) any later version. |
1c71d7335d02
plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
10 |
1c71d7335d02
plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
11 # This program is distributed in the hope that it will be useful, |
1c71d7335d02
plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
1c71d7335d02
plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
1c71d7335d02
plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
14 # GNU Affero General Public License for more details. |
1c71d7335d02
plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
15 |
1c71d7335d02
plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
16 # You should have received a copy of the GNU Affero General Public License |
1c71d7335d02
plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
17 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
1c71d7335d02
plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
18 |
1574
babd97d80049
plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents:
1571
diff
changeset
|
19 from sat.core.i18n import _ |
1528
1c71d7335d02
plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
20 from sat.core.constants import Const as C |
1c71d7335d02
plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
21 from sat.core.log import getLogger |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
22 |
1528
1c71d7335d02
plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
23 log = getLogger(__name__) |
1c71d7335d02
plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
24 from sat.core import exceptions |
1c71d7335d02
plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
25 from wokkel import disco, iwokkel |
3028 | 26 from zope.interface import implementer |
1528
1c71d7335d02
plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
27 from sat.tools import utils |
2489
e2a7bb875957
plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
28 from sat.tools import stream |
2612
3e4e78de9cca
tools (date_utils): moved date_parse to common.date_utils, because it can be used in frontends
Goffi <goffi@goffi.org>
parents:
2589
diff
changeset
|
29 from sat.tools.common import date_utils |
1528
1c71d7335d02
plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
30 import os.path |
1c71d7335d02
plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
31 from twisted.words.xish import domish |
1c71d7335d02
plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
32 from twisted.words.protocols.jabber import jid |
1c71d7335d02
plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
33 from twisted.python import failure |
1566
ec3848916ee8
plugin ip: implemented XEP-0279 for external ip retrieval + fixed bad exception handling
Goffi <goffi@goffi.org>
parents:
1557
diff
changeset
|
34 from twisted.words.protocols.jabber.xmlstream import XMPPHandler |
1571
c668081eba1c
plugins XEP-0234, XEP-0260, XEP-0261: jingle session termination is managed by application (XEP-0234) instead of transport
Goffi <goffi@goffi.org>
parents:
1568
diff
changeset
|
35 from twisted.internet import defer |
1620
4dd07d026214
plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents:
1618
diff
changeset
|
36 from twisted.internet import reactor |
4dd07d026214
plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents:
1618
diff
changeset
|
37 from twisted.internet import error as internet_error |
2502
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
38 from collections import namedtuple |
2535
a19b2c43e719
plugin XEP-0234: fixed regex import
Goffi <goffi@goffi.org>
parents:
2512
diff
changeset
|
39 from sat.tools.common import regex |
2512
025afb04c10b
plugin XEP-0234: some cleaning + added triggers to allow plugins to change parsing/generation of <file> element
Goffi <goffi@goffi.org>
parents:
2502
diff
changeset
|
40 import mimetypes |
1528
1c71d7335d02
plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
41 |
1c71d7335d02
plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
42 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
43 NS_JINGLE_FT = "urn:xmpp:jingle:apps:file-transfer:5" |
1528
1c71d7335d02
plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
44 |
1c71d7335d02
plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
45 PLUGIN_INFO = { |
2145
33c8c4973743
core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents:
2144
diff
changeset
|
46 C.PI_NAME: "Jingle File Transfer", |
33c8c4973743
core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents:
2144
diff
changeset
|
47 C.PI_IMPORT_NAME: "XEP-0234", |
33c8c4973743
core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents:
2144
diff
changeset
|
48 C.PI_TYPE: "XEP", |
2502
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
49 C.PI_MODES: C.PLUG_MODE_BOTH, |
2145
33c8c4973743
core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents:
2144
diff
changeset
|
50 C.PI_PROTOCOLS: ["XEP-0234"], |
33c8c4973743
core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents:
2144
diff
changeset
|
51 C.PI_DEPENDENCIES: ["XEP-0166", "XEP-0300", "FILE"], |
33c8c4973743
core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents:
2144
diff
changeset
|
52 C.PI_MAIN: "XEP_0234", |
33c8c4973743
core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents:
2144
diff
changeset
|
53 C.PI_HANDLER: "yes", |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
54 C.PI_DESCRIPTION: _("""Implementation of Jingle File Transfer"""), |
1528
1c71d7335d02
plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
55 } |
1c71d7335d02
plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
56 |
3040 | 57 EXTRA_ALLOWED = {"path", "namespace", "file_desc", "file_hash", "hash_algo"} |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
58 Range = namedtuple("Range", ("offset", "length")) |
2502
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
59 |
1528
1c71d7335d02
plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
60 |
1c71d7335d02
plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
61 class XEP_0234(object): |
1571
c668081eba1c
plugins XEP-0234, XEP-0260, XEP-0261: jingle session termination is managed by application (XEP-0234) instead of transport
Goffi <goffi@goffi.org>
parents:
1568
diff
changeset
|
62 # TODO: assure everything is closed when file is sent or session terminate is received |
1620
4dd07d026214
plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents:
1618
diff
changeset
|
63 # TODO: call self._f.unregister when unloading order will be managing (i.e. when dependencies will be unloaded at the end) |
2512
025afb04c10b
plugin XEP-0234: some cleaning + added triggers to allow plugins to change parsing/generation of <file> element
Goffi <goffi@goffi.org>
parents:
2502
diff
changeset
|
64 Range = Range # we copy the class here, so it can be used by other plugins |
1528
1c71d7335d02
plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
65 |
1c71d7335d02
plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
66 def __init__(self, host): |
1c71d7335d02
plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
67 log.info(_("plugin Jingle File Transfer initialization")) |
1c71d7335d02
plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
68 self.host = host |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
69 host.registerNamespace("jingle-ft", NS_JINGLE_FT) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
70 self._j = host.plugins["XEP-0166"] # shortcut to access jingle |
1528
1c71d7335d02
plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
71 self._j.registerApplication(NS_JINGLE_FT, self) |
1c71d7335d02
plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
72 self._f = host.plugins["FILE"] |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
73 self._f.register( |
3028 | 74 NS_JINGLE_FT, self.fileJingleSend, priority=10000, method_name="Jingle" |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
75 ) |
1618
0de5f210fe56
plugin XEP-0300: implemented hashing:
Goffi <goffi@goffi.org>
parents:
1598
diff
changeset
|
76 self._hash = self.host.plugins["XEP-0300"] |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
77 host.bridge.addMethod( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
78 "fileJingleSend", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
79 ".plugin", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
80 in_sign="ssssa{ss}s", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
81 out_sign="", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
82 method=self._fileJingleSend, |
3028 | 83 async_=True, |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
84 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
85 host.bridge.addMethod( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
86 "fileJingleRequest", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
87 ".plugin", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
88 in_sign="sssssa{ss}s", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
89 out_sign="s", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
90 method=self._fileJingleRequest, |
3028 | 91 async_=True, |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
92 ) |
1528
1c71d7335d02
plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
93 |
2144
1d3f73e065e1
core, jp: component handling + client handling refactoring:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
94 def getHandler(self, client): |
1528
1c71d7335d02
plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
95 return XEP_0234_handler() |
1c71d7335d02
plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
96 |
2502
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
97 def getProgressId(self, session, content_name): |
1585
846a39900fa6
plugins XEP-0096, XEP-0260, file: sendFile method is managed by file plugin, which choose the best available method + progress_id fix
Goffi <goffi@goffi.org>
parents:
1575
diff
changeset
|
98 """Return a unique progress ID |
846a39900fa6
plugins XEP-0096, XEP-0260, file: sendFile method is managed by file plugin, which choose the best available method + progress_id fix
Goffi <goffi@goffi.org>
parents:
1575
diff
changeset
|
99 |
846a39900fa6
plugins XEP-0096, XEP-0260, file: sendFile method is managed by file plugin, which choose the best available method + progress_id fix
Goffi <goffi@goffi.org>
parents:
1575
diff
changeset
|
100 @param session(dict): jingle session |
846a39900fa6
plugins XEP-0096, XEP-0260, file: sendFile method is managed by file plugin, which choose the best available method + progress_id fix
Goffi <goffi@goffi.org>
parents:
1575
diff
changeset
|
101 @param content_name(unicode): name of the content |
846a39900fa6
plugins XEP-0096, XEP-0260, file: sendFile method is managed by file plugin, which choose the best available method + progress_id fix
Goffi <goffi@goffi.org>
parents:
1575
diff
changeset
|
102 @return (unicode): unique progress id |
846a39900fa6
plugins XEP-0096, XEP-0260, file: sendFile method is managed by file plugin, which choose the best available method + progress_id fix
Goffi <goffi@goffi.org>
parents:
1575
diff
changeset
|
103 """ |
3028 | 104 return "{}_{}".format(session["id"], content_name) |
1585
846a39900fa6
plugins XEP-0096, XEP-0260, file: sendFile method is managed by file plugin, which choose the best available method + progress_id fix
Goffi <goffi@goffi.org>
parents:
1575
diff
changeset
|
105 |
2502
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
106 # generic methods |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
107 |
3314
5887fb414758
component file sharing: add/parse affiliation when possible
Goffi <goffi@goffi.org>
parents:
3303
diff
changeset
|
108 def buildFileElement( |
5887fb414758
component file sharing: add/parse affiliation when possible
Goffi <goffi@goffi.org>
parents:
3303
diff
changeset
|
109 self, client, name=None, file_hash=None, hash_algo=None, size=None, |
2927
69e4716d6268
plugins (jingle) file transfer: use initial "from" attribute as local jid instead of client.jid:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
110 mime_type=None, desc=None, modified=None, transfer_range=None, path=None, |
69e4716d6268
plugins (jingle) file transfer: use initial "from" attribute as local jid instead of client.jid:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
111 namespace=None, file_elt=None, **kwargs): |
2502
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
112 """Generate a <file> element with available metadata |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
113 |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
114 @param file_hash(unicode, None): hash of the file |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
115 empty string to set <hash-used/> element |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
116 @param hash_algo(unicode, None): hash algorithm used |
3314
5887fb414758
component file sharing: add/parse affiliation when possible
Goffi <goffi@goffi.org>
parents:
3303
diff
changeset
|
117 if file_hash is None and hash_algo is set, a <hash-used/> element will be |
5887fb414758
component file sharing: add/parse affiliation when possible
Goffi <goffi@goffi.org>
parents:
3303
diff
changeset
|
118 generated |
2512
025afb04c10b
plugin XEP-0234: some cleaning + added triggers to allow plugins to change parsing/generation of <file> element
Goffi <goffi@goffi.org>
parents:
2502
diff
changeset
|
119 @param transfer_range(Range, None): where transfer must start/stop |
025afb04c10b
plugin XEP-0234: some cleaning + added triggers to allow plugins to change parsing/generation of <file> element
Goffi <goffi@goffi.org>
parents:
2502
diff
changeset
|
120 @param modified(int, unicode, None): date of last modification |
2502
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
121 0 to use current date |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
122 int to use an unix timestamp |
3314
5887fb414758
component file sharing: add/parse affiliation when possible
Goffi <goffi@goffi.org>
parents:
3303
diff
changeset
|
123 else must be an unicode string which will be used as it (it must be an XMPP |
5887fb414758
component file sharing: add/parse affiliation when possible
Goffi <goffi@goffi.org>
parents:
3303
diff
changeset
|
124 time) |
2502
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
125 @param file_elt(domish.Element, None): element to use |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
126 None to create a new one |
2512
025afb04c10b
plugin XEP-0234: some cleaning + added triggers to allow plugins to change parsing/generation of <file> element
Goffi <goffi@goffi.org>
parents:
2502
diff
changeset
|
127 @param **kwargs: data for plugin extension (ignored by default) |
2502
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
128 @return (domish.Element): generated element |
3314
5887fb414758
component file sharing: add/parse affiliation when possible
Goffi <goffi@goffi.org>
parents:
3303
diff
changeset
|
129 @trigger XEP-0234_buildFileElement(file_elt, extra_args): can be used to extend |
5887fb414758
component file sharing: add/parse affiliation when possible
Goffi <goffi@goffi.org>
parents:
3303
diff
changeset
|
130 elements to add |
2502
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
131 """ |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
132 if file_elt is None: |
3028 | 133 file_elt = domish.Element((NS_JINGLE_FT, "file")) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
134 for name, value in ( |
3028 | 135 ("name", name), |
136 ("size", size), | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
137 ("media-type", mime_type), |
3028 | 138 ("desc", desc), |
139 ("path", path), | |
140 ("namespace", namespace), | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
141 ): |
2502
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
142 if value is not None: |
3028 | 143 file_elt.addElement(name, content=str(value)) |
2512
025afb04c10b
plugin XEP-0234: some cleaning + added triggers to allow plugins to change parsing/generation of <file> element
Goffi <goffi@goffi.org>
parents:
2502
diff
changeset
|
144 |
025afb04c10b
plugin XEP-0234: some cleaning + added triggers to allow plugins to change parsing/generation of <file> element
Goffi <goffi@goffi.org>
parents:
2502
diff
changeset
|
145 if modified is not None: |
025afb04c10b
plugin XEP-0234: some cleaning + added triggers to allow plugins to change parsing/generation of <file> element
Goffi <goffi@goffi.org>
parents:
2502
diff
changeset
|
146 if isinstance(modified, int): |
3028 | 147 file_elt.addElement("date", utils.xmpp_date(modified or None)) |
2502
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
148 else: |
3028 | 149 file_elt.addElement("date", modified) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
150 elif "created" in kwargs: |
3028 | 151 file_elt.addElement("date", utils.xmpp_date(kwargs.pop("created"))) |
2512
025afb04c10b
plugin XEP-0234: some cleaning + added triggers to allow plugins to change parsing/generation of <file> element
Goffi <goffi@goffi.org>
parents:
2502
diff
changeset
|
152 |
3028 | 153 range_elt = file_elt.addElement("range") |
2512
025afb04c10b
plugin XEP-0234: some cleaning + added triggers to allow plugins to change parsing/generation of <file> element
Goffi <goffi@goffi.org>
parents:
2502
diff
changeset
|
154 if transfer_range is not None: |
025afb04c10b
plugin XEP-0234: some cleaning + added triggers to allow plugins to change parsing/generation of <file> element
Goffi <goffi@goffi.org>
parents:
2502
diff
changeset
|
155 if transfer_range.offset is not None: |
3028 | 156 range_elt["offset"] = transfer_range.offset |
2512
025afb04c10b
plugin XEP-0234: some cleaning + added triggers to allow plugins to change parsing/generation of <file> element
Goffi <goffi@goffi.org>
parents:
2502
diff
changeset
|
157 if transfer_range.length is not None: |
3028 | 158 range_elt["length"] = transfer_range.length |
2502
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
159 if file_hash is not None: |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
160 if not file_hash: |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
161 file_elt.addChild(self._hash.buildHashUsedElt()) |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
162 else: |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
163 file_elt.addChild(self._hash.buildHashElt(file_hash, hash_algo)) |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
164 elif hash_algo is not None: |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
165 file_elt.addChild(self._hash.buildHashUsedElt(hash_algo)) |
3314
5887fb414758
component file sharing: add/parse affiliation when possible
Goffi <goffi@goffi.org>
parents:
3303
diff
changeset
|
166 self.host.trigger.point( |
5887fb414758
component file sharing: add/parse affiliation when possible
Goffi <goffi@goffi.org>
parents:
3303
diff
changeset
|
167 "XEP-0234_buildFileElement", client, file_elt, extra_args=kwargs) |
2512
025afb04c10b
plugin XEP-0234: some cleaning + added triggers to allow plugins to change parsing/generation of <file> element
Goffi <goffi@goffi.org>
parents:
2502
diff
changeset
|
168 if kwargs: |
025afb04c10b
plugin XEP-0234: some cleaning + added triggers to allow plugins to change parsing/generation of <file> element
Goffi <goffi@goffi.org>
parents:
2502
diff
changeset
|
169 for kw in kwargs: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
170 log.debug("ignored keyword: {}".format(kw)) |
2502
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
171 return file_elt |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
172 |
3314
5887fb414758
component file sharing: add/parse affiliation when possible
Goffi <goffi@goffi.org>
parents:
3303
diff
changeset
|
173 def buildFileElementFromDict(self, client, file_data, **kwargs): |
2502
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
174 """like buildFileElement but get values from a file_data dict |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
175 |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
176 @param file_data(dict): metadata to use |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
177 @param **kwargs: data to override |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
178 """ |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
179 if kwargs: |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
180 file_data = file_data.copy() |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
181 file_data.update(kwargs) |
3314
5887fb414758
component file sharing: add/parse affiliation when possible
Goffi <goffi@goffi.org>
parents:
3303
diff
changeset
|
182 return self.buildFileElement(client, **file_data) |
2502
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
183 |
3314
5887fb414758
component file sharing: add/parse affiliation when possible
Goffi <goffi@goffi.org>
parents:
3303
diff
changeset
|
184 def parseFileElement( |
5887fb414758
component file sharing: add/parse affiliation when possible
Goffi <goffi@goffi.org>
parents:
3303
diff
changeset
|
185 self, client, file_elt, file_data=None, given=False, parent_elt=None, |
5887fb414758
component file sharing: add/parse affiliation when possible
Goffi <goffi@goffi.org>
parents:
3303
diff
changeset
|
186 keep_empty_range=False): |
2502
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
187 """Parse a <file> element and file dictionary accordingly |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
188 |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
189 @param file_data(dict, None): dict where the data will be set |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
190 following keys will be set (and overwritten if they already exist): |
2512
025afb04c10b
plugin XEP-0234: some cleaning + added triggers to allow plugins to change parsing/generation of <file> element
Goffi <goffi@goffi.org>
parents:
2502
diff
changeset
|
191 name, file_hash, hash_algo, size, mime_type, desc, path, namespace, range |
2502
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
192 if None, a new dict is created |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
193 @param given(bool): if True, prefix hash key with "given_" |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
194 @param parent_elt(domish.Element, None): parent of the file element |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
195 if set, file_elt must not be set |
3040 | 196 @param keep_empty_range(bool): if True, keep empty range (i.e. range when offset |
197 and length are None). | |
198 Empty range is useful to know if a peer_jid can handle range | |
2502
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
199 @return (dict): file_data |
3040 | 200 @trigger XEP-0234_parseFileElement(file_elt, file_data): can be used to parse new |
201 elements | |
2502
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
202 @raise exceptions.NotFound: there is not <file> element in parent_elt |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
203 @raise exceptions.DataError: if file_elt uri is not NS_JINGLE_FT |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
204 """ |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
205 if parent_elt is not None: |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
206 if file_elt is not None: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
207 raise exceptions.InternalError( |
3028 | 208 "file_elt must be None if parent_elt is set" |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
209 ) |
2502
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
210 try: |
3028 | 211 file_elt = next(parent_elt.elements(NS_JINGLE_FT, "file")) |
2502
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
212 except StopIteration: |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
213 raise exceptions.NotFound() |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
214 else: |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
215 if not file_elt or file_elt.uri != NS_JINGLE_FT: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
216 raise exceptions.DataError( |
3028 | 217 "invalid <file> element: {stanza}".format(stanza=file_elt.toXml()) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
218 ) |
2502
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
219 |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
220 if file_data is None: |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
221 file_data = {} |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
222 |
3028 | 223 for name in ("name", "desc", "path", "namespace"): |
2502
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
224 try: |
3028 | 225 file_data[name] = str(next(file_elt.elements(NS_JINGLE_FT, name))) |
2502
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
226 except StopIteration: |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
227 pass |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
228 |
3028 | 229 name = file_data.get("name") |
230 if name == "..": | |
2502
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
231 # we don't want to go to parent dir when joining to a path |
3028 | 232 name = "--" |
233 file_data["name"] = name | |
3040 | 234 elif name is not None and ("/" in name or "\\" in name): |
3028 | 235 file_data["name"] = regex.pathEscape(name) |
2502
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
236 |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
237 try: |
3028 | 238 file_data["mime_type"] = str( |
239 next(file_elt.elements(NS_JINGLE_FT, "media-type")) | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
240 ) |
2512
025afb04c10b
plugin XEP-0234: some cleaning + added triggers to allow plugins to change parsing/generation of <file> element
Goffi <goffi@goffi.org>
parents:
2502
diff
changeset
|
241 except StopIteration: |
025afb04c10b
plugin XEP-0234: some cleaning + added triggers to allow plugins to change parsing/generation of <file> element
Goffi <goffi@goffi.org>
parents:
2502
diff
changeset
|
242 pass |
025afb04c10b
plugin XEP-0234: some cleaning + added triggers to allow plugins to change parsing/generation of <file> element
Goffi <goffi@goffi.org>
parents:
2502
diff
changeset
|
243 |
025afb04c10b
plugin XEP-0234: some cleaning + added triggers to allow plugins to change parsing/generation of <file> element
Goffi <goffi@goffi.org>
parents:
2502
diff
changeset
|
244 try: |
3028 | 245 file_data["size"] = int( |
246 str(next(file_elt.elements(NS_JINGLE_FT, "size"))) | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
247 ) |
2512
025afb04c10b
plugin XEP-0234: some cleaning + added triggers to allow plugins to change parsing/generation of <file> element
Goffi <goffi@goffi.org>
parents:
2502
diff
changeset
|
248 except StopIteration: |
025afb04c10b
plugin XEP-0234: some cleaning + added triggers to allow plugins to change parsing/generation of <file> element
Goffi <goffi@goffi.org>
parents:
2502
diff
changeset
|
249 pass |
025afb04c10b
plugin XEP-0234: some cleaning + added triggers to allow plugins to change parsing/generation of <file> element
Goffi <goffi@goffi.org>
parents:
2502
diff
changeset
|
250 |
025afb04c10b
plugin XEP-0234: some cleaning + added triggers to allow plugins to change parsing/generation of <file> element
Goffi <goffi@goffi.org>
parents:
2502
diff
changeset
|
251 try: |
3028 | 252 file_data["modified"] = date_utils.date_parse( |
253 next(file_elt.elements(NS_JINGLE_FT, "date")) | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
254 ) |
2502
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
255 except StopIteration: |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
256 pass |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
257 |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
258 try: |
3028 | 259 range_elt = next(file_elt.elements(NS_JINGLE_FT, "range")) |
2502
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
260 except StopIteration: |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
261 pass |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
262 else: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
263 offset = range_elt.getAttribute("offset") |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
264 length = range_elt.getAttribute("length") |
2512
025afb04c10b
plugin XEP-0234: some cleaning + added triggers to allow plugins to change parsing/generation of <file> element
Goffi <goffi@goffi.org>
parents:
2502
diff
changeset
|
265 if offset or length or keep_empty_range: |
3028 | 266 file_data["transfer_range"] = Range(offset=offset, length=length) |
2502
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
267 |
3028 | 268 prefix = "given_" if given else "" |
269 hash_algo_key, hash_key = "hash_algo", prefix + "file_hash" | |
2502
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
270 try: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
271 file_data[hash_algo_key], file_data[hash_key] = self._hash.parseHashElt( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
272 file_elt |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
273 ) |
2502
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
274 except exceptions.NotFound: |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
275 pass |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
276 |
3314
5887fb414758
component file sharing: add/parse affiliation when possible
Goffi <goffi@goffi.org>
parents:
3303
diff
changeset
|
277 self.host.trigger.point("XEP-0234_parseFileElement", client, file_elt, file_data) |
2512
025afb04c10b
plugin XEP-0234: some cleaning + added triggers to allow plugins to change parsing/generation of <file> element
Goffi <goffi@goffi.org>
parents:
2502
diff
changeset
|
278 |
2502
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
279 return file_data |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
280 |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
281 # bridge methods |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
282 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
283 def _fileJingleSend( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
284 self, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
285 peer_jid, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
286 filepath, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
287 name="", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
288 file_desc="", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
289 extra=None, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
290 profile=C.PROF_KEY_NONE, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
291 ): |
2489
e2a7bb875957
plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
292 client = self.host.getClient(profile) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
293 return self.fileJingleSend( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
294 client, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
295 jid.JID(peer_jid), |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
296 filepath, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
297 name or None, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
298 file_desc or None, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
299 extra or None, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
300 ) |
1528
1c71d7335d02
plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
301 |
2502
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
302 @defer.inlineCallbacks |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
303 def fileJingleSend( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
304 self, client, peer_jid, filepath, name, file_desc=None, extra=None |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
305 ): |
1585
846a39900fa6
plugins XEP-0096, XEP-0260, file: sendFile method is managed by file plugin, which choose the best available method + progress_id fix
Goffi <goffi@goffi.org>
parents:
1575
diff
changeset
|
306 """Send a file using jingle file transfer |
846a39900fa6
plugins XEP-0096, XEP-0260, file: sendFile method is managed by file plugin, which choose the best available method + progress_id fix
Goffi <goffi@goffi.org>
parents:
1575
diff
changeset
|
307 |
846a39900fa6
plugins XEP-0096, XEP-0260, file: sendFile method is managed by file plugin, which choose the best available method + progress_id fix
Goffi <goffi@goffi.org>
parents:
1575
diff
changeset
|
308 @param peer_jid(jid.JID): destinee jid |
846a39900fa6
plugins XEP-0096, XEP-0260, file: sendFile method is managed by file plugin, which choose the best available method + progress_id fix
Goffi <goffi@goffi.org>
parents:
1575
diff
changeset
|
309 @param filepath(str): absolute path of the file |
846a39900fa6
plugins XEP-0096, XEP-0260, file: sendFile method is managed by file plugin, which choose the best available method + progress_id fix
Goffi <goffi@goffi.org>
parents:
1575
diff
changeset
|
310 @param name(unicode, None): name of the file |
846a39900fa6
plugins XEP-0096, XEP-0260, file: sendFile method is managed by file plugin, which choose the best available method + progress_id fix
Goffi <goffi@goffi.org>
parents:
1575
diff
changeset
|
311 @param file_desc(unicode, None): description of the file |
846a39900fa6
plugins XEP-0096, XEP-0260, file: sendFile method is managed by file plugin, which choose the best available method + progress_id fix
Goffi <goffi@goffi.org>
parents:
1575
diff
changeset
|
312 @return (D(unicode)): progress id |
846a39900fa6
plugins XEP-0096, XEP-0260, file: sendFile method is managed by file plugin, which choose the best available method + progress_id fix
Goffi <goffi@goffi.org>
parents:
1575
diff
changeset
|
313 """ |
846a39900fa6
plugins XEP-0096, XEP-0260, file: sendFile method is managed by file plugin, which choose the best available method + progress_id fix
Goffi <goffi@goffi.org>
parents:
1575
diff
changeset
|
314 progress_id_d = defer.Deferred() |
2502
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
315 if extra is None: |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
316 extra = {} |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
317 if file_desc is not None: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
318 extra["file_desc"] = file_desc |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
319 yield self._j.initiate( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
320 client, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
321 peer_jid, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
322 [ |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
323 { |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
324 "app_ns": NS_JINGLE_FT, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
325 "senders": self._j.ROLE_INITIATOR, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
326 "app_kwargs": { |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
327 "filepath": filepath, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
328 "name": name, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
329 "extra": extra, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
330 "progress_id_d": progress_id_d, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
331 }, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
332 } |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
333 ], |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
334 ) |
2502
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
335 progress_id = yield progress_id_d |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
336 defer.returnValue(progress_id) |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
337 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
338 def _fileJingleRequest( |
3040 | 339 self, peer_jid, filepath, name="", file_hash="", hash_algo="", extra=None, |
340 profile=C.PROF_KEY_NONE): | |
2502
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
341 client = self.host.getClient(profile) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
342 return self.fileJingleRequest( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
343 client, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
344 jid.JID(peer_jid), |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
345 filepath, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
346 name or None, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
347 file_hash or None, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
348 hash_algo or None, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
349 extra or None, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
350 ) |
2502
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
351 |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
352 @defer.inlineCallbacks |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
353 def fileJingleRequest( |
3040 | 354 self, client, peer_jid, filepath, name=None, file_hash=None, hash_algo=None, |
355 extra=None): | |
2502
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
356 """Request a file using jingle file transfer |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
357 |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
358 @param peer_jid(jid.JID): destinee jid |
2589
282d1314d574
plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
359 @param filepath(str): absolute path where the file will be downloaded |
2502
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
360 @param name(unicode, None): name of the file |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
361 @param file_hash(unicode, None): hash of the file |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
362 @return (D(unicode)): progress id |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
363 """ |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
364 progress_id_d = defer.Deferred() |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
365 if extra is None: |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
366 extra = {} |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
367 if file_hash is not None: |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
368 if hash_algo is None: |
3028 | 369 raise ValueError(_("hash_algo must be set if file_hash is set")) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
370 extra["file_hash"] = file_hash |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
371 extra["hash_algo"] = hash_algo |
2502
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
372 else: |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
373 if hash_algo is not None: |
3028 | 374 raise ValueError(_("file_hash must be set if hash_algo is set")) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
375 yield self._j.initiate( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
376 client, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
377 peer_jid, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
378 [ |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
379 { |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
380 "app_ns": NS_JINGLE_FT, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
381 "senders": self._j.ROLE_RESPONDER, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
382 "app_kwargs": { |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
383 "filepath": filepath, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
384 "name": name, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
385 "extra": extra, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
386 "progress_id_d": progress_id_d, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
387 }, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
388 } |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
389 ], |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
390 ) |
2502
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
391 progress_id = yield progress_id_d |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
392 defer.returnValue(progress_id) |
1528
1c71d7335d02
plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
393 |
1c71d7335d02
plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
394 # jingle callbacks |
1c71d7335d02
plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
395 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
396 def jingleSessionInit( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
397 self, client, session, content_name, filepath, name, extra, progress_id_d |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
398 ): |
2502
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
399 if extra is None: |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
400 extra = {} |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
401 else: |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
402 if not EXTRA_ALLOWED.issuperset(extra): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
403 raise ValueError( |
3028 | 404 _("only the following keys are allowed in extra: {keys}").format( |
405 keys=", ".join(EXTRA_ALLOWED) | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
406 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
407 ) |
2502
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
408 progress_id_d.callback(self.getProgressId(session, content_name)) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
409 content_data = session["contents"][content_name] |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
410 application_data = content_data["application_data"] |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
411 assert "file_path" not in application_data |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
412 application_data["file_path"] = filepath |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
413 file_data = application_data["file_data"] = {} |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
414 desc_elt = domish.Element((NS_JINGLE_FT, "description")) |
1528
1c71d7335d02
plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
415 file_elt = desc_elt.addElement("file") |
2502
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
416 |
3028 | 417 if content_data["senders"] == self._j.ROLE_INITIATOR: |
2502
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
418 # we send a file |
2512
025afb04c10b
plugin XEP-0234: some cleaning + added triggers to allow plugins to change parsing/generation of <file> element
Goffi <goffi@goffi.org>
parents:
2502
diff
changeset
|
419 if name is None: |
025afb04c10b
plugin XEP-0234: some cleaning + added triggers to allow plugins to change parsing/generation of <file> element
Goffi <goffi@goffi.org>
parents:
2502
diff
changeset
|
420 name = os.path.basename(filepath) |
3028 | 421 file_data["date"] = utils.xmpp_date() |
422 file_data["desc"] = extra.pop("file_desc", "") | |
423 file_data["name"] = name | |
2512
025afb04c10b
plugin XEP-0234: some cleaning + added triggers to allow plugins to change parsing/generation of <file> element
Goffi <goffi@goffi.org>
parents:
2502
diff
changeset
|
424 mime_type = mimetypes.guess_type(name, strict=False)[0] |
025afb04c10b
plugin XEP-0234: some cleaning + added triggers to allow plugins to change parsing/generation of <file> element
Goffi <goffi@goffi.org>
parents:
2502
diff
changeset
|
425 if mime_type is not None: |
3028 | 426 file_data["mime_type"] = mime_type |
427 file_data["size"] = os.path.getsize(filepath) | |
428 if "namespace" in extra: | |
429 file_data["namespace"] = extra["namespace"] | |
430 if "path" in extra: | |
431 file_data["path"] = extra["path"] | |
3314
5887fb414758
component file sharing: add/parse affiliation when possible
Goffi <goffi@goffi.org>
parents:
3303
diff
changeset
|
432 self.buildFileElementFromDict( |
5887fb414758
component file sharing: add/parse affiliation when possible
Goffi <goffi@goffi.org>
parents:
3303
diff
changeset
|
433 client, file_data, file_elt=file_elt, file_hash="") |
2502
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
434 else: |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
435 # we request a file |
3028 | 436 file_hash = extra.pop("file_hash", "") |
2502
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
437 if not name and not file_hash: |
3028 | 438 raise ValueError(_("you need to provide at least name or file hash")) |
2502
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
439 if name: |
3028 | 440 file_data["name"] = name |
2502
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
441 if file_hash: |
3028 | 442 file_data["file_hash"] = file_hash |
443 file_data["hash_algo"] = extra["hash_algo"] | |
2502
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
444 else: |
3028 | 445 file_data["hash_algo"] = self._hash.getDefaultAlgo() |
446 if "namespace" in extra: | |
447 file_data["namespace"] = extra["namespace"] | |
448 if "path" in extra: | |
449 file_data["path"] = extra["path"] | |
3314
5887fb414758
component file sharing: add/parse affiliation when possible
Goffi <goffi@goffi.org>
parents:
3303
diff
changeset
|
450 self.buildFileElementFromDict(client, file_data, file_elt=file_elt) |
2502
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
451 |
1528
1c71d7335d02
plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
452 return desc_elt |
1c71d7335d02
plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
453 |
2489
e2a7bb875957
plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
454 def jingleRequestConfirmation(self, client, action, session, content_name, desc_elt): |
1528
1c71d7335d02
plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
455 """This method request confirmation for a jingle session""" |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
456 content_data = session["contents"][content_name] |
3028 | 457 senders = content_data["senders"] |
2502
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
458 if senders not in (self._j.ROLE_INITIATOR, self._j.ROLE_RESPONDER): |
3028 | 459 log.warning("Bad sender, assuming initiator") |
460 senders = content_data["senders"] = self._j.ROLE_INITIATOR | |
1528
1c71d7335d02
plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
461 # first we grab file informations |
1c71d7335d02
plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
462 try: |
3028 | 463 file_elt = next(desc_elt.elements(NS_JINGLE_FT, "file")) |
1528
1c71d7335d02
plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
464 except StopIteration: |
1c71d7335d02
plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
465 raise failure.Failure(exceptions.DataError) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
466 file_data = {"progress_id": self.getProgressId(session, content_name)} |
2502
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
467 |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
468 if senders == self._j.ROLE_RESPONDER: |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
469 # we send the file |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
470 return self._fileSendingRequestConf( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
471 client, session, content_data, content_name, file_data, file_elt |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
472 ) |
2502
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
473 else: |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
474 # we receive the file |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
475 return self._fileReceivingRequestConf( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
476 client, session, content_data, content_name, file_data, file_elt |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
477 ) |
2502
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
478 |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
479 @defer.inlineCallbacks |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
480 def _fileSendingRequestConf( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
481 self, client, session, content_data, content_name, file_data, file_elt |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
482 ): |
2502
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
483 """parse file_elt, and handle file retrieving/permission checking""" |
3314
5887fb414758
component file sharing: add/parse affiliation when possible
Goffi <goffi@goffi.org>
parents:
3303
diff
changeset
|
484 self.parseFileElement(client, file_elt, file_data) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
485 content_data["application_data"]["file_data"] = file_data |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
486 finished_d = content_data["finished_d"] = defer.Deferred() |
2502
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
487 |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
488 # confirmed_d is a deferred returning confimed value (only used if cont is False) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
489 cont, confirmed_d = self.host.trigger.returnPoint( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
490 "XEP-0234_fileSendingRequest", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
491 client, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
492 session, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
493 content_data, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
494 content_name, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
495 file_data, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
496 file_elt, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
497 ) |
2502
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
498 if not cont: |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
499 confirmed = yield confirmed_d |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
500 if confirmed: |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
501 args = [client, session, content_name, content_data] |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
502 finished_d.addCallbacks( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
503 self._finishedCb, self._finishedEb, args, None, args |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
504 ) |
2502
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
505 defer.returnValue(confirmed) |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
506 |
3028 | 507 log.warning(_("File continue is not implemented yet")) |
2502
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
508 defer.returnValue(False) |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
509 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
510 def _fileReceivingRequestConf( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
511 self, client, session, content_data, content_name, file_data, file_elt |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
512 ): |
2502
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
513 """parse file_elt, and handle user permission/file opening""" |
3314
5887fb414758
component file sharing: add/parse affiliation when possible
Goffi <goffi@goffi.org>
parents:
3303
diff
changeset
|
514 self.parseFileElement(client, file_elt, file_data, given=True) |
2502
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
515 try: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
516 hash_algo, file_data["given_file_hash"] = self._hash.parseHashElt(file_elt) |
2502
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
517 except exceptions.NotFound: |
1528
1c71d7335d02
plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
518 try: |
2502
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
519 hash_algo = self._hash.parseHashUsedElt(file_elt) |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
520 except exceptions.NotFound: |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
521 raise failure.Failure(exceptions.DataError) |
1620
4dd07d026214
plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents:
1618
diff
changeset
|
522 |
4dd07d026214
plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents:
1618
diff
changeset
|
523 if hash_algo is not None: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
524 file_data["hash_algo"] = hash_algo |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
525 file_data["hash_hasher"] = hasher = self._hash.getHasher(hash_algo) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
526 file_data["data_cb"] = lambda data: hasher.update(data) |
1620
4dd07d026214
plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents:
1618
diff
changeset
|
527 |
4dd07d026214
plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents:
1618
diff
changeset
|
528 try: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
529 file_data["size"] = int(file_data["size"]) |
1528
1c71d7335d02
plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
530 except ValueError: |
1c71d7335d02
plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
531 raise failure.Failure(exceptions.DataError) |
1c71d7335d02
plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
532 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
533 name = file_data["name"] |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
534 if "/" in name or "\\" in name: |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
535 log.warning( |
3028 | 536 "File name contain path characters, we replace them: {}".format(name) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
537 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
538 file_data["name"] = name.replace("/", "_").replace("\\", "_") |
1528
1c71d7335d02
plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
539 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
540 content_data["application_data"]["file_data"] = file_data |
1528
1c71d7335d02
plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
541 |
1c71d7335d02
plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
542 # now we actualy request permission to user |
1574
babd97d80049
plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents:
1571
diff
changeset
|
543 def gotConfirmation(confirmed): |
babd97d80049
plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents:
1571
diff
changeset
|
544 if confirmed: |
2489
e2a7bb875957
plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
545 args = [client, session, content_name, content_data] |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
546 finished_d.addCallbacks( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
547 self._finishedCb, self._finishedEb, args, None, args |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
548 ) |
1574
babd97d80049
plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents:
1571
diff
changeset
|
549 return confirmed |
babd97d80049
plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents:
1571
diff
changeset
|
550 |
2502
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
551 # deferred to track end of transfer |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
552 finished_d = content_data["finished_d"] = defer.Deferred() |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
553 d = self._f.getDestDir( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
554 client, session["peer_jid"], content_data, file_data, stream_object=True |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
555 ) |
1574
babd97d80049
plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents:
1571
diff
changeset
|
556 d.addCallback(gotConfirmation) |
babd97d80049
plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents:
1571
diff
changeset
|
557 return d |
1528
1c71d7335d02
plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
558 |
3040 | 559 @defer.inlineCallbacks |
2489
e2a7bb875957
plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
560 def jingleHandler(self, client, action, session, content_name, desc_elt): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
561 content_data = session["contents"][content_name] |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
562 application_data = content_data["application_data"] |
1556
cbfbe028d099
plugin XEP-0166, XEP-0234, XEP-0261:
Goffi <goffi@goffi.org>
parents:
1529
diff
changeset
|
563 if action in (self._j.A_ACCEPTED_ACK,): |
1528
1c71d7335d02
plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
564 pass |
1556
cbfbe028d099
plugin XEP-0166, XEP-0234, XEP-0261:
Goffi <goffi@goffi.org>
parents:
1529
diff
changeset
|
565 elif action == self._j.A_SESSION_INITIATE: |
3028 | 566 file_elt = next(desc_elt.elements(NS_JINGLE_FT, "file")) |
1556
cbfbe028d099
plugin XEP-0166, XEP-0234, XEP-0261:
Goffi <goffi@goffi.org>
parents:
1529
diff
changeset
|
567 try: |
3028 | 568 next(file_elt.elements(NS_JINGLE_FT, "range")) |
1556
cbfbe028d099
plugin XEP-0166, XEP-0234, XEP-0261:
Goffi <goffi@goffi.org>
parents:
1529
diff
changeset
|
569 except StopIteration: |
cbfbe028d099
plugin XEP-0166, XEP-0234, XEP-0261:
Goffi <goffi@goffi.org>
parents:
1529
diff
changeset
|
570 # initiator doesn't manage <range>, but we do so we advertise it |
3040 | 571 # FIXME: to be checked |
1556
cbfbe028d099
plugin XEP-0166, XEP-0234, XEP-0261:
Goffi <goffi@goffi.org>
parents:
1529
diff
changeset
|
572 log.debug("adding <range> element") |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
573 file_elt.addElement("range") |
1528
1c71d7335d02
plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
574 elif action == self._j.A_SESSION_ACCEPT: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
575 assert not "stream_object" in content_data |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
576 file_data = application_data["file_data"] |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
577 file_path = application_data["file_path"] |
3028 | 578 senders = content_data["senders"] |
579 if senders != session["role"]: | |
2502
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
580 # we are receiving the file |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
581 try: |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
582 # did the responder specified the size of the file? |
3028 | 583 file_elt = next(desc_elt.elements(NS_JINGLE_FT, "file")) |
584 size_elt = next(file_elt.elements(NS_JINGLE_FT, "size")) | |
585 size = int(str(size_elt)) | |
2502
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
586 except (StopIteration, ValueError): |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
587 size = None |
3040 | 588 # XXX: hash security is not critical here, so we just take the higher |
589 # mandatory one | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
590 hasher = file_data["hash_hasher"] = self._hash.getHasher() |
3040 | 591 progress_id = self.getProgressId(session, content_name) |
592 try: | |
593 content_data["stream_object"] = stream.FileStreamObject( | |
594 self.host, | |
595 client, | |
596 file_path, | |
597 mode="wb", | |
598 uid=progress_id, | |
599 size=size, | |
600 data_cb=lambda data: hasher.update(data), | |
601 ) | |
602 except Exception as e: | |
603 self.host.bridge.progressError( | |
604 progress_id, C.PROGRESS_ERROR_FAILED, client.profile | |
605 ) | |
606 yield self._j.terminate( | |
607 client, self._j.REASON_FAILED_APPLICATION, session) | |
608 raise e | |
2502
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
609 else: |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
610 # we are sending the file |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
611 size = file_data["size"] |
3040 | 612 # XXX: hash security is not critical here, so we just take the higher |
613 # mandatory one | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
614 hasher = file_data["hash_hasher"] = self._hash.getHasher() |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
615 content_data["stream_object"] = stream.FileStreamObject( |
2502
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
616 self.host, |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
617 client, |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
618 file_path, |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
619 uid=self.getProgressId(session, content_name), |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
620 size=size, |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
621 data_cb=lambda data: hasher.update(data), |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
622 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
623 finished_d = content_data["finished_d"] = defer.Deferred() |
2489
e2a7bb875957
plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
624 args = [client, session, content_name, content_data] |
1571
c668081eba1c
plugins XEP-0234, XEP-0260, XEP-0261: jingle session termination is managed by application (XEP-0234) instead of transport
Goffi <goffi@goffi.org>
parents:
1568
diff
changeset
|
625 finished_d.addCallbacks(self._finishedCb, self._finishedEb, args, None, args) |
1528
1c71d7335d02
plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
626 else: |
3028 | 627 log.warning("FIXME: unmanaged action {}".format(action)) |
3040 | 628 defer.returnValue(desc_elt) |
1528
1c71d7335d02
plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
629 |
2489
e2a7bb875957
plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
630 def jingleSessionInfo(self, client, action, session, content_name, jingle_elt): |
1620
4dd07d026214
plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents:
1618
diff
changeset
|
631 """Called on session-info action |
4dd07d026214
plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents:
1618
diff
changeset
|
632 |
4dd07d026214
plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents:
1618
diff
changeset
|
633 manage checksum, and ignore <received/> element |
4dd07d026214
plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents:
1618
diff
changeset
|
634 """ |
4dd07d026214
plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents:
1618
diff
changeset
|
635 # TODO: manage <received/> element |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
636 content_data = session["contents"][content_name] |
1620
4dd07d026214
plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents:
1618
diff
changeset
|
637 elts = [elt for elt in jingle_elt.elements() if elt.uri == NS_JINGLE_FT] |
4dd07d026214
plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents:
1618
diff
changeset
|
638 if not elts: |
4dd07d026214
plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents:
1618
diff
changeset
|
639 return |
4dd07d026214
plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents:
1618
diff
changeset
|
640 for elt in elts: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
641 if elt.name == "received": |
1620
4dd07d026214
plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents:
1618
diff
changeset
|
642 pass |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
643 elif elt.name == "checksum": |
1620
4dd07d026214
plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents:
1618
diff
changeset
|
644 # we have received the file hash, we need to parse it |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
645 if content_data["senders"] == session["role"]: |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
646 log.warning( |
3028 | 647 "unexpected checksum received while we are the file sender" |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
648 ) |
1620
4dd07d026214
plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents:
1618
diff
changeset
|
649 raise exceptions.DataError |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
650 info_content_name = elt["name"] |
1620
4dd07d026214
plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents:
1618
diff
changeset
|
651 if info_content_name != content_name: |
4dd07d026214
plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents:
1618
diff
changeset
|
652 # it was for an other content... |
4dd07d026214
plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents:
1618
diff
changeset
|
653 return |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
654 file_data = content_data["application_data"]["file_data"] |
1620
4dd07d026214
plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents:
1618
diff
changeset
|
655 try: |
3028 | 656 file_elt = next(elt.elements((NS_JINGLE_FT, "file"))) |
1620
4dd07d026214
plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents:
1618
diff
changeset
|
657 except StopIteration: |
4dd07d026214
plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents:
1618
diff
changeset
|
658 raise exceptions.DataError |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
659 algo, file_data["given_file_hash"] = self._hash.parseHashElt(file_elt) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
660 if algo != file_data.get("hash_algo"): |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
661 log.warning( |
3028 | 662 "Hash algorithm used in given hash ({peer_algo}) doesn't correspond to the one we have used ({our_algo}) [{profile}]".format( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
663 peer_algo=algo, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
664 our_algo=file_data.get("hash_algo"), |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
665 profile=client.profile, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
666 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
667 ) |
1620
4dd07d026214
plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents:
1618
diff
changeset
|
668 else: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
669 self._receiverTryTerminate( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
670 client, session, content_name, content_data |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
671 ) |
1620
4dd07d026214
plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents:
1618
diff
changeset
|
672 else: |
4dd07d026214
plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents:
1618
diff
changeset
|
673 raise NotImplementedError |
4dd07d026214
plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents:
1618
diff
changeset
|
674 |
2502
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
675 def jingleTerminate(self, client, action, session, content_name, jingle_elt): |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
676 if jingle_elt.decline: |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
677 # progress is the only way to tell to frontends that session has been declined |
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
678 progress_id = self.getProgressId(session, content_name) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
679 self.host.bridge.progressError( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
680 progress_id, C.PROGRESS_ERROR_DECLINED, client.profile |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
681 ) |
3040 | 682 elif not jingle_elt.success: |
683 progress_id = self.getProgressId(session, content_name) | |
684 first_child = jingle_elt.firstChildElement() | |
685 if first_child is not None: | |
686 reason = first_child.name | |
687 else: | |
688 reason = C.PROGRESS_ERROR_FAILED | |
689 self.host.bridge.progressError( | |
690 progress_id, reason, client.profile | |
691 ) | |
2502
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
692 |
2489
e2a7bb875957
plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
693 def _sendCheckSum(self, client, session, content_name, content_data): |
1620
4dd07d026214
plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents:
1618
diff
changeset
|
694 """Send the session-info with the hash checksum""" |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
695 file_data = content_data["application_data"]["file_data"] |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
696 hasher = file_data["hash_hasher"] |
1620
4dd07d026214
plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents:
1618
diff
changeset
|
697 hash_ = hasher.hexdigest() |
3028 | 698 log.debug("Calculated hash: {}".format(hash_)) |
2489
e2a7bb875957
plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
699 iq_elt, jingle_elt = self._j.buildSessionInfo(client, session) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
700 checksum_elt = jingle_elt.addElement((NS_JINGLE_FT, "checksum")) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
701 checksum_elt["creator"] = content_data["creator"] |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
702 checksum_elt["name"] = content_name |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
703 file_elt = checksum_elt.addElement("file") |
1620
4dd07d026214
plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents:
1618
diff
changeset
|
704 file_elt.addChild(self._hash.buildHashElt(hash_)) |
4dd07d026214
plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents:
1618
diff
changeset
|
705 iq_elt.send() |
4dd07d026214
plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents:
1618
diff
changeset
|
706 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
707 def _receiverTryTerminate( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
708 self, client, session, content_name, content_data, last_try=False |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
709 ): |
1620
4dd07d026214
plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents:
1618
diff
changeset
|
710 """Try to terminate the session |
4dd07d026214
plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents:
1618
diff
changeset
|
711 |
4dd07d026214
plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents:
1618
diff
changeset
|
712 This method must only be used by the receiver. |
4dd07d026214
plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents:
1618
diff
changeset
|
713 It check if transfer is finished, and hash available, |
4dd07d026214
plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents:
1618
diff
changeset
|
714 if everything is OK, it check hash and terminate the session |
4dd07d026214
plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents:
1618
diff
changeset
|
715 @param last_try(bool): if True this mean than session must be terminated even given hash is not available |
4dd07d026214
plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents:
1618
diff
changeset
|
716 @return (bool): True if session was terminated |
4dd07d026214
plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents:
1618
diff
changeset
|
717 """ |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
718 if not content_data.get("transfer_finished", False): |
1620
4dd07d026214
plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents:
1618
diff
changeset
|
719 return False |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
720 file_data = content_data["application_data"]["file_data"] |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
721 given_hash = file_data.get("given_file_hash") |
2502
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
722 if given_hash is None: |
1620
4dd07d026214
plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents:
1618
diff
changeset
|
723 if last_try: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
724 log.warning( |
3028 | 725 "sender didn't sent hash checksum, we can't check the file [{profile}]".format( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
726 profile=client.profile |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
727 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
728 ) |
2489
e2a7bb875957
plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
729 self._j.delayedContentTerminate(client, session, content_name) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
730 content_data["stream_object"].close() |
1620
4dd07d026214
plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents:
1618
diff
changeset
|
731 return True |
4dd07d026214
plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents:
1618
diff
changeset
|
732 return False |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
733 hasher = file_data["hash_hasher"] |
3040 | 734 hash_ = hasher.hexdigest() |
1620
4dd07d026214
plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents:
1618
diff
changeset
|
735 |
2502
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
736 if hash_ == given_hash: |
3040 | 737 log.info(f"Hash checked, file was successfully transfered: {hash_}") |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
738 progress_metadata = { |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
739 "hash": hash_, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
740 "hash_algo": file_data["hash_algo"], |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
741 "hash_verified": C.BOOL_TRUE, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
742 } |
1626
63cef4dbf2a4
core, plugins file, XEP-0234, bridge: progression api enhancement:
Goffi <goffi@goffi.org>
parents:
1620
diff
changeset
|
743 error = None |
1620
4dd07d026214
plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents:
1618
diff
changeset
|
744 else: |
3028 | 745 log.warning("Hash mismatch, the file was not transfered correctly") |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
746 progress_metadata = None |
3028 | 747 error = "Hash mismatch: given={algo}:{given}, calculated={algo}:{our}".format( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
748 algo=file_data["hash_algo"], given=given_hash, our=hash_ |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
749 ) |
1620
4dd07d026214
plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents:
1618
diff
changeset
|
750 |
2489
e2a7bb875957
plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
751 self._j.delayedContentTerminate(client, session, content_name) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
752 content_data["stream_object"].close(progress_metadata, error) |
1620
4dd07d026214
plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents:
1618
diff
changeset
|
753 # we may have the last_try timer still active, so we try to cancel it |
4dd07d026214
plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents:
1618
diff
changeset
|
754 try: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
755 content_data["last_try_timer"].cancel() |
1620
4dd07d026214
plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents:
1618
diff
changeset
|
756 except (KeyError, internet_error.AlreadyCalled): |
4dd07d026214
plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents:
1618
diff
changeset
|
757 pass |
4dd07d026214
plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents:
1618
diff
changeset
|
758 return True |
4dd07d026214
plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents:
1618
diff
changeset
|
759 |
2765
378188abe941
misc: replaced all "dummy" by the more conventional and readable "__" ("_" being used for gettext)
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
760 def _finishedCb(self, __, client, session, content_name, content_data): |
3028 | 761 log.info("File transfer terminated") |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
762 if content_data["senders"] != session["role"]: |
1620
4dd07d026214
plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents:
1618
diff
changeset
|
763 # we terminate the session only if we are the receiver, |
1571
c668081eba1c
plugins XEP-0234, XEP-0260, XEP-0261: jingle session termination is managed by application (XEP-0234) instead of transport
Goffi <goffi@goffi.org>
parents:
1568
diff
changeset
|
764 # as recommanded in XEP-0234 §2 (after example 6) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
765 content_data["transfer_finished"] = True |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
766 if not self._receiverTryTerminate( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
767 client, session, content_name, content_data |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
768 ): |
1620
4dd07d026214
plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents:
1618
diff
changeset
|
769 # we have not received the hash yet, we wait 5 more seconds |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
770 content_data["last_try_timer"] = reactor.callLater( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
771 5, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
772 self._receiverTryTerminate, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
773 client, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
774 session, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
775 content_name, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
776 content_data, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
777 last_try=True, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
778 ) |
1620
4dd07d026214
plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents:
1618
diff
changeset
|
779 else: |
4dd07d026214
plugin XEP-0234: hash checksum proper handling
Goffi <goffi@goffi.org>
parents:
1618
diff
changeset
|
780 # we are the sender, we send the checksum |
2489
e2a7bb875957
plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
781 self._sendCheckSum(client, session, content_name, content_data) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
782 content_data["stream_object"].close() |
1571
c668081eba1c
plugins XEP-0234, XEP-0260, XEP-0261: jingle session termination is managed by application (XEP-0234) instead of transport
Goffi <goffi@goffi.org>
parents:
1568
diff
changeset
|
783 |
2489
e2a7bb875957
plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
784 def _finishedEb(self, failure, client, session, content_name, content_data): |
3028 | 785 log.warning("Error while streaming file: {}".format(failure)) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
786 content_data["stream_object"].close() |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
787 self._j.contentTerminate( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
788 client, session, content_name, reason=self._j.REASON_FAILED_TRANSPORT |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
789 ) |
1571
c668081eba1c
plugins XEP-0234, XEP-0260, XEP-0261: jingle session termination is managed by application (XEP-0234) instead of transport
Goffi <goffi@goffi.org>
parents:
1568
diff
changeset
|
790 |
1528
1c71d7335d02
plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
791 |
3028 | 792 @implementer(iwokkel.IDisco) |
1528
1c71d7335d02
plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
793 class XEP_0234_handler(XMPPHandler): |
1c71d7335d02
plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
794 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
795 def getDiscoInfo(self, requestor, target, nodeIdentifier=""): |
1528
1c71d7335d02
plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
796 return [disco.DiscoFeature(NS_JINGLE_FT)] |
1c71d7335d02
plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
797 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
798 def getDiscoItems(self, requestor, target, nodeIdentifier=""): |
1528
1c71d7335d02
plugin XEP-0234: jingle file transfer first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
799 return [] |