Skip to content

Commit 99a653a

Browse files
authored
Merge pull request #736 from nyaruka/attachment-mimetype
Prioritize the content-type header for attachments response for mimet…
2 parents ff06bf4 + 5de742b commit 99a653a

File tree

2 files changed

+36
-20
lines changed

2 files changed

+36
-20
lines changed

Diff for: attachments.go

+19-17
Original file line numberDiff line numberDiff line change
@@ -113,23 +113,10 @@ func FetchAndStoreAttachment(ctx context.Context, b Backend, channel Channel, at
113113
extension = extension[1:]
114114
}
115115

116-
// first try getting our mime type from the first 300 bytes of our body
117-
fileType, _ := filetype.Match(trace.ResponseBody[:300])
118-
if fileType != filetype.Unknown {
119-
mimeType = fileType.MIME.Value
120-
extension = fileType.Extension
121-
} else {
122-
// if that didn't work, try from our extension
123-
fileType = filetype.GetType(extension)
124-
if fileType != filetype.Unknown {
125-
mimeType = fileType.MIME.Value
126-
extension = fileType.Extension
127-
}
128-
}
129-
130-
// we still don't know our mime type, use our content header instead
131-
if mimeType == "" {
132-
mimeType, _, _ = mime.ParseMediaType(trace.Response.Header.Get("Content-Type"))
116+
// prioritize to use the response content type header if provided
117+
contentTypeHeader := trace.Response.Header.Get("Content-Type")
118+
if contentTypeHeader != "" {
119+
mimeType, _, _ = mime.ParseMediaType(contentTypeHeader)
133120
if extension == "" {
134121
extensions, err := mime.ExtensionsByType(mimeType)
135122
if extensions == nil || err != nil {
@@ -138,6 +125,21 @@ func FetchAndStoreAttachment(ctx context.Context, b Backend, channel Channel, at
138125
extension = extensions[0][1:]
139126
}
140127
}
128+
} else {
129+
130+
// first try getting our mime type from the first 300 bytes of our body
131+
fileType, _ := filetype.Match(trace.ResponseBody[:300])
132+
if fileType != filetype.Unknown {
133+
mimeType = fileType.MIME.Value
134+
extension = fileType.Extension
135+
} else {
136+
// if that didn't work, try from our extension
137+
fileType = filetype.GetType(extension)
138+
if fileType != filetype.Unknown {
139+
mimeType = fileType.MIME.Value
140+
extension = fileType.Extension
141+
}
142+
}
141143
}
142144

143145
storageURL, err := b.SaveAttachment(ctx, channel, mimeType, trace.ResponseBody, extension)

Diff for: attachments_test.go

+17-3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ func TestFetchAndStoreAttachment(t *testing.T) {
2020
"http://mock.com/media/hello.jpg": {
2121
httpx.NewMockResponse(200, nil, testJPG),
2222
},
23+
"http://mock.com/media/hello2": {
24+
httpx.NewMockResponse(200, map[string]string{"Content-Type": "image/jpeg"}, testJPG),
25+
},
2326
"http://mock.com/media/hello.mp3": {
2427
httpx.NewMockResponse(502, nil, []byte(`My gateways!`)),
2528
},
@@ -53,15 +56,26 @@ func TestFetchAndStoreAttachment(t *testing.T) {
5356
assert.Len(t, clog.HTTPLogs(), 1)
5457
assert.Equal(t, "http://mock.com/media/hello.jpg", clog.HTTPLogs()[0].URL)
5558

59+
att, err = courier.FetchAndStoreAttachment(ctx, mb, mockChannel, "http://mock.com/media/hello2", clog)
60+
assert.NoError(t, err)
61+
assert.Equal(t, "image/jpeg", att.ContentType)
62+
assert.Equal(t, "https://backend.com/attachments/547deaf7-7620-4434-95b3-58675999c4b7.jpe", att.URL)
63+
assert.Equal(t, 17301, att.Size)
64+
65+
assert.Len(t, mb.SavedAttachments(), 2)
66+
assert.Equal(t, &test.SavedAttachment{Channel: mockChannel, ContentType: "image/jpeg", Data: testJPG, Extension: "jpg"}, mb.SavedAttachments()[0])
67+
assert.Len(t, clog.HTTPLogs(), 2)
68+
assert.Equal(t, "http://mock.com/media/hello2", clog.HTTPLogs()[1].URL)
69+
5670
// a non-200 response should return an unavailable attachment
5771
att, err = courier.FetchAndStoreAttachment(ctx, mb, mockChannel, "http://mock.com/media/hello.mp3", clog)
5872
assert.NoError(t, err)
5973
assert.Equal(t, &courier.Attachment{ContentType: "unavailable", URL: "http://mock.com/media/hello.mp3"}, att)
6074

6175
// should have a logged HTTP request but no attachments will have been saved to storage
62-
assert.Len(t, clog.HTTPLogs(), 2)
63-
assert.Equal(t, "http://mock.com/media/hello.mp3", clog.HTTPLogs()[1].URL)
64-
assert.Len(t, mb.SavedAttachments(), 1)
76+
assert.Len(t, clog.HTTPLogs(), 3)
77+
assert.Equal(t, "http://mock.com/media/hello.mp3", clog.HTTPLogs()[2].URL)
78+
assert.Len(t, mb.SavedAttachments(), 2)
6579

6680
// same for a connection error
6781
att, err = courier.FetchAndStoreAttachment(ctx, mb, mockChannel, "http://mock.com/media/hello.pdf", clog)

0 commit comments

Comments
 (0)