Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prioritize the content-type header for attachments response for mimet… #736

Merged
merged 2 commits into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 19 additions & 17 deletions attachments.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,23 +113,10 @@
extension = extension[1:]
}

// first try getting our mime type from the first 300 bytes of our body
fileType, _ := filetype.Match(trace.ResponseBody[:300])
if fileType != filetype.Unknown {
mimeType = fileType.MIME.Value
extension = fileType.Extension
} else {
// if that didn't work, try from our extension
fileType = filetype.GetType(extension)
if fileType != filetype.Unknown {
mimeType = fileType.MIME.Value
extension = fileType.Extension
}
}

// we still don't know our mime type, use our content header instead
if mimeType == "" {
mimeType, _, _ = mime.ParseMediaType(trace.Response.Header.Get("Content-Type"))
// prioritize to use the response content type header if provided
contentTypeHeader := trace.Response.Header.Get("Content-Type")
if contentTypeHeader != "" {
mimeType, _, _ = mime.ParseMediaType(contentTypeHeader)
if extension == "" {
extensions, err := mime.ExtensionsByType(mimeType)
if extensions == nil || err != nil {
Expand All @@ -138,6 +125,21 @@
extension = extensions[0][1:]
}
}
} else {

// first try getting our mime type from the first 300 bytes of our body
fileType, _ := filetype.Match(trace.ResponseBody[:300])
if fileType != filetype.Unknown {
mimeType = fileType.MIME.Value
extension = fileType.Extension
} else {
// if that didn't work, try from our extension
fileType = filetype.GetType(extension)
if fileType != filetype.Unknown {
mimeType = fileType.MIME.Value
extension = fileType.Extension
}

Check warning on line 141 in attachments.go

View check run for this annotation

Codecov / codecov/patch

attachments.go#L139-L141

Added lines #L139 - L141 were not covered by tests
}
}

storageURL, err := b.SaveAttachment(ctx, channel, mimeType, trace.ResponseBody, extension)
Expand Down
20 changes: 17 additions & 3 deletions attachments_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ func TestFetchAndStoreAttachment(t *testing.T) {
"http://mock.com/media/hello.jpg": {
httpx.NewMockResponse(200, nil, testJPG),
},
"http://mock.com/media/hello2": {
httpx.NewMockResponse(200, map[string]string{"Content-Type": "image/jpeg"}, testJPG),
},
"http://mock.com/media/hello.mp3": {
httpx.NewMockResponse(502, nil, []byte(`My gateways!`)),
},
Expand Down Expand Up @@ -53,15 +56,26 @@ func TestFetchAndStoreAttachment(t *testing.T) {
assert.Len(t, clog.HTTPLogs(), 1)
assert.Equal(t, "http://mock.com/media/hello.jpg", clog.HTTPLogs()[0].URL)

att, err = courier.FetchAndStoreAttachment(ctx, mb, mockChannel, "http://mock.com/media/hello2", clog)
assert.NoError(t, err)
assert.Equal(t, "image/jpeg", att.ContentType)
assert.Equal(t, "https://backend.com/attachments/547deaf7-7620-4434-95b3-58675999c4b7.jpe", att.URL)
assert.Equal(t, 17301, att.Size)

assert.Len(t, mb.SavedAttachments(), 2)
assert.Equal(t, &test.SavedAttachment{Channel: mockChannel, ContentType: "image/jpeg", Data: testJPG, Extension: "jpg"}, mb.SavedAttachments()[0])
assert.Len(t, clog.HTTPLogs(), 2)
assert.Equal(t, "http://mock.com/media/hello2", clog.HTTPLogs()[1].URL)

// a non-200 response should return an unavailable attachment
att, err = courier.FetchAndStoreAttachment(ctx, mb, mockChannel, "http://mock.com/media/hello.mp3", clog)
assert.NoError(t, err)
assert.Equal(t, &courier.Attachment{ContentType: "unavailable", URL: "http://mock.com/media/hello.mp3"}, att)

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

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