comparison sat/plugins/plugin_misc_upload.py @ 3922:0ff265725489

plugin XEP-0447: handle attachment and download: - plugin XEP-0447 can now be used in message attachments and to retrieve an attachment - plugin attach: `attachment` being processed is added to `extra` so the handler can inspect it - plugin attach: `size` is added to attachment - plugin download: a whole attachment dict is now used in `download` and `file_download`/`file_download_complete`. `download_uri` can be used as a shortcut when just a URI is used. In addition to URI scheme handler, whole attachment handlers can now be registered with `register_download_handler` - plugin XEP-0363: `file_http_upload` `XEP-0363_upload_size` triggers have been renamed to `XEP-0363_upload_pre_slot` and is now using a dict with arguments, allowing for the size but also the filename to be modified, which is necessary for encryption (filename may be hidden from URL this way). - plugin XEP-0446: fix wrong element name - plugin XEP-0447: source handler can now be registered (`url-data` is registered by default) - plugin XEP-0447: source parsing has been put in a separated `parse_sources_elt` method, as it may be useful to do it independently (notably with XEP-0448) - plugin XEP-0447: parse received message and complete attachments when suitable - plugin XEP-0447: can now be used with message attachments - plugin XEP-0447: can now be used with attachments download - renamed `options` arguments to `extra` for consistency - some style change (progressive move from legacy camelCase to PEP8 snake_case) - some typing rel 379
author Goffi <goffi@goffi.org>
date Thu, 06 Oct 2022 16:02:05 +0200
parents 1658472abd77
children 524856bd7b19
comparison
equal deleted inserted replaced
3921:cc2705225778 3922:0ff265725489
16 # You should have received a copy of the GNU Affero General Public License 16 # You should have received a copy of the GNU Affero General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>. 17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18 18
19 import os 19 import os
20 import os.path 20 import os.path
21 from pathlib import Path
22 from typing import Optional, Tuple, Union
23
21 from twisted.internet import defer 24 from twisted.internet import defer
22 from twisted.words.protocols.jabber import jid 25 from twisted.words.protocols.jabber import jid
23 from twisted.words.protocols.jabber import error as jabber_error 26 from twisted.words.protocols.jabber import error as jabber_error
24 from sat.core.i18n import _, D_ 27
28 from sat.core import exceptions
25 from sat.core.constants import Const as C 29 from sat.core.constants import Const as C
30 from sat.core.core_types import SatXMPPEntity
31 from sat.core.i18n import D_, _
32 from sat.core.log import getLogger
33 from sat.tools import xml_tools
26 from sat.tools.common import data_format 34 from sat.tools.common import data_format
27 from sat.core.log import getLogger
28 from sat.core import exceptions
29 from sat.tools import xml_tools
30 35
31 log = getLogger(__name__) 36 log = getLogger(__name__)
32 37
33 38
34 PLUGIN_INFO = { 39 PLUGIN_INFO = {
97 ).toXml() 102 ).toXml()
98 } 103 }
99 else: 104 else:
100 return {"progress": progress_id} 105 return {"progress": progress_id}
101 106
102 async def upload(self, client, filepath, filename=None, upload_jid=None, 107 async def upload(
103 options=None): 108 self,
109 client: SatXMPPEntity,
110 filepath: Union[Path, str],
111 filename: Optional[str] = None,
112 upload_jid: Optional[jid.JID] = None,
113 extra: Optional[dict]=None
114 ) -> Tuple[str, defer.Deferred]:
104 """Send a file using best available method 115 """Send a file using best available method
105 116
106 @param filepath(str): absolute path to the file 117 @param filepath: absolute path to the file
107 @param filename(None, unicode): name to use for the upload 118 @param filename: name to use for the upload
108 None to use basename of the path 119 None to use basename of the path
109 @param upload_jid(jid.JID, None): upload capable entity jid, 120 @param upload_jid: upload capable entity jid,
110 or None to use autodetected, if possible 121 or None to use autodetected, if possible
111 @param options(dict): option to use for the upload, may be: 122 @param extra: extra data/options to use for the upload, may be:
112 - ignore_tls_errors(bool): True to ignore SSL/TLS certificate verification 123 - ignore_tls_errors(bool): True to ignore SSL/TLS certificate verification
113 used only if HTTPS transport is needed 124 used only if HTTPS transport is needed
114 - progress_id(str): id to use for progression 125 - progress_id(str): id to use for progression
115 if not specified, one will be generated 126 if not specified, one will be generated
116 @param profile: %(doc_profile)s 127 @param profile: %(doc_profile)s
117 @return (tuple[unicode,D(unicode)]): progress_id and a Deferred which fire 128 @return: progress_id and a Deferred which fire download URL when upload is
118 download URL when upload is finished 129 finished
119 """ 130 """
120 if options is None: 131 if extra is None:
121 options = {} 132 extra = {}
122 if not os.path.isfile(filepath): 133 if not os.path.isfile(filepath):
123 raise exceptions.DataError("The given path doesn't link to a file") 134 raise exceptions.DataError("The given path doesn't link to a file")
124 for method_name, available_cb, upload_cb, priority in self._upload_callbacks: 135 for method_name, available_cb, upload_cb, priority in self._upload_callbacks:
125 if upload_jid is None: 136 if upload_jid is None:
126 try: 137 try:
130 141
131 log.info( 142 log.info(
132 "{name} method will be used to upload the file".format(name=method_name) 143 "{name} method will be used to upload the file".format(name=method_name)
133 ) 144 )
134 progress_id, download_d = await upload_cb( 145 progress_id, download_d = await upload_cb(
135 client, filepath, filename, upload_jid, options 146 client, filepath, filename, upload_jid, extra
136 ) 147 )
137 return progress_id, download_d 148 return progress_id, download_d
138 149
139 raise exceptions.NotFound("Can't find any method to upload a file") 150 raise exceptions.NotFound("Can't find any method to upload a file")
140 151