annotate libervia/backend/plugins/plugin_xep_0363.py @ 4226:3f7ca590a5da

cli (pubsub): use `PEP` when service is not specified in error message.
author Goffi <goffi@goffi.org>
date Tue, 05 Mar 2024 17:31:56 +0100
parents 72b95cdc3432
children 0d7bb4df2343
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2866
diff changeset
1 #!/usr/bin/env python3
3137
559a625a236b fixed shebangs
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
2
3192
883fb4981958 plugin attach: disable TLS check if "check_certificate" is disabled
Goffi <goffi@goffi.org>
parents: 3182
diff changeset
3 # SàT plugin for HTTP File Upload (XEP-0363)
3479
be6d91572633 date update
Goffi <goffi@goffi.org>
parents: 3399
diff changeset
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
3922
0ff265725489 plugin XEP-0447: handle attachment and download:
Goffi <goffi@goffi.org>
parents: 3526
diff changeset
19 from dataclasses import dataclass
3089
e75024e41f81 plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
20 import mimetypes
3922
0ff265725489 plugin XEP-0447: handle attachment and download:
Goffi <goffi@goffi.org>
parents: 3526
diff changeset
21 import os.path
0ff265725489 plugin XEP-0447: handle attachment and download:
Goffi <goffi@goffi.org>
parents: 3526
diff changeset
22 from pathlib import Path
0ff265725489 plugin XEP-0447: handle attachment and download:
Goffi <goffi@goffi.org>
parents: 3526
diff changeset
23 from typing import Callable, NamedTuple, Optional, Tuple
3289
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3223
diff changeset
24 from urllib import parse
3922
0ff265725489 plugin XEP-0447: handle attachment and download:
Goffi <goffi@goffi.org>
parents: 3526
diff changeset
25
1640
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
26 from twisted.internet import reactor
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
27 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
28 from twisted.web import client as http_client
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
29 from twisted.web import http_headers
3922
0ff265725489 plugin XEP-0447: handle attachment and download:
Goffi <goffi@goffi.org>
parents: 3526
diff changeset
30 from twisted.words.protocols.jabber import error, jid, xmlstream
0ff265725489 plugin XEP-0447: handle attachment and download:
Goffi <goffi@goffi.org>
parents: 3526
diff changeset
31 from twisted.words.xish import domish
0ff265725489 plugin XEP-0447: handle attachment and download:
Goffi <goffi@goffi.org>
parents: 3526
diff changeset
32 from wokkel import disco, iwokkel
0ff265725489 plugin XEP-0447: handle attachment and download:
Goffi <goffi@goffi.org>
parents: 3526
diff changeset
33 from zope.interface import implementer
0ff265725489 plugin XEP-0447: handle attachment and download:
Goffi <goffi@goffi.org>
parents: 3526
diff changeset
34
4071
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
35 from libervia.backend.core import exceptions
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
36 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
37 from libervia.backend.core.core_types import SatXMPPEntity
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
38 from libervia.backend.core.i18n import _
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
39 from libervia.backend.core.log import getLogger
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
40 from libervia.backend.core.xmpp import SatXMPPComponent
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
41 from libervia.backend.tools import utils, web as sat_web
1640
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
42
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
43
3089
e75024e41f81 plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
44 log = getLogger(__name__)
e75024e41f81 plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
45
1640
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
46 PLUGIN_INFO = {
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_NAME: "HTTP File Upload",
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 2144
diff changeset
48 C.PI_IMPORT_NAME: "XEP-0363",
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 2144
diff changeset
49 C.PI_TYPE: "XEP",
3289
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3223
diff changeset
50 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
51 C.PI_PROTOCOLS: ["XEP-0363"],
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 2144
diff changeset
52 C.PI_DEPENDENCIES: ["FILE", "UPLOAD"],
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 2144
diff changeset
53 C.PI_MAIN: "XEP_0363",
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 2144
diff changeset
54 C.PI_HANDLER: "yes",
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2866
diff changeset
55 C.PI_DESCRIPTION: _("""Implementation of HTTP File Upload"""),
1640
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
2866
8ce5748bfe97 plugin XEP-0363: updated to namespace "urn:xmpp:http:upload:0", handle headers
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
58 NS_HTTP_UPLOAD = "urn:xmpp:http:upload:0"
3289
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3223
diff changeset
59 IQ_HTTP_UPLOAD_REQUEST = C.IQ_GET + '/request[@xmlns="' + NS_HTTP_UPLOAD + '"]'
2866
8ce5748bfe97 plugin XEP-0363: updated to namespace "urn:xmpp:http:upload:0", handle headers
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
60 ALLOWED_HEADERS = ('authorization', 'cookie', 'expires')
1640
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
61
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
62
3089
e75024e41f81 plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
63 @dataclass
e75024e41f81 plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
64 class Slot:
e75024e41f81 plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
65 """Upload slot"""
e75024e41f81 plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
66 put: str
e75024e41f81 plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
67 get: str
e75024e41f81 plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
68 headers: list
1640
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
69
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
70
3289
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3223
diff changeset
71 class UploadRequest(NamedTuple):
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3223
diff changeset
72 from_: jid.JID
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3223
diff changeset
73 filename: str
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3223
diff changeset
74 size: int
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3223
diff changeset
75 content_type: Optional[str]
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3223
diff changeset
76
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3223
diff changeset
77
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3223
diff changeset
78 class RequestHandler(NamedTuple):
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3223
diff changeset
79 callback: Callable[[SatXMPPComponent, UploadRequest], Optional[Slot]]
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3223
diff changeset
80 priority: int
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3223
diff changeset
81
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3223
diff changeset
82
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3223
diff changeset
83 class XEP_0363:
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3223
diff changeset
84 Slot=Slot
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3223
diff changeset
85
1640
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
86 def __init__(self, host):
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
87 log.info(_("plugin HTTP File Upload initialization"))
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
88 self.host = host
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4021
diff changeset
89 host.bridge.add_method(
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4021
diff changeset
90 "file_http_upload",
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
91 ".plugin",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
92 in_sign="sssbs",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
93 out_sign="",
3922
0ff265725489 plugin XEP-0447: handle attachment and download:
Goffi <goffi@goffi.org>
parents: 3526
diff changeset
94 method=self._file_http_upload,
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
95 )
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4021
diff changeset
96 host.bridge.add_method(
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4021
diff changeset
97 "file_http_upload_get_slot",
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
98 ".plugin",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
99 in_sign="sisss",
3293
f5a5aa9fa73a plugin XEP-0363: fixed fileHTTPUploadGetSlot
Goffi <goffi@goffi.org>
parents: 3289
diff changeset
100 out_sign="(ssaa{ss})",
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4021
diff changeset
101 method=self._get_slot,
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2866
diff changeset
102 async_=True,
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
103 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
104 host.plugins["UPLOAD"].register(
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4021
diff changeset
105 "HTTP Upload", self.get_http_upload_entity, self.file_http_upload
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
106 )
3289
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3223
diff changeset
107 # list of callbacks used when a request is done to a component
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3223
diff changeset
108 self.handlers = []
3294
6505e5cc6ab9 plugin XEP-0363: register namespace
Goffi <goffi@goffi.org>
parents: 3293
diff changeset
109 # XXX: there is not yet official short name, so we use "http_upload"
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4021
diff changeset
110 host.register_namespace("http_upload", NS_HTTP_UPLOAD)
1640
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
111
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4021
diff changeset
112 def get_handler(self, client):
3289
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3223
diff changeset
113 return XEP_0363_handler(self)
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3223
diff changeset
114
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4021
diff changeset
115 def register_handler(self, callback, priority=0):
3289
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3223
diff changeset
116 """Register a request handler
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3223
diff changeset
117
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3223
diff changeset
118 @param callack: method to call when a request is done
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3223
diff changeset
119 the callback must return a Slot if the request is handled,
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3223
diff changeset
120 otherwise, other callbacks will be tried.
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3223
diff changeset
121 If the callback raises a StanzaError, its condition will be used if no other
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3223
diff changeset
122 callback can handle the request.
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3223
diff changeset
123 @param priority: handlers with higher priorities will be called first
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3223
diff changeset
124 """
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3223
diff changeset
125 assert callback not in self.handlers
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3223
diff changeset
126 req_handler = RequestHandler(callback, priority)
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3223
diff changeset
127 self.handlers.append(req_handler)
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3223
diff changeset
128 self.handlers.sort(key=lambda handler: handler.priority, reverse=True)
1640
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
129
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4021
diff changeset
130 def get_file_too_large_elt(self, max_size: int) -> domish.Element:
3526
e84ffb48acd4 plugin XEP-0363: allow async callbacks in handlers + method to generate `file-too-large` element
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
131 """Generate <file-too-large> app condition for errors"""
e84ffb48acd4 plugin XEP-0363: allow async callbacks in handlers + method to generate `file-too-large` element
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
132 file_too_large_elt = domish.Element((NS_HTTP_UPLOAD, "file-too-large"))
e84ffb48acd4 plugin XEP-0363: allow async callbacks in handlers + method to generate `file-too-large` element
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
133 file_too_large_elt.addElement("max-file-size", str(max_size))
e84ffb48acd4 plugin XEP-0363: allow async callbacks in handlers + method to generate `file-too-large` element
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
134 return file_too_large_elt
e84ffb48acd4 plugin XEP-0363: allow async callbacks in handlers + method to generate `file-too-large` element
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
135
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4021
diff changeset
136 async def get_http_upload_entity(self, client, upload_jid=None):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
137 """Get HTTP upload capable entity
1640
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
138
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
139 upload_jid is checked, then its components
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
140 @param upload_jid(None, jid.JID): entity to check
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
141 @return(D(jid.JID)): first HTTP upload capable entity
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
142 @raise exceptions.NotFound: no entity found
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
143 """
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
144 try:
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
145 entity = client.http_upload_service
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
146 except AttributeError:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4021
diff changeset
147 found_entities = await self.host.find_features_set(client, (NS_HTTP_UPLOAD,))
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
148 try:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2866
diff changeset
149 entity = client.http_upload_service = next(iter(found_entities))
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
150 except StopIteration:
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
151 entity = client.http_upload_service = None
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
152
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
153 if entity is None:
3089
e75024e41f81 plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
154 raise exceptions.NotFound("No HTTP upload entity found")
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
155
3089
e75024e41f81 plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
156 return entity
1640
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
157
3922
0ff265725489 plugin XEP-0447: handle attachment and download:
Goffi <goffi@goffi.org>
parents: 3526
diff changeset
158 def _file_http_upload(self, filepath, filename="", upload_jid="",
2866
8ce5748bfe97 plugin XEP-0363: updated to namespace "urn:xmpp:http:upload:0", handle headers
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
159 ignore_tls_errors=False, profile=C.PROF_KEY_NONE):
1640
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
160 assert os.path.isabs(filepath) and os.path.isfile(filepath)
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4021
diff changeset
161 client = self.host.get_client(profile)
3922
0ff265725489 plugin XEP-0447: handle attachment and download:
Goffi <goffi@goffi.org>
parents: 3526
diff changeset
162 return defer.ensureDeferred(self.file_http_upload(
3089
e75024e41f81 plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
163 client,
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
164 filepath,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
165 filename or None,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
166 jid.JID(upload_jid) if upload_jid else None,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
167 {"ignore_tls_errors": ignore_tls_errors},
3089
e75024e41f81 plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
168 ))
1640
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
169
3922
0ff265725489 plugin XEP-0447: handle attachment and download:
Goffi <goffi@goffi.org>
parents: 3526
diff changeset
170 async def file_http_upload(
0ff265725489 plugin XEP-0447: handle attachment and download:
Goffi <goffi@goffi.org>
parents: 3526
diff changeset
171 self,
0ff265725489 plugin XEP-0447: handle attachment and download:
Goffi <goffi@goffi.org>
parents: 3526
diff changeset
172 client: SatXMPPEntity,
0ff265725489 plugin XEP-0447: handle attachment and download:
Goffi <goffi@goffi.org>
parents: 3526
diff changeset
173 filepath: Path,
0ff265725489 plugin XEP-0447: handle attachment and download:
Goffi <goffi@goffi.org>
parents: 3526
diff changeset
174 filename: Optional[str] = None,
0ff265725489 plugin XEP-0447: handle attachment and download:
Goffi <goffi@goffi.org>
parents: 3526
diff changeset
175 upload_jid: Optional[jid.JID] = None,
0ff265725489 plugin XEP-0447: handle attachment and download:
Goffi <goffi@goffi.org>
parents: 3526
diff changeset
176 extra: Optional[dict] = None
0ff265725489 plugin XEP-0447: handle attachment and download:
Goffi <goffi@goffi.org>
parents: 3526
diff changeset
177 ) -> Tuple[str, defer.Deferred]:
2866
8ce5748bfe97 plugin XEP-0363: updated to namespace "urn:xmpp:http:upload:0", handle headers
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
178 """Upload a file through HTTP
1640
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
179
3922
0ff265725489 plugin XEP-0447: handle attachment and download:
Goffi <goffi@goffi.org>
parents: 3526
diff changeset
180 @param filepath: absolute path of the file
0ff265725489 plugin XEP-0447: handle attachment and download:
Goffi <goffi@goffi.org>
parents: 3526
diff changeset
181 @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
182 None to use basename of the path
3922
0ff265725489 plugin XEP-0447: handle attachment and download:
Goffi <goffi@goffi.org>
parents: 3526
diff changeset
183 @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
184 or None to use autodetected, if possible
3922
0ff265725489 plugin XEP-0447: handle attachment and download:
Goffi <goffi@goffi.org>
parents: 3526
diff changeset
185 @param extra: options where key can be:
1640
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
186 - ignore_tls_errors(bool): if True, SSL certificate will not be checked
3922
0ff265725489 plugin XEP-0447: handle attachment and download:
Goffi <goffi@goffi.org>
parents: 3526
diff changeset
187 - attachment(dict): file attachment data
1640
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
188 @param profile: %(doc_profile)s
3922
0ff265725489 plugin XEP-0447: handle attachment and download:
Goffi <goffi@goffi.org>
parents: 3526
diff changeset
189 @return: progress id and Deferred which fire download URL
1640
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
190 """
3922
0ff265725489 plugin XEP-0447: handle attachment and download:
Goffi <goffi@goffi.org>
parents: 3526
diff changeset
191 if extra is None:
0ff265725489 plugin XEP-0447: handle attachment and download:
Goffi <goffi@goffi.org>
parents: 3526
diff changeset
192 extra = {}
0ff265725489 plugin XEP-0447: handle attachment and download:
Goffi <goffi@goffi.org>
parents: 3526
diff changeset
193 ignore_tls_errors = extra.get("ignore_tls_errors", False)
0ff265725489 plugin XEP-0447: handle attachment and download:
Goffi <goffi@goffi.org>
parents: 3526
diff changeset
194 file_metadata = {
0ff265725489 plugin XEP-0447: handle attachment and download:
Goffi <goffi@goffi.org>
parents: 3526
diff changeset
195 "filename": filename or os.path.basename(filepath),
0ff265725489 plugin XEP-0447: handle attachment and download:
Goffi <goffi@goffi.org>
parents: 3526
diff changeset
196 "filepath": filepath,
0ff265725489 plugin XEP-0447: handle attachment and download:
Goffi <goffi@goffi.org>
parents: 3526
diff changeset
197 "size": os.path.getsize(filepath),
0ff265725489 plugin XEP-0447: handle attachment and download:
Goffi <goffi@goffi.org>
parents: 3526
diff changeset
198 }
3089
e75024e41f81 plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
199
3922
0ff265725489 plugin XEP-0447: handle attachment and download:
Goffi <goffi@goffi.org>
parents: 3526
diff changeset
200 #: this trigger can be used to modify the filename or size requested when geting
0ff265725489 plugin XEP-0447: handle attachment and download:
Goffi <goffi@goffi.org>
parents: 3526
diff changeset
201 #: the slot, it is notably useful with encryption.
3089
e75024e41f81 plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
202 self.host.trigger.point(
3922
0ff265725489 plugin XEP-0447: handle attachment and download:
Goffi <goffi@goffi.org>
parents: 3526
diff changeset
203 "XEP-0363_upload_pre_slot", client, extra, file_metadata,
0ff265725489 plugin XEP-0447: handle attachment and download:
Goffi <goffi@goffi.org>
parents: 3526
diff changeset
204 triggers_no_cancel=True
0ff265725489 plugin XEP-0447: handle attachment and download:
Goffi <goffi@goffi.org>
parents: 3526
diff changeset
205 )
3089
e75024e41f81 plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
206 try:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4021
diff changeset
207 slot = await self.get_slot(
3922
0ff265725489 plugin XEP-0447: handle attachment and download:
Goffi <goffi@goffi.org>
parents: 3526
diff changeset
208 client, file_metadata["filename"], file_metadata["size"],
0ff265725489 plugin XEP-0447: handle attachment and download:
Goffi <goffi@goffi.org>
parents: 3526
diff changeset
209 upload_jid=upload_jid
0ff265725489 plugin XEP-0447: handle attachment and download:
Goffi <goffi@goffi.org>
parents: 3526
diff changeset
210 )
3089
e75024e41f81 plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
211 except Exception as e:
e75024e41f81 plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
212 log.warning(_("Can't get upload slot: {reason}").format(reason=e))
e75024e41f81 plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
213 raise e
e75024e41f81 plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
214 else:
e75024e41f81 plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
215 log.debug(f"Got upload slot: {slot}")
e75024e41f81 plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
216 sat_file = self.host.plugins["FILE"].File(
3922
0ff265725489 plugin XEP-0447: handle attachment and download:
Goffi <goffi@goffi.org>
parents: 3526
diff changeset
217 self.host, client, filepath, uid=extra.get("progress_id"),
0ff265725489 plugin XEP-0447: handle attachment and download:
Goffi <goffi@goffi.org>
parents: 3526
diff changeset
218 size=file_metadata["size"],
3182
f2bb57348587 plugin attach, XEP-0363: progress id can now be specified:
Goffi <goffi@goffi.org>
parents: 3155
diff changeset
219 auto_end_signals=False
3089
e75024e41f81 plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
220 )
e75024e41f81 plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
221 progress_id = sat_file.uid
1640
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
222
3089
e75024e41f81 plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
223 file_producer = http_client.FileBodyProducer(sat_file)
e75024e41f81 plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
224
e75024e41f81 plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
225 if ignore_tls_errors:
e75024e41f81 plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
226 agent = http_client.Agent(reactor, sat_web.NoCheckContextFactory())
e75024e41f81 plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
227 else:
e75024e41f81 plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
228 agent = http_client.Agent(reactor)
1640
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
229
3089
e75024e41f81 plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
230 headers = {"User-Agent": [C.APP_NAME.encode("utf-8")]}
e75024e41f81 plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
231
e75024e41f81 plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
232 for name, value in slot.headers:
e75024e41f81 plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
233 name = name.encode('utf-8')
e75024e41f81 plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
234 value = value.encode('utf-8')
4136
72b95cdc3432 plugin XEP-0363: fix headers setting
Goffi <goffi@goffi.org>
parents: 4098
diff changeset
235 headers[name] = [value]
3089
e75024e41f81 plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
236
e75024e41f81 plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
237
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4021
diff changeset
238 await self.host.trigger.async_point(
3922
0ff265725489 plugin XEP-0447: handle attachment and download:
Goffi <goffi@goffi.org>
parents: 3526
diff changeset
239 "XEP-0363_upload", client, extra, sat_file, file_producer, slot,
3089
e75024e41f81 plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
240 triggers_no_cancel=True)
2866
8ce5748bfe97 plugin XEP-0363: updated to namespace "urn:xmpp:http:upload:0", handle headers
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
241
3089
e75024e41f81 plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
242 download_d = agent.request(
e75024e41f81 plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
243 b"PUT",
e75024e41f81 plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
244 slot.put.encode("utf-8"),
e75024e41f81 plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
245 http_headers.Headers(headers),
e75024e41f81 plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
246 file_producer,
e75024e41f81 plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
247 )
e75024e41f81 plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
248 download_d.addCallbacks(
3922
0ff265725489 plugin XEP-0447: handle attachment and download:
Goffi <goffi@goffi.org>
parents: 3526
diff changeset
249 self._upload_cb,
0ff265725489 plugin XEP-0447: handle attachment and download:
Goffi <goffi@goffi.org>
parents: 3526
diff changeset
250 self._upload_eb,
3089
e75024e41f81 plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
251 (sat_file, slot),
e75024e41f81 plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
252 None,
3155
3a8755fdc78d plugin XEP-0363: fixed errback args in fileHTTPUpload
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
253 (sat_file,),
3089
e75024e41f81 plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
254 )
2866
8ce5748bfe97 plugin XEP-0363: updated to namespace "urn:xmpp:http:upload:0", handle headers
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
255
3089
e75024e41f81 plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
256 return progress_id, download_d
1640
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
257
3922
0ff265725489 plugin XEP-0447: handle attachment and download:
Goffi <goffi@goffi.org>
parents: 3526
diff changeset
258 def _upload_cb(self, __, sat_file, slot):
1640
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
259 """Called once file is successfully uploaded
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
260
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
261 @param sat_file(SatFile): file used for the upload
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4021
diff changeset
262 should be closed, but it is needed to send the progress_finished signal
1640
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
263 @param slot(Slot): put/get urls
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
264 """
3223
163014f09bf4 plugin attach: handle large images resizing:
Goffi <goffi@goffi.org>
parents: 3219
diff changeset
265 log.info(f"HTTP upload finished ({slot.get})")
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4021
diff changeset
266 sat_file.progress_finished({"url": slot.get})
3089
e75024e41f81 plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
267 return slot.get
1640
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
268
3922
0ff265725489 plugin XEP-0447: handle attachment and download:
Goffi <goffi@goffi.org>
parents: 3526
diff changeset
269 def _upload_eb(self, failure_, sat_file):
1640
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
270 """Called on unsuccessful upload
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
271
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
272 @param sat_file(SatFile): file used for the upload
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4021
diff changeset
273 should be closed, be is needed to send the progress_error signal
1640
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
274 """
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
275 try:
3089
e75024e41f81 plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
276 wrapped_fail = failure_.value.reasons[0]
2866
8ce5748bfe97 plugin XEP-0363: updated to namespace "urn:xmpp:http:upload:0", handle headers
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
277 except (AttributeError, IndexError) as e:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2866
diff changeset
278 log.warning(_("upload failed: {reason}").format(reason=e))
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4021
diff changeset
279 sat_file.progress_error(str(failure_))
1640
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
280 else:
3089
e75024e41f81 plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
281 if wrapped_fail.check(sat_web.SSLError):
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2866
diff changeset
282 msg = "TLS validation error, can't connect to HTTPS server"
2866
8ce5748bfe97 plugin XEP-0363: updated to namespace "urn:xmpp:http:upload:0", handle headers
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
283 else:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2866
diff changeset
284 msg = "can't upload file"
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2866
diff changeset
285 log.warning(msg + ": " + str(wrapped_fail.value))
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4021
diff changeset
286 sat_file.progress_error(msg)
3089
e75024e41f81 plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
287 raise failure_
1640
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
288
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4021
diff changeset
289 def _get_slot(self, filename, size, content_type, upload_jid,
2866
8ce5748bfe97 plugin XEP-0363: updated to namespace "urn:xmpp:http:upload:0", handle headers
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
290 profile_key=C.PROF_KEY_NONE):
8ce5748bfe97 plugin XEP-0363: updated to namespace "urn:xmpp:http:upload:0", handle headers
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
291 """Get an upload slot
1640
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
292
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
293 This method can be used when uploading is done by the frontend
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
294 @param filename(unicode): name of the file to upload
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
295 @param size(int): size of the file (must be non null)
3293
f5a5aa9fa73a plugin XEP-0363: fixed fileHTTPUploadGetSlot
Goffi <goffi@goffi.org>
parents: 3289
diff changeset
296 @param upload_jid(str, ''): HTTP upload capable entity
1640
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
297 @param content_type(unicode, None): MIME type of the content
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
298 empty string or None to guess automatically
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
299 """
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4021
diff changeset
300 client = self.host.get_client(profile_key)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
301 filename = filename.replace("/", "_")
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4021
diff changeset
302 d = defer.ensureDeferred(self.get_slot(
4098
4a8b29ab34c0 plugin XEP-0363: fix handling of empty `upload_jid` in `_get_slot`
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
303 client, filename, size, content_type or None,
4a8b29ab34c0 plugin XEP-0363: fix handling of empty `upload_jid` in `_get_slot`
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
304 jid.JID(upload_jid) if upload_jid else None
3089
e75024e41f81 plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
305 ))
3293
f5a5aa9fa73a plugin XEP-0363: fixed fileHTTPUploadGetSlot
Goffi <goffi@goffi.org>
parents: 3289
diff changeset
306 d.addCallback(lambda slot: (slot.get, slot.put, slot.headers))
f5a5aa9fa73a plugin XEP-0363: fixed fileHTTPUploadGetSlot
Goffi <goffi@goffi.org>
parents: 3289
diff changeset
307 return d
1640
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
308
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4021
diff changeset
309 async def get_slot(self, client, filename, size, content_type=None, upload_jid=None):
1640
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
310 """Get a slot (i.e. download/upload links)
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
311
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
312 @param filename(unicode): name to use for the upload
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
313 @param size(int): size of the file to upload (must be >0)
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
314 @param content_type(None, unicode): MIME type of the content
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
315 None to autodetect
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
316 @param upload_jid(jid.JID, None): HTTP upload capable upload_jid
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
317 or None to use the server component (if any)
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
318 @param client: %(doc_client)s
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
319 @return (Slot): the upload (put) and download (get) URLs
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
320 @raise exceptions.NotFound: no HTTP upload capable upload_jid has been found
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
321 """
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
322 assert filename and size
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
323 if content_type is None:
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
324 # TODO: manage python magic for file guessing (in a dedicated plugin ?)
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
325 content_type = mimetypes.guess_type(filename, strict=False)[0]
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
326
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
327 if upload_jid is None:
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
328 try:
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
329 upload_jid = client.http_upload_service
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
330 except AttributeError:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4021
diff changeset
331 found_entity = await self.get_http_upload_entity(client)
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4021
diff changeset
332 return await self.get_slot(
3089
e75024e41f81 plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
333 client, filename, size, content_type, found_entity)
1640
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
334 else:
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
335 if upload_jid is None:
3089
e75024e41f81 plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
336 raise exceptions.NotFound("No HTTP upload entity found")
1640
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
337
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
338 iq_elt = client.IQ("get")
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
339 iq_elt["to"] = upload_jid.full()
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
340 request_elt = iq_elt.addElement((NS_HTTP_UPLOAD, "request"))
2866
8ce5748bfe97 plugin XEP-0363: updated to namespace "urn:xmpp:http:upload:0", handle headers
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
341 request_elt["filename"] = filename
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2866
diff changeset
342 request_elt["size"] = str(size)
1640
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
343 if content_type is not None:
2866
8ce5748bfe97 plugin XEP-0363: updated to namespace "urn:xmpp:http:upload:0", handle headers
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
344 request_elt["content-type"] = content_type
1640
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
345
3089
e75024e41f81 plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
346 iq_result_elt = await iq_elt.send()
e75024e41f81 plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
347
e75024e41f81 plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
348 try:
e75024e41f81 plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
349 slot_elt = next(iq_result_elt.elements(NS_HTTP_UPLOAD, "slot"))
e75024e41f81 plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
350 put_elt = next(slot_elt.elements(NS_HTTP_UPLOAD, "put"))
e75024e41f81 plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
351 put_url = put_elt['url']
e75024e41f81 plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
352 get_elt = next(slot_elt.elements(NS_HTTP_UPLOAD, "get"))
e75024e41f81 plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
353 get_url = get_elt['url']
e75024e41f81 plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
354 except (StopIteration, KeyError):
e75024e41f81 plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
355 raise exceptions.DataError("Incorrect stanza received from server")
1640
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
356
3089
e75024e41f81 plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
357 headers = []
e75024e41f81 plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
358 for header_elt in put_elt.elements(NS_HTTP_UPLOAD, "header"):
e75024e41f81 plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
359 try:
e75024e41f81 plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
360 name = header_elt["name"]
e75024e41f81 plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
361 value = str(header_elt)
e75024e41f81 plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
362 except KeyError:
e75024e41f81 plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
363 log.warning(_("Invalid header element: {xml}").format(
e75024e41f81 plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
364 iq_result_elt.toXml()))
e75024e41f81 plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
365 continue
e75024e41f81 plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
366 name = name.replace('\n', '')
e75024e41f81 plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
367 value = value.replace('\n', '')
e75024e41f81 plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
368 if name.lower() not in ALLOWED_HEADERS:
e75024e41f81 plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
369 log.warning(_('Ignoring unauthorised header "{name}": {xml}')
e75024e41f81 plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
370 .format(name=name, xml = iq_result_elt.toXml()))
e75024e41f81 plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
371 continue
e75024e41f81 plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
372 headers.append((name, value))
e75024e41f81 plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
373
e75024e41f81 plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
374 return Slot(put=put_url, get=get_url, headers=headers)
1640
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
375
3289
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3223
diff changeset
376 # component
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3223
diff changeset
377
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4021
diff changeset
378 def on_component_request(self, iq_elt, client):
3289
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3223
diff changeset
379 iq_elt.handled=True
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4021
diff changeset
380 defer.ensureDeferred(self.handle_component_request(client, iq_elt))
3526
e84ffb48acd4 plugin XEP-0363: allow async callbacks in handlers + method to generate `file-too-large` element
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
381
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4021
diff changeset
382 async def handle_component_request(self, client, iq_elt):
3289
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3223
diff changeset
383 try:
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3223
diff changeset
384 request_elt = next(iq_elt.elements(NS_HTTP_UPLOAD, "request"))
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3223
diff changeset
385 request = UploadRequest(
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3223
diff changeset
386 from_=jid.JID(iq_elt['from']),
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3223
diff changeset
387 filename=parse.quote(request_elt['filename'].replace('/', '_'), safe=''),
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3223
diff changeset
388 size=int(request_elt['size']),
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3223
diff changeset
389 content_type=request_elt.getAttribute('content-type')
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3223
diff changeset
390 )
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3223
diff changeset
391 except (StopIteration, KeyError, ValueError):
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3223
diff changeset
392 client.sendError(iq_elt, "bad-request")
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3223
diff changeset
393 return
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3223
diff changeset
394
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3223
diff changeset
395 err = None
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3223
diff changeset
396
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3223
diff changeset
397 for handler in self.handlers:
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3223
diff changeset
398 try:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4021
diff changeset
399 slot = await utils.as_deferred(handler.callback, client, request)
3289
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3223
diff changeset
400 except error.StanzaError as e:
3399
506fa3d91d3a plugin XEP-0363: fixed invalid reference to `slot` in catched exception:
Goffi <goffi@goffi.org>
parents: 3294
diff changeset
401 log.warning(
506fa3d91d3a plugin XEP-0363: fixed invalid reference to `slot` in catched exception:
Goffi <goffi@goffi.org>
parents: 3294
diff changeset
402 "a stanza error has been raised while processing HTTP Upload of "
506fa3d91d3a plugin XEP-0363: fixed invalid reference to `slot` in catched exception:
Goffi <goffi@goffi.org>
parents: 3294
diff changeset
403 f"request: {e}"
506fa3d91d3a plugin XEP-0363: fixed invalid reference to `slot` in catched exception:
Goffi <goffi@goffi.org>
parents: 3294
diff changeset
404 )
3289
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3223
diff changeset
405 if err is None:
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3223
diff changeset
406 # we keep the first error to return its condition later,
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3223
diff changeset
407 # if no other callback handle the request
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3223
diff changeset
408 err = e
3399
506fa3d91d3a plugin XEP-0363: fixed invalid reference to `slot` in catched exception:
Goffi <goffi@goffi.org>
parents: 3294
diff changeset
409 else:
506fa3d91d3a plugin XEP-0363: fixed invalid reference to `slot` in catched exception:
Goffi <goffi@goffi.org>
parents: 3294
diff changeset
410 if slot:
506fa3d91d3a plugin XEP-0363: fixed invalid reference to `slot` in catched exception:
Goffi <goffi@goffi.org>
parents: 3294
diff changeset
411 break
3289
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3223
diff changeset
412 else:
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3223
diff changeset
413 log.warning(
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3223
diff changeset
414 _("no service can handle HTTP Upload request: {elt}")
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3223
diff changeset
415 .format(elt=iq_elt.toXml()))
3526
e84ffb48acd4 plugin XEP-0363: allow async callbacks in handlers + method to generate `file-too-large` element
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
416 if err is None:
e84ffb48acd4 plugin XEP-0363: allow async callbacks in handlers + method to generate `file-too-large` element
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
417 err = error.StanzaError("feature-not-implemented")
e84ffb48acd4 plugin XEP-0363: allow async callbacks in handlers + method to generate `file-too-large` element
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
418 client.send(err.toResponse(iq_elt))
3289
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3223
diff changeset
419 return
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3223
diff changeset
420
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3223
diff changeset
421 iq_result_elt = xmlstream.toResponse(iq_elt, "result")
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3223
diff changeset
422 slot_elt = iq_result_elt.addElement((NS_HTTP_UPLOAD, 'slot'))
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3223
diff changeset
423 put_elt = slot_elt.addElement('put')
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3223
diff changeset
424 put_elt['url'] = slot.put
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3223
diff changeset
425 get_elt = slot_elt.addElement('get')
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3223
diff changeset
426 get_elt['url'] = slot.get
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3223
diff changeset
427 client.send(iq_result_elt)
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3223
diff changeset
428
1640
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
429
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2866
diff changeset
430 @implementer(iwokkel.IDisco)
3289
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3223
diff changeset
431 class XEP_0363_handler(xmlstream.XMPPHandler):
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3223
diff changeset
432
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3223
diff changeset
433 def __init__(self, plugin_parent):
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3223
diff changeset
434 self.plugin_parent = plugin_parent
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3223
diff changeset
435
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3223
diff changeset
436 def connectionInitialized(self):
4021
412b99c29d83 core (xmpp), component file sharing, plugin XEP-0363: `enabled_features` + HTTP Upload:
Goffi <goffi@goffi.org>
parents: 3922
diff changeset
437 if ((self.parent.is_component
412b99c29d83 core (xmpp), component file sharing, plugin XEP-0363: `enabled_features` + HTTP Upload:
Goffi <goffi@goffi.org>
parents: 3922
diff changeset
438 and PLUGIN_INFO[C.PI_IMPORT_NAME] in self.parent.enabled_features)):
3289
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3223
diff changeset
439 self.xmlstream.addObserver(
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4021
diff changeset
440 IQ_HTTP_UPLOAD_REQUEST, self.plugin_parent.on_component_request,
3289
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3223
diff changeset
441 client=self.parent
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3223
diff changeset
442 )
1640
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
443
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
444 def getDiscoInfo(self, requestor, target, nodeIdentifier=""):
4021
412b99c29d83 core (xmpp), component file sharing, plugin XEP-0363: `enabled_features` + HTTP Upload:
Goffi <goffi@goffi.org>
parents: 3922
diff changeset
445 if ((self.parent.is_component
412b99c29d83 core (xmpp), component file sharing, plugin XEP-0363: `enabled_features` + HTTP Upload:
Goffi <goffi@goffi.org>
parents: 3922
diff changeset
446 and not PLUGIN_INFO[C.PI_IMPORT_NAME] in self.parent.enabled_features)):
412b99c29d83 core (xmpp), component file sharing, plugin XEP-0363: `enabled_features` + HTTP Upload:
Goffi <goffi@goffi.org>
parents: 3922
diff changeset
447 return []
412b99c29d83 core (xmpp), component file sharing, plugin XEP-0363: `enabled_features` + HTTP Upload:
Goffi <goffi@goffi.org>
parents: 3922
diff changeset
448 else:
412b99c29d83 core (xmpp), component file sharing, plugin XEP-0363: `enabled_features` + HTTP Upload:
Goffi <goffi@goffi.org>
parents: 3922
diff changeset
449 return [disco.DiscoFeature(NS_HTTP_UPLOAD)]
1640
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
450
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
451 def getDiscoItems(self, requestor, target, nodeIdentifier=""):
1640
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
452 return []