Skip to content

Commit 0ad2ae1

Browse files
committed
Add support to filter media by dimensions and content type precedence for media support config
1 parent 6019d3d commit 0ad2ae1

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed

Diff for: handlers/media.go

+17-3
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@ const (
2020
)
2121

2222
type MediaTypeSupport struct {
23-
Types []string
24-
MaxBytes int
23+
Types []string
24+
MaxBytes int
25+
MaxWidth int
26+
MaxHeight int
2527
}
2628

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

8486
mediaType, _ := parseContentType(media.ContentType())
85-
mediaSupport := support[mediaType]
87+
mediaSupport, ok := support[MediaType(media.ContentType())]
88+
if !ok {
89+
mediaSupport = support[mediaType]
90+
}
8691

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

105+
// narrow down the candidates to the ones that don't exceed our max dimensions
106+
if mediaSupport.MaxWidth > 0 && mediaSupport.MaxHeight > 0 {
107+
candidates = filterMediaByDimensions(candidates, mediaSupport.MaxWidth, mediaSupport.MaxHeight)
108+
}
109+
100110
// if we have no candidates, we can't use this media
101111
if len(candidates) == 0 {
102112
return nil, nil
@@ -142,6 +152,10 @@ func filterMediaBySize(in []courier.Media, maxBytes int) []courier.Media {
142152
return filterMedia(in, func(m courier.Media) bool { return m.Size() <= maxBytes })
143153
}
144154

155+
func filterMediaByDimensions(in []courier.Media, maxWidth int, MaxHeight int) []courier.Media {
156+
return filterMedia(in, func(m courier.Media) bool { return m.Width() <= maxWidth && m.Height() <= MaxHeight })
157+
}
158+
145159
func filterMedia(in []courier.Media, f func(courier.Media) bool) []courier.Media {
146160
filtered := make([]courier.Media, 0, len(in))
147161
for _, m := range in {

Diff for: handlers/media_test.go

+22
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,28 @@ func TestResolveAttachments(t *testing.T) {
134134
mediaSupport: map[handlers.MediaType]handlers.MediaTypeSupport{},
135135
err: "invalid attachment format: http://mock.com/1234/test.jpg",
136136
},
137+
{ // 14: resolveable uploaded image URL with matching dimensions
138+
attachments: []string{"image/jpeg:http://mock.com/1234/test.jpg"},
139+
mediaSupport: map[handlers.MediaType]handlers.MediaTypeSupport{handlers.MediaTypeImage: {Types: []string{"image/jpeg", "image/png"}, MaxWidth: 1000, MaxHeight: 1000}},
140+
allowURLOnly: true,
141+
resolved: []*handlers.Attachment{
142+
{Type: handlers.MediaTypeImage, Name: "test.jpg", ContentType: "image/jpeg", URL: "http://mock.com/1234/test.jpg", Media: imageJPG, Thumbnail: nil},
143+
},
144+
},
145+
{ // 15: resolveable uploaded image URL without matching dimensions
146+
attachments: []string{"image/jpeg:http://mock.com/1234/test.jpg"},
147+
mediaSupport: map[handlers.MediaType]handlers.MediaTypeSupport{handlers.MediaTypeImage: {Types: []string{"image/jpeg", "image/png"}, MaxWidth: 100, MaxHeight: 100}},
148+
allowURLOnly: true,
149+
resolved: []*handlers.Attachment{},
150+
},
151+
{ // 16: resolveable uploaded image URL without matching dimensions by specific content type precendence
152+
attachments: []string{"image/jpeg:http://mock.com/1234/test.jpg"},
153+
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}},
154+
allowURLOnly: true,
155+
resolved: []*handlers.Attachment{
156+
{Type: handlers.MediaTypeImage, Name: "test.jpg", ContentType: "image/jpeg", URL: "http://mock.com/1234/test.jpg", Media: imageJPG, Thumbnail: nil},
157+
},
158+
},
137159
}
138160

139161
for i, tc := range tcs {

0 commit comments

Comments
 (0)