forked from MirrorHub/mautrix-whatsapp
Add some sanity overrides for mime -> extension mapping
This commit is contained in:
parent
ca5fcc42ba
commit
7b5764919a
1 changed files with 33 additions and 3 deletions
34
portal.go
34
portal.go
|
@ -1619,6 +1619,32 @@ type MediaMessageWithDuration interface {
|
|||
GetSeconds() uint32
|
||||
}
|
||||
|
||||
// MimeExtensionSanityOverrides includes extensions for various common mimetypes.
|
||||
//
|
||||
// This is necessary because sometimes the OS mimetype database and Go interact in weird ways,
|
||||
// which causes very obscure extensions to be first in the array for common mimetypes
|
||||
// (e.g. image/jpeg -> .jpe, text/plain -> ,v).
|
||||
var MimeExtensionSanityOverrides = map[string]string{
|
||||
"image/png": ".png",
|
||||
"image/webp": ".webp",
|
||||
"image/jpeg": ".jpg",
|
||||
"image/tiff": ".tiff",
|
||||
"image/heif": ".heic",
|
||||
"image/heic": ".heic",
|
||||
|
||||
"audio/mpeg": ".mp3",
|
||||
"audio/ogg": ".ogg",
|
||||
"audio/webm": ".webm",
|
||||
"video/mp4": ".mp4",
|
||||
"video/mpeg": ".mpeg",
|
||||
"video/webm": ".webm",
|
||||
|
||||
"text/plain": ".txt",
|
||||
"text/html": ".html",
|
||||
|
||||
"application/xml": ".xml",
|
||||
}
|
||||
|
||||
func (portal *Portal) convertMediaMessage(intent *appservice.IntentAPI, source *User, info *types.MessageInfo, msg MediaMessage) *ConvertedMessage {
|
||||
messageWithCaption, ok := msg.(MediaMessageWithCaption)
|
||||
var captionContent *event.MessageEventContent
|
||||
|
@ -1695,11 +1721,15 @@ func (portal *Portal) convertMediaMessage(intent *appservice.IntentAPI, source *
|
|||
content.Body = mimeClass
|
||||
}
|
||||
|
||||
ext, ok := MimeExtensionSanityOverrides[strings.Split(msg.GetMimetype(), ";")[0]]
|
||||
if !ok {
|
||||
exts, _ := mime.ExtensionsByType(msg.GetMimetype())
|
||||
if exts != nil && len(exts) > 0 {
|
||||
content.Body += exts[0]
|
||||
if len(exts) > 0 {
|
||||
ext = exts[0]
|
||||
}
|
||||
}
|
||||
content.Body += ext
|
||||
}
|
||||
|
||||
msgWithDuration, ok := msg.(MediaMessageWithDuration)
|
||||
if ok {
|
||||
|
|
Loading…
Reference in a new issue