Skip to content

Commit c9402d0

Browse files
committed
Fix download file wrong content-type
1 parent 7c0570d commit c9402d0

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

routers/repo/download.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"strings"
1313

1414
"code.gitea.io/gitea/modules/base"
15+
"code.gitea.io/gitea/modules/charset"
1516
"code.gitea.io/gitea/modules/context"
1617
"code.gitea.io/gitea/modules/git"
1718
"code.gitea.io/gitea/modules/lfs"
@@ -33,7 +34,12 @@ func ServeData(ctx *context.Context, name string, reader io.Reader) error {
3334
name = strings.Replace(name, ",", " ", -1)
3435

3536
if base.IsTextFile(buf) || ctx.QueryBool("render") {
36-
ctx.Resp.Header().Set("Content-Type", "text/plain; charset=utf-8")
37+
cs, err := charset.DetectEncoding(buf)
38+
if err != nil {
39+
log.Error("Detect raw file %s charset failed: %v, just ignored", name, err)
40+
cs = "utf-8"
41+
}
42+
ctx.Resp.Header().Set("Content-Type", "text/plain; charset="+cs)
3743
} else if base.IsImageFile(buf) || base.IsPDFFile(buf) {
3844
ctx.Resp.Header().Set("Content-Disposition", fmt.Sprintf(`inline; filename="%s"`, name))
3945
} else {

0 commit comments

Comments
 (0)