comparison sat/plugins/plugin_sec_aesgcm.py @ 3186:84b0c8b4dee0

plugin download, aesgcm: fixed handling of HTTP errors
author Goffi <goffi@goffi.org>
date Wed, 26 Feb 2020 15:54:43 +0100
parents 98b321234068
children adf1aeaa0d37
comparison
equal deleted inserted replaced
3185:554b3b632378 3186:84b0c8b4dee0
17 # along with this program. If not, see <http://www.gnu.org/licenses/>. 17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18 18
19 import re 19 import re
20 from textwrap import dedent 20 from textwrap import dedent
21 from functools import partial 21 from functools import partial
22 from urllib.parse import urlparse 22 from urllib import parse
23 import mimetypes 23 import mimetypes
24 import secrets 24 import secrets
25 from cryptography.hazmat.primitives import ciphers 25 from cryptography.hazmat.primitives import ciphers
26 from cryptography.hazmat.primitives.ciphers import modes 26 from cryptography.hazmat.primitives.ciphers import modes
27 from cryptography.hazmat import backends 27 from cryptography.hazmat import backends
28 from cryptography.exceptions import AlreadyFinalized 28 from cryptography.exceptions import AlreadyFinalized
29 from urllib import parse
30 import treq 29 import treq
30 from twisted.internet import defer
31 from sat.core.i18n import _ 31 from sat.core.i18n import _
32 from sat.core.constants import Const as C 32 from sat.core.constants import Const as C
33 from sat.core import exceptions 33 from sat.core import exceptions
34 from sat.tools import stream 34 from sat.tools import stream
35 from sat.core.log import getLogger 35 from sat.core.log import getLogger
106 ) 106 )
107 107
108 progress_id = file_obj.uid 108 progress_id = file_obj.uid
109 109
110 resp = await treq.get(download_url, unbuffered=True) 110 resp = await treq.get(download_url, unbuffered=True)
111 d = treq.collect(resp, partial( 111 if resp.code == 200:
112 self.onDataDownload, 112 d = treq.collect(resp, partial(
113 client=client, 113 self.onDataDownload,
114 file_obj=file_obj, 114 client=client,
115 decryptor=decryptor)) 115 file_obj=file_obj,
116 decryptor=decryptor))
117 else:
118 d = defer.Deferred()
119 self.host.plugins["DOWNLOAD"].errbackDownload(file_obj, d, resp)
116 return progress_id, d 120 return progress_id, d
117 121
118 def onDataDownload(self, data, client, file_obj, decryptor): 122 def onDataDownload(self, data, client, file_obj, decryptor):
119 if file_obj.tell() + len(data) > file_obj.size: 123 if file_obj.tell() + len(data) > file_obj.size:
120 # we're reaching end of file with this bunch of data 124 # we're reaching end of file with this bunch of data
224 else: 228 else:
225 data['message'][lang] = message 229 data['message'][lang] = message
226 mess_encrypted = client.encryption.isEncrypted(data) 230 mess_encrypted = client.encryption.isEncrypted(data)
227 attachments = data['extra'].setdefault(C.MESS_KEY_ATTACHMENTS, []) 231 attachments = data['extra'].setdefault(C.MESS_KEY_ATTACHMENTS, [])
228 for link in links: 232 for link in links:
229 path = urlparse(link).path 233 path = parse.urlparse(link).path
230 attachment = { 234 attachment = {
231 "url": link, 235 "url": link,
232 } 236 }
233 media_type = mimetypes.guess_type(path, strict=False)[0] 237 media_type = mimetypes.guess_type(path, strict=False)[0]
234 if media_type is not None: 238 if media_type is not None: