From 3c965af2df608b4f5bcde4226c4273276bbfa7d1 Mon Sep 17 00:00:00 2001 From: francisco souza Date: Tue, 13 Jul 2021 10:01:34 -0400 Subject: [PATCH] Set ContentType when loading objects from disk Closes #531. --- Dockerfile | 1 + ci/Dockerfile | 1 + main.go | 12 ++++++----- main_test.go | 60 ++++++++++++++++++++++++++++++--------------------- 4 files changed, 45 insertions(+), 29 deletions(-) diff --git a/Dockerfile b/Dockerfile index 33c5004bd4..bb16ad2e2d 100644 --- a/Dockerfile +++ b/Dockerfile @@ -11,6 +11,7 @@ ENV CGO_ENABLED=0 RUN go build -o fake-gcs-server FROM alpine:3.14.0 +RUN apk add --no-cache mailcap COPY --from=builder /code/fake-gcs-server /bin/fake-gcs-server RUN /bin/fake-gcs-server -h EXPOSE 4443 diff --git a/ci/Dockerfile b/ci/Dockerfile index 081be65a74..ee6319cfde 100644 --- a/ci/Dockerfile +++ b/ci/Dockerfile @@ -1,4 +1,5 @@ FROM alpine:3.14.0 +RUN apk add --no-cache mailcap ADD fake-gcs-server /bin/fake-gcs-server ENTRYPOINT ["/bin/fake-gcs-server", "-data", "/data"] diff --git a/main.go b/main.go index 5822dc7e84..652acef91f 100644 --- a/main.go +++ b/main.go @@ -9,6 +9,7 @@ import ( "fmt" "io/ioutil" "log" + "mime" "os" "os/signal" "path/filepath" @@ -91,11 +92,12 @@ func objectsFromBucket(localBucketPath, bucketName string) ([]fakestorage.Object return fmt.Errorf("could not read file %q: %w", path, err) } objects = append(objects, fakestorage.Object{ - BucketName: bucketName, - Name: objectKey, - Content: fileContent, - Crc32c: checksum.EncodedCrc32cChecksum(fileContent), - Md5Hash: checksum.EncodedMd5Hash(fileContent), + BucketName: bucketName, + Name: objectKey, + ContentType: mime.TypeByExtension(filepath.Ext(path)), + Content: fileContent, + Crc32c: checksum.EncodedCrc32cChecksum(fileContent), + Md5Hash: checksum.EncodedMd5Hash(fileContent), }) } return nil diff --git a/main_test.go b/main_test.go index c272fca213..3889f4a2b9 100644 --- a/main_test.go +++ b/main_test.go @@ -8,6 +8,7 @@ import ( "fmt" "io" "io/ioutil" + "mime" "os" "testing" @@ -17,7 +18,10 @@ import ( "github.com/sirupsen/logrus" ) +const testContentType = "text/plain; charset=utf-8" + func TestMain(m *testing.M) { + mime.AddExtensionType(".txt", testContentType) const emptyBucketDir = "testdata/basic/empty-bucket" err := ensureEmptyDir(emptyBucketDir) if err != nil { @@ -44,9 +48,10 @@ func TestGenerateObjectsFromFiles(t *testing.T) { folder: "testdata/basic", expectedObjects: []fakestorage.Object{ { - BucketName: "sample-bucket", - Name: "some_file.txt", - Content: []byte("Some amazing content to be loaded"), + BucketName: "sample-bucket", + Name: "some_file.txt", + Content: []byte("Some amazing content to be loaded"), + ContentType: testContentType, }, }, expectedEmptyBuckets: []string{"empty-bucket"}, @@ -56,19 +61,22 @@ func TestGenerateObjectsFromFiles(t *testing.T) { folder: "testdata/multi-level", expectedObjects: []fakestorage.Object{ { - BucketName: "some-bucket", - Name: "a/b/c/d/e/f/object1.txt", - Content: []byte("this is object 1\n"), + BucketName: "some-bucket", + Name: "a/b/c/d/e/f/object1.txt", + Content: []byte("this is object 1\n"), + ContentType: testContentType, }, { - BucketName: "some-bucket", - Name: "a/b/c/d/e/f/object2.txt", - Content: []byte("this is object 2\n"), + BucketName: "some-bucket", + Name: "a/b/c/d/e/f/object2.txt", + Content: []byte("this is object 2\n"), + ContentType: testContentType, }, { - BucketName: "some-bucket", - Name: "root-object.txt", - Content: []byte("r00t\n"), + BucketName: "some-bucket", + Name: "root-object.txt", + Content: []byte("r00t\n"), + ContentType: testContentType, }, }, }, @@ -85,24 +93,28 @@ func TestGenerateObjectsFromFiles(t *testing.T) { folder: "testdata/chaos", expectedObjects: []fakestorage.Object{ { - BucketName: "bucket1", - Name: "object1.txt", - Content: []byte("object 1\n"), + BucketName: "bucket1", + Name: "object1.txt", + Content: []byte("object 1\n"), + ContentType: testContentType, }, { - BucketName: "bucket1", - Name: "object2.txt", - Content: []byte("object 2\n"), + BucketName: "bucket1", + Name: "object2.txt", + Content: []byte("object 2\n"), + ContentType: testContentType, }, { - BucketName: "bucket2", - Name: "object1.txt", - Content: []byte("object 1\n"), + BucketName: "bucket2", + Name: "object1.txt", + Content: []byte("object 1\n"), + ContentType: testContentType, }, { - BucketName: "bucket2", - Name: "object2.txt", - Content: []byte("object 2\n"), + BucketName: "bucket2", + Name: "object2.txt", + Content: []byte("object 2\n"), + ContentType: testContentType, }, }, },