Support creating Document attachments

In particular, this will support audio attachments.

Ref T872
This commit is contained in:
Matt Baer
2021-09-13 15:32:10 -04:00
parent fd1559928a
commit 8b45bd4fcc
2 changed files with 43 additions and 5 deletions
+19 -5
View File
@@ -14,20 +14,34 @@ type Attachment struct {
type AttachmentType string type AttachmentType string
const AttachImage AttachmentType = "Image" const (
AttachImage AttachmentType = "Image"
AttachDocument AttachmentType = "Document"
)
// NewImageAttachment creates a new Attachment from the given URL, setting the // NewImageAttachment creates a new Attachment from the given URL, setting the
// correct type and automatically detecting the MediaType based on the file // correct type and automatically detecting the MediaType based on the file
// extension. // extension.
func NewImageAttachment(url string) Attachment { func NewImageAttachment(url string) Attachment {
var imgType string return newAttachment(url, AttachImage)
}
// NewDocumentAttachment creates a new Attachment from the given URL, setting the
// correct type and automatically detecting the MediaType based on the file
// extension.
func NewDocumentAttachment(url string) Attachment {
return newAttachment(url, AttachDocument)
}
func newAttachment(url string, attachType AttachmentType) Attachment {
var fileType string
extIdx := strings.LastIndexByte(url, '.') extIdx := strings.LastIndexByte(url, '.')
if extIdx > -1 { if extIdx > -1 {
imgType = mime.TypeByExtension(url[extIdx:]) fileType = mime.TypeByExtension(url[extIdx:])
} }
return Attachment{ return Attachment{
Type: AttachImage, Type: attachType,
URL: url, URL: url,
MediaType: imgType, MediaType: fileType,
} }
} }
+24
View File
@@ -38,3 +38,27 @@ func TestNewImageAttachment(t *testing.T) {
}) })
} }
} }
func TestNewDocumentAttachment(t *testing.T) {
type args struct {
url string
}
tests := []struct {
name string
args args
want Attachment
}{
{name: "mp3", args: args{"https://listen.as/matt/abc.mp3"}, want: Attachment{
Type: "Document",
URL: "https://listen.as/matt/abc.mp3",
MediaType: "audio/mpeg",
}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := NewDocumentAttachment(tt.args.url); !reflect.DeepEqual(got, tt.want) {
t.Errorf("NewDocumentAttachment() = %+v, want %+v", got, tt.want)
}
})
}
}