Permalink
Browse files

Added real MIME type guessing

  • Loading branch information...
eko committed Jan 8, 2019
1 parent 4bd1007 commit bfa3f1ba2ebb7c7c1682202b3a1c31d5688dbde3
Showing with 19 additions and 1 deletion.
  1. +19 −1 middleware.go
@@ -170,6 +170,14 @@ func mapTemporaryFileToOperations() error {
}
defer file.Close()

mimeType, err := getMimeType(file)
if err != nil {
fmt.Println(err)

return fmt.Errorf("Unable to detect file MIME type. Reason: %v", err)
}

f.Seek(0, 0)
data, err := ioutil.ReadAll(file)
if err != nil {
return fmt.Errorf("Could not read multipart file. Reason: %v", err)
@@ -186,7 +194,7 @@ func mapTemporaryFileToOperations() error {
}

upload := &GraphQLUpload{
MIMEType: handle.Header.Get("Content-Type"),
MIMEType: mimeType,
Filename: handle.Filename,
Filepath: f.Name(),
}
@@ -197,3 +205,13 @@ 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
}

0 comments on commit bfa3f1b

Please sign in to comment.