annotate libervia/backend/plugins/plugin_xep_0363.py @ 4278:240d8b7ad906

component Conferences: implementation of SFU component to make multi-party A/V conferences: This component wrap `Galène` SFU and translate its signaling to XMPP Jingle in both direction. rel 445
author Goffi <goffi@goffi.org>
date Fri, 05 Jul 2024 17:18:37 +0200
parents 0d7bb4df2343
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 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 + '"]'
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4136
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"""
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4136
diff changeset
66
3089
e75024e41f81 plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
67 put: str
e75024e41f81 plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
68 get: str
e75024e41f81 plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
69 headers: list
1640
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
70
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
71
3289
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3223
diff changeset
72 class UploadRequest(NamedTuple):
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3223
diff changeset
73 from_: jid.JID
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3223
diff changeset
74 filename: str
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3223
diff changeset
75 size: int
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3223
diff changeset
76 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
77
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3223
diff changeset
78
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3223
diff changeset
79 class RequestHandler(NamedTuple):
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3223
diff changeset
80 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
81 priority: int
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
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3223
diff changeset
84 class XEP_0363:
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4136
diff changeset
85 Slot = Slot
3289
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3223
diff changeset
86
1640
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
87 def __init__(self, host):
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
88 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
89 self.host = host
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4021
diff changeset
90 host.bridge.add_method(
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4021
diff changeset
91 "file_http_upload",
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
92 ".plugin",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
93 in_sign="sssbs",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
94 out_sign="",
3922
0ff265725489 plugin XEP-0447: handle attachment and download:
Goffi <goffi@goffi.org>
parents: 3526
diff changeset
95 method=self._file_http_upload,
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
96 )
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4021
diff changeset
97 host.bridge.add_method(
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4021
diff changeset
98 "file_http_upload_get_slot",
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
99 ".plugin",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
100 in_sign="sisss",
3293
f5a5aa9fa73a plugin XEP-0363: fixed fileHTTPUploadGetSlot
Goffi <goffi@goffi.org>
parents: 3289
diff changeset
101 out_sign="(ssaa{ss})",
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4021
diff changeset
102 method=self._get_slot,
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2866
diff changeset
103 async_=True,
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
104 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
105 host.plugins["UPLOAD"].register(
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4021
diff changeset
106 "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
107 )
3289
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3223
diff changeset
108 # 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
109 self.handlers = []
3294
6505e5cc6ab9 plugin XEP-0363: register namespace
Goffi <goffi@goffi.org>
parents: 3293
diff changeset
110 # 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
111 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
112
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4021
diff changeset
113 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
114 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
115
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4021
diff changeset
116 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
117 """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
118
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3223
diff changeset
119 @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
120 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
121 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
122 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
123 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
124 @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
125 """
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3223
diff changeset
126 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
127 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
128 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
129 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
130
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4021
diff changeset
131 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
132 """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
133 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
134 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
135 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
136
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4021
diff changeset
137 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
138 """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
139
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4136
diff changeset
140 upload_jid is checked, then its components
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4136
diff changeset
141 @param upload_jid(None, jid.JID): entity to check
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4136
diff changeset
142 @return(D(jid.JID)): first HTTP upload capable entity
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4136
diff changeset
143 @raise exceptions.NotFound: no entity found
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4136
diff changeset
144 """
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
145 try:
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
146 entity = client.http_upload_service
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
147 except AttributeError:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4021
diff changeset
148 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
149 try:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2866
diff changeset
150 entity = client.http_upload_service = next(iter(found_entities))
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
151 except StopIteration:
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
152 entity = client.http_upload_service = None
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
153
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
154 if entity is None:
3089
e75024e41f81 plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
155 raise exceptions.NotFound("No HTTP upload entity found")
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
156
3089
e75024e41f81 plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
157 return entity
1640
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
158
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4136
diff changeset
159 def _file_http_upload(
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4136
diff changeset
160 self,
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4136
diff changeset
161 filepath,
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4136
diff changeset
162 filename="",
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4136
diff changeset
163 upload_jid="",
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4136
diff changeset
164 ignore_tls_errors=False,
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4136
diff changeset
165 profile=C.PROF_KEY_NONE,
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4136
diff changeset
166 ):
1640
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
167 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
168 client = self.host.get_client(profile)
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4136
diff changeset
169 return defer.ensureDeferred(
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4136
diff changeset
170 self.file_http_upload(
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4136
diff changeset
171 client,
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4136
diff changeset
172 filepath,
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4136
diff changeset
173 filename or None,
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4136
diff changeset
174 jid.JID(upload_jid) if upload_jid else None,
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4136
diff changeset
175 {"ignore_tls_errors": ignore_tls_errors},
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4136
diff changeset
176 )
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4136
diff changeset
177 )
1640
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
178
3922
0ff265725489 plugin XEP-0447: handle attachment and download:
Goffi <goffi@goffi.org>
parents: 3526
diff changeset
179 async def file_http_upload(
0ff265725489 plugin XEP-0447: handle attachment and download:
Goffi <goffi@goffi.org>
parents: 3526
diff changeset
180 self,
0ff265725489 plugin XEP-0447: handle attachment and download:
Goffi <goffi@goffi.org>
parents: 3526
diff changeset
181 client: SatXMPPEntity,
0ff265725489 plugin XEP-0447: handle attachment and download:
Goffi <goffi@goffi.org>
parents: 3526
diff changeset
182 filepath: Path,
0ff265725489 plugin XEP-0447: handle attachment and download:
Goffi <goffi@goffi.org>
parents: 3526
diff changeset
183 filename: Optional[str] = None,
0ff265725489 plugin XEP-0447: handle attachment and download:
Goffi <goffi@goffi.org>
parents: 3526
diff changeset
184 upload_jid: Optional[jid.JID] = None,
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4136
diff changeset
185 extra: Optional[dict] = None,
3922
0ff265725489 plugin XEP-0447: handle attachment and download:
Goffi <goffi@goffi.org>
parents: 3526
diff changeset
186 ) -> 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
187 """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
188
3922
0ff265725489 plugin XEP-0447: handle attachment and download:
Goffi <goffi@goffi.org>
parents: 3526
diff changeset
189 @param filepath: absolute path of the file
0ff265725489 plugin XEP-0447: handle attachment and download:
Goffi <goffi@goffi.org>
parents: 3526
diff changeset
190 @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
191 None to use basename of the path
3922
0ff265725489 plugin XEP-0447: handle attachment and download:
Goffi <goffi@goffi.org>
parents: 3526
diff changeset
192 @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
193 or None to use autodetected, if possible
3922
0ff265725489 plugin XEP-0447: handle attachment and download:
Goffi <goffi@goffi.org>
parents: 3526
diff changeset
194 @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
195 - 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
196 - 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
197 @param profile: %(doc_profile)s
3922
0ff265725489 plugin XEP-0447: handle attachment and download:
Goffi <goffi@goffi.org>
parents: 3526
diff changeset
198 @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
199 """
3922
0ff265725489 plugin XEP-0447: handle attachment and download:
Goffi <goffi@goffi.org>
parents: 3526
diff changeset
200 if extra is None:
0ff265725489 plugin XEP-0447: handle attachment and download:
Goffi <goffi@goffi.org>
parents: 3526
diff changeset
201 extra = {}
0ff265725489 plugin XEP-0447: handle attachment and download:
Goffi <goffi@goffi.org>
parents: 3526
diff changeset
202 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
203 file_metadata = {
0ff265725489 plugin XEP-0447: handle attachment and download:
Goffi <goffi@goffi.org>
parents: 3526
diff changeset
204 "filename": filename or os.path.basename(filepath),
0ff265725489 plugin XEP-0447: handle attachment and download:
Goffi <goffi@goffi.org>
parents: 3526
diff changeset
205 "filepath": filepath,
0ff265725489 plugin XEP-0447: handle attachment and download:
Goffi <goffi@goffi.org>
parents: 3526
diff changeset
206 "size": os.path.getsize(filepath),
0ff265725489 plugin XEP-0447: handle attachment and download:
Goffi <goffi@goffi.org>
parents: 3526
diff changeset
207 }
3089
e75024e41f81 plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
208
3922
0ff265725489 plugin XEP-0447: handle attachment and download:
Goffi <goffi@goffi.org>
parents: 3526
diff changeset
209 #: 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
210 #: 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
211 self.host.trigger.point(
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4136
diff changeset
212 "XEP-0363_upload_pre_slot",
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4136
diff changeset
213 client,
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4136
diff changeset
214 extra,
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4136
diff changeset
215 file_metadata,
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4136
diff changeset
216 triggers_no_cancel=True,
3922
0ff265725489 plugin XEP-0447: handle attachment and download:
Goffi <goffi@goffi.org>
parents: 3526
diff changeset
217 )
3089
e75024e41f81 plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
218 try:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4021
diff changeset
219 slot = await self.get_slot(
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4136
diff changeset
220 client,
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4136
diff changeset
221 file_metadata["filename"],
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4136
diff changeset
222 file_metadata["size"],
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4136
diff changeset
223 upload_jid=upload_jid,
3922
0ff265725489 plugin XEP-0447: handle attachment and download:
Goffi <goffi@goffi.org>
parents: 3526
diff changeset
224 )
3089
e75024e41f81 plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
225 except Exception as e:
e75024e41f81 plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
226 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
227 raise e
e75024e41f81 plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
228 else:
e75024e41f81 plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
229 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
230 sat_file = self.host.plugins["FILE"].File(
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4136
diff changeset
231 self.host,
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4136
diff changeset
232 client,
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4136
diff changeset
233 filepath,
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4136
diff changeset
234 uid=extra.get("progress_id"),
3922
0ff265725489 plugin XEP-0447: handle attachment and download:
Goffi <goffi@goffi.org>
parents: 3526
diff changeset
235 size=file_metadata["size"],
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4136
diff changeset
236 auto_end_signals=False,
3089
e75024e41f81 plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
237 )
e75024e41f81 plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
238 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
239
3089
e75024e41f81 plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
240 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
241
e75024e41f81 plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
242 if ignore_tls_errors:
e75024e41f81 plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
243 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
244 else:
e75024e41f81 plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
245 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
246
3089
e75024e41f81 plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
247 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
248
e75024e41f81 plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
249 for name, value in slot.headers:
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4136
diff changeset
250 name = name.encode("utf-8")
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4136
diff changeset
251 value = value.encode("utf-8")
4136
72b95cdc3432 plugin XEP-0363: fix headers setting
Goffi <goffi@goffi.org>
parents: 4098
diff changeset
252 headers[name] = [value]
3089
e75024e41f81 plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
253
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4021
diff changeset
254 await self.host.trigger.async_point(
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4136
diff changeset
255 "XEP-0363_upload",
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4136
diff changeset
256 client,
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4136
diff changeset
257 extra,
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4136
diff changeset
258 sat_file,
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4136
diff changeset
259 file_producer,
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4136
diff changeset
260 slot,
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4136
diff changeset
261 triggers_no_cancel=True,
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4136
diff changeset
262 )
2866
8ce5748bfe97 plugin XEP-0363: updated to namespace "urn:xmpp:http:upload:0", handle headers
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
263
3089
e75024e41f81 plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
264 download_d = agent.request(
e75024e41f81 plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
265 b"PUT",
e75024e41f81 plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
266 slot.put.encode("utf-8"),
e75024e41f81 plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
267 http_headers.Headers(headers),
e75024e41f81 plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
268 file_producer,
e75024e41f81 plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
269 )
e75024e41f81 plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
270 download_d.addCallbacks(
3922
0ff265725489 plugin XEP-0447: handle attachment and download:
Goffi <goffi@goffi.org>
parents: 3526
diff changeset
271 self._upload_cb,
0ff265725489 plugin XEP-0447: handle attachment and download:
Goffi <goffi@goffi.org>
parents: 3526
diff changeset
272 self._upload_eb,
3089
e75024e41f81 plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
273 (sat_file, slot),
e75024e41f81 plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
274 None,
3155
3a8755fdc78d plugin XEP-0363: fixed errback args in fileHTTPUpload
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
275 (sat_file,),
3089
e75024e41f81 plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
276 )
2866
8ce5748bfe97 plugin XEP-0363: updated to namespace "urn:xmpp:http:upload:0", handle headers
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
277
3089
e75024e41f81 plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
278 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
279
3922
0ff265725489 plugin XEP-0447: handle attachment and download:
Goffi <goffi@goffi.org>
parents: 3526
diff changeset
280 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
281 """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
282
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
283 @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
284 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
285 @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
286 """
3223
163014f09bf4 plugin attach: handle large images resizing:
Goffi <goffi@goffi.org>
parents: 3219
diff changeset
287 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
288 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
289 return slot.get
1640
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
290
3922
0ff265725489 plugin XEP-0447: handle attachment and download:
Goffi <goffi@goffi.org>
parents: 3526
diff changeset
291 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
292 """Called on unsuccessful upload
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
293
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
294 @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
295 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
296 """
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
297 try:
3089
e75024e41f81 plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
298 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
299 except (AttributeError, IndexError) as e:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2866
diff changeset
300 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
301 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
302 else:
3089
e75024e41f81 plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
303 if wrapped_fail.check(sat_web.SSLError):
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2866
diff changeset
304 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
305 else:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2866
diff changeset
306 msg = "can't upload file"
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2866
diff changeset
307 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
308 sat_file.progress_error(msg)
3089
e75024e41f81 plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
309 raise failure_
1640
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
310
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4136
diff changeset
311 def _get_slot(
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4136
diff changeset
312 self, filename, size, content_type, upload_jid, profile_key=C.PROF_KEY_NONE
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4136
diff changeset
313 ):
2866
8ce5748bfe97 plugin XEP-0363: updated to namespace "urn:xmpp:http:upload:0", handle headers
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
314 """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
315
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
316 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
317 @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
318 @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
319 @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
320 @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
321 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
322 """
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4021
diff changeset
323 client = self.host.get_client(profile_key)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
324 filename = filename.replace("/", "_")
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4136
diff changeset
325 d = defer.ensureDeferred(
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4136
diff changeset
326 self.get_slot(
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4136
diff changeset
327 client,
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4136
diff changeset
328 filename,
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4136
diff changeset
329 size,
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4136
diff changeset
330 content_type or None,
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4136
diff changeset
331 jid.JID(upload_jid) if upload_jid else None,
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4136
diff changeset
332 )
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4136
diff changeset
333 )
3293
f5a5aa9fa73a plugin XEP-0363: fixed fileHTTPUploadGetSlot
Goffi <goffi@goffi.org>
parents: 3289
diff changeset
334 d.addCallback(lambda slot: (slot.get, slot.put, slot.headers))
f5a5aa9fa73a plugin XEP-0363: fixed fileHTTPUploadGetSlot
Goffi <goffi@goffi.org>
parents: 3289
diff changeset
335 return d
1640
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
336
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4021
diff changeset
337 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
338 """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
339
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
340 @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
341 @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
342 @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
343 None to autodetect
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
344 @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
345 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
346 @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
347 @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
348 @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
349 """
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
350 assert filename and size
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
351 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
352 # 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
353 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
354
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
355 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
356 try:
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
357 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
358 except AttributeError:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4021
diff changeset
359 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
360 return await self.get_slot(
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4136
diff changeset
361 client, filename, size, content_type, found_entity
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4136
diff changeset
362 )
1640
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
363 else:
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
364 if upload_jid is None:
3089
e75024e41f81 plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
365 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
366
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
367 iq_elt = client.IQ("get")
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
368 iq_elt["to"] = upload_jid.full()
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
369 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
370 request_elt["filename"] = filename
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2866
diff changeset
371 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
372 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
373 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
374
3089
e75024e41f81 plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
375 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
376
e75024e41f81 plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
377 try:
e75024e41f81 plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
378 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
379 put_elt = next(slot_elt.elements(NS_HTTP_UPLOAD, "put"))
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4136
diff changeset
380 put_url = put_elt["url"]
3089
e75024e41f81 plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
381 get_elt = next(slot_elt.elements(NS_HTTP_UPLOAD, "get"))
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4136
diff changeset
382 get_url = get_elt["url"]
3089
e75024e41f81 plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
383 except (StopIteration, KeyError):
e75024e41f81 plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
384 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
385
3089
e75024e41f81 plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
386 headers = []
e75024e41f81 plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
387 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
388 try:
e75024e41f81 plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
389 name = header_elt["name"]
e75024e41f81 plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
390 value = str(header_elt)
e75024e41f81 plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
391 except KeyError:
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4136
diff changeset
392 log.warning(
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4136
diff changeset
393 _("Invalid header element: {xml}").format(iq_result_elt.toXml())
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4136
diff changeset
394 )
3089
e75024e41f81 plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
395 continue
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4136
diff changeset
396 name = name.replace("\n", "")
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4136
diff changeset
397 value = value.replace("\n", "")
3089
e75024e41f81 plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
398 if name.lower() not in ALLOWED_HEADERS:
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4136
diff changeset
399 log.warning(
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4136
diff changeset
400 _('Ignoring unauthorised header "{name}": {xml}').format(
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4136
diff changeset
401 name=name, xml=iq_result_elt.toXml()
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4136
diff changeset
402 )
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4136
diff changeset
403 )
3089
e75024e41f81 plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
404 continue
e75024e41f81 plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
405 headers.append((name, value))
e75024e41f81 plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
406
e75024e41f81 plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
407 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
408
3289
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3223
diff changeset
409 # component
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3223
diff changeset
410
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4021
diff changeset
411 def on_component_request(self, iq_elt, client):
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4136
diff changeset
412 iq_elt.handled = True
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4021
diff changeset
413 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
414
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4021
diff changeset
415 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
416 try:
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3223
diff changeset
417 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
418 request = UploadRequest(
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4136
diff changeset
419 from_=jid.JID(iq_elt["from"]),
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4136
diff changeset
420 filename=parse.quote(request_elt["filename"].replace("/", "_"), safe=""),
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4136
diff changeset
421 size=int(request_elt["size"]),
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4136
diff changeset
422 content_type=request_elt.getAttribute("content-type"),
3289
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3223
diff changeset
423 )
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3223
diff changeset
424 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
425 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
426 return
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3223
diff changeset
427
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3223
diff changeset
428 err = None
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3223
diff changeset
429
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3223
diff changeset
430 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
431 try:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4021
diff changeset
432 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
433 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
434 log.warning(
506fa3d91d3a plugin XEP-0363: fixed invalid reference to `slot` in catched exception:
Goffi <goffi@goffi.org>
parents: 3294
diff changeset
435 "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
436 f"request: {e}"
506fa3d91d3a plugin XEP-0363: fixed invalid reference to `slot` in catched exception:
Goffi <goffi@goffi.org>
parents: 3294
diff changeset
437 )
3289
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3223
diff changeset
438 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
439 # 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
440 # 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
441 err = e
3399
506fa3d91d3a plugin XEP-0363: fixed invalid reference to `slot` in catched exception:
Goffi <goffi@goffi.org>
parents: 3294
diff changeset
442 else:
506fa3d91d3a plugin XEP-0363: fixed invalid reference to `slot` in catched exception:
Goffi <goffi@goffi.org>
parents: 3294
diff changeset
443 if slot:
506fa3d91d3a plugin XEP-0363: fixed invalid reference to `slot` in catched exception:
Goffi <goffi@goffi.org>
parents: 3294
diff changeset
444 break
3289
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3223
diff changeset
445 else:
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3223
diff changeset
446 log.warning(
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4136
diff changeset
447 _("no service can handle HTTP Upload request: {elt}").format(
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4136
diff changeset
448 elt=iq_elt.toXml()
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4136
diff changeset
449 )
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4136
diff changeset
450 )
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
451 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
452 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
453 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
454 return
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3223
diff changeset
455
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3223
diff changeset
456 iq_result_elt = xmlstream.toResponse(iq_elt, "result")
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4136
diff changeset
457 slot_elt = iq_result_elt.addElement((NS_HTTP_UPLOAD, "slot"))
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4136
diff changeset
458 put_elt = slot_elt.addElement("put")
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4136
diff changeset
459 put_elt["url"] = slot.put
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4136
diff changeset
460 get_elt = slot_elt.addElement("get")
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4136
diff changeset
461 get_elt["url"] = slot.get
3289
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3223
diff changeset
462 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
463
1640
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
464
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2866
diff changeset
465 @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
466 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
467
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3223
diff changeset
468 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
469 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
470
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3223
diff changeset
471 def connectionInitialized(self):
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4136
diff changeset
472 if (
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4136
diff changeset
473 self.parent.is_component
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4136
diff changeset
474 and PLUGIN_INFO[C.PI_IMPORT_NAME] in self.parent.enabled_features
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4136
diff changeset
475 ):
3289
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3223
diff changeset
476 self.xmlstream.addObserver(
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4136
diff changeset
477 IQ_HTTP_UPLOAD_REQUEST,
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4136
diff changeset
478 self.plugin_parent.on_component_request,
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4136
diff changeset
479 client=self.parent,
3289
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3223
diff changeset
480 )
1640
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
481
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
482 def getDiscoInfo(self, requestor, target, nodeIdentifier=""):
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4136
diff changeset
483 if (
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4136
diff changeset
484 self.parent.is_component
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4136
diff changeset
485 and not PLUGIN_INFO[C.PI_IMPORT_NAME] in self.parent.enabled_features
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4136
diff changeset
486 ):
4021
412b99c29d83 core (xmpp), component file sharing, plugin XEP-0363: `enabled_features` + HTTP Upload:
Goffi <goffi@goffi.org>
parents: 3922
diff changeset
487 return []
412b99c29d83 core (xmpp), component file sharing, plugin XEP-0363: `enabled_features` + HTTP Upload:
Goffi <goffi@goffi.org>
parents: 3922
diff changeset
488 else:
412b99c29d83 core (xmpp), component file sharing, plugin XEP-0363: `enabled_features` + HTTP Upload:
Goffi <goffi@goffi.org>
parents: 3922
diff changeset
489 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
490
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
491 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
492 return []