Skip to content
Permalink
Browse files

Added package gabriel-vasile/mimetype for better mime type detection

  • Loading branch information...
eko committed May 14, 2019
1 parent fc1b508 commit c76f1f2ba5dc0a2e9ca605f2cb7d3a619e2fe217
Showing with 40 additions and 35 deletions.
  1. +1 −0 .travis.yml
  2. +18 −16 Gopkg.lock
  3. +4 −0 Gopkg.toml
  4. +2 −3 go.mod
  5. +10 −0 go.sum
  6. +4 −15 middleware.go
  7. +1 −1 middleware_test.go
@@ -10,5 +10,6 @@ go:

install:
- go get github.com/stretchr/testify
- go get github.com/gabriel-vasile/mimetype

script: go test -v ./...

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
@@ -2,6 +2,10 @@
go-tests = true
unused-packages = true

[[constraint]]
name = "github.com/gabriel-vasile/mimetype"
version = "0.3.7"

[[constraint]]
name = "github.com/stretchr/testify"
version = "1.3.0"
5 go.mod
@@ -1,8 +1,7 @@
module github.com/eko/graphql-go-upload

require (
github.com/davecgh/go-spew v1.1.1
github.com/pmezard/go-difflib v1.0.0
github.com/stretchr/objx v0.1.1
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/gabriel-vasile/mimetype v0.3.7
github.com/stretchr/testify v1.3.0
)
10 go.sum
@@ -0,0 +1,10 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gabriel-vasile/mimetype v0.3.7 h1:CgMynyHCbDm+sM9KU3M6KIlYaI86TtCg70Ai7OP2bIA=
github.com/gabriel-vasile/mimetype v0.3.7/go.mod h1:kMJbg3SlWZCsj4R73F1WDzbT9AyGCOVmUtIxxwO5pmI=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
@@ -12,6 +12,8 @@ import (
"path/filepath"
"strings"
"sync"

"github.com/gabriel-vasile/mimetype"
)

type postedFiles func(key string) (multipart.File, *multipart.FileHeader, error)
@@ -175,17 +177,14 @@ func mapTemporaryFileToOperations() error {
}
defer file.Close()

mimeType, err := getMimeType(file)
if err != nil {
return fmt.Errorf("Unable to detect file MIME type. Reason: %v", err)
}

file.Seek(0, 0)
data, err := ioutil.ReadAll(file)
if err != nil {
return fmt.Errorf("Could not read multipart file. Reason: %v", err)
}

mimeType, _ := mimetype.Detect(data)

f, err := ioutil.TempFile(os.TempDir(), fmt.Sprintf("graphqlupload-*%s", filepath.Ext(handle.Filename)))
if err != nil {
return fmt.Errorf("Unable to create temporary file. Reason: %v", err)
@@ -208,13 +207,3 @@ func mapTemporaryFileToOperations() error {

return nil
}

func getMimeType(file multipart.File) (string, error) {
buffer := make([]byte, 512)
n, err := file.Read(buffer)
if err != nil {
return "", err
}

return http.DetectContentType(buffer[:n]), nil
}
@@ -91,7 +91,7 @@ func TestHandlerWhenSuccess(t *testing.T) {

assert.NotEmpty(t, data.Variables.File.Filepath)
assert.Equal(t, data.Variables.File.Filename, "middleware.go")
assert.Equal(t, data.Variables.File.MIMEType, "text/plain; charset=utf-8")
assert.Equal(t, data.Variables.File.MIMEType, "text/plain")

assert.Equal(t, data.Variables.Title, "my test title")
}

0 comments on commit c76f1f2

Please sign in to comment.
You can’t perform that action at this time.