Skip to content

Commit

Permalink
Add support to filter media by dimensions and content type precedence…
Browse files Browse the repository at this point in the history
… for media support config
  • Loading branch information
norkans7 committed Mar 11, 2024
1 parent 6019d3d commit 0ad2ae1
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
20 changes: 17 additions & 3 deletions handlers/media.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ const (
)

type MediaTypeSupport struct {
Types []string
MaxBytes int
Types []string
MaxBytes int
MaxWidth int
MaxHeight int
}

// Attachment is a resolved attachment
Expand Down Expand Up @@ -82,7 +84,10 @@ func resolveAttachment(ctx context.Context, b courier.Backend, attachment string
}

mediaType, _ := parseContentType(media.ContentType())
mediaSupport := support[mediaType]
mediaSupport, ok := support[MediaType(media.ContentType())]
if !ok {
mediaSupport = support[mediaType]
}

// our candidates are the uploaded media and any alternates of the same media type
candidates := append([]courier.Media{media}, filterMediaByType(media.Alternates(), mediaType)...)
Expand All @@ -97,6 +102,11 @@ func resolveAttachment(ctx context.Context, b courier.Backend, attachment string
candidates = filterMediaBySize(candidates, mediaSupport.MaxBytes)
}

// narrow down the candidates to the ones that don't exceed our max dimensions
if mediaSupport.MaxWidth > 0 && mediaSupport.MaxHeight > 0 {
candidates = filterMediaByDimensions(candidates, mediaSupport.MaxWidth, mediaSupport.MaxHeight)
}

// if we have no candidates, we can't use this media
if len(candidates) == 0 {
return nil, nil
Expand Down Expand Up @@ -142,6 +152,10 @@ func filterMediaBySize(in []courier.Media, maxBytes int) []courier.Media {
return filterMedia(in, func(m courier.Media) bool { return m.Size() <= maxBytes })
}

func filterMediaByDimensions(in []courier.Media, maxWidth int, MaxHeight int) []courier.Media {
return filterMedia(in, func(m courier.Media) bool { return m.Width() <= maxWidth && m.Height() <= MaxHeight })
}

func filterMedia(in []courier.Media, f func(courier.Media) bool) []courier.Media {
filtered := make([]courier.Media, 0, len(in))
for _, m := range in {
Expand Down
22 changes: 22 additions & 0 deletions handlers/media_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,28 @@ func TestResolveAttachments(t *testing.T) {
mediaSupport: map[handlers.MediaType]handlers.MediaTypeSupport{},
err: "invalid attachment format: http://mock.com/1234/test.jpg",
},
{ // 14: resolveable uploaded image URL with matching dimensions
attachments: []string{"image/jpeg:http://mock.com/1234/test.jpg"},
mediaSupport: map[handlers.MediaType]handlers.MediaTypeSupport{handlers.MediaTypeImage: {Types: []string{"image/jpeg", "image/png"}, MaxWidth: 1000, MaxHeight: 1000}},
allowURLOnly: true,
resolved: []*handlers.Attachment{
{Type: handlers.MediaTypeImage, Name: "test.jpg", ContentType: "image/jpeg", URL: "http://mock.com/1234/test.jpg", Media: imageJPG, Thumbnail: nil},
},
},
{ // 15: resolveable uploaded image URL without matching dimensions
attachments: []string{"image/jpeg:http://mock.com/1234/test.jpg"},
mediaSupport: map[handlers.MediaType]handlers.MediaTypeSupport{handlers.MediaTypeImage: {Types: []string{"image/jpeg", "image/png"}, MaxWidth: 100, MaxHeight: 100}},
allowURLOnly: true,
resolved: []*handlers.Attachment{},
},
{ // 16: resolveable uploaded image URL without matching dimensions by specific content type precendence
attachments: []string{"image/jpeg:http://mock.com/1234/test.jpg"},
mediaSupport: map[handlers.MediaType]handlers.MediaTypeSupport{handlers.MediaTypeImage: {Types: []string{"image/jpeg", "image/png"}, MaxWidth: 100, MaxHeight: 100}, handlers.MediaType("image/jpeg"): {Types: []string{"image/jpeg", "image/png"}, MaxWidth: 1000, MaxHeight: 1000}},
allowURLOnly: true,
resolved: []*handlers.Attachment{
{Type: handlers.MediaTypeImage, Name: "test.jpg", ContentType: "image/jpeg", URL: "http://mock.com/1234/test.jpg", Media: imageJPG, Thumbnail: nil},
},
},
}

for i, tc := range tcs {
Expand Down

0 comments on commit 0ad2ae1

Please sign in to comment.