Mercurial > libervia-backend
annotate src/plugins/plugin_misc_upload.py @ 2511:20a5e7db0609
plugin XEP-0231: separated the requestData code in a method which can called independantly + some minor improvments
author | Goffi <goffi@goffi.org> |
---|---|
date | Fri, 02 Mar 2018 17:45:23 +0100 |
parents | 0046283a285d |
children |
rev | line source |
---|---|
1934
2daf7b4c6756
use of /usr/bin/env instead of /usr/bin/python in shebang
Goffi <goffi@goffi.org>
parents:
1851
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 |
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 |
2483 | 5 # Copyright (C) 2009-2018 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 |
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
23 log = getLogger(__name__) |
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
24 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
|
25 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
|
26 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
|
27 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
|
28 import os |
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
29 import os.path |
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
30 |
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
31 |
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
32 PLUGIN_INFO = { |
2145
33c8c4973743
core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents:
2144
diff
changeset
|
33 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
|
34 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
|
35 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
|
36 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
|
37 C.PI_HANDLER: "no", |
33c8c4973743
core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents:
2144
diff
changeset
|
38 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
|
39 } |
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
40 |
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 UPLOADING = D_(u'Please select a file to upload') |
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
43 UPLOADING_TITLE = D_(u'File upload') |
1824
a19161bb3ff7
plugin upload, XEP-0363: splitted fileUpload in fileUpload + upload:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
44 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
|
45 |
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
46 |
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
47 class UploadPlugin(object): |
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
48 # TODO: plugin unload |
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 def __init__(self, host): |
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
51 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
|
52 self.host = host |
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
53 host.bridge.addMethod("fileUpload", ".plugin", in_sign='sssa{ss}s', out_sign='a{ss}', method=self._fileUpload, async=True) |
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
54 self._upload_callbacks = [] |
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
55 |
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
56 def _fileUpload(self, filepath, filename, upload_jid_s='', options=None, profile=C.PROF_KEY_NONE): |
1824
a19161bb3ff7
plugin upload, XEP-0363: splitted fileUpload in fileUpload + upload:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
57 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
|
58 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
|
59 if options is None: |
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
60 options = {} |
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
61 # 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
|
62 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
|
63 try: |
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
64 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
|
65 except KeyError: |
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
66 pass |
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
67 |
1824
a19161bb3ff7
plugin upload, XEP-0363: splitted fileUpload in fileUpload + upload:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
68 return self.fileUpload(client, filepath, filename or None, upload_jid, options or None) |
1640
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
69 |
1824
a19161bb3ff7
plugin upload, XEP-0363: splitted fileUpload in fileUpload + upload:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
70 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
|
71 """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
|
72 |
a19161bb3ff7
plugin upload, XEP-0363: splitted fileUpload in fileUpload + upload:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
73 parameters are the same as for [upload] |
a19161bb3ff7
plugin upload, XEP-0363: splitted fileUpload in fileUpload + upload:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
74 @return (dict): action dictionary, with progress id in case of success, else xmlui message |
a19161bb3ff7
plugin upload, XEP-0363: splitted fileUpload in fileUpload + upload:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
75 """ |
a19161bb3ff7
plugin upload, XEP-0363: splitted fileUpload in fileUpload + upload:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
76 def uploadCb(data): |
a19161bb3ff7
plugin upload, XEP-0363: splitted fileUpload in fileUpload + upload:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
77 progress_id, dummy = data |
a19161bb3ff7
plugin upload, XEP-0363: splitted fileUpload in fileUpload + upload:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
78 return {'progress': progress_id} |
a19161bb3ff7
plugin upload, XEP-0363: splitted fileUpload in fileUpload + upload:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
79 |
a19161bb3ff7
plugin upload, XEP-0363: splitted fileUpload in fileUpload + upload:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
80 def uploadEb(fail): |
a19161bb3ff7
plugin upload, XEP-0363: splitted fileUpload in fileUpload + upload:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
81 msg = unicode(fail) |
a19161bb3ff7
plugin upload, XEP-0363: splitted fileUpload in fileUpload + upload:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
82 log.warning(msg) |
a19161bb3ff7
plugin upload, XEP-0363: splitted fileUpload in fileUpload + upload:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
83 return {'xmlui': xml_tools.note(u"Can't upload file", msg, C.XMLUI_DATA_LVL_WARNING).toXml()} |
a19161bb3ff7
plugin upload, XEP-0363: splitted fileUpload in fileUpload + upload:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
84 |
1851
ff603003d40a
plugin misc_upload: fixes things that have been forgotten after a refactoring
souliane <souliane@mailoo.org>
parents:
1824
diff
changeset
|
85 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
|
86 d.addCallback(uploadCb) |
a19161bb3ff7
plugin upload, XEP-0363: splitted fileUpload in fileUpload + upload:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
87 d.addErrback(uploadEb) |
a19161bb3ff7
plugin upload, XEP-0363: splitted fileUpload in fileUpload + upload:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
88 return d |
a19161bb3ff7
plugin upload, XEP-0363: splitted fileUpload in fileUpload + upload:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
89 |
a19161bb3ff7
plugin upload, XEP-0363: splitted fileUpload in fileUpload + upload:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
90 @defer.inlineCallbacks |
a19161bb3ff7
plugin upload, XEP-0363: splitted fileUpload in fileUpload + upload:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
91 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
|
92 """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
|
93 |
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
94 @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
|
95 @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
|
96 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
|
97 @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
|
98 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
|
99 @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
|
100 - 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
|
101 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
|
102 @param profile: %(doc_profile)s |
1824
a19161bb3ff7
plugin upload, XEP-0363: splitted fileUpload in fileUpload + upload:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
103 @return (tuple[unicode,D(unicode)]): progress_id and a Deferred which fire download URL |
a19161bb3ff7
plugin upload, XEP-0363: splitted fileUpload in fileUpload + upload:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
104 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
|
105 """ |
1824
a19161bb3ff7
plugin upload, XEP-0363: splitted fileUpload in fileUpload + upload:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
106 if options is None: |
a19161bb3ff7
plugin upload, XEP-0363: splitted fileUpload in fileUpload + upload:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
107 options = {} |
1640
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
108 if not os.path.isfile(filepath): |
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
109 raise exceptions.DataError(u"The given path doesn't link to a file") |
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
110 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
|
111 try: |
1824
a19161bb3ff7
plugin upload, XEP-0363: splitted fileUpload in fileUpload + upload:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
112 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
|
113 except exceptions.NotFound: |
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
114 continue # no entity managing this extension found |
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
115 |
1824
a19161bb3ff7
plugin upload, XEP-0363: splitted fileUpload in fileUpload + upload:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
116 log.info(u"{name} method will be used to upload the file".format(name=method_name)) |
2144
1d3f73e065e1
core, jp: component handling + client handling refactoring:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
117 progress_id_d, download_d = yield upload_cb(filepath, filename, upload_jid, options, client.profile) |
1824
a19161bb3ff7
plugin upload, XEP-0363: splitted fileUpload in fileUpload + upload:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
118 progress_id = yield progress_id_d |
a19161bb3ff7
plugin upload, XEP-0363: splitted fileUpload in fileUpload + upload:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
119 defer.returnValue((progress_id, download_d)) |
a19161bb3ff7
plugin upload, XEP-0363: splitted fileUpload in fileUpload + upload:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
120 |
a19161bb3ff7
plugin upload, XEP-0363: splitted fileUpload in fileUpload + upload:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
121 raise exceptions.NotFound(u"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
|
122 |
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
123 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
|
124 """Register a fileUploading method |
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
125 |
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
126 @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
|
127 @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
|
128 the callback must take two arguments: upload_jid (can be None) and profile |
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
129 the callback must return the first entity found (being upload_jid or one of its components) |
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
130 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
|
131 @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
|
132 must have the same signature as [fileUpload] |
a19161bb3ff7
plugin upload, XEP-0363: splitted fileUpload in fileUpload + upload:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
133 must return a tuple with progress_id and a Deferred which fire download URL when |
a19161bb3ff7
plugin upload, XEP-0363: splitted fileUpload in fileUpload + upload:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
134 upload is finished |
1640
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
135 @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
|
136 """ |
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
137 assert method_name |
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
138 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
|
139 if method_name == data[0]: |
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
140 raise exceptions.ConflictError(u'A method with this name is already registered') |
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
141 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
|
142 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
|
143 |
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
144 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
|
145 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
|
146 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
|
147 del [idx] |
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
148 return |
d470affbe65c
plugin XEP-0363, upload: File upload (through HTTP upload only for now):
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
149 raise exceptions.NotFound(u"The name to unregister doesn't exist") |