Skip to content

Commit

Permalink
#7252 - read content directly instead of sending 512bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
gowthamgts authored and Stebalien committed May 4, 2020
1 parent bedb077 commit c2f4162
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions core/corehttp/gateway_handler.go
Expand Up @@ -377,12 +377,16 @@ func (i *gatewayHandler) serveFile(w http.ResponseWriter, req *http.Request, nam
} else {
ctype = mime.TypeByExtension(gopath.Ext(name))
if ctype == "" {
buf := make([]byte, 512)
n, _ := io.ReadFull(content, buf[:])
// uses https://github.com/gabriel-vasile/mimetype library to determine the content type.
// Fixes https://github.com/ipfs/go-ipfs/issues/7252
ctype = mimetype.Detect(buf[:n]).String()
_, err := content.Seek(0, io.SeekStart)
mimeType, err := mimetype.DetectReader(content)
if err != nil {
http.Error(w, "cannot detect content-type", http.StatusInternalServerError)
return
}

ctype = mimeType.String()
_, err = content.Seek(0, io.SeekStart)
if err != nil {
http.Error(w, "seeker can't seek", http.StatusInternalServerError)
return
Expand Down

0 comments on commit c2f4162

Please sign in to comment.