2021-03-24 15:33:34 -04:00
|
|
|
package activitystreams
|
|
|
|
|
|
2021-03-24 15:45:58 -04:00
|
|
|
import (
|
|
|
|
|
"mime"
|
|
|
|
|
"strings"
|
|
|
|
|
)
|
|
|
|
|
|
2021-03-24 15:33:34 -04:00
|
|
|
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"
|
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.
|
2021-03-24 15:47:23 -04:00
|
|
|
func NewImageAttachment(url string) Attachment {
|
2021-03-24 15:45:58 -04:00
|
|
|
var imgType string
|
|
|
|
|
extIdx := strings.LastIndexByte(url, '.')
|
|
|
|
|
if extIdx > -1 {
|
|
|
|
|
imgType = mime.TypeByExtension(url[extIdx:])
|
|
|
|
|
}
|
2021-03-24 15:47:23 -04:00
|
|
|
return Attachment{
|
2021-03-24 15:45:58 -04:00
|
|
|
Type: AttachImage,
|
|
|
|
|
URL: url,
|
|
|
|
|
MediaType: imgType,
|
|
|
|
|
}
|
|
|
|
|
}
|