diff --git a/go.mod b/go.mod index 72c3e9e..4f41358 100644 --- a/go.mod +++ b/go.mod @@ -43,6 +43,7 @@ require ( github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect github.com/jackc/puddle/v2 v2.2.1 // indirect github.com/klauspost/compress v1.17.9 // indirect + github.com/ldez/mimetype v0.3.0 // indirect github.com/leodido/go-urn v1.4.0 // indirect github.com/mattn/go-colorable v0.1.13 // indirect github.com/mattn/go-isatty v0.0.20 // indirect diff --git a/go.sum b/go.sum index 9e0fe0d..da6162a 100644 --- a/go.sum +++ b/go.sum @@ -94,6 +94,8 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc= github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw= +github.com/ldez/mimetype v0.3.0 h1:F4g3zubm207mB4I8wv9dBfcSMiIVUme7CQLz7ocx8hk= +github.com/ldez/mimetype v0.3.0/go.mod h1:Lupzzb723ai7sMWfUuO6roX/jF48TiV22zVJrh3cGTc= github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ= github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI= github.com/lib/pq v1.2.0 h1:LXpIM/LZ5xGFhOpXAQUIMM1HdyqzVYM13zNdjCEEcA0= diff --git a/pkg/versia/utils/content_map.go b/pkg/versia/utils/content_map.go index dd69385..3f58a8d 100644 --- a/pkg/versia/utils/content_map.go +++ b/pkg/versia/utils/content_map.go @@ -4,12 +4,12 @@ import ( "encoding/json" "errors" "fmt" - "slices" + "github.com/ldez/mimetype" ) var ( - validTextContentTypes = []string{"text/html", "text/plain"} - validImageContentTypes = []string{"image/png", "image/jpeg", "image/gif", "image/svg+xml"} + preferredTextContentTypes = []string{"text/html", "text/plain"} + preferredImageContentTypes = []string{"image/png", "image/jpeg", "image/gif", "image/svg+xml", "image/webp"} ) // ContentMap is a map of content types to their respective content. @@ -23,7 +23,7 @@ func (e UnexpectedContentTypeError) Error() string { return fmt.Sprintf("unexpected content type: %s", e.MIMEType) } -func (m ContentMap[T]) unmarshalJSON(raw []byte, valid []string) error { +func (m ContentMap[T]) unmarshalJSON(raw []byte, mimetypeChecker func(type_ string) bool) error { var cm map[string]json.RawMessage if err := json.Unmarshal(raw, &cm); err != nil { return err @@ -33,11 +33,9 @@ func (m ContentMap[T]) unmarshalJSON(raw []byte, valid []string) error { errs := make([]error, 0) for k, v := range cm { - if valid != nil { - if !slices.Contains(valid, k) { - errs = append(errs, UnexpectedContentTypeError{k}) - continue - } + if !mimetypeChecker(k) { + errs = append(errs, UnexpectedContentTypeError{k}) + continue } var c T @@ -76,11 +74,11 @@ type TextContent struct { type TextContentTypeMap ContentMap[TextContent] func (t TextContentTypeMap) UnmarshalJSON(data []byte) error { - return (ContentMap[TextContent])(t).unmarshalJSON(data, validTextContentTypes) + return (ContentMap[TextContent])(t).unmarshalJSON(data, mimetype.IsText) } func (t TextContentTypeMap) String() string { - if c := (ContentMap[TextContent])(t).getPreferred(validTextContentTypes); c != nil { + if c := (ContentMap[TextContent])(t).getPreferred(preferredImageContentTypes); c != nil { return c.Content } @@ -116,11 +114,11 @@ type DataHash struct { type ImageContentMap ContentMap[File] func (i ImageContentMap) UnmarshalJSON(data []byte) error { - return (ContentMap[File])(i).unmarshalJSON(data, validImageContentTypes) + return (ContentMap[File])(i).unmarshalJSON(data, mimetype.IsImage) } func (i ImageContentMap) String() string { - if c := (ContentMap[File])(i).getPreferred(validImageContentTypes); c != nil { + if c := (ContentMap[File])(i).getPreferred(preferredImageContentTypes); c != nil { return c.Content.String() } @@ -132,7 +130,9 @@ type NoteAttachmentContentMap ContentMap[File] var ErrContentMapEntryNotRemote = errors.New("content map entry not remote") func (i NoteAttachmentContentMap) UnmarshalJSON(data []byte) error { - if err := (ContentMap[File])(i).unmarshalJSON(data, nil); err != nil { + if err := (ContentMap[File])(i).unmarshalJSON(data, func(type_ string) bool { + return true + }); err != nil { return err }