comparison sat/plugins/plugin_xep_0231.py @ 3273:4230aaeab9a7

plugin XEP-0231, XEP-0264: fixed some remaining encoding issue following Python 3 port
author Goffi <goffi@goffi.org>
date Mon, 18 May 2020 23:39:41 +0200
parents 559a625a236b
children ac9342f359e9
comparison
equal deleted inserted replaced
3272:4c98f4972db5 3273:4230aaeab9a7
182 if metadata is None: 182 if metadata is None:
183 error_elt = jabber_error.StanzaError("item-not-found").toResponse(iq_elt) 183 error_elt = jabber_error.StanzaError("item-not-found").toResponse(iq_elt)
184 client.send(error_elt) 184 client.send(error_elt)
185 return 185 return
186 186
187 with open(metadata["path"]) as f: 187 with open(metadata["path"], 'rb') as f:
188 data = f.read() 188 data = f.read()
189 189
190 result_elt = xmlstream.toResponse(iq_elt, "result") 190 result_elt = xmlstream.toResponse(iq_elt, "result")
191 data_elt = result_elt.addElement((NS_BOB, "data"), content=data.encode("base64")) 191 data_elt = result_elt.addElement(
192 (NS_BOB, "data"), content=base64.b64encode(data).decode())
192 data_elt["cid"] = cid 193 data_elt["cid"] = cid
193 data_elt["type"] = metadata["mime_type"] 194 data_elt["type"] = metadata["mime_type"]
194 data_elt["max-age"] = str(int(max(0, metadata["eol"] - time.time()))) 195 data_elt["max-age"] = str(int(max(0, metadata["eol"] - time.time())))
195 client.send(result_elt) 196 client.send(result_elt)
196 197
197 def _getFile(self, peer_jid_s, cid, profile): 198 def _getFile(self, peer_jid_s, cid, profile):
198 peer_jid = jid.JID(peer_jid_s) 199 peer_jid = jid.JID(peer_jid_s)
199 assert cid 200 assert cid
200 client = self.host.getClient(profile) 201 client = self.host.getClient(profile)
201 return self.getFile(client, peer_jid, cid) 202 d = self.getFile(client, peer_jid, cid)
203 d.addCallback(lambda path: str(path))
204 return d
202 205
203 def getFile(self, client, peer_jid, cid, parent_elt=None): 206 def getFile(self, client, peer_jid, cid, parent_elt=None):
204 """Retrieve a file from it's content-id 207 """Retrieve a file from it's content-id
205 208
206 @param peer_jid(jid.JID): jid of the entity offering the data 209 @param peer_jid(jid.JID): jid of the entity offering the data
207 @param cid(unicode): content-id of file data 210 @param cid(unicode): content-id of file data
208 @param parent_elt(domish.Element, None): if file is not in cache, 211 @param parent_elt(domish.Element, None): if file is not in cache,
209 data will be looked after in children of this elements. 212 data will be looked after in children of this elements.
210 None to ignore 213 None to ignore
211 @return D(unicode): path to cached data 214 @return D(Path): path to cached data
212 """ 215 """
213 file_path = client.cache.getFilePath(cid) 216 file_path = client.cache.getFilePath(cid)
214 if file_path is not None: 217 if file_path is not None:
215 #  file is in cache 218 #  file is in cache
216 return defer.succeed(file_path) 219 return defer.succeed(file_path)