Skip to content

Commit

Permalink
fix(utils/content-map): match more mimetypes
Browse files Browse the repository at this point in the history
  • Loading branch information
TheDevMinerTV committed Aug 28, 2024
1 parent b62192b commit 5343465
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 14 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
28 changes: 14 additions & 14 deletions pkg/versia/utils/content_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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()
}

Expand All @@ -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
}

Expand Down

0 comments on commit 5343465

Please sign in to comment.