annotate libervia/backend/plugins/plugin_misc_file.py @ 4332:71c939e34ca6

XEP-0373 (OX): Adjust to gpgme updates: generate with explicit algorithm and subkeys
author Syndace <me@syndace.dev>
date Sat, 13 Jul 2024 18:28:28 +0200
parents 0d7bb4df2343
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
1 #!/usr/bin/env python3
3137
559a625a236b fixed shebangs
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
2
1525
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
3
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
4 # SAT plugin for file tansfer
3479
be6d91572633 date update
Goffi <goffi@goffi.org>
parents: 3403
diff changeset
5 # Copyright (C) 2009-2021 Jérôme Poisson (goffi@goffi.org)
1525
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
6
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
7 # This program is free software: you can redistribute it and/or modify
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
8 # it under the terms of the GNU Affero General Public License as published by
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
9 # the Free Software Foundation, either version 3 of the License, or
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
10 # (at your option) any later version.
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
11
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
12 # This program is distributed in the hope that it will be useful,
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
15 # GNU Affero General Public License for more details.
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
16
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
17 # You should have received a copy of the GNU Affero General Public License
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
19
4231
e11b13418ba6 plugin XEP-0353, XEP-0234, jingle: WebRTC data channel signaling implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
20 from functools import partial
3403
404d4b29de52 plugin file, XEP-0234: registering is now done by class + use of async:
Goffi <goffi@goffi.org>
parents: 3182
diff changeset
21 import os
404d4b29de52 plugin file, XEP-0234: registering is now done by class + use of async:
Goffi <goffi@goffi.org>
parents: 3182
diff changeset
22 import os.path
4231
e11b13418ba6 plugin XEP-0353, XEP-0234, jingle: WebRTC data channel signaling implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
23 from pathlib import Path
e11b13418ba6 plugin XEP-0353, XEP-0234, jingle: WebRTC data channel signaling implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
24
3403
404d4b29de52 plugin file, XEP-0234: registering is now done by class + use of async:
Goffi <goffi@goffi.org>
parents: 3182
diff changeset
25 from twisted.internet import defer
404d4b29de52 plugin file, XEP-0234: registering is now done by class + use of async:
Goffi <goffi@goffi.org>
parents: 3182
diff changeset
26 from twisted.words.protocols.jabber import jid
4231
e11b13418ba6 plugin XEP-0353, XEP-0234, jingle: WebRTC data channel signaling implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
27
e11b13418ba6 plugin XEP-0353, XEP-0234, jingle: WebRTC data channel signaling implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
28 from libervia.backend.core import exceptions
4071
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4042
diff changeset
29 from libervia.backend.core.constants import Const as C
4231
e11b13418ba6 plugin XEP-0353, XEP-0234, jingle: WebRTC data channel signaling implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
30 from libervia.backend.core.core_types import SatXMPPEntity
e11b13418ba6 plugin XEP-0353, XEP-0234, jingle: WebRTC data channel signaling implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
31 from libervia.backend.core.i18n import D_, _
4071
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4042
diff changeset
32 from libervia.backend.core.log import getLogger
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4042
diff changeset
33 from libervia.backend.tools import xml_tools
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4042
diff changeset
34 from libervia.backend.tools import stream
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4042
diff changeset
35 from libervia.backend.tools import utils
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4042
diff changeset
36 from libervia.backend.tools.common import data_format, utils as common_utils
3403
404d4b29de52 plugin file, XEP-0234: registering is now done by class + use of async:
Goffi <goffi@goffi.org>
parents: 3182
diff changeset
37
404d4b29de52 plugin file, XEP-0234: registering is now done by class + use of async:
Goffi <goffi@goffi.org>
parents: 3182
diff changeset
38
404d4b29de52 plugin file, XEP-0234: registering is now done by class + use of async:
Goffi <goffi@goffi.org>
parents: 3182
diff changeset
39 log = getLogger(__name__)
1525
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
40
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
41
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
42 PLUGIN_INFO = {
2145
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 2144
diff changeset
43 C.PI_NAME: "File Tansfer",
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 2144
diff changeset
44 C.PI_IMPORT_NAME: "FILE",
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 2144
diff changeset
45 C.PI_TYPE: C.PLUG_TYPE_MISC,
2502
7ad5f2c4e34a XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
46 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
47 C.PI_MAIN: "FilePlugin",
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 2144
diff changeset
48 C.PI_HANDLER: "no",
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
49 C.PI_DESCRIPTION: _(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
50 """File Tansfer Management:
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
51 This plugin manage the various ways of sending a file, and choose the best one."""
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
52 ),
1525
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
53 }
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
54
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
55
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
56 SENDING = D_("Please select a file to send to {peer}")
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
57 SENDING_TITLE = D_("File sending")
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
58 CONFIRM = D_(
3182
f2bb57348587 plugin attach, XEP-0363: progress id can now be specified:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
59 '{peer} wants to send the file "{name}" to you:\n{desc}\n\nThe file has a size of '
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4231
diff changeset
60 "{size_human}\n\nDo you accept ?"
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
61 )
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
62 CONFIRM_TITLE = D_("Confirm file transfer")
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
63 CONFIRM_OVERWRITE = D_("File {} already exists, are you sure you want to overwrite ?")
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
64 CONFIRM_OVERWRITE_TITLE = D_("File exists")
1647
31b96ac3eec2 core (param), plugin file: set security_limit for acount changes, backend (dis)connection, and put a higher one for file send
Goffi <goffi@goffi.org>
parents: 1646
diff changeset
65 SECURITY_LIMIT = 30
1574
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
66
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
67 PROGRESS_ID_KEY = "progress_id"
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
68
1574
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
69
3182
f2bb57348587 plugin attach, XEP-0363: progress id can now be specified:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
70 class FilePlugin:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
71 File = stream.SatFile
1525
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
72
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
73 def __init__(self, host):
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
74 log.info(_("plugin File initialization"))
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
75 self.host = host
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3970
diff changeset
76 host.bridge.add_method(
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3970
diff changeset
77 "file_send",
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
78 ".plugin",
3970
4c3361e2bf55 cli (file/send): add `--encrypt` argument to request encryption:
Goffi <goffi@goffi.org>
parents: 3655
diff changeset
79 in_sign="ssssss",
4231
e11b13418ba6 plugin XEP-0353, XEP-0234, jingle: WebRTC data channel signaling implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
80 out_sign="s",
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3970
diff changeset
81 method=self._file_send,
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
82 async_=True,
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
83 )
3403
404d4b29de52 plugin file, XEP-0234: registering is now done by class + use of async:
Goffi <goffi@goffi.org>
parents: 3182
diff changeset
84 self._file_managers = []
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3970
diff changeset
85 host.import_menu(
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
86 (D_("Action"), D_("send file")),
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3970
diff changeset
87 self._file_send_menu,
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
88 security_limit=10,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
89 help_string=D_("Send a file"),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
90 type_=C.MENU_SINGLE,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
91 )
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
92
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3970
diff changeset
93 def _file_send(
3970
4c3361e2bf55 cli (file/send): add `--encrypt` argument to request encryption:
Goffi <goffi@goffi.org>
parents: 3655
diff changeset
94 self,
4c3361e2bf55 cli (file/send): add `--encrypt` argument to request encryption:
Goffi <goffi@goffi.org>
parents: 3655
diff changeset
95 peer_jid_s: str,
4c3361e2bf55 cli (file/send): add `--encrypt` argument to request encryption:
Goffi <goffi@goffi.org>
parents: 3655
diff changeset
96 filepath: str,
4c3361e2bf55 cli (file/send): add `--encrypt` argument to request encryption:
Goffi <goffi@goffi.org>
parents: 3655
diff changeset
97 name: str,
4c3361e2bf55 cli (file/send): add `--encrypt` argument to request encryption:
Goffi <goffi@goffi.org>
parents: 3655
diff changeset
98 file_desc: str,
4c3361e2bf55 cli (file/send): add `--encrypt` argument to request encryption:
Goffi <goffi@goffi.org>
parents: 3655
diff changeset
99 extra_s: str,
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4231
diff changeset
100 profile: str = C.PROF_KEY_NONE,
3970
4c3361e2bf55 cli (file/send): add `--encrypt` argument to request encryption:
Goffi <goffi@goffi.org>
parents: 3655
diff changeset
101 ) -> defer.Deferred:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3970
diff changeset
102 client = self.host.get_client(profile)
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4231
diff changeset
103 d = defer.ensureDeferred(
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4231
diff changeset
104 self.file_send(
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4231
diff changeset
105 client,
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4231
diff changeset
106 jid.JID(peer_jid_s),
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4231
diff changeset
107 filepath,
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4231
diff changeset
108 name or None,
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4231
diff changeset
109 file_desc or None,
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4231
diff changeset
110 data_format.deserialise(extra_s),
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4231
diff changeset
111 )
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4231
diff changeset
112 )
4231
e11b13418ba6 plugin XEP-0353, XEP-0234, jingle: WebRTC data channel signaling implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
113 d.addCallback(data_format.serialise)
e11b13418ba6 plugin XEP-0353, XEP-0234, jingle: WebRTC data channel signaling implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
114 return d
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
115
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3970
diff changeset
116 async def file_send(
4231
e11b13418ba6 plugin XEP-0353, XEP-0234, jingle: WebRTC data channel signaling implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
117 self,
e11b13418ba6 plugin XEP-0353, XEP-0234, jingle: WebRTC data channel signaling implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
118 client: SatXMPPEntity,
e11b13418ba6 plugin XEP-0353, XEP-0234, jingle: WebRTC data channel signaling implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
119 peer_jid: jid.JID,
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4231
diff changeset
120 filepath: str | Path,
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4231
diff changeset
121 filename: str | None = None,
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4231
diff changeset
122 file_desc: str | None = None,
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4231
diff changeset
123 extra: dict | None = None,
4231
e11b13418ba6 plugin XEP-0353, XEP-0234, jingle: WebRTC data channel signaling implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
124 ) -> dict:
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
125 """Send a file using best available method
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
126
4231
e11b13418ba6 plugin XEP-0353, XEP-0234, jingle: WebRTC data channel signaling implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
127 @param peer_jid: jid of the destinee
e11b13418ba6 plugin XEP-0353, XEP-0234, jingle: WebRTC data channel signaling implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
128 @param filepath: absolute path to the file
e11b13418ba6 plugin XEP-0353, XEP-0234, jingle: WebRTC data channel signaling implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
129 @param filename: name to use, or None to find it from filepath
e11b13418ba6 plugin XEP-0353, XEP-0234, jingle: WebRTC data channel signaling implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
130 @param file_desc: description of the file
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
131 @param profile: %(doc_profile)s
4231
e11b13418ba6 plugin XEP-0353, XEP-0234, jingle: WebRTC data channel signaling implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
132 @return: action dictionary, with progress id in case of success, else
3182
f2bb57348587 plugin attach, XEP-0363: progress id can now be specified:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
133 xmlui message
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
134 """
4231
e11b13418ba6 plugin XEP-0353, XEP-0234, jingle: WebRTC data channel signaling implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
135 filepath = Path(filepath)
e11b13418ba6 plugin XEP-0353, XEP-0234, jingle: WebRTC data channel signaling implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
136 if not filepath.is_file():
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
137 raise exceptions.DataError("The given path doesn't link to a file")
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
138 if not filename:
4231
e11b13418ba6 plugin XEP-0353, XEP-0234, jingle: WebRTC data channel signaling implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
139 filename = filepath.name
e11b13418ba6 plugin XEP-0353, XEP-0234, jingle: WebRTC data channel signaling implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
140 for manager, __ in self._file_managers:
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4231
diff changeset
141 if await utils.as_deferred(
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4231
diff changeset
142 manager.can_handle_file_send, client, peer_jid, str(filepath)
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4231
diff changeset
143 ):
3403
404d4b29de52 plugin file, XEP-0234: registering is now done by class + use of async:
Goffi <goffi@goffi.org>
parents: 3182
diff changeset
144 try:
404d4b29de52 plugin file, XEP-0234: registering is now done by class + use of async:
Goffi <goffi@goffi.org>
parents: 3182
diff changeset
145 method_name = manager.name
404d4b29de52 plugin file, XEP-0234: registering is now done by class + use of async:
Goffi <goffi@goffi.org>
parents: 3182
diff changeset
146 except AttributeError:
404d4b29de52 plugin file, XEP-0234: registering is now done by class + use of async:
Goffi <goffi@goffi.org>
parents: 3182
diff changeset
147 method_name = manager.__class__.__name__
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
148 log.info(
3403
404d4b29de52 plugin file, XEP-0234: registering is now done by class + use of async:
Goffi <goffi@goffi.org>
parents: 3182
diff changeset
149 _("{name} method will be used to send the file").format(
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
150 name=method_name
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
151 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
152 )
3403
404d4b29de52 plugin file, XEP-0234: registering is now done by class + use of async:
Goffi <goffi@goffi.org>
parents: 3182
diff changeset
153 try:
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4231
diff changeset
154 file_data = await utils.as_deferred(
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4231
diff changeset
155 manager.file_send,
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4231
diff changeset
156 client,
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4231
diff changeset
157 peer_jid,
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4231
diff changeset
158 str(filepath),
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4231
diff changeset
159 filename,
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4231
diff changeset
160 file_desc,
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4231
diff changeset
161 extra,
3403
404d4b29de52 plugin file, XEP-0234: registering is now done by class + use of async:
Goffi <goffi@goffi.org>
parents: 3182
diff changeset
162 )
404d4b29de52 plugin file, XEP-0234: registering is now done by class + use of async:
Goffi <goffi@goffi.org>
parents: 3182
diff changeset
163 except Exception as e:
404d4b29de52 plugin file, XEP-0234: registering is now done by class + use of async:
Goffi <goffi@goffi.org>
parents: 3182
diff changeset
164 log.warning(
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4231
diff changeset
165 _(
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4231
diff changeset
166 "Can't send {filepath} to {peer_jid} with {method_name}: "
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4231
diff changeset
167 "{reason}"
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4231
diff changeset
168 ).format(
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4231
diff changeset
169 filepath=filepath,
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4231
diff changeset
170 peer_jid=peer_jid,
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4231
diff changeset
171 method_name=method_name,
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4231
diff changeset
172 reason=e,
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4231
diff changeset
173 )
3403
404d4b29de52 plugin file, XEP-0234: registering is now done by class + use of async:
Goffi <goffi@goffi.org>
parents: 3182
diff changeset
174 )
404d4b29de52 plugin file, XEP-0234: registering is now done by class + use of async:
Goffi <goffi@goffi.org>
parents: 3182
diff changeset
175 continue
4231
e11b13418ba6 plugin XEP-0353, XEP-0234, jingle: WebRTC data channel signaling implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
176 if "progress" not in file_data:
e11b13418ba6 plugin XEP-0353, XEP-0234, jingle: WebRTC data channel signaling implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
177 raise exceptions.InternalError(
e11b13418ba6 plugin XEP-0353, XEP-0234, jingle: WebRTC data channel signaling implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
178 '"progress" should be set in "file_send" returned file data dict.'
e11b13418ba6 plugin XEP-0353, XEP-0234, jingle: WebRTC data channel signaling implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
179 )
e11b13418ba6 plugin XEP-0353, XEP-0234, jingle: WebRTC data channel signaling implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
180 return file_data
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
181 msg = "Can't find any method to send file to {jid}".format(jid=peer_jid.full())
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
182 log.warning(msg)
3403
404d4b29de52 plugin file, XEP-0234: registering is now done by class + use of async:
Goffi <goffi@goffi.org>
parents: 3182
diff changeset
183 return {
404d4b29de52 plugin file, XEP-0234: registering is now done by class + use of async:
Goffi <goffi@goffi.org>
parents: 3182
diff changeset
184 "xmlui": xml_tools.note(
404d4b29de52 plugin file, XEP-0234: registering is now done by class + use of async:
Goffi <goffi@goffi.org>
parents: 3182
diff changeset
185 "Can't transfer file", msg, C.XMLUI_DATA_LVL_WARNING
404d4b29de52 plugin file, XEP-0234: registering is now done by class + use of async:
Goffi <goffi@goffi.org>
parents: 3182
diff changeset
186 ).toXml()
404d4b29de52 plugin file, XEP-0234: registering is now done by class + use of async:
Goffi <goffi@goffi.org>
parents: 3182
diff changeset
187 }
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
188
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3970
diff changeset
189 def _on_file_choosed(self, peer_jid, data, profile):
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3970
diff changeset
190 client = self.host.get_client(profile)
1635
591e04f0103c plugin file: added "Action/send" file menu
Goffi <goffi@goffi.org>
parents: 1626
diff changeset
191 cancelled = C.bool(data.get("cancelled", C.BOOL_FALSE))
591e04f0103c plugin file: added "Action/send" file menu
Goffi <goffi@goffi.org>
parents: 1626
diff changeset
192 if cancelled:
591e04f0103c plugin file: added "Action/send" file menu
Goffi <goffi@goffi.org>
parents: 1626
diff changeset
193 return
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
194 path = data["path"]
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3970
diff changeset
195 return self.file_send(client, peer_jid, path)
1635
591e04f0103c plugin file: added "Action/send" file menu
Goffi <goffi@goffi.org>
parents: 1626
diff changeset
196
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3970
diff changeset
197 def _file_send_menu(self, data, profile):
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4231
diff changeset
198 """XMLUI activated by menu: return file sending UI
1635
591e04f0103c plugin file: added "Action/send" file menu
Goffi <goffi@goffi.org>
parents: 1626
diff changeset
199
591e04f0103c plugin file: added "Action/send" file menu
Goffi <goffi@goffi.org>
parents: 1626
diff changeset
200 @param profile: %(doc_profile)s
591e04f0103c plugin file: added "Action/send" file menu
Goffi <goffi@goffi.org>
parents: 1626
diff changeset
201 """
591e04f0103c plugin file: added "Action/send" file menu
Goffi <goffi@goffi.org>
parents: 1626
diff changeset
202 try:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
203 jid_ = jid.JID(data["jid"])
1635
591e04f0103c plugin file: added "Action/send" file menu
Goffi <goffi@goffi.org>
parents: 1626
diff changeset
204 except RuntimeError:
591e04f0103c plugin file: added "Action/send" file menu
Goffi <goffi@goffi.org>
parents: 1626
diff changeset
205 raise exceptions.DataError(_("Invalid JID"))
591e04f0103c plugin file: added "Action/send" file menu
Goffi <goffi@goffi.org>
parents: 1626
diff changeset
206
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3970
diff changeset
207 file_choosed_id = self.host.register_callback(
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3970
diff changeset
208 partial(self._on_file_choosed, jid_),
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
209 with_data=True,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
210 one_shot=True,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
211 )
1635
591e04f0103c plugin file: added "Action/send" file menu
Goffi <goffi@goffi.org>
parents: 1626
diff changeset
212 xml_ui = xml_tools.XMLUI(
591e04f0103c plugin file: added "Action/send" file menu
Goffi <goffi@goffi.org>
parents: 1626
diff changeset
213 C.XMLUI_DIALOG,
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
214 dialog_opt={
1635
591e04f0103c plugin file: added "Action/send" file menu
Goffi <goffi@goffi.org>
parents: 1626
diff changeset
215 C.XMLUI_DATA_TYPE: C.XMLUI_DIALOG_FILE,
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
216 C.XMLUI_DATA_MESS: _(SENDING).format(peer=jid_.full()),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
217 },
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
218 title=_(SENDING_TITLE),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
219 submit_id=file_choosed_id,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
220 )
1635
591e04f0103c plugin file: added "Action/send" file menu
Goffi <goffi@goffi.org>
parents: 1626
diff changeset
221
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
222 return {"xmlui": xml_ui.toXml()}
1635
591e04f0103c plugin file: added "Action/send" file menu
Goffi <goffi@goffi.org>
parents: 1626
diff changeset
223
3403
404d4b29de52 plugin file, XEP-0234: registering is now done by class + use of async:
Goffi <goffi@goffi.org>
parents: 3182
diff changeset
224 def register(self, manager, priority: int = 0) -> None:
404d4b29de52 plugin file, XEP-0234: registering is now done by class + use of async:
Goffi <goffi@goffi.org>
parents: 3182
diff changeset
225 """Register a fileSending manager
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
226
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3970
diff changeset
227 @param manager: object implementing can_handle_file_send, and file_send methods
3403
404d4b29de52 plugin file, XEP-0234: registering is now done by class + use of async:
Goffi <goffi@goffi.org>
parents: 3182
diff changeset
228 @param priority: pririoty of this manager, the higher available will be used
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
229 """
3403
404d4b29de52 plugin file, XEP-0234: registering is now done by class + use of async:
Goffi <goffi@goffi.org>
parents: 3182
diff changeset
230 m_data = (manager, priority)
404d4b29de52 plugin file, XEP-0234: registering is now done by class + use of async:
Goffi <goffi@goffi.org>
parents: 3182
diff changeset
231 if m_data in self._file_managers:
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4231
diff changeset
232 raise exceptions.ConflictError(f"Manager {manager} is already registered")
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4231
diff changeset
233 if not hasattr(manager, "can_handle_file_send") or not hasattr(
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4231
diff changeset
234 manager, "file_send"
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4231
diff changeset
235 ):
3403
404d4b29de52 plugin file, XEP-0234: registering is now done by class + use of async:
Goffi <goffi@goffi.org>
parents: 3182
diff changeset
236 raise ValueError(
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3970
diff changeset
237 f'{manager} must have both "can_handle_file_send" and "file_send" methods to '
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4231
diff changeset
238 "be registered"
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4231
diff changeset
239 )
3403
404d4b29de52 plugin file, XEP-0234: registering is now done by class + use of async:
Goffi <goffi@goffi.org>
parents: 3182
diff changeset
240 self._file_managers.append(m_data)
404d4b29de52 plugin file, XEP-0234: registering is now done by class + use of async:
Goffi <goffi@goffi.org>
parents: 3182
diff changeset
241 self._file_managers.sort(key=lambda m: m[1], reverse=True)
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
242
3403
404d4b29de52 plugin file, XEP-0234: registering is now done by class + use of async:
Goffi <goffi@goffi.org>
parents: 3182
diff changeset
243 def unregister(self, manager):
404d4b29de52 plugin file, XEP-0234: registering is now done by class + use of async:
Goffi <goffi@goffi.org>
parents: 3182
diff changeset
244 for idx, data in enumerate(self._file_managers):
404d4b29de52 plugin file, XEP-0234: registering is now done by class + use of async:
Goffi <goffi@goffi.org>
parents: 3182
diff changeset
245 if data[0] == manager:
404d4b29de52 plugin file, XEP-0234: registering is now done by class + use of async:
Goffi <goffi@goffi.org>
parents: 3182
diff changeset
246 break
404d4b29de52 plugin file, XEP-0234: registering is now done by class + use of async:
Goffi <goffi@goffi.org>
parents: 3182
diff changeset
247 else:
404d4b29de52 plugin file, XEP-0234: registering is now done by class + use of async:
Goffi <goffi@goffi.org>
parents: 3182
diff changeset
248 raise exceptions.NotFound("The file manager {manager} is not registered")
404d4b29de52 plugin file, XEP-0234: registering is now done by class + use of async:
Goffi <goffi@goffi.org>
parents: 3182
diff changeset
249 del self._file_managers[idx]
1525
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
250
1574
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
251 # Dialogs with user
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
252 # the overwrite check is done here
1525
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
253
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3970
diff changeset
254 def open_file_write(self, client, file_path, transfer_data, file_data, stream_object):
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4231
diff changeset
255 """create SatFile or FileStremaObject for the requested file and fill suitable data"""
2489
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
256 if stream_object:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
257 assert "stream_object" not in transfer_data
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
258 transfer_data["stream_object"] = stream.FileStreamObject(
2489
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
259 self.host,
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
260 client,
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
261 file_path,
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
262 mode="wb",
2489
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
263 uid=file_data[PROGRESS_ID_KEY],
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
264 size=file_data["size"],
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
265 data_cb=file_data.get("data_cb"),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
266 )
2489
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
267 else:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
268 assert "file_obj" not in transfer_data
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
269 transfer_data["file_obj"] = stream.SatFile(
2489
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
270 self.host,
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
271 client,
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
272 file_path,
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
273 mode="wb",
2489
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
274 uid=file_data[PROGRESS_ID_KEY],
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
275 size=file_data["size"],
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
276 data_cb=file_data.get("data_cb"),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
277 )
1574
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
278
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3970
diff changeset
279 async def _got_confirmation(
3655
ca980569318c plugin misc file: fix `getDestDir` returning a Deferred in a coroutine
Goffi <goffi@goffi.org>
parents: 3528
diff changeset
280 self, client, data, peer_jid, transfer_data, file_data, stream_object
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
281 ):
1574
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
282 """Called when the permission and dest path have been received
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
283
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
284 @param peer_jid(jid.JID): jid of the file sender
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3970
diff changeset
285 @param transfer_data(dict): same as for [self.get_dest_dir]
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3970
diff changeset
286 @param file_data(dict): same as for [self.get_dest_dir]
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3970
diff changeset
287 @param stream_object(bool): same as for [self.get_dest_dir]
1574
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
288 return (bool): True if copy is wanted and OK
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
289 False if user wants to cancel
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
290 if file exists ask confirmation and call again self._getDestDir if needed
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
291 """
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
292 if data.get("cancelled", False):
1574
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
293 return False
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
294 path = data["path"]
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
295 file_data["file_path"] = file_path = os.path.join(path, file_data["name"])
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
296 log.debug("destination file path set to {}".format(file_path))
1574
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
297
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
298 # we manage case where file already exists
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
299 if os.path.exists(file_path):
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3970
diff changeset
300 overwrite = await xml_tools.defer_confirm(
1574
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
301 self.host,
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
302 _(CONFIRM_OVERWRITE).format(file_path),
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
303 _(CONFIRM_OVERWRITE_TITLE),
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
304 action_extra={
4042
877145b4ba01 core: don't use `meta_` prefix anymore for `action_extra` in `action_new` signal.
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
305 "from_jid": peer_jid.full(),
877145b4ba01 core: don't use `meta_` prefix anymore for `action_extra` in `action_new` signal.
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
306 "type": C.META_TYPE_OVERWRITE,
877145b4ba01 core: don't use `meta_` prefix anymore for `action_extra` in `action_new` signal.
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
307 "progress_id": file_data[PROGRESS_ID_KEY],
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
308 },
1646
7f0c8856e4e1 plugin file: deferred XMLUI now use security limit
Goffi <goffi@goffi.org>
parents: 1639
diff changeset
309 security_limit=SECURITY_LIMIT,
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
310 profile=client.profile,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
311 )
3655
ca980569318c plugin misc file: fix `getDestDir` returning a Deferred in a coroutine
Goffi <goffi@goffi.org>
parents: 3528
diff changeset
312
ca980569318c plugin misc file: fix `getDestDir` returning a Deferred in a coroutine
Goffi <goffi@goffi.org>
parents: 3528
diff changeset
313 if not overwrite:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3970
diff changeset
314 return await self.get_dest_dir(client, peer_jid, transfer_data, file_data)
1574
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
315
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3970
diff changeset
316 self.open_file_write(client, file_path, transfer_data, file_data, stream_object)
1574
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
317 return True
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
318
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3970
diff changeset
319 async def get_dest_dir(
3528
849374e59178 component file sharing: quotas implementation:
Goffi <goffi@goffi.org>
parents: 3524
diff changeset
320 self, client, peer_jid, transfer_data, file_data, stream_object=False
849374e59178 component file sharing: quotas implementation:
Goffi <goffi@goffi.org>
parents: 3524
diff changeset
321 ):
1574
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
322 """Request confirmation and destination dir to user
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
323
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
324 Overwrite confirmation is managed.
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
325 if transfer is confirmed, 'file_obj' is added to transfer_data
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
326 @param peer_jid(jid.JID): jid of the file sender
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
327 @param filename(unicode): name of the file
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
328 @param transfer_data(dict): data of the transfer session,
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
329 it will be only used to store the file_obj.
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3970
diff changeset
330 "file_obj" (or "stream_object") key *MUST NOT* exist before using get_dest_dir
1574
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
331 @param file_data(dict): information about the file to be transfered
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
332 It MUST contain the following keys:
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
333 - peer_jid (jid.JID): other peer jid
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
334 - name (unicode): name of the file to trasnsfer
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
335 the name must not be empty or contain a "/" character
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
336 - size (int): size of the file
1619
3ec7511dbf28 plugin file: 'data_cb' key can be used in file_data to specified a callback used on each read/write
Goffi <goffi@goffi.org>
parents: 1607
diff changeset
337 - desc (unicode): description of the file
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
338 - progress_id (unicode): id to use for progression
1574
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
339 It *MUST NOT* contain the "peer" key
1619
3ec7511dbf28 plugin file: 'data_cb' key can be used in file_data to specified a callback used on each read/write
Goffi <goffi@goffi.org>
parents: 1607
diff changeset
340 It may contain:
3ec7511dbf28 plugin file: 'data_cb' key can be used in file_data to specified a callback used on each read/write
Goffi <goffi@goffi.org>
parents: 1607
diff changeset
341 - data_cb (callable): method called on each data read/write
1574
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
342 "file_path" will be added to this dict once destination selected
1575
833bdb227b16 plugins XEP-0234, file: moved human file size conversion to file plugi
Goffi <goffi@goffi.org>
parents: 1574
diff changeset
343 "size_human" will also be added with human readable file size
2489
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
344 @param stream_object(bool): if True, a stream_object will be used instead of file_obj
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
345 a stream.FileStreamObject will be used
3655
ca980569318c plugin misc file: fix `getDestDir` returning a Deferred in a coroutine
Goffi <goffi@goffi.org>
parents: 3528
diff changeset
346 return: True if transfer is accepted
1574
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
347 """
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3970
diff changeset
348 cont, ret_value = await self.host.trigger.async_return_point(
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
349 "FILE_getDestDir", client, peer_jid, transfer_data, file_data, stream_object
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
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 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
352 return ret_value
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
353 filename = file_data["name"]
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
354 assert filename and not "/" in filename
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
355 assert PROGRESS_ID_KEY in file_data
1575
833bdb227b16 plugins XEP-0234, file: moved human file size conversion to file plugi
Goffi <goffi@goffi.org>
parents: 1574
diff changeset
356 # human readable size
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3970
diff changeset
357 file_data["size_human"] = common_utils.get_human_size(file_data["size"])
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3970
diff changeset
358 resp_data = await xml_tools.defer_dialog(
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
359 self.host,
1574
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
360 _(CONFIRM).format(peer=peer_jid.full(), **file_data),
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
361 _(CONFIRM_TITLE),
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
362 type_=C.XMLUI_DIALOG_FILE,
babd97d80049 plugins XEP-0234, file: moved file request dialog to file plugin
Goffi <goffi@goffi.org>
parents: 1568
diff changeset
363 options={C.XMLUI_DATA_FILETYPE: C.XMLUI_DATA_FILETYPE_DIR},
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
364 action_extra={
4042
877145b4ba01 core: don't use `meta_` prefix anymore for `action_extra` in `action_new` signal.
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
365 "from_jid": peer_jid.full(),
877145b4ba01 core: don't use `meta_` prefix anymore for `action_extra` in `action_new` signal.
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
366 "type": C.META_TYPE_FILE,
877145b4ba01 core: don't use `meta_` prefix anymore for `action_extra` in `action_new` signal.
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
367 "progress_id": file_data[PROGRESS_ID_KEY],
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
368 },
1646
7f0c8856e4e1 plugin file: deferred XMLUI now use security limit
Goffi <goffi@goffi.org>
parents: 1639
diff changeset
369 security_limit=SECURITY_LIMIT,
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
370 profile=client.profile,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
371 )
3655
ca980569318c plugin misc file: fix `getDestDir` returning a Deferred in a coroutine
Goffi <goffi@goffi.org>
parents: 3528
diff changeset
372
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3970
diff changeset
373 accepted = await self._got_confirmation(
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
374 client,
3655
ca980569318c plugin misc file: fix `getDestDir` returning a Deferred in a coroutine
Goffi <goffi@goffi.org>
parents: 3528
diff changeset
375 resp_data,
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
376 peer_jid,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
377 transfer_data,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
378 file_data,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
379 stream_object,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
380 )
3655
ca980569318c plugin misc file: fix `getDestDir` returning a Deferred in a coroutine
Goffi <goffi@goffi.org>
parents: 3528
diff changeset
381 return accepted