Files
web-core-fr-gp/activitystreams/attachment.go
T

48 lines
1.2 KiB
Go
Raw Normal View History

package activitystreams
2021-03-24 15:45:58 -04:00
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
2021-09-13 15:32:10 -04:00
const (
AttachImage AttachmentType = "Image"
AttachDocument AttachmentType = "Document"
)
2021-03-24 15:45:58 -04:00
// 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 {
2021-09-13 15:32:10 -04:00
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
2021-03-24 15:45:58 -04:00
extIdx := strings.LastIndexByte(url, '.')
if extIdx > -1 {
2021-09-13 15:32:10 -04:00
fileType = mime.TypeByExtension(url[extIdx:])
2021-03-24 15:45:58 -04:00
}
return Attachment{
2021-09-13 15:32:10 -04:00
Type: attachType,
2021-03-24 15:45:58 -04:00
URL: url,
2021-09-13 15:32:10 -04:00
MediaType: fileType,
2021-03-24 15:45:58 -04:00
}
}