Skip to content

Commit

Permalink
Added real MIME type guessing
Browse files Browse the repository at this point in the history
  • Loading branch information
eko committed Jan 8, 2019
1 parent 4bd1007 commit bfa3f1b
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion middleware.go
Expand Up @@ -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)
Expand All @@ -186,7 +194,7 @@ func mapTemporaryFileToOperations() error {
}

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