@cryptotaxi247 / kubo / commits / a29a9dbb9

gateway: ServeFile: use file extension to determine Content-Type

License: MIT Signed-off-by: Abdeldjalil Hebal <dreamski21@gmail.com>

Djalil Dreamski committed Nov 6, 2019 at 01:52 UTC a29a9dbb98207c8c3312c5d0c0eb53abeb7eec05
1 file changed +17 -8
core/corehttp/gateway_handler.go
+17 -8
@@ -11,7 +11,8 @@ import (
11 "runtime/debug"
12 "strings"
13 "time"
14 -
14 + "mime"
15 +
16 "github.com/dustin/go-humanize"
17 "github.com/ipfs/go-cid"
18 files "github.com/ipfs/go-ipfs-files"
@@ -383,15 +384,23 @@ func (i *gatewayHandler) serveFile(w http.ResponseWriter, req *http.Request, nam
384 }
385 }
386
386 - buf := make([]byte, 512)
387 - _, _ = content.Read(buf)
388 - mime := http.DetectContentType(buf)
389 -
390 - if strings.HasPrefix(mime, "text/html;") {
391 - mime = "text/html"
387 + ctype := mime.TypeByExtension(gopath.Ext(name))
388 + if ctype == "" {
389 + buf := make([]byte, 512)
390 + n, _ := io.ReadFull(content, buf[:])
391 + ctype = http.DetectContentType(buf[:n])
392 + _, err := content.Seek(0, io.SeekStart)
393 + if err != nil {
394 + Error(w, "seeker can't seek", http.StatusInternalServerError)
395 + return
396 + }
397 + }
398 + if strings.HasPrefix(ctype, "text/html;") {
399 + ctype = "text/html"
400 }
393 - w.Header().Set("Content-Type", mime)
401 + w.Header().Set("Content-Type", ctype)
402
403 + _, _ = content.Seek(0, io.SeekStart)
404 http.ServeContent(w, req, name, modtime, content)
405 }
406