Skip to content

Commit b3c60bc

Browse files
committed
Add support to filter media by dimensions and content type precedence for media support config
1 parent ff06bf4 commit b3c60bc

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-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
@@ -84,7 +86,10 @@ func resolveAttachment(ctx context.Context, b courier.Backend, contentType, medi
8486
}
8587

8688
mediaType, _ := parseContentType(media.ContentType())
87-
mediaSupport := support[mediaType]
89+
mediaSupport, ok := support[MediaType(media.ContentType())]
90+
if !ok {
91+
mediaSupport = support[mediaType]
92+
}
8893

8994
// our candidates are the uploaded media and any alternates of the same media type
9095
candidates := append([]courier.Media{media}, filterMediaByType(media.Alternates(), mediaType)...)
@@ -99,6 +104,11 @@ func resolveAttachment(ctx context.Context, b courier.Backend, contentType, medi
99104
candidates = filterMediaBySize(candidates, mediaSupport.MaxBytes)
100105
}
101106

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

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

Diff for: handlers/media_test.go

+23
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,29 @@ func TestResolveAttachments(t *testing.T) {
139139
mediaSupport: map[handlers.MediaType]handlers.MediaTypeSupport{},
140140
err: "invalid attachment format: http://mock.com/1234/test.jpg",
141141
},
142+
{ // 14: resolveable uploaded image URL with matching dimensions
143+
attachments: []string{"image/jpeg:http://mock.com/1234/test.jpg"},
144+
mediaSupport: map[handlers.MediaType]handlers.MediaTypeSupport{handlers.MediaTypeImage: {Types: []string{"image/jpeg", "image/png"}, MaxWidth: 1000, MaxHeight: 1000}},
145+
allowURLOnly: true,
146+
resolved: []*handlers.Attachment{
147+
{Type: handlers.MediaTypeImage, Name: "test.jpg", ContentType: "image/jpeg", URL: "http://mock.com/1234/test.jpg", Media: imageJPG, Thumbnail: nil},
148+
},
149+
},
150+
{ // 15: resolveable uploaded image URL without matching dimensions
151+
attachments: []string{"image/jpeg:http://mock.com/1234/test.jpg"},
152+
mediaSupport: map[handlers.MediaType]handlers.MediaTypeSupport{handlers.MediaTypeImage: {Types: []string{"image/jpeg", "image/png"}, MaxWidth: 100, MaxHeight: 100}},
153+
allowURLOnly: true,
154+
resolved: []*handlers.Attachment{},
155+
errors: []*courier.ChannelError{courier.ErrorMediaUnresolveable("image/jpeg")},
156+
},
157+
{ // 16: resolveable uploaded image URL without matching dimensions by specific content type precendence
158+
attachments: []string{"image/jpeg:http://mock.com/1234/test.jpg"},
159+
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}},
160+
allowURLOnly: true,
161+
resolved: []*handlers.Attachment{
162+
{Type: handlers.MediaTypeImage, Name: "test.jpg", ContentType: "image/jpeg", URL: "http://mock.com/1234/test.jpg", Media: imageJPG, Thumbnail: nil},
163+
},
164+
},
142165
}
143166

144167
for i, tc := range tcs {

0 commit comments

Comments
 (0)