Mercurial > libervia-backend
annotate libervia/backend/plugins/plugin_misc_upload.py @ 4098:4a8b29ab34c0
plugin XEP-0363: fix handling of empty `upload_jid` in `_get_slot`
author | Goffi <goffi@goffi.org> |
---|---|
date | Thu, 22 Jun 2023 15:46:42 +0200 |
parents | 4b842c1fb686 |
children | 0d7bb4df2343 |
rev | line source |
---|---|
3028 | 1 #!/usr/bin/env python3 |
1640
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
2 |
3089
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
3 # SAT plugin for uploading files |
3479 | 4 # Copyright (C) 2009-2021 Jérôme Poisson (goffi@goffi.org) |
1640
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
5 |
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
6 # This program is free software: you can redistribute it and/or modify |
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
7 # it under the terms of the GNU Affero General Public License as published by |
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
8 # the Free Software Foundation, either version 3 of the License, or |
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
9 # (at your option) any later version. |
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
10 |
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
11 # This program is distributed in the hope that it will be useful, |
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
14 # GNU Affero General Public License for more details. |
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
15 |
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
16 # You should have received a copy of the GNU Affero General Public License |
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
17 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
18 |
3089
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
19 import os |
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
20 import os.path |
3922
0ff265725489
plugin XEP-0447: handle attachment and download:
Goffi <goffi@goffi.org>
parents:
3534
diff
changeset
|
21 from pathlib import Path |
0ff265725489
plugin XEP-0447: handle attachment and download:
Goffi <goffi@goffi.org>
parents:
3534
diff
changeset
|
22 from typing import Optional, Tuple, Union |
0ff265725489
plugin XEP-0447: handle attachment and download:
Goffi <goffi@goffi.org>
parents:
3534
diff
changeset
|
23 |
1640
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
24 from twisted.internet import defer |
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
25 from twisted.words.protocols.jabber import jid |
2863
a97a5c73594d
plugin upload: fixed inversion of title and message body + better error message on failed upload
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
26 from twisted.words.protocols.jabber import error as jabber_error |
3922
0ff265725489
plugin XEP-0447: handle attachment and download:
Goffi <goffi@goffi.org>
parents:
3534
diff
changeset
|
27 |
4071
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4037
diff
changeset
|
28 from libervia.backend.core import exceptions |
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4037
diff
changeset
|
29 from libervia.backend.core.constants import Const as C |
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4037
diff
changeset
|
30 from libervia.backend.core.core_types import SatXMPPEntity |
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4037
diff
changeset
|
31 from libervia.backend.core.i18n import D_, _ |
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4037
diff
changeset
|
32 from libervia.backend.core.log import getLogger |
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4037
diff
changeset
|
33 from libervia.backend.tools import xml_tools |
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4037
diff
changeset
|
34 from libervia.backend.tools.common import data_format |
3089
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
35 |
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
36 log = getLogger(__name__) |
1640
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
37 |
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
38 |
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
39 PLUGIN_INFO = { |
2145
33c8c4973743
core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents:
2144
diff
changeset
|
40 C.PI_NAME: "File Upload", |
33c8c4973743
core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents:
2144
diff
changeset
|
41 C.PI_IMPORT_NAME: "UPLOAD", |
33c8c4973743
core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents:
2144
diff
changeset
|
42 C.PI_TYPE: C.PLUG_TYPE_MISC, |
3289
9057713ab124
plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents:
3182
diff
changeset
|
43 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
|
44 C.PI_MAIN: "UploadPlugin", |
33c8c4973743
core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents:
2144
diff
changeset
|
45 C.PI_HANDLER: "no", |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
46 C.PI_DESCRIPTION: _("""File upload management"""), |
1640
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
47 } |
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
48 |
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
49 |
3028 | 50 UPLOADING = D_("Please select a file to upload") |
51 UPLOADING_TITLE = D_("File upload") | |
1640
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
52 |
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
53 |
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
54 class UploadPlugin(object): |
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
55 # TODO: plugin unload |
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
56 |
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
57 def __init__(self, host): |
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
58 log.info(_("plugin Upload initialization")) |
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
59 self.host = host |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3922
diff
changeset
|
60 host.bridge.add_method( |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3922
diff
changeset
|
61 "file_upload", |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
62 ".plugin", |
3089
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
63 in_sign="sssss", |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
64 out_sign="a{ss}", |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3922
diff
changeset
|
65 method=self._file_upload, |
3028 | 66 async_=True, |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
67 ) |
1640
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
68 self._upload_callbacks = [] |
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
69 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3922
diff
changeset
|
70 def _file_upload( |
3089
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
71 self, filepath, filename, upload_jid_s="", options='', profile=C.PROF_KEY_NONE |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
72 ): |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3922
diff
changeset
|
73 client = self.host.get_client(profile) |
1640
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
74 upload_jid = jid.JID(upload_jid_s) if upload_jid_s else None |
3089
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
75 options = data_format.deserialise(options) |
1640
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
76 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3922
diff
changeset
|
77 return defer.ensureDeferred(self.file_upload( |
3089
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
78 client, filepath, filename or None, upload_jid, options |
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
79 )) |
1640
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
80 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3922
diff
changeset
|
81 async def file_upload(self, client, filepath, filename, upload_jid, options): |
1824
a19161bb3ff7
plugin upload, XEP-0363: splitted fileUpload in fileUpload + upload:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
82 """Send a file using best available method |
a19161bb3ff7
plugin upload, XEP-0363: splitted fileUpload in fileUpload + upload:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
83 |
a19161bb3ff7
plugin upload, XEP-0363: splitted fileUpload in fileUpload + upload:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
84 parameters are the same as for [upload] |
2867
3cac3d050046
plugin upload: minor fixes in some docstring length
Goffi <goffi@goffi.org>
parents:
2863
diff
changeset
|
85 @return (dict): action dictionary, with progress id in case of success, else xmlui |
3cac3d050046
plugin upload: minor fixes in some docstring length
Goffi <goffi@goffi.org>
parents:
2863
diff
changeset
|
86 message |
1824
a19161bb3ff7
plugin upload, XEP-0363: splitted fileUpload in fileUpload + upload:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
87 """ |
3089
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
88 try: |
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
89 progress_id, __ = await self.upload( |
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
90 client, filepath, filename, upload_jid, options) |
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
91 except Exception as e: |
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
92 if (isinstance(e, jabber_error.StanzaError) |
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
93 and e.condition == 'not-acceptable'): |
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
94 reason = e.text |
2863
a97a5c73594d
plugin upload: fixed inversion of title and message body + better error message on failed upload
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
95 else: |
3089
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
96 reason = str(e) |
3028 | 97 msg = D_("Can't upload file: {reason}").format(reason=reason) |
1824
a19161bb3ff7
plugin upload, XEP-0363: splitted fileUpload in fileUpload + upload:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
98 log.warning(msg) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
99 return { |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
100 "xmlui": xml_tools.note( |
3028 | 101 msg, D_("Can't upload file"), C.XMLUI_DATA_LVL_WARNING |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
102 ).toXml() |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
103 } |
3089
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
104 else: |
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
105 return {"progress": progress_id} |
1824
a19161bb3ff7
plugin upload, XEP-0363: splitted fileUpload in fileUpload + upload:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
106 |
3922
0ff265725489
plugin XEP-0447: handle attachment and download:
Goffi <goffi@goffi.org>
parents:
3534
diff
changeset
|
107 async def upload( |
0ff265725489
plugin XEP-0447: handle attachment and download:
Goffi <goffi@goffi.org>
parents:
3534
diff
changeset
|
108 self, |
0ff265725489
plugin XEP-0447: handle attachment and download:
Goffi <goffi@goffi.org>
parents:
3534
diff
changeset
|
109 client: SatXMPPEntity, |
0ff265725489
plugin XEP-0447: handle attachment and download:
Goffi <goffi@goffi.org>
parents:
3534
diff
changeset
|
110 filepath: Union[Path, str], |
0ff265725489
plugin XEP-0447: handle attachment and download:
Goffi <goffi@goffi.org>
parents:
3534
diff
changeset
|
111 filename: Optional[str] = None, |
0ff265725489
plugin XEP-0447: handle attachment and download:
Goffi <goffi@goffi.org>
parents:
3534
diff
changeset
|
112 upload_jid: Optional[jid.JID] = None, |
0ff265725489
plugin XEP-0447: handle attachment and download:
Goffi <goffi@goffi.org>
parents:
3534
diff
changeset
|
113 extra: Optional[dict]=None |
0ff265725489
plugin XEP-0447: handle attachment and download:
Goffi <goffi@goffi.org>
parents:
3534
diff
changeset
|
114 ) -> Tuple[str, defer.Deferred]: |
1640
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
115 """Send a file using best available method |
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
116 |
3922
0ff265725489
plugin XEP-0447: handle attachment and download:
Goffi <goffi@goffi.org>
parents:
3534
diff
changeset
|
117 @param filepath: absolute path to the file |
0ff265725489
plugin XEP-0447: handle attachment and download:
Goffi <goffi@goffi.org>
parents:
3534
diff
changeset
|
118 @param filename: name to use for the upload |
1640
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
119 None to use basename of the path |
3922
0ff265725489
plugin XEP-0447: handle attachment and download:
Goffi <goffi@goffi.org>
parents:
3534
diff
changeset
|
120 @param upload_jid: upload capable entity jid, |
1640
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
121 or None to use autodetected, if possible |
3922
0ff265725489
plugin XEP-0447: handle attachment and download:
Goffi <goffi@goffi.org>
parents:
3534
diff
changeset
|
122 @param extra: extra data/options to use for the upload, may be: |
1824
a19161bb3ff7
plugin upload, XEP-0363: splitted fileUpload in fileUpload + upload:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
123 - ignore_tls_errors(bool): True to ignore SSL/TLS certificate verification |
1640
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
124 used only if HTTPS transport is needed |
3182
f2bb57348587
plugin attach, XEP-0363: progress id can now be specified:
Goffi <goffi@goffi.org>
parents:
3136
diff
changeset
|
125 - progress_id(str): id to use for progression |
f2bb57348587
plugin attach, XEP-0363: progress id can now be specified:
Goffi <goffi@goffi.org>
parents:
3136
diff
changeset
|
126 if not specified, one will be generated |
1640
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
127 @param profile: %(doc_profile)s |
3922
0ff265725489
plugin XEP-0447: handle attachment and download:
Goffi <goffi@goffi.org>
parents:
3534
diff
changeset
|
128 @return: progress_id and a Deferred which fire download URL when upload is |
0ff265725489
plugin XEP-0447: handle attachment and download:
Goffi <goffi@goffi.org>
parents:
3534
diff
changeset
|
129 finished |
1640
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
130 """ |
3922
0ff265725489
plugin XEP-0447: handle attachment and download:
Goffi <goffi@goffi.org>
parents:
3534
diff
changeset
|
131 if extra is None: |
0ff265725489
plugin XEP-0447: handle attachment and download:
Goffi <goffi@goffi.org>
parents:
3534
diff
changeset
|
132 extra = {} |
1640
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
133 if not os.path.isfile(filepath): |
3028 | 134 raise exceptions.DataError("The given path doesn't link to a file") |
1640
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
135 for method_name, available_cb, upload_cb, priority in self._upload_callbacks: |
3534
1658472abd77
plugin upload: don't run `available_cb` if `upload_jid` is used in `upload` method
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
136 if upload_jid is None: |
1658472abd77
plugin upload: don't run `available_cb` if `upload_jid` is used in `upload` method
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
137 try: |
1658472abd77
plugin upload: don't run `available_cb` if `upload_jid` is used in `upload` method
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
138 upload_jid = await available_cb(client, upload_jid) |
1658472abd77
plugin upload: don't run `available_cb` if `upload_jid` is used in `upload` method
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
139 except exceptions.NotFound: |
1658472abd77
plugin upload: don't run `available_cb` if `upload_jid` is used in `upload` method
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
140 continue # no entity managing this extension found |
1640
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
141 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
142 log.info( |
3028 | 143 "{name} method will be used to upload the file".format(name=method_name) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
144 ) |
3089
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
145 progress_id, download_d = await upload_cb( |
3922
0ff265725489
plugin XEP-0447: handle attachment and download:
Goffi <goffi@goffi.org>
parents:
3534
diff
changeset
|
146 client, filepath, filename, upload_jid, extra |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
147 ) |
3089
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
148 return progress_id, download_d |
1824
a19161bb3ff7
plugin upload, XEP-0363: splitted fileUpload in fileUpload + upload:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
149 |
3028 | 150 raise exceptions.NotFound("Can't find any method to upload a file") |
1640
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
151 |
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
152 def register(self, method_name, available_cb, upload_cb, priority=0): |
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
153 """Register a fileUploading method |
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
154 |
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
155 @param method_name(unicode): short name for the method, must be unique |
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
156 @param available_cb(callable): method to call to check if this method is usable |
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
157 the callback must take two arguments: upload_jid (can be None) and profile |
2867
3cac3d050046
plugin upload: minor fixes in some docstring length
Goffi <goffi@goffi.org>
parents:
2863
diff
changeset
|
158 the callback must return the first entity found (being upload_jid or one of its |
3cac3d050046
plugin upload: minor fixes in some docstring length
Goffi <goffi@goffi.org>
parents:
2863
diff
changeset
|
159 components) |
1640
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
160 exceptions.NotFound must be raised if no entity has been found |
1824
a19161bb3ff7
plugin upload, XEP-0363: splitted fileUpload in fileUpload + upload:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
161 @param upload_cb(callable): method to upload a file |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3922
diff
changeset
|
162 must have the same signature as [file_upload] |
2867
3cac3d050046
plugin upload: minor fixes in some docstring length
Goffi <goffi@goffi.org>
parents:
2863
diff
changeset
|
163 must return a tuple with progress_id and a Deferred which fire download URL |
3cac3d050046
plugin upload: minor fixes in some docstring length
Goffi <goffi@goffi.org>
parents:
2863
diff
changeset
|
164 when upload is finished |
1640
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
165 @param priority(int): pririoty of this method, the higher available will be used |
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
166 """ |
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
167 assert method_name |
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
168 for data in self._upload_callbacks: |
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
169 if method_name == data[0]: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
170 raise exceptions.ConflictError( |
3028 | 171 "A method with this name is already registered" |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
172 ) |
1640
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
173 self._upload_callbacks.append((method_name, available_cb, upload_cb, priority)) |
1824
a19161bb3ff7
plugin upload, XEP-0363: splitted fileUpload in fileUpload + upload:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
174 self._upload_callbacks.sort(key=lambda data: data[3], reverse=True) |
1640
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
175 |
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
176 def unregister(self, method_name): |
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
177 for idx, data in enumerate(self._upload_callbacks): |
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
178 if data[0] == method_name: |
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
179 del [idx] |
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
180 return |
3028 | 181 raise exceptions.NotFound("The name to unregister doesn't exist") |