Mercurial > libervia-backend
annotate sat/plugins/plugin_xep_0363.py @ 4009:48e8b3dba793
component AP gateway (http/outbox): return data when no RSM is available
author | Goffi <goffi@goffi.org> |
---|---|
date | Thu, 16 Mar 2023 15:56:11 +0100 |
parents | 0ff265725489 |
children | 412b99c29d83 |
rev | line source |
---|---|
3028 | 1 #!/usr/bin/env python3 |
3137 | 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 | 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 |
0ff265725489
plugin XEP-0447: handle attachment and download:
Goffi <goffi@goffi.org>
parents:
3526
diff
changeset
|
35 from sat.core import exceptions |
3089
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
36 from sat.core.constants import Const as C |
3922
0ff265725489
plugin XEP-0447: handle attachment and download:
Goffi <goffi@goffi.org>
parents:
3526
diff
changeset
|
37 from sat.core.core_types import SatXMPPEntity |
0ff265725489
plugin XEP-0447: handle attachment and download:
Goffi <goffi@goffi.org>
parents:
3526
diff
changeset
|
38 from sat.core.i18n import _ |
3089
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
39 from sat.core.log import getLogger |
3922
0ff265725489
plugin XEP-0447: handle attachment and download:
Goffi <goffi@goffi.org>
parents:
3526
diff
changeset
|
40 from sat.core.xmpp import SatXMPPComponent |
0ff265725489
plugin XEP-0447: handle attachment and download:
Goffi <goffi@goffi.org>
parents:
3526
diff
changeset
|
41 from sat.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 | 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 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
89 host.bridge.addMethod( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
90 "fileHTTPUpload", |
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 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
96 host.bridge.addMethod( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
97 "fileHTTPUploadGetSlot", |
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})", |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
101 method=self._getSlot, |
3028 | 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( |
3922
0ff265725489
plugin XEP-0447: handle attachment and download:
Goffi <goffi@goffi.org>
parents:
3526
diff
changeset
|
105 "HTTP Upload", self.getHTTPUploadEntity, 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" |
6505e5cc6ab9
plugin XEP-0363: register namespace
Goffi <goffi@goffi.org>
parents:
3293
diff
changeset
|
110 host.registerNamespace("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 |
2144
1d3f73e065e1
core, jp: component handling + client handling refactoring:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
112 def getHandler(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 |
9057713ab124
plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents:
3223
diff
changeset
|
115 def registerHandler(self, callback, priority=0): |
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 |
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
|
130 def getFileTooLargeElt(self, max_size: int) -> domish.Element: |
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 |
3089
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
136 async def getHTTPUploadEntity(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: |
3089
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
147 found_entities = await self.host.findFeaturesSet(client, (NS_HTTP_UPLOAD,)) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
148 try: |
3028 | 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) |
3089
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
161 client = self.host.getClient(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: |
3922
0ff265725489
plugin XEP-0447: handle attachment and download:
Goffi <goffi@goffi.org>
parents:
3526
diff
changeset
|
207 slot = await self.getSlot( |
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') |
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
235 headers[name] = value |
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 |
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
238 await self.host.trigger.asyncPoint( |
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 |
3089
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
262 should be closed, but it is needed to send the progressFinished 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})") |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
266 sat_file.progressFinished({"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 |
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
273 should be closed, be is needed to send the progressError signal |
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 | 278 log.warning(_("upload failed: {reason}").format(reason=e)) |
3089
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
279 sat_file.progressError(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 | 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 | 284 msg = "can't upload file" |
285 log.warning(msg + ": " + str(wrapped_fail.value)) | |
2866
8ce5748bfe97
plugin XEP-0363: updated to namespace "urn:xmpp:http:upload:0", handle headers
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
286 sat_file.progressError(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 |
2866
8ce5748bfe97
plugin XEP-0363: updated to namespace "urn:xmpp:http:upload:0", handle headers
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
289 def _getSlot(self, filename, size, content_type, upload_jid, |
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 """ |
3089
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
300 client = self.host.getClient(profile_key) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
301 filename = filename.replace("/", "_") |
3293
f5a5aa9fa73a
plugin XEP-0363: fixed fileHTTPUploadGetSlot
Goffi <goffi@goffi.org>
parents:
3289
diff
changeset
|
302 d = defer.ensureDeferred(self.getSlot( |
f5a5aa9fa73a
plugin XEP-0363: fixed fileHTTPUploadGetSlot
Goffi <goffi@goffi.org>
parents:
3289
diff
changeset
|
303 client, filename, size, content_type or None, jid.JID(upload_jid) or None |
3089
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
304 )) |
3293
f5a5aa9fa73a
plugin XEP-0363: fixed fileHTTPUploadGetSlot
Goffi <goffi@goffi.org>
parents:
3289
diff
changeset
|
305 d.addCallback(lambda slot: (slot.get, slot.put, slot.headers)) |
f5a5aa9fa73a
plugin XEP-0363: fixed fileHTTPUploadGetSlot
Goffi <goffi@goffi.org>
parents:
3289
diff
changeset
|
306 return d |
1640
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
307 |
3089
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
308 async def getSlot(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
|
309 """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
|
310 |
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
311 @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
|
312 @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
|
313 @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
|
314 None to autodetect |
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
315 @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
|
316 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
|
317 @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
|
318 @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
|
319 @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
|
320 """ |
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
321 assert filename and size |
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
322 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
|
323 # 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
|
324 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
|
325 |
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
326 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
|
327 try: |
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
328 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
|
329 except AttributeError: |
3293
f5a5aa9fa73a
plugin XEP-0363: fixed fileHTTPUploadGetSlot
Goffi <goffi@goffi.org>
parents:
3289
diff
changeset
|
330 found_entity = await self.getHTTPUploadEntity(client) |
3089
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
331 return await self.getSlot( |
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
332 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
|
333 else: |
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
334 if upload_jid is None: |
3089
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
335 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
|
336 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
337 iq_elt = client.IQ("get") |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
338 iq_elt["to"] = upload_jid.full() |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
339 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
|
340 request_elt["filename"] = filename |
3028 | 341 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
|
342 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
|
343 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
|
344 |
3089
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
345 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
|
346 |
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
347 try: |
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
348 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
|
349 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
|
350 put_url = put_elt['url'] |
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
351 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
|
352 get_url = get_elt['url'] |
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
353 except (StopIteration, KeyError): |
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
354 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
|
355 |
3089
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
356 headers = [] |
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
357 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
|
358 try: |
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
359 name = header_elt["name"] |
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
360 value = str(header_elt) |
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
361 except KeyError: |
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
362 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
|
363 iq_result_elt.toXml())) |
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
364 continue |
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
365 name = name.replace('\n', '') |
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
366 value = value.replace('\n', '') |
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
367 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
|
368 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
|
369 .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
|
370 continue |
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
371 headers.append((name, value)) |
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
372 |
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
373 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
|
374 |
3289
9057713ab124
plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents:
3223
diff
changeset
|
375 # component |
9057713ab124
plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents:
3223
diff
changeset
|
376 |
9057713ab124
plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents:
3223
diff
changeset
|
377 def onComponentRequest(self, iq_elt, client): |
9057713ab124
plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents:
3223
diff
changeset
|
378 iq_elt.handled=True |
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
|
379 defer.ensureDeferred(self.handleComponentRequest(client, iq_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
|
380 |
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 async def handleComponentRequest(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
|
382 try: |
9057713ab124
plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents:
3223
diff
changeset
|
383 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
|
384 request = UploadRequest( |
9057713ab124
plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents:
3223
diff
changeset
|
385 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
|
386 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
|
387 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
|
388 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
|
389 ) |
9057713ab124
plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents:
3223
diff
changeset
|
390 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
|
391 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
|
392 return |
9057713ab124
plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents:
3223
diff
changeset
|
393 |
9057713ab124
plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents:
3223
diff
changeset
|
394 err = None |
9057713ab124
plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents:
3223
diff
changeset
|
395 |
9057713ab124
plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents:
3223
diff
changeset
|
396 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
|
397 try: |
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
|
398 slot = await utils.asDeferred(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
|
399 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
|
400 log.warning( |
506fa3d91d3a
plugin XEP-0363: fixed invalid reference to `slot` in catched exception:
Goffi <goffi@goffi.org>
parents:
3294
diff
changeset
|
401 "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
|
402 f"request: {e}" |
506fa3d91d3a
plugin XEP-0363: fixed invalid reference to `slot` in catched exception:
Goffi <goffi@goffi.org>
parents:
3294
diff
changeset
|
403 ) |
3289
9057713ab124
plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents:
3223
diff
changeset
|
404 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
|
405 # 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
|
406 # 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
|
407 err = e |
3399
506fa3d91d3a
plugin XEP-0363: fixed invalid reference to `slot` in catched exception:
Goffi <goffi@goffi.org>
parents:
3294
diff
changeset
|
408 else: |
506fa3d91d3a
plugin XEP-0363: fixed invalid reference to `slot` in catched exception:
Goffi <goffi@goffi.org>
parents:
3294
diff
changeset
|
409 if slot: |
506fa3d91d3a
plugin XEP-0363: fixed invalid reference to `slot` in catched exception:
Goffi <goffi@goffi.org>
parents:
3294
diff
changeset
|
410 break |
3289
9057713ab124
plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents:
3223
diff
changeset
|
411 else: |
9057713ab124
plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents:
3223
diff
changeset
|
412 log.warning( |
9057713ab124
plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents:
3223
diff
changeset
|
413 _("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
|
414 .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
|
415 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
|
416 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
|
417 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
|
418 return |
9057713ab124
plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents:
3223
diff
changeset
|
419 |
9057713ab124
plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents:
3223
diff
changeset
|
420 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
|
421 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
|
422 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
|
423 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
|
424 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
|
425 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
|
426 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
|
427 |
1640
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
428 |
3028 | 429 @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
|
430 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
|
431 |
9057713ab124
plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents:
3223
diff
changeset
|
432 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
|
433 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
|
434 |
9057713ab124
plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents:
3223
diff
changeset
|
435 def connectionInitialized(self): |
9057713ab124
plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents:
3223
diff
changeset
|
436 if self.parent.is_component: |
9057713ab124
plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents:
3223
diff
changeset
|
437 self.xmlstream.addObserver( |
9057713ab124
plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents:
3223
diff
changeset
|
438 IQ_HTTP_UPLOAD_REQUEST, self.plugin_parent.onComponentRequest, |
9057713ab124
plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents:
3223
diff
changeset
|
439 client=self.parent |
9057713ab124
plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents:
3223
diff
changeset
|
440 ) |
1640
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
441 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
442 def getDiscoInfo(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
|
443 return [disco.DiscoFeature(NS_HTTP_UPLOAD)] |
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
444 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
445 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
|
446 return [] |