From ddfef5a53c91512195cd10b83bbe2224f9f2bd74 Mon Sep 17 00:00:00 2001 From: Amolith Date: Sun, 12 Oct 2025 18:45:10 -0600 Subject: [PATCH] fix: detect MIME type for raw blob responses 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 --- pkg/web/webui_blob.go | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/pkg/web/webui_blob.go b/pkg/web/webui_blob.go index 3c45469cc85ff0914228aac6064ecb6062e00bfd..89f92d3127f257e1714acb267ed5db416fbe8b16 100644 --- a/pkg/web/webui_blob.go +++ b/pkg/web/webui_blob.go @@ -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.