correctly handle attachments mimetype and set proper msgtype, fixes #27

This commit is contained in:
Aine
2022-09-13 17:21:00 +03:00
parent 7e92c023c8
commit 76bffd931c
5 changed files with 37 additions and 4 deletions

View File

@@ -2,25 +2,32 @@ package utils
import (
"bytes"
"strings"
"github.com/gabriel-vasile/mimetype"
"maunium.net/go/mautrix"
"maunium.net/go/mautrix/event"
)
type File struct {
Name string
Type string
MsgType event.MessageType
Length int
Content []byte
}
func NewFile(name, contentType string, content []byte) *File {
func NewFile(name string, content []byte) *File {
file := &File{
Name: name,
Type: contentType,
Content: content,
}
file.Length = len(content)
mtype := mimetype.Detect(content)
file.Type = mtype.String()
file.MsgType = mimeMsgType(file.Type)
return file
}
@@ -33,3 +40,23 @@ func (f *File) Convert() mautrix.ReqUploadMedia {
FileName: f.Name,
}
}
func mimeMsgType(mime string) event.MessageType {
if mime == "" {
return event.MsgFile
}
if !strings.Contains(mime, "/") {
return event.MsgFile
}
msection := strings.SplitN(mime, "/", 1)[0]
switch msection {
case "image":
return event.MsgImage
case "video":
return event.MsgVideo
case "audio":
return event.MsgAudio
default:
return event.MsgFile
}
}