annotate sat/plugins/plugin_misc_upload.py @ 3028:ab2696e34d29

Python 3 port: /!\ this is a huge commit /!\ starting from this commit, SàT is needs Python 3.6+ /!\ SàT maybe be instable or some feature may not work anymore, this will improve with time This patch port backend, bridge and frontends to Python 3. Roughly this has been done this way: - 2to3 tools has been applied (with python 3.7) - all references to python2 have been replaced with python3 (notably shebangs) - fixed files not handled by 2to3 (notably the shell script) - several manual fixes - fixed issues reported by Python 3 that where not handled in Python 2 - replaced "async" with "async_" when needed (it's a reserved word from Python 3.7) - replaced zope's "implements" with @implementer decorator - temporary hack to handle data pickled in database, as str or bytes may be returned, to be checked later - fixed hash comparison for password - removed some code which is not needed anymore with Python 3 - deactivated some code which needs to be checked (notably certificate validation) - tested with jp, fixed reported issues until some basic commands worked - ported Primitivus (after porting dependencies like urwid satext) - more manual fixes
author Goffi <goffi@goffi.org>
date Tue, 13 Aug 2019 19:08:41 +0200
parents 3cac3d050046
children e75024e41f81
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2867
diff changeset
1 #!/usr/bin/env python3
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
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
4 # SAT plugin for file tansfer
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 _, D_
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 sat.tools import xml_tools
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
27 from twisted.internet import defer
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
28 from twisted.words.protocols.jabber import jid
2863
a97a5c73594d plugin upload: fixed inversion of title and message body + better error message on failed upload
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
29 from twisted.words.protocols.jabber import error as jabber_error
1640
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
30 import os
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
31 import os.path
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
32
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
33
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
34 PLUGIN_INFO = {
2145
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 2144
diff changeset
35 C.PI_NAME: "File Upload",
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 2144
diff changeset
36 C.PI_IMPORT_NAME: "UPLOAD",
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 2144
diff changeset
37 C.PI_TYPE: C.PLUG_TYPE_MISC,
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 2144
diff changeset
38 C.PI_MAIN: "UploadPlugin",
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 2144
diff changeset
39 C.PI_HANDLER: "no",
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
40 C.PI_DESCRIPTION: _("""File upload management"""),
1640
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
41 }
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
42
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
43
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2867
diff changeset
44 UPLOADING = D_("Please select a file to upload")
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2867
diff changeset
45 UPLOADING_TITLE = D_("File upload")
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
46 BOOL_OPTIONS = ("ignore_tls_errors",)
1640
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
47
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
48
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
49 class UploadPlugin(object):
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
50 # TODO: plugin unload
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
51
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
52 def __init__(self, host):
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
53 log.info(_("plugin Upload initialization"))
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
54 self.host = host
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
55 host.bridge.addMethod(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
56 "fileUpload",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
57 ".plugin",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
58 in_sign="sssa{ss}s",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
59 out_sign="a{ss}",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
60 method=self._fileUpload,
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2867
diff changeset
61 async_=True,
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
62 )
1640
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
63 self._upload_callbacks = []
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
64
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
65 def _fileUpload(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
66 self, filepath, filename, upload_jid_s="", options=None, profile=C.PROF_KEY_NONE
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
67 ):
1824
a19161bb3ff7 plugin upload, XEP-0363: splitted fileUpload in fileUpload + upload:
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
68 client = self.host.getClient(profile)
1640
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
69 upload_jid = jid.JID(upload_jid_s) if upload_jid_s else None
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
70 if options is None:
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
71 options = {}
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
72 # we convert values that are well-known booleans
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
73 for bool_option in BOOL_OPTIONS:
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
74 try:
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
75 options[bool_option] = C.bool(options[bool_option])
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
76 except KeyError:
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
77 pass
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
78
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
79 return self.fileUpload(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
80 client, filepath, filename or None, upload_jid, options or None
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
81 )
1640
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
82
1824
a19161bb3ff7 plugin upload, XEP-0363: splitted fileUpload in fileUpload + upload:
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
83 def fileUpload(self, client, filepath, filename, upload_jid, options):
a19161bb3ff7 plugin upload, XEP-0363: splitted fileUpload in fileUpload + upload:
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
84 """Send a file using best available method
a19161bb3ff7 plugin upload, XEP-0363: splitted fileUpload in fileUpload + upload:
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
85
a19161bb3ff7 plugin upload, XEP-0363: splitted fileUpload in fileUpload + upload:
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
86 parameters are the same as for [upload]
2867
3cac3d050046 plugin upload: minor fixes in some docstring length
Goffi <goffi@goffi.org>
parents: 2863
diff changeset
87 @return (dict): action dictionary, with progress id in case of success, else xmlui
3cac3d050046 plugin upload: minor fixes in some docstring length
Goffi <goffi@goffi.org>
parents: 2863
diff changeset
88 message
1824
a19161bb3ff7 plugin upload, XEP-0363: splitted fileUpload in fileUpload + upload:
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
89 """
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
90
1824
a19161bb3ff7 plugin upload, XEP-0363: splitted fileUpload in fileUpload + upload:
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
91 def uploadCb(data):
2765
378188abe941 misc: replaced all "dummy" by the more conventional and readable "__" ("_" being used for gettext)
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
92 progress_id, __ = data
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
93 return {"progress": progress_id}
1824
a19161bb3ff7 plugin upload, XEP-0363: splitted fileUpload in fileUpload + upload:
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
94
a19161bb3ff7 plugin upload, XEP-0363: splitted fileUpload in fileUpload + upload:
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
95 def uploadEb(fail):
2863
a97a5c73594d plugin upload: fixed inversion of title and message body + better error message on failed upload
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
96 if (isinstance(fail.value, jabber_error.StanzaError)
a97a5c73594d plugin upload: fixed inversion of title and message body + better error message on failed upload
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
97 and fail.value.condition == 'not-acceptable'):
a97a5c73594d plugin upload: fixed inversion of title and message body + better error message on failed upload
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
98 reason = fail.value.text
a97a5c73594d plugin upload: fixed inversion of title and message body + better error message on failed upload
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
99 else:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2867
diff changeset
100 reason = str(fail.value)
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2867
diff changeset
101 msg = D_("Can't upload file: {reason}").format(reason=reason)
1824
a19161bb3ff7 plugin upload, XEP-0363: splitted fileUpload in fileUpload + upload:
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
102 log.warning(msg)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
103 return {
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
104 "xmlui": xml_tools.note(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2867
diff changeset
105 msg, D_("Can't upload file"), C.XMLUI_DATA_LVL_WARNING
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
106 ).toXml()
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
107 }
1824
a19161bb3ff7 plugin upload, XEP-0363: splitted fileUpload in fileUpload + upload:
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
108
1851
ff603003d40a plugin misc_upload: fixes things that have been forgotten after a refactoring
souliane <souliane@mailoo.org>
parents: 1824
diff changeset
109 d = self.upload(client, filepath, filename, upload_jid, options)
1824
a19161bb3ff7 plugin upload, XEP-0363: splitted fileUpload in fileUpload + upload:
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
110 d.addCallback(uploadCb)
a19161bb3ff7 plugin upload, XEP-0363: splitted fileUpload in fileUpload + upload:
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
111 d.addErrback(uploadEb)
a19161bb3ff7 plugin upload, XEP-0363: splitted fileUpload in fileUpload + upload:
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
112 return d
a19161bb3ff7 plugin upload, XEP-0363: splitted fileUpload in fileUpload + upload:
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
113
a19161bb3ff7 plugin upload, XEP-0363: splitted fileUpload in fileUpload + upload:
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
114 @defer.inlineCallbacks
a19161bb3ff7 plugin upload, XEP-0363: splitted fileUpload in fileUpload + upload:
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
115 def upload(self, client, filepath, filename=None, upload_jid=None, options=None):
1640
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
116 """Send a file using best available method
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 @param filepath(str): absolute path to the file
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
119 @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
120 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
121 @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
122 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
123 @param options(dict): option to use for the upload, may be:
1824
a19161bb3ff7 plugin upload, XEP-0363: splitted fileUpload in fileUpload + upload:
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
124 - ignore_tls_errors(bool): True to ignore SSL/TLS certificate verification
1640
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
125 used only if HTTPS transport is needed
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
126 @param profile: %(doc_profile)s
2867
3cac3d050046 plugin upload: minor fixes in some docstring length
Goffi <goffi@goffi.org>
parents: 2863
diff changeset
127 @return (tuple[unicode,D(unicode)]): progress_id and a Deferred which fire
3cac3d050046 plugin upload: minor fixes in some docstring length
Goffi <goffi@goffi.org>
parents: 2863
diff changeset
128 download URL when upload is finished
1640
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
129 """
1824
a19161bb3ff7 plugin upload, XEP-0363: splitted fileUpload in fileUpload + upload:
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
130 if options is None:
a19161bb3ff7 plugin upload, XEP-0363: splitted fileUpload in fileUpload + upload:
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
131 options = {}
1640
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
132 if not os.path.isfile(filepath):
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2867
diff changeset
133 raise exceptions.DataError("The given path doesn't link to a file")
1640
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
134 for method_name, available_cb, upload_cb, priority in self._upload_callbacks:
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
135 try:
1824
a19161bb3ff7 plugin upload, XEP-0363: splitted fileUpload in fileUpload + upload:
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
136 upload_jid = yield available_cb(upload_jid, client.profile)
1640
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
137 except exceptions.NotFound:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
138 continue # no entity managing this extension found
1640
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
139
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
140 log.info(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2867
diff changeset
141 "{name} method will be used to upload the file".format(name=method_name)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
142 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
143 progress_id_d, download_d = yield upload_cb(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
144 filepath, filename, upload_jid, options, client.profile
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
145 )
1824
a19161bb3ff7 plugin upload, XEP-0363: splitted fileUpload in fileUpload + upload:
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
146 progress_id = yield progress_id_d
a19161bb3ff7 plugin upload, XEP-0363: splitted fileUpload in fileUpload + upload:
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
147 defer.returnValue((progress_id, download_d))
a19161bb3ff7 plugin upload, XEP-0363: splitted fileUpload in fileUpload + upload:
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
148
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2867
diff changeset
149 raise exceptions.NotFound("Can't find any method to upload a file")
1640
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
150
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
151 def register(self, method_name, available_cb, upload_cb, priority=0):
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
152 """Register a fileUploading method
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
153
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
154 @param method_name(unicode): short name for the method, must be unique
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
155 @param available_cb(callable): method to call to check if this method is usable
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
156 the callback must take two arguments: upload_jid (can be None) and profile
2867
3cac3d050046 plugin upload: minor fixes in some docstring length
Goffi <goffi@goffi.org>
parents: 2863
diff changeset
157 the callback must return the first entity found (being upload_jid or one of its
3cac3d050046 plugin upload: minor fixes in some docstring length
Goffi <goffi@goffi.org>
parents: 2863
diff changeset
158 components)
1640
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
159 exceptions.NotFound must be raised if no entity has been found
1824
a19161bb3ff7 plugin upload, XEP-0363: splitted fileUpload in fileUpload + upload:
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
160 @param upload_cb(callable): method to upload a file
a19161bb3ff7 plugin upload, XEP-0363: splitted fileUpload in fileUpload + upload:
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
161 must have the same signature as [fileUpload]
2867
3cac3d050046 plugin upload: minor fixes in some docstring length
Goffi <goffi@goffi.org>
parents: 2863
diff changeset
162 must return a tuple with progress_id and a Deferred which fire download URL
3cac3d050046 plugin upload: minor fixes in some docstring length
Goffi <goffi@goffi.org>
parents: 2863
diff changeset
163 when upload is finished
1640
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
164 @param priority(int): pririoty of this method, the higher available will be used
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
165 """
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
166 assert method_name
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
167 for data in self._upload_callbacks:
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
168 if method_name == data[0]:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
169 raise exceptions.ConflictError(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2867
diff changeset
170 "A method with this name is already registered"
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
171 )
1640
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
172 self._upload_callbacks.append((method_name, available_cb, upload_cb, priority))
1824
a19161bb3ff7 plugin upload, XEP-0363: splitted fileUpload in fileUpload + upload:
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
173 self._upload_callbacks.sort(key=lambda data: data[3], reverse=True)
1640
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
174
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
175 def unregister(self, method_name):
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
176 for idx, data in enumerate(self._upload_callbacks):
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
177 if data[0] == method_name:
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
178 del [idx]
d470affbe65c plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff changeset
179 return
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2867
diff changeset
180 raise exceptions.NotFound("The name to unregister doesn't exist")