Page MenuHomeMusing Studio

No OneTemporary

diff --git a/activitystreams/attachment.go b/activitystreams/attachment.go
index d221169..f8763e3 100644
--- a/activitystreams/attachment.go
+++ b/activitystreams/attachment.go
@@ -1,33 +1,47 @@
package activitystreams
import (
"mime"
"strings"
)
type Attachment struct {
Type AttachmentType `json:"type"`
URL string `json:"url"`
MediaType string `json:"mediaType"`
Name string `json:"name"`
}
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
// correct type and automatically detecting the MediaType based on the file
// extension.
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, '.')
if extIdx > -1 {
- imgType = mime.TypeByExtension(url[extIdx:])
+ fileType = mime.TypeByExtension(url[extIdx:])
}
return Attachment{
- Type: AttachImage,
+ Type: attachType,
URL: url,
- MediaType: imgType,
+ MediaType: fileType,
}
}
diff --git a/activitystreams/attachment_test.go b/activitystreams/attachment_test.go
index c96d1cd..e0d3b4d 100644
--- a/activitystreams/attachment_test.go
+++ b/activitystreams/attachment_test.go
@@ -1,40 +1,64 @@
package activitystreams
import (
"reflect"
"testing"
)
func TestNewImageAttachment(t *testing.T) {
type args struct {
url string
}
tests := []struct {
name string
args args
want *Attachment
}{
{name: "good svg", args: args{"https://writefreely.org/img/writefreely.svg"}, want: &Attachment{
Type: "Image",
URL: "https://writefreely.org/img/writefreely.svg",
MediaType: "image/svg+xml",
}},
{name: "good png", args: args{"https://i.snap.as/12345678.png"}, want: &Attachment{
Type: "Image",
URL: "https://i.snap.as/12345678.png",
MediaType: "image/png",
}},
{name: "no extension", args: args{"https://i.snap.as/12345678"}, want: &Attachment{
Type: "Image",
URL: "https://i.snap.as/12345678",
MediaType: "",
}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := NewImageAttachment(tt.args.url); !reflect.DeepEqual(got, tt.want) {
t.Errorf("NewImageAttachment() = %v, want %v", got, tt.want)
}
})
}
}
+
+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)
+ }
+ })
+ }
+}

File Metadata

Mime Type
text/x-diff
Expires
Sun, Apr 27, 10:17 AM (2 h, 27 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
3217110

Event Timeline