Skip to content

Commit

Permalink
feat: respect specified content-type if specified and is other than a…
Browse files Browse the repository at this point in the history
…pplication/octet-stream (#139)
  • Loading branch information
dbarrosop authored Jan 4, 2023
1 parent be5e5f1 commit b9aebcb
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 10 deletions.
2 changes: 1 addition & 1 deletion controller/update_file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func TestUpdateFile(t *testing.T) {
logger.SetLevel(logrus.ErrorLevel)

file := fakeFile{
"some content", fakeFileMetadata{"a_file.txt", uuid.New().String()},
"some content", "", fakeFileMetadata{"a_file.txt", uuid.New().String()},
}

c := gomock.NewController(t)
Expand Down
9 changes: 7 additions & 2 deletions controller/upload_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,18 @@ func (ctrl *Controller) getMultipartFile(file fileData) (multipart.File, string,
return nil, "", InternalServerError(fmt.Errorf("problem opening file %s: %w", file.Name, err))
}

contentType, err := mimetype.DetectReader(fileContent)
contentType := file.header.Header.Get("Content-Type")
if contentType != "" && contentType != "application/octet-stream" {
return fileContent, contentType, nil
}

mt, err := mimetype.DetectReader(fileContent)
if err != nil {
return nil, "",
InternalServerError(fmt.Errorf("problem figuring out content type for file %s: %w", file.Name, err))
}

return fileContent, contentType.String(), nil
return fileContent, mt.String(), nil
}

func (ctrl *Controller) upload(
Expand Down
70 changes: 63 additions & 7 deletions controller/upload_file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"mime/multipart"
"net/http"
"net/http/httptest"
"net/textproto"
"strings"
"testing"

Expand All @@ -33,8 +35,9 @@ func (f fakeFileMetadata) encode() string {
}

type fakeFile struct {
contents string
md fakeFileMetadata
contents string
contentType string
md fakeFileMetadata
}

func createMultiForm(t *testing.T, files ...fakeFile) (io.Reader, string) {
Expand All @@ -53,7 +56,12 @@ func createMultiForm(t *testing.T, files ...fakeFile) (io.Reader, string) {
}

for _, file := range files {
formWriter, err = writer.CreateFormFile("file[]", file.md.Name)
h := make(textproto.MIMEHeader)
h.Set("Content-Disposition",
fmt.Sprintf(`form-data; name="%s"; filename="%s"`,
"file[]", file.md.Name))
h.Set("Content-Type", file.contentType)
formWriter, err := writer.CreatePart(h)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -105,8 +113,8 @@ func TestUploadFile(t *testing.T) {
logger.SetLevel(logrus.ErrorLevel)

files := []fakeFile{
{"some content", fakeFileMetadata{"a_file.txt", uuid.New().String()}},
{"more content", fakeFileMetadata{"another_file.md", uuid.New().String()}},
{"some content", "", fakeFileMetadata{"a_file.txt", uuid.New().String()}},
{"more content", "text/markdown", fakeFileMetadata{"another_file.md", uuid.New().String()}},
}

c := gomock.NewController(t)
Expand All @@ -127,8 +135,9 @@ func TestUploadFile(t *testing.T) {
UpdatedAt: "2021-12-15T13:26:52.082485+00:00",
}, nil)

for _, file := range files {
{ //nolint: dupl
// file 1
file := files[0]
metadataStorage.EXPECT().InitializeFile(
gomock.Any(),
file.md.ID,
Expand Down Expand Up @@ -173,6 +182,53 @@ func TestUploadFile(t *testing.T) {
nil)
}

{ //nolint: dupl
// file 2
file := files[1]
metadataStorage.EXPECT().InitializeFile(
gomock.Any(),
file.md.ID,
file.md.Name,
int64(len(file.contents)),
"blah",
"text/markdown",
gomock.Any(),
).Return(nil)

contentStorage.EXPECT().PutFile(
ReaderMatcher(
file.contents,
),
file.md.ID,
"text/markdown",
).Return("some-etag", nil)

metadataStorage.EXPECT().PopulateMetadata(
gomock.Any(),
file.md.ID,
file.md.Name,
int64(len(file.contents)),
"blah",
"some-etag",
true,
"text/markdown",
gomock.Any(),
).Return(
controller.FileMetadata{
ID: file.md.ID,
Name: file.md.Name,
Size: int64(len(file.contents)),
BucketID: "blah",
ETag: "some-etag",
CreatedAt: "", // ignored
UpdatedAt: "", // ignored
IsUploaded: true,
MimeType: "text/markdown",
UploadedByUserID: "some-valid-uuid",
},
nil)
}

ctrl := controller.New("http://asd", "/v1", "asdasd", metadataStorage, contentStorage, nil, logger)

router, _ := ctrl.SetupRouter(nil, "/v1", ginLogger(logger))
Expand Down Expand Up @@ -216,7 +272,7 @@ func TestUploadFile(t *testing.T) {
CreatedAt: "",
UpdatedAt: "",
IsUploaded: true,
MimeType: "text/plain; charset=utf-8",
MimeType: "text/markdown",
UploadedByUserID: "some-valid-uuid",
},
},
Expand Down

0 comments on commit b9aebcb

Please sign in to comment.