comparison src/plugins/plugin_xep_0363.py @ 1824:a19161bb3ff7

plugin upload, XEP-0363: splitted fileUpload in fileUpload + upload: fileUpload is used by external frontends, while upload can be used by frontends. upload return in addition to the progress_id a download_d Deferred which fire with URI when file is uploaded. changed option ignore-tls-errors to ignore_tls_errors.
author Goffi <goffi@goffi.org>
date Fri, 22 Jan 2016 20:24:17 +0100
parents d17772b0fe22
children c55220fc03e4
comparison
equal deleted inserted replaced
1823:1424cc6f8e98 1824:a19161bb3ff7
121 121
122 defer.returnValue(entity) 122 defer.returnValue(entity)
123 123
124 def _fileHTTPUpload(self, filepath, filename='', upload_jid='', ignore_tls_errors=False, profile=C.PROF_KEY_NONE): 124 def _fileHTTPUpload(self, filepath, filename='', upload_jid='', ignore_tls_errors=False, profile=C.PROF_KEY_NONE):
125 assert os.path.isabs(filepath) and os.path.isfile(filepath) 125 assert os.path.isabs(filepath) and os.path.isfile(filepath)
126 return self.fileHTTPUpload(filepath, filename or None, jid.JID(upload_jid) if upload_jid else None, {'ignore-tls-errors': ignore_tls_errors}, profile) 126 progress_id_d, dummy = self.fileHTTPUpload(filepath, filename or None, jid.JID(upload_jid) if upload_jid else None, {'ignore_tls_errors': ignore_tls_errors}, profile)
127 return progress_id_d
127 128
128 def fileHTTPUpload(self, filepath, filename=None, upload_jid=None, options=None, profile=C.PROF_KEY_NONE): 129 def fileHTTPUpload(self, filepath, filename=None, upload_jid=None, options=None, profile=C.PROF_KEY_NONE):
129 """upload a file through HTTP 130 """upload a file through HTTP
130 131
131 @param filepath(str): absolute path of the file 132 @param filepath(str): absolute path of the file
134 @param upload_jid(jid.JID, None): upload capable entity jid, 135 @param upload_jid(jid.JID, None): upload capable entity jid,
135 or None to use autodetected, if possible 136 or None to use autodetected, if possible
136 @param options(dict): options where key can be: 137 @param options(dict): options where key can be:
137 - ignore_tls_errors(bool): if True, SSL certificate will not be checked 138 - ignore_tls_errors(bool): if True, SSL certificate will not be checked
138 @param profile: %(doc_profile)s 139 @param profile: %(doc_profile)s
139 @return (D(unicode)): progress id 140 @return (D(tuple[D(unicode), D(unicode)])): progress id and Deferred which fire download URL
140 """ 141 """
141 if options is None: 142 if options is None:
142 options = {} 143 options = {}
143 ignore_tls_errors = options.get('ignore-tls-errors', False) 144 ignore_tls_errors = options.get('ignore_tls_errors', False)
144 client = self.host.getClient(profile) 145 client = self.host.getClient(profile)
145 filename = filename or os.path.basename(filepath) 146 filename = filename or os.path.basename(filepath)
146 size = os.path.getsize(filepath) 147 size = os.path.getsize(filepath)
147 progress_id_d = defer.Deferred() 148 progress_id_d = defer.Deferred()
149 download_d = defer.Deferred()
148 d = self.getSlot(client, filename, size, upload_jid=upload_jid) 150 d = self.getSlot(client, filename, size, upload_jid=upload_jid)
149 d.addCallbacks(self._getSlotCb, self._getSlotEb, (client, progress_id_d, filepath, size, ignore_tls_errors), None, (client, progress_id_d)) 151 d.addCallbacks(self._getSlotCb, self._getSlotEb, (client, progress_id_d, download_d, filepath, size, ignore_tls_errors), None, (client, progress_id_d, download_d))
150 return progress_id_d 152 return progress_id_d, download_d
151 153
152 def _getSlotEb(self, fail, client, progress_id_d): 154 def _getSlotEb(self, fail, client, progress_id_d, download_d):
153 """an error happened while trying to get slot""" 155 """an error happened while trying to get slot"""
154 log.warning(u"Can't get upload slot: {reason}".format(reason=fail.value)) 156 log.warning(u"Can't get upload slot: {reason}".format(reason=fail.value))
155 progress_id_d.errback(fail) 157 progress_id_d.errback(fail)
156 158 download_d.errback(fail)
157 def _getSlotCb(self, slot, client, progress_id_d, path, size, ignore_tls_errors=False): 159
160 def _getSlotCb(self, slot, client, progress_id_d, download_d, path, size, ignore_tls_errors=False):
158 """Called when slot is received, try to do the upload 161 """Called when slot is received, try to do the upload
159 162
160 @param slot(Slot): slot instance with the get and put urls 163 @param slot(Slot): slot instance with the get and put urls
161 @param progress_id_d(defer.Deferred): Deferred to call when progress_id is known 164 @param progress_id_d(defer.Deferred): Deferred to call when progress_id is known
165 @param progress_id_d(defer.Deferred): Deferred to call with URL when upload is done
162 @param path(str): path to the file to upload 166 @param path(str): path to the file to upload
163 @param size(int): size of the file to upload 167 @param size(int): size of the file to upload
164 @param ignore_tls_errors(bool): ignore TLS certificate is True 168 @param ignore_tls_errors(bool): ignore TLS certificate is True
165 @return (tuple 169 @return (tuple
166 """ 170 """
171 if ignore_tls_errors: 175 if ignore_tls_errors:
172 agent = http_client.Agent(reactor, NoCheckContextFactory()) 176 agent = http_client.Agent(reactor, NoCheckContextFactory())
173 else: 177 else:
174 agent = http_client.Agent(reactor) 178 agent = http_client.Agent(reactor)
175 d = agent.request('PUT', slot.put.encode('utf-8'), http_headers.Headers({'User-Agent': [C.APP_NAME.encode('utf-8')]}), file_producer) 179 d = agent.request('PUT', slot.put.encode('utf-8'), http_headers.Headers({'User-Agent': [C.APP_NAME.encode('utf-8')]}), file_producer)
176 d.addCallbacks(self._uploadCb, self._uploadEb, (sat_file, slot), None, (sat_file,)) 180 d.addCallbacks(self._uploadCb, self._uploadEb, (sat_file, slot, download_d), None, (sat_file, download_d))
177 return d 181 return d
178 182
179 def _uploadCb(self, dummy, sat_file, slot): 183 def _uploadCb(self, dummy, sat_file, slot, download_d):
180 """Called once file is successfully uploaded 184 """Called once file is successfully uploaded
181 185
182 @param sat_file(SatFile): file used for the upload 186 @param sat_file(SatFile): file used for the upload
183 should be closed, be is needed to send the progressFinished signal 187 should be closed, be is needed to send the progressFinished signal
184 @param slot(Slot): put/get urls 188 @param slot(Slot): put/get urls
185 """ 189 """
186 log.info(u"HTTP upload finished") 190 log.info(u"HTTP upload finished")
187 sat_file.progressFinished({'url': slot.get}) 191 sat_file.progressFinished({'url': slot.get})
188 192 download_d.callback(slot.get)
189 def _uploadEb(self, fail, sat_file): 193
194 def _uploadEb(self, fail, sat_file, download_d):
190 """Called on unsuccessful upload 195 """Called on unsuccessful upload
191 196
192 @param sat_file(SatFile): file used for the upload 197 @param sat_file(SatFile): file used for the upload
193 should be closed, be is needed to send the progressError signal 198 should be closed, be is needed to send the progressError signal
194 """ 199 """
200 download_d.errback(fail)
195 try: 201 try:
196 wrapped_fail = fail.value.reasons[0] 202 wrapped_fail = fail.value.reasons[0]
197 except (AttributeError, IndexError): 203 except (AttributeError, IndexError):
198 sat_file.progressError(unicode(fail)) 204 sat_file.progressError(unicode(fail))
199 raise fail 205 raise fail