fix: detect MIME type for raw blob responses
Amolith
created 3 weeks ago
Replace hardcoded text/plain content-type with proper MIME type
detection using mime.TypeByExtension and http.DetectContentType
as fallback. Ensures binary files are served with correct types
while maintaining UTF-8 charset for text files.
Fixes: bug-74a99e4
Change summary
pkg/web/webui_blob.go | 13 +++++++++++--
1 file changed, 11 insertions(+), 2 deletions(-)
Detailed changes
@@ -3,6 +3,7 @@ package web
import (
"bytes"
"html/template"
+ "mime"
"net/http"
"path/filepath"
"strings"
@@ -166,8 +167,16 @@ func repoBlobRaw(w http.ResponseWriter, r *http.Request) {
return
}
- w.Header().Set("Content-Type", "text/plain; charset=utf-8")
- w.Write(content)
+ contentType := mime.TypeByExtension(filepath.Ext(path))
+ if contentType == "" {
+ contentType = http.DetectContentType(content)
+ }
+ if strings.HasPrefix(contentType, "text/") && !strings.Contains(contentType, "charset") {
+ contentType += "; charset=utf-8"
+ }
+
+ w.Header().Set("Content-Type", contentType)
+ _, _ = w.Write(content)
}
// isBinaryContent detects if file content is binary using a simple heuristic.