Unverified 提交 a29a9dbb 作者: Djalil Dreamski 提交者: GitHub

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


License: MIT
Signed-off-by: 's avatarAbdeldjalil Hebal <dreamski21@gmail.com>
上级 69f81a11
...@@ -11,7 +11,8 @@ import ( ...@@ -11,7 +11,8 @@ import (
"runtime/debug" "runtime/debug"
"strings" "strings"
"time" "time"
"mime"
"github.com/dustin/go-humanize" "github.com/dustin/go-humanize"
"github.com/ipfs/go-cid" "github.com/ipfs/go-cid"
files "github.com/ipfs/go-ipfs-files" files "github.com/ipfs/go-ipfs-files"
...@@ -383,15 +384,23 @@ func (i *gatewayHandler) serveFile(w http.ResponseWriter, req *http.Request, nam ...@@ -383,15 +384,23 @@ func (i *gatewayHandler) serveFile(w http.ResponseWriter, req *http.Request, nam
} }
} }
buf := make([]byte, 512) ctype := mime.TypeByExtension(gopath.Ext(name))
_, _ = content.Read(buf) if ctype == "" {
mime := http.DetectContentType(buf) buf := make([]byte, 512)
n, _ := io.ReadFull(content, buf[:])
if strings.HasPrefix(mime, "text/html;") { ctype = http.DetectContentType(buf[:n])
mime = "text/html" _, err := content.Seek(0, io.SeekStart)
if err != nil {
Error(w, "seeker can't seek", http.StatusInternalServerError)
return
}
}
if strings.HasPrefix(ctype, "text/html;") {
ctype = "text/html"
} }
w.Header().Set("Content-Type", mime) w.Header().Set("Content-Type", ctype)
_, _ = content.Seek(0, io.SeekStart)
http.ServeContent(w, req, name, modtime, content) http.ServeContent(w, req, name, modtime, content)
} }
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论