comparison sat/core/xmpp.py @ 3184:e8ce30798d15

core (xmpp): use pathlib.Path to complete attachments names, and use name to guess type
author Goffi <goffi@goffi.org>
date Wed, 26 Feb 2020 15:54:34 +0100
parents 8565af209234
children adf1aeaa0d37
comparison
equal deleted inserted replaced
3183:8565af209234 3184:e8ce30798d15
21 import calendar 21 import calendar
22 import uuid 22 import uuid
23 import mimetypes 23 import mimetypes
24 from urllib.parse import urlparse, unquote 24 from urllib.parse import urlparse, unquote
25 from functools import partial 25 from functools import partial
26 from pathlib import Path
26 import shortuuid 27 import shortuuid
27 from sat.core.i18n import _ 28 from sat.core.i18n import _
28 from sat.core.constants import Const as C 29 from sat.core.constants import Const as C
29 from sat.memory import cache 30 from sat.memory import cache
30 from twisted.internet import defer, error as internet_error 31 from twisted.internet import defer, error as internet_error
1131 1132
1132 def completeAttachments(self, data): 1133 def completeAttachments(self, data):
1133 """Complete missing metadata of attachments""" 1134 """Complete missing metadata of attachments"""
1134 for attachment in data['extra'].get(C.MESS_KEY_ATTACHMENTS, []): 1135 for attachment in data['extra'].get(C.MESS_KEY_ATTACHMENTS, []):
1135 if "name" not in attachment: 1136 if "name" not in attachment:
1136 name = unquote(urlparse(attachment['url']).path.rsplit('/', 1)[-1]) 1137 name = (Path(unquote(urlparse(attachment['url']).path)).name
1138 or C.FILE_DEFAULT_NAME)
1137 attachment["name"] = name 1139 attachment["name"] = name
1138 if C.MESS_KEY_MEDIA_TYPE not in attachment: 1140 if C.MESS_KEY_MEDIA_TYPE not in attachment:
1139 media_type = mimetypes.guess_type(attachment['url'], strict=False)[0] 1141 media_type = mimetypes.guess_type(attachment['name'], strict=False)[0]
1140 if media_type: 1142 if media_type:
1141 attachment[C.MESS_KEY_MEDIA_TYPE] = media_type 1143 attachment[C.MESS_KEY_MEDIA_TYPE] = media_type
1142 return data 1144 return data
1143 1145
1144 def skipEmptyMessage(self, data): 1146 def skipEmptyMessage(self, data):