Skip to content

Commit

Permalink
Merge pull request ipfs/kubo#7262 from gowthamgts/fix/http/content-type
Browse files Browse the repository at this point in the history
Fixes #7252 - Uses gabriel-vasile/mimetype to support additional content types

This commit was moved from ipfs/kubo@01e27f9
  • Loading branch information
Stebalien committed May 4, 2020
2 parents c30b301 + 973bcef commit b3a59a1
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions gateway/core/corehttp/gateway_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"time"

humanize "github.com/dustin/go-humanize"
"github.com/gabriel-vasile/mimetype"
"github.com/ipfs/go-cid"
files "github.com/ipfs/go-ipfs-files"
dag "github.com/ipfs/go-merkledag"
Expand Down Expand Up @@ -376,10 +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[:])
ctype = http.DetectContentType(buf[:n])
_, err := content.Seek(0, io.SeekStart)
// uses https://github.com/gabriel-vasile/mimetype library to determine the content type.
// Fixes https://github.com/ipfs/go-ipfs/issues/7252
mimeType, err := mimetype.DetectReader(content)
if err != nil {
http.Error(w, fmt.Sprintf("cannot detect content-type: %s", err.Error()), 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 b3a59a1

Please sign in to comment.