annotate sat/plugins/plugin_xep_0363.py @ 2865:9213c6dff48d

plugin XEP-0198: reset req_timer on session.reset(): req_timer may not have been called when session.reset() is called (e.g. on resuming failed after computer has been suspended), so reset() must cancel it and set its value to None.
author Goffi <goffi@goffi.org>
date Thu, 21 Mar 2019 08:54:59 +0100
parents 003b8b4b56a7
children 8ce5748bfe97
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1934
2daf7b4c6756 use of /usr/bin/env instead of /usr/bin/python in shebang
Goffi <goffi@goffi.org>
parents: 1875
diff changeset
1 #!/usr/bin/env python2
1640
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
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)
2771
003b8b4b56a7 date update
Goffi <goffi@goffi.org>
parents: 2765
diff changeset
5 # Copyright (C) 2009-2019 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
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
20 from sat.core.i18n import _
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
21 from sat.core.constants import Const as C
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
22 from sat.core.log import getLogger
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
23
1640
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
24 log = getLogger(__name__)
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
25 from sat.core import exceptions
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
26 from wokkel import disco, iwokkel
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
27 from zope.interface import implements
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
28 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
29 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
30 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
31 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
32 from twisted.internet import ssl
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
33 from twisted.internet.interfaces import IOpenSSLClientConnectionCreator
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
34 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
35 from twisted.web import http_headers
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
36 from twisted.web import iweb
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
37 from twisted.python import failure
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
38 from collections import namedtuple
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
39 from zope.interface import implementer
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
40 from OpenSSL import SSL
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
41 import os.path
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
42 import mimetypes
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
43
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
44
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
45 PLUGIN_INFO = {
2145
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 2144
diff changeset
46 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
47 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
48 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
49 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
50 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
51 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
52 C.PI_HANDLER: "yes",
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
53 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
54 }
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
55
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
56 NS_HTTP_UPLOAD = "urn:xmpp:http:upload"
1640
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
57
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
58
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
59 Slot = namedtuple("Slot", ["put", "get"])
1640
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
60
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 @implementer(IOpenSSLClientConnectionCreator)
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
63 class NoCheckConnectionCreator(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, hostname, ctx):
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
65 self._ctx = ctx
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
66
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
67 def clientConnectionForTLS(self, tlsProtocol):
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
68 context = self._ctx
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
69 connection = SSL.Connection(context, None)
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
70 connection.set_app_data(tlsProtocol)
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
71 return connection
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
72
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
73
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
74 @implementer(iweb.IPolicyForHTTPS)
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
75 class NoCheckContextFactory(ssl.ClientContextFactory):
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
76 """Context factory which doesn't do TLS certificate check
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
77
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
78 /!\\ it's obvisously a security flaw to use this class,
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
79 and it should be used only wiht explicite agreement from the end used
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
80 """
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
81
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
82 def creatorForNetloc(self, hostname, port):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
83 log.warning(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
84 u"TLS check disabled for {host} on port {port}".format(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
85 host=hostname, port=port
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
86 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
87 )
1640
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
88 certificateOptions = ssl.CertificateOptions(trustRoot=None)
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
89 return NoCheckConnectionCreator(hostname, certificateOptions.getContext())
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
90
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 class XEP_0363(object):
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
93 def __init__(self, host):
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
94 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
95 self.host = host
2624
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 "fileHTTPUpload",
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="sssbs",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
100 out_sign="",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
101 method=self._fileHTTPUpload,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
102 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
103 host.bridge.addMethod(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
104 "fileHTTPUploadGetSlot",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
105 ".plugin",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
106 in_sign="sisss",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
107 out_sign="(ss)",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
108 method=self._getSlot,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
109 async=True,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
110 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
111 host.plugins["UPLOAD"].register(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
112 u"HTTP Upload", self.getHTTPUploadEntity, self.fileHTTPUpload
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
113 )
1640
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
114
2144
1d3f73e065e1 core, jp: component handling + client handling refactoring:
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
115 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
116 return XEP_0363_handler()
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
117
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
118 @defer.inlineCallbacks
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
119 def getHTTPUploadEntity(self, upload_jid=None, profile=C.PROF_KEY_NONE):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
120 """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
121
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
122 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
123 @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
124 @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
125 @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
126 """
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
127 client = self.host.getClient(profile)
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
128 try:
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
129 entity = client.http_upload_service
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
130 except AttributeError:
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
131 found_entities = yield self.host.findFeaturesSet(client, (NS_HTTP_UPLOAD,))
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
132 try:
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
133 entity = client.http_upload_service = iter(found_entities).next()
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
134 except StopIteration:
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
135 entity = client.http_upload_service = None
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
136
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
137 if entity is None:
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
138 raise failure.Failure(exceptions.NotFound(u"No HTTP upload entity found"))
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
139
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
140 defer.returnValue(entity)
1640
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
141
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
142 def _fileHTTPUpload(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
143 self,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
144 filepath,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
145 filename="",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
146 upload_jid="",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
147 ignore_tls_errors=False,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
148 profile=C.PROF_KEY_NONE,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
149 ):
1640
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
150 assert os.path.isabs(filepath) and os.path.isfile(filepath)
2765
378188abe941 misc: replaced all "dummy" by the more conventional and readable "__" ("_" being used for gettext)
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
151 progress_id_d, __ = self.fileHTTPUpload(
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
152 filepath,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
153 filename or None,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
154 jid.JID(upload_jid) if upload_jid else None,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
155 {"ignore_tls_errors": ignore_tls_errors},
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
156 profile,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
157 )
1824
a19161bb3ff7 plugin upload, XEP-0363: splitted fileUpload in fileUpload + upload:
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
158 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
159
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
160 def fileHTTPUpload(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
161 self,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
162 filepath,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
163 filename=None,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
164 upload_jid=None,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
165 options=None,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
166 profile=C.PROF_KEY_NONE,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
167 ):
1640
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
168 """upload a file through HTTP
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
169
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
170 @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
171 @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
172 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
173 @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
174 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
175 @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
176 - 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
177 @param profile: %(doc_profile)s
1824
a19161bb3ff7 plugin upload, XEP-0363: splitted fileUpload in fileUpload + upload:
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
178 @return (D(tuple[D(unicode), D(unicode)])): 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
179 """
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
180 if options is None:
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
181 options = {}
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
182 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
183 client = self.host.getClient(profile)
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
184 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
185 size = os.path.getsize(filepath)
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
186 progress_id_d = defer.Deferred()
1824
a19161bb3ff7 plugin upload, XEP-0363: splitted fileUpload in fileUpload + upload:
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
187 download_d = defer.Deferred()
1640
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
188 d = self.getSlot(client, filename, size, upload_jid=upload_jid)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
189 d.addCallbacks(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
190 self._getSlotCb,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
191 self._getSlotEb,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
192 (client, progress_id_d, download_d, filepath, size, ignore_tls_errors),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
193 None,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
194 (client, progress_id_d, download_d),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
195 )
1824
a19161bb3ff7 plugin upload, XEP-0363: splitted fileUpload in fileUpload + upload:
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
196 return progress_id_d, download_d
1640
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
197
1824
a19161bb3ff7 plugin upload, XEP-0363: splitted fileUpload in fileUpload + upload:
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
198 def _getSlotEb(self, fail, client, progress_id_d, download_d):
1640
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
199 """an error happened while trying to get slot"""
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
200 log.warning(u"Can't get upload slot: {reason}".format(reason=fail.value))
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
201 progress_id_d.errback(fail)
1824
a19161bb3ff7 plugin upload, XEP-0363: splitted fileUpload in fileUpload + upload:
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
202 download_d.errback(fail)
1640
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
203
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
204 def _getSlotCb(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
205 self, slot, client, progress_id_d, download_d, path, size, ignore_tls_errors=False
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
206 ):
1640
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
207 """Called when slot is received, try to do the upload
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
208
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
209 @param slot(Slot): slot instance with the get and put urls
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
210 @param progress_id_d(defer.Deferred): Deferred to call when progress_id is known
1824
a19161bb3ff7 plugin upload, XEP-0363: splitted fileUpload in fileUpload + upload:
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
211 @param progress_id_d(defer.Deferred): Deferred to call with URL when upload is done
1640
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
212 @param path(str): path to the file to upload
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
213 @param size(int): size 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
214 @param ignore_tls_errors(bool): ignore TLS certificate is True
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
215 @return (tuple
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 log.debug(u"Got upload slot: {}".format(slot))
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
218 sat_file = self.host.plugins["FILE"].File(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
219 self.host, client, path, size=size, auto_end_signals=False
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
220 )
1640
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
221 progress_id_d.callback(sat_file.uid)
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
222 file_producer = http_client.FileBodyProducer(sat_file)
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
223 if ignore_tls_errors:
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
224 agent = http_client.Agent(reactor, NoCheckContextFactory())
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
225 else:
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
226 agent = http_client.Agent(reactor)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
227 d = agent.request(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
228 "PUT",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
229 slot.put.encode("utf-8"),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
230 http_headers.Headers({"User-Agent": [C.APP_NAME.encode("utf-8")]}),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
231 file_producer,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
232 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
233 d.addCallbacks(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
234 self._uploadCb,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
235 self._uploadEb,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
236 (sat_file, slot, download_d),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
237 None,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
238 (sat_file, download_d),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
239 )
1640
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
240 return d
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
241
2765
378188abe941 misc: replaced all "dummy" by the more conventional and readable "__" ("_" being used for gettext)
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
242 def _uploadCb(self, __, sat_file, slot, download_d):
1640
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
243 """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
244
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
245 @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
246 should be closed, be is needed to send the progressFinished signal
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
247 @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
248 """
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
249 log.info(u"HTTP upload finished")
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
250 sat_file.progressFinished({"url": slot.get})
1824
a19161bb3ff7 plugin upload, XEP-0363: splitted fileUpload in fileUpload + upload:
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
251 download_d.callback(slot.get)
1640
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
252
1824
a19161bb3ff7 plugin upload, XEP-0363: splitted fileUpload in fileUpload + upload:
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
253 def _uploadEb(self, fail, sat_file, download_d):
1640
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
254 """Called on unsuccessful upload
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
255
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
256 @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
257 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
258 """
1824
a19161bb3ff7 plugin upload, XEP-0363: splitted fileUpload in fileUpload + upload:
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
259 download_d.errback(fail)
1640
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
260 try:
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
261 wrapped_fail = fail.value.reasons[0]
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
262 except (AttributeError, IndexError):
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
263 sat_file.progressError(unicode(fail))
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
264 raise fail
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
265 else:
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
266 if wrapped_fail.check(SSL.Error):
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
267 msg = u"TLS validation error, can't connect to HTTPS server"
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
268 log.warning(msg + ": " + unicode(wrapped_fail.value))
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
269 sat_file.progressError(msg)
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
270
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
271 def _gotSlot(self, iq_elt, client):
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
272 """Slot have been received
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
273
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
274 This method convert the iq_elt result to a Slot instance
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
275 @param iq_elt(domish.Element): <IQ/> result as specified in XEP-0363
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
276 """
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
277 try:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
278 slot_elt = iq_elt.elements(NS_HTTP_UPLOAD, "slot").next()
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
279 put_url = unicode(slot_elt.elements(NS_HTTP_UPLOAD, "put").next())
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
280 get_url = unicode(slot_elt.elements(NS_HTTP_UPLOAD, "get").next())
1640
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
281 except StopIteration:
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
282 raise exceptions.DataError(u"Incorrect stanza received from server")
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
283 slot = Slot(put=put_url, get=get_url)
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
284 return slot
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
285
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
286 def _getSlot(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
287 self, filename, size, content_type, upload_jid, profile_key=C.PROF_KEY_NONE
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
288 ):
1640
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
289 """Get a upload slot
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
290
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
291 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
292 @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
293 @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
294 @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
295 @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
296 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
297 """
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
298 filename = filename.replace("/", "_")
1640
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
299 client = self.host.getClient(profile_key)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
300 return self.getSlot(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
301 client, filename, size, content_type or None, upload_jid or None
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
302 )
1640
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
303
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
304 def getSlot(self, client, filename, size, content_type=None, upload_jid=None):
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
305 """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
306
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
307 @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
308 @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
309 @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
310 None to autodetect
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
311 @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
312 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
313 @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
314 @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
315 @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
316 """
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
317 assert filename and size
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
318 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
319 # 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
320 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
321
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
322 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
323 try:
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
324 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
325 except AttributeError:
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
326 d = self.getHTTPUploadEntity(profile=client.profile)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
327 d.addCallback(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
328 lambda found_entity: self.getSlot(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
329 client, filename, size, content_type, found_entity
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
330 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
331 )
1640
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
332 return d
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:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
335 raise failure.Failure(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
336 exceptions.NotFound(u"No HTTP upload entity found")
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
337 )
1640
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
338
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
339 iq_elt = client.IQ("get")
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
340 iq_elt["to"] = upload_jid.full()
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
341 request_elt = iq_elt.addElement((NS_HTTP_UPLOAD, "request"))
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
342 request_elt.addElement("filename", content=filename)
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
343 request_elt.addElement("size", content=unicode(size))
1640
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
344 if content_type is not None:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
345 request_elt.addElement("content-type", content=content_type)
1640
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
346
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
347 d = iq_elt.send()
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
348 d.addCallback(self._gotSlot, client)
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
349
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
350 return d
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
351
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
352
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
353 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
354 implements(iwokkel.IDisco)
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
355
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
356 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
357 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
358
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
359 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
360 return []