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>
This commit is contained in:
parent
9979983283
commit
ce286f9d9c
6 changed files with 43 additions and 9 deletions
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue