core (xmpp): use pathlib.Path to complete attachments names, and use name to guess type

This commit is contained in:
Goffi 2020-02-26 15:54:34 +01:00
commit b0c3d4d8ac

View file

@ -23,6 +23,7 @@ import uuid
import mimetypes import mimetypes
from urllib.parse import urlparse, unquote from urllib.parse import urlparse, unquote
from functools import partial from functools import partial
from pathlib import Path
import shortuuid import shortuuid
from sat.core.i18n import _ from sat.core.i18n import _
from sat.core.constants import Const as C from sat.core.constants import Const as C
@ -1133,10 +1134,11 @@ class SatMessageProtocol(xmppim.MessageProtocol):
"""Complete missing metadata of attachments""" """Complete missing metadata of attachments"""
for attachment in data['extra'].get(C.MESS_KEY_ATTACHMENTS, []): for attachment in data['extra'].get(C.MESS_KEY_ATTACHMENTS, []):
if "name" not in attachment: if "name" not in attachment:
name = unquote(urlparse(attachment['url']).path.rsplit('/', 1)[-1]) name = (Path(unquote(urlparse(attachment['url']).path)).name
or C.FILE_DEFAULT_NAME)
attachment["name"] = name attachment["name"] = name
if C.MESS_KEY_MEDIA_TYPE not in attachment: if C.MESS_KEY_MEDIA_TYPE not in attachment:
media_type = mimetypes.guess_type(attachment['url'], strict=False)[0] media_type = mimetypes.guess_type(attachment['name'], strict=False)[0]
if media_type: if media_type:
attachment[C.MESS_KEY_MEDIA_TYPE] = media_type attachment[C.MESS_KEY_MEDIA_TYPE] = media_type
return data return data