diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2020-01-17 21:56:25 +0800 |
---|---|---|
committer | Antoine GIRARD <sapk@users.noreply.github.com> | 2020-01-17 14:56:25 +0100 |
commit | b31068652aaffd3a157ae87ea52b2f7cd89970da (patch) | |
tree | c5daa7f817150ddb980f7ff5349ae869a81c4a1b | |
parent | 918e6405904459a3a544541b3b351278bafa6a04 (diff) | |
download | gitea-b31068652aaffd3a157ae87ea52b2f7cd89970da.tar.gz gitea-b31068652aaffd3a157ae87ea52b2f7cd89970da.zip |
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 <lauris@nix.lv>
-rw-r--r-- | routers/repo/download.go | 8 |
1 files changed, 7 insertions, 1 deletions
diff --git a/routers/repo/download.go b/routers/repo/download.go index 6f10fe36a3..7ef0574b1d 100644 --- a/routers/repo/download.go +++ b/routers/repo/download.go @@ -12,6 +12,7 @@ import ( "strings" "code.gitea.io/gitea/modules/base" + "code.gitea.io/gitea/modules/charset" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/lfs" @@ -33,7 +34,12 @@ func ServeData(ctx *context.Context, name string, reader io.Reader) error { name = strings.Replace(name, ",", " ", -1) if base.IsTextFile(buf) || ctx.QueryBool("render") { - ctx.Resp.Header().Set("Content-Type", "text/plain; charset=utf-8") + cs, err := charset.DetectEncoding(buf) + if err != nil { + log.Error("Detect raw file %s charset failed: %v, using by default utf-8", name, err) + cs = "utf-8" + } + ctx.Resp.Header().Set("Content-Type", "text/plain; charset="+strings.ToLower(cs)) } else if base.IsImageFile(buf) || base.IsPDFFile(buf) { ctx.Resp.Header().Set("Content-Disposition", fmt.Sprintf(`inline; filename="%s"`, name)) } else { |