ActivityPub: Include MimeType in profile picture attribute

This commit is contained in:
Jan-Lukas Else 2022-11-21 16:08:34 +01:00
parent 4dee23e561
commit a60a3f92bd
3 changed files with 42 additions and 5 deletions

View File

@ -69,8 +69,9 @@ type asPerson struct {
}
type asAttachment struct {
Type string `json:"type,omitempty"`
URL string `json:"url,omitempty"`
Type string `json:"type,omitempty"`
URL string `json:"url,omitempty"`
MediaType string `json:"mediaType,omitempty"`
}
type asTag struct {
@ -188,10 +189,11 @@ func (a *goBlog) toAsPerson(blog string) *asPerson {
})),
},
}
if a.cfg.User.Picture != "" {
if pic := a.cfg.User.Picture; pic != "" {
asBlog.Icon = &asAttachment{
Type: "Image",
URL: a.cfg.User.Picture,
Type: "Image",
URL: pic,
MediaType: mimeTypeFromUrl(pic),
}
}
return asBlog

View File

@ -6,6 +6,7 @@ import (
"fmt"
"io"
"math/rand"
"mime"
"net/http"
"net/http/httptest"
"net/url"
@ -403,3 +404,17 @@ func stringToInt(s string) int {
func loStringNotEmpty(s string, _ int) bool {
return s != ""
}
func mimeTypeFromUrl(url string) string {
ext := path.Ext(url)
mimeType := mime.TypeByExtension(ext)
if mimeType == "" {
switch ext {
case ".jpg":
mimeType = "image/jpeg"
default:
mimeType = "image/" + strings.TrimPrefix(ext, ".")
}
}
return mimeType
}

View File

@ -1,6 +1,7 @@
package main
import (
"strconv"
"testing"
"github.com/stretchr/testify/assert"
@ -167,3 +168,22 @@ func Test_groupStrings(t *testing.T) {
assert.Equal(t, "H", groups[3].Identifier)
assert.Equal(t, "🚴", groups[4].Identifier)
}
func Test_mimeTypeFromUrl(t *testing.T) {
type test struct {
url string
want string
}
tests := []*test{
{url: "https://example.com/profile.jpg", want: "image/jpeg"},
{url: "https://example.com/profile.jpeg", want: "image/jpeg"},
{url: "https://example.com/profile.png", want: "image/png"},
}
for i, tt := range tests {
t.Run(strconv.Itoa(i), func(t *testing.T) {
if got := mimeTypeFromUrl(tt.url); got != tt.want {
t.Errorf("mimeTypeFromUrl() = %v, want %v", got, tt.want)
}
})
}
}