Skip to content

Commit

Permalink
Merge pull request #737 from nyaruka/attachment-mimetype
Browse files Browse the repository at this point in the history
Use filetype package to check file content for application/octet-stre…
  • Loading branch information
rowanseymour authored Apr 26, 2024
2 parents 95eedb6 + cd0bedd commit d254cb2
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
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

0 comments on commit d254cb2

Please sign in to comment.