Add NewImageAttachment func

This commit is contained in:
Matt Baer
2021-03-24 15:45:58 -04:00
parent 48cbbf652a
commit a77cafb3ae
2 changed files with 61 additions and 0 deletions
+21
View File
@@ -1,5 +1,10 @@
package activitystreams
import (
"mime"
"strings"
)
type Attachment struct {
Type AttachmentType `json:"type"`
URL string `json:"url"`
@@ -10,3 +15,19 @@ type Attachment struct {
type AttachmentType string
const AttachImage AttachmentType = "Image"
// 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
extIdx := strings.LastIndexByte(url, '.')
if extIdx > -1 {
imgType = mime.TypeByExtension(url[extIdx:])
}
return &Attachment{
Type: AttachImage,
URL: url,
MediaType: imgType,
}
}