fix(utils/content-map): match more mimetypes

This commit is contained in:
DevMiner 2024-08-28 02:04:16 +02:00
parent b62192b20b
commit 5343465da4
3 changed files with 17 additions and 14 deletions

1
go.mod
View file

@ -43,6 +43,7 @@ require (
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
github.com/jackc/puddle/v2 v2.2.1 // indirect github.com/jackc/puddle/v2 v2.2.1 // indirect
github.com/klauspost/compress v1.17.9 // 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/leodido/go-urn v1.4.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect github.com/mattn/go-isatty v0.0.20 // indirect

2
go.sum
View file

@ -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/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 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw= 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 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ=
github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI= 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= github.com/lib/pq v1.2.0 h1:LXpIM/LZ5xGFhOpXAQUIMM1HdyqzVYM13zNdjCEEcA0=

View file

@ -4,12 +4,12 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
"slices" "github.com/ldez/mimetype"
) )
var ( var (
validTextContentTypes = []string{"text/html", "text/plain"} preferredTextContentTypes = []string{"text/html", "text/plain"}
validImageContentTypes = []string{"image/png", "image/jpeg", "image/gif", "image/svg+xml"} preferredImageContentTypes = []string{"image/png", "image/jpeg", "image/gif", "image/svg+xml", "image/webp"}
) )
// ContentMap is a map of content types to their respective content. // 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) 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 var cm map[string]json.RawMessage
if err := json.Unmarshal(raw, &cm); err != nil { if err := json.Unmarshal(raw, &cm); err != nil {
return err return err
@ -33,11 +33,9 @@ func (m ContentMap[T]) unmarshalJSON(raw []byte, valid []string) error {
errs := make([]error, 0) errs := make([]error, 0)
for k, v := range cm { for k, v := range cm {
if valid != nil { if !mimetypeChecker(k) {
if !slices.Contains(valid, k) { errs = append(errs, UnexpectedContentTypeError{k})
errs = append(errs, UnexpectedContentTypeError{k}) continue
continue
}
} }
var c T var c T
@ -76,11 +74,11 @@ type TextContent struct {
type TextContentTypeMap ContentMap[TextContent] type TextContentTypeMap ContentMap[TextContent]
func (t TextContentTypeMap) UnmarshalJSON(data []byte) error { 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 { 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 return c.Content
} }
@ -116,11 +114,11 @@ type DataHash struct {
type ImageContentMap ContentMap[File] type ImageContentMap ContentMap[File]
func (i ImageContentMap) UnmarshalJSON(data []byte) error { 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 { 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() return c.Content.String()
} }
@ -132,7 +130,9 @@ type NoteAttachmentContentMap ContentMap[File]
var ErrContentMapEntryNotRemote = errors.New("content map entry not remote") var ErrContentMapEntryNotRemote = errors.New("content map entry not remote")
func (i NoteAttachmentContentMap) UnmarshalJSON(data []byte) error { 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 return err
} }