Mercurial > libervia-backend
annotate sat/plugins/plugin_xep_0363.py @ 3187:d92a144f3589
plugin download: use cache if dest_path is empty:
if dest_path is not set, the file will be downloaded in cache. A new name will be
generated using a hash of the uri. If the file is already downloded in cache, it won't be
downloaded again (in this case returned progress_id is an empty string).
author | Goffi <goffi@goffi.org> |
---|---|
date | Wed, 26 Feb 2020 22:03:11 +0100 |
parents | f2bb57348587 |
children | 883fb4981958 |
rev | line source |
---|---|
3028 | 1 #!/usr/bin/env python3 |
3137 | 2 |
1640
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
3 |
2502
7ad5f2c4e34a
XEP-0065,XEP-0096,XEP-0166,XEP-0235,XEP-0300: file transfer improvments:
Goffi <goffi@goffi.org>
parents:
2489
diff
changeset
|
4 # SAT plugin for HTTP File Upload (XEP-0363) |
3136 | 5 # Copyright (C) 2009-2020 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
|
6 |
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
7 # 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
|
8 # 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
|
9 # 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
|
10 # (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
|
11 |
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
12 # 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
|
13 # 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
|
14 # 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
|
15 # 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
|
16 |
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
17 # 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
|
18 # 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
|
19 |
3089
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
20 import os.path |
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
21 import mimetypes |
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
22 from dataclasses import dataclass |
1640
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
23 from wokkel import disco, iwokkel |
3028 | 24 from zope.interface import implementer |
1640
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
25 from twisted.words.protocols.jabber import jid |
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
26 from twisted.words.protocols.jabber.xmlstream import XMPPHandler |
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 reactor |
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
28 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
|
29 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
|
30 from twisted.web import http_headers |
3089
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
31 from sat.core.i18n import _ |
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
32 from sat.core.constants import Const as C |
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
33 from sat.core.log import getLogger |
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
34 from sat.core import exceptions |
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
35 from sat.tools import 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
|
36 |
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
37 |
3089
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
38 log = getLogger(__name__) |
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
39 |
1640
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
40 PLUGIN_INFO = { |
2145
33c8c4973743
core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents:
2144
diff
changeset
|
41 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
|
42 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
|
43 C.PI_TYPE: "XEP", |
33c8c4973743
core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents:
2144
diff
changeset
|
44 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
|
45 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
|
46 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
|
47 C.PI_HANDLER: "yes", |
3028 | 48 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
|
49 } |
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
50 |
2866
8ce5748bfe97
plugin XEP-0363: updated to namespace "urn:xmpp:http:upload:0", handle headers
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
51 NS_HTTP_UPLOAD = "urn:xmpp:http:upload:0" |
8ce5748bfe97
plugin XEP-0363: updated to namespace "urn:xmpp:http:upload:0", handle headers
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
52 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
|
53 |
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
54 |
3089
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
55 @dataclass |
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
56 class Slot: |
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
57 """Upload slot""" |
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
58 put: str |
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
59 get: str |
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
60 headers: list |
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 |
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
63 class XEP_0363(object): |
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
64 def __init__(self, host): |
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
65 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
|
66 self.host = host |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
67 host.bridge.addMethod( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
68 "fileHTTPUpload", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
69 ".plugin", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
70 in_sign="sssbs", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
71 out_sign="", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
72 method=self._fileHTTPUpload, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
73 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
74 host.bridge.addMethod( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
75 "fileHTTPUploadGetSlot", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
76 ".plugin", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
77 in_sign="sisss", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
78 out_sign="(ss)", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
79 method=self._getSlot, |
3028 | 80 async_=True, |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
81 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
82 host.plugins["UPLOAD"].register( |
3028 | 83 "HTTP Upload", self.getHTTPUploadEntity, self.fileHTTPUpload |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
84 ) |
1640
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
85 |
2144
1d3f73e065e1
core, jp: component handling + client handling refactoring:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
86 def getHandler(self, client): |
1640
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
87 return XEP_0363_handler() |
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
88 |
3089
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
89 async def getHTTPUploadEntity(self, client, upload_jid=None): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
90 """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
|
91 |
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
92 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
|
93 @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
|
94 @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
|
95 @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
|
96 """ |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
97 try: |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
98 entity = client.http_upload_service |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
99 except AttributeError: |
3089
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
100 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
|
101 try: |
3028 | 102 entity = client.http_upload_service = next(iter(found_entities)) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
103 except StopIteration: |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
104 entity = client.http_upload_service = None |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
105 |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
106 if entity is None: |
3089
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
107 raise exceptions.NotFound("No HTTP upload entity found") |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
108 |
3089
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
109 return entity |
1640
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
110 |
2866
8ce5748bfe97
plugin XEP-0363: updated to namespace "urn:xmpp:http:upload:0", handle headers
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
111 def _fileHTTPUpload(self, filepath, filename="", upload_jid="", |
8ce5748bfe97
plugin XEP-0363: updated to namespace "urn:xmpp:http:upload:0", handle headers
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
112 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
|
113 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
|
114 client = self.host.getClient(profile) |
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
115 progress_id_d, __ = defer.ensureDeferred(self.fileHTTPUpload( |
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
116 client, |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
117 filepath, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
118 filename or None, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
119 jid.JID(upload_jid) if upload_jid else None, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
120 {"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
|
121 )) |
1824
a19161bb3ff7
plugin upload, XEP-0363: splitted fileUpload in fileUpload + upload:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
122 return progress_id_d |
1640
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
123 |
3089
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
124 async def fileHTTPUpload( |
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
125 self, client, filepath, filename=None, upload_jid=None, options=None): |
2866
8ce5748bfe97
plugin XEP-0363: updated to namespace "urn:xmpp:http:upload:0", handle headers
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
126 """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
|
127 |
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
128 @param filepath(str): absolute path of the file |
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
129 @param filename(None, 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
|
130 None to use basename of the path |
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
131 @param upload_jid(jid.JID, None): upload capable entity jid, |
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
132 or None to use autodetected, if possible |
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
133 @param options(dict): options where key can be: |
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
134 - ignore_tls_errors(bool): if True, SSL certificate will not be checked |
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
135 @param profile: %(doc_profile)s |
2866
8ce5748bfe97
plugin XEP-0363: updated to namespace "urn:xmpp:http:upload:0", handle headers
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
136 @return (D(tuple[D(unicode), D(unicode)])): progress id and Deferred which fire |
8ce5748bfe97
plugin XEP-0363: updated to namespace "urn:xmpp:http:upload:0", handle headers
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
137 download URL |
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 if options is None: |
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
140 options = {} |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
141 ignore_tls_errors = options.get("ignore_tls_errors", False) |
1640
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
142 filename = filename or os.path.basename(filepath) |
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
143 size = os.path.getsize(filepath) |
3089
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
144 |
1640
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
145 |
3089
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
146 size_adjust = [] |
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
147 #: this trigger can be used to modify the requested size, it is notably useful |
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
148 #: with encryption. The size_adjust is a list which can be filled by int to add |
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
149 #: to the initial size |
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
150 self.host.trigger.point( |
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
151 "XEP-0363_upload_size", client, options, filepath, size, size_adjust, |
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
152 triggers_no_cancel=True) |
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
153 if size_adjust: |
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
154 size = sum([size, *size_adjust]) |
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
155 try: |
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
156 slot = await self.getSlot(client, filename, size, upload_jid=upload_jid) |
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
157 except Exception as e: |
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
158 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
|
159 raise e |
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
160 else: |
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
161 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
|
162 sat_file = self.host.plugins["FILE"].File( |
3182
f2bb57348587
plugin attach, XEP-0363: progress id can now be specified:
Goffi <goffi@goffi.org>
parents:
3155
diff
changeset
|
163 self.host, client, filepath, uid=options.get("progress_id"), size=size, |
f2bb57348587
plugin attach, XEP-0363: progress id can now be specified:
Goffi <goffi@goffi.org>
parents:
3155
diff
changeset
|
164 auto_end_signals=False |
3089
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
165 ) |
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
166 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
|
167 |
3089
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
168 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
|
169 |
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
170 if ignore_tls_errors: |
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
171 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
|
172 else: |
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
173 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
|
174 |
3089
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
175 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
|
176 |
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
177 for name, value in slot.headers: |
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
178 name = name.encode('utf-8') |
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
179 value = value.encode('utf-8') |
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
180 headers[name] = value |
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
181 |
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
182 |
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
183 await self.host.trigger.asyncPoint( |
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
184 "XEP-0363_upload", client, options, sat_file, file_producer, slot, |
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
185 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
|
186 |
3089
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
187 download_d = agent.request( |
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
188 b"PUT", |
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
189 slot.put.encode("utf-8"), |
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
190 http_headers.Headers(headers), |
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
191 file_producer, |
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
192 ) |
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
193 download_d.addCallbacks( |
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
194 self._uploadCb, |
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
195 self._uploadEb, |
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
196 (sat_file, slot), |
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
197 None, |
3155
3a8755fdc78d
plugin XEP-0363: fixed errback args in fileHTTPUpload
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
198 (sat_file,), |
3089
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
199 ) |
2866
8ce5748bfe97
plugin XEP-0363: updated to namespace "urn:xmpp:http:upload:0", handle headers
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
200 |
3089
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
201 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
|
202 |
3089
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
203 def _uploadCb(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
|
204 """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
|
205 |
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
206 @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
|
207 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
|
208 @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
|
209 """ |
3028 | 210 log.info("HTTP upload finished") |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
211 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
|
212 return slot.get |
1640
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
213 |
3089
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
214 def _uploadEb(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
|
215 """Called on unsuccessful upload |
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
216 |
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
217 @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
|
218 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
|
219 """ |
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
220 try: |
3089
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
221 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
|
222 except (AttributeError, IndexError) as e: |
3028 | 223 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
|
224 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
|
225 else: |
3089
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
226 if wrapped_fail.check(sat_web.SSLError): |
3028 | 227 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
|
228 else: |
3028 | 229 msg = "can't upload file" |
230 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
|
231 sat_file.progressError(msg) |
3089
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
232 raise failure_ |
1640
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
233 |
2866
8ce5748bfe97
plugin XEP-0363: updated to namespace "urn:xmpp:http:upload:0", handle headers
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
234 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
|
235 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
|
236 """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
|
237 |
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
238 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
|
239 @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
|
240 @param size(int): size of the file (must be non null) |
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
241 @param upload_jid(jid.JID(), None, ''): HTTP upload capable entity |
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
242 @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
|
243 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
|
244 """ |
3089
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
245 client = self.host.getClient(profile_key) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
246 filename = filename.replace("/", "_") |
3089
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
247 return defer.ensureDeferred(self.getSlot( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
248 client, filename, size, content_type or None, upload_jid or None |
3089
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
249 )) |
1640
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
250 |
3089
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
251 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
|
252 """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
|
253 |
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
254 @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
|
255 @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
|
256 @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
|
257 None to autodetect |
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
258 @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
|
259 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
|
260 @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
|
261 @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
|
262 @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
|
263 """ |
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
264 assert filename and size |
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
265 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
|
266 # 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
|
267 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
|
268 |
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
269 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
|
270 try: |
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
271 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
|
272 except AttributeError: |
3089
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
273 found_entity = await self.getHTTPUploadEntity(profile=client.profile) |
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
274 return await self.getSlot( |
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
275 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
|
276 else: |
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
277 if upload_jid is None: |
3089
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
278 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
|
279 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
280 iq_elt = client.IQ("get") |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
281 iq_elt["to"] = upload_jid.full() |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
282 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
|
283 request_elt["filename"] = filename |
3028 | 284 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
|
285 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
|
286 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
|
287 |
3089
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
288 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
|
289 |
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
290 try: |
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
291 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
|
292 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
|
293 put_url = put_elt['url'] |
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
294 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
|
295 get_url = get_elt['url'] |
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
296 except (StopIteration, KeyError): |
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
297 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
|
298 |
3089
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
299 headers = [] |
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
300 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
|
301 try: |
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
302 name = header_elt["name"] |
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
303 value = str(header_elt) |
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
304 except KeyError: |
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
305 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
|
306 iq_result_elt.toXml())) |
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
307 continue |
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
308 name = name.replace('\n', '') |
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
309 value = value.replace('\n', '') |
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
310 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
|
311 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
|
312 .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
|
313 continue |
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
314 headers.append((name, value)) |
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
315 |
e75024e41f81
plugin upload, XEP-0363: code modernisation + preparation for extension:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
316 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
|
317 |
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
318 |
3028 | 319 @implementer(iwokkel.IDisco) |
1640
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
320 class XEP_0363_handler(XMPPHandler): |
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
321 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
322 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
|
323 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
|
324 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
325 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
|
326 return [] |