Skip to content

Use filetype package to check file content for application/octet-stre… #737

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

Merged
merged 1 commit 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
2 changes: 1 addition & 1 deletion attachments.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func FetchAndStoreAttachment(ctx context.Context, b Backend, channel Channel, at

// prioritize to use the response content type header if provided
contentTypeHeader := trace.Response.Header.Get("Content-Type")
if contentTypeHeader != "" {
if contentTypeHeader != "" && contentTypeHeader != "application/octet-stream" {
mimeType, _, _ = mime.ParseMediaType(contentTypeHeader)
if extension == "" {
extensions, err := mime.ExtensionsByType(mimeType)
Expand Down
9 changes: 9 additions & 0 deletions attachments_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ func TestFetchAndStoreAttachment(t *testing.T) {
"http://mock.com/media/hello2": {
httpx.NewMockResponse(200, map[string]string{"Content-Type": "image/jpeg"}, testJPG),
},
"http://mock.com/media/hello3": {
httpx.NewMockResponse(200, map[string]string{"Content-Type": "application/octet-stream"}, testJPG),
},
"http://mock.com/media/hello.mp3": {
httpx.NewMockResponse(502, nil, []byte(`My gateways!`)),
},
Expand Down Expand Up @@ -82,6 +85,12 @@ func TestFetchAndStoreAttachment(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, &courier.Attachment{ContentType: "unavailable", URL: "http://mock.com/media/hello.pdf"}, att)

att, err = courier.FetchAndStoreAttachment(ctx, mb, mockChannel, "http://mock.com/media/hello3", clog)
assert.NoError(t, err)
assert.Equal(t, "image/jpeg", att.ContentType)
assert.Equal(t, "https://backend.com/attachments/338ff339-5663-49ed-8ef6-384876655d1b.jpg", att.URL)
assert.Equal(t, 17301, att.Size)

// an actual error on our part should be returned as an error
mb.SetStorageError(errors.New("boom"))

Expand Down
Loading