diff options
author | Jimmy Praet <jimmy.praet@telenet.be> | 2021-06-30 23:31:54 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-06-30 17:31:54 -0400 |
commit | ce286f9d9c00ceb24fb4eab4cab56c0b0678765a (patch) | |
tree | e8ef0385b108332f63bb744e45fe2ad62bb5a3cf /routers | |
parent | 99799832835aae6a8641112cb71eb87baef32afa (diff) | |
download | gitea-ce286f9d9c00ceb24fb4eab4cab56c0b0678765a.tar.gz gitea-ce286f9d9c00ceb24fb4eab4cab56c0b0678765a.zip |
Support custom mime type mapping for text files (#16304)
* Support custom mime type mapping for text files
* Apply suggested change to routers/common/repo.go
Co-authored-by: KN4CK3R <admin@oldschoolhack.me>
Co-authored-by: 6543 <6543@obermui.de>
Co-authored-by: KN4CK3R <admin@oldschoolhack.me>
Diffstat (limited to 'routers')
-rw-r--r-- | routers/common/repo.go | 20 |
1 files changed, 12 insertions, 8 deletions
diff --git a/routers/common/repo.go b/routers/common/repo.go index 22403da097..8d33fb07fb 100644 --- a/routers/common/repo.go +++ b/routers/common/repo.go @@ -64,16 +64,26 @@ func ServeData(ctx *context.Context, name string, size int64, reader io.Reader) st := typesniffer.DetectContentType(buf) + mappedMimeType := "" + if setting.MimeTypeMap.Enabled { + fileExtension := strings.ToLower(filepath.Ext(name)) + mappedMimeType = setting.MimeTypeMap.Map[fileExtension] + } if st.IsText() || ctx.QueryBool("render") { 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)) + if mappedMimeType == "" { + mappedMimeType = "text/plain" + } + ctx.Resp.Header().Set("Content-Type", mappedMimeType+"; charset="+strings.ToLower(cs)) } else { ctx.Resp.Header().Set("Access-Control-Expose-Headers", "Content-Disposition") - + if mappedMimeType != "" { + ctx.Resp.Header().Set("Content-Type", mappedMimeType) + } if (st.IsImage() || st.IsPDF()) && (setting.UI.SVG.Enabled || !st.IsSvgImage()) { ctx.Resp.Header().Set("Content-Disposition", fmt.Sprintf(`inline; filename="%s"`, name)) if st.IsSvgImage() { @@ -83,12 +93,6 @@ func ServeData(ctx *context.Context, name string, size int64, reader io.Reader) } } else { ctx.Resp.Header().Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, name)) - if setting.MimeTypeMap.Enabled { - fileExtension := strings.ToLower(filepath.Ext(name)) - if mimetype, ok := setting.MimeTypeMap.Map[fileExtension]; ok { - ctx.Resp.Header().Set("Content-Type", mimetype) - } - } } } |