Skip to content

Commit b310686

Browse files
lunnylafriks
authored andcommitted
Fix download file wrong content-type (#9825) (#9835)
* Fix download file wrong content-type * change the error text to be more precise * fix test Co-authored-by: Lauris BH <[email protected]>
1 parent 918e640 commit b310686

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, using by default utf-8", name, err)
40+
cs = "utf-8"
41+
}
42+
ctx.Resp.Header().Set("Content-Type", "text/plain; charset="+strings.ToLower(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)