diff --git a/modules/typesniffer/typesniffer.go b/modules/typesniffer/typesniffer.go index b6a6646d50ce9..05b097fbd7474 100644 --- a/modules/typesniffer/typesniffer.go +++ b/modules/typesniffer/typesniffer.go @@ -20,6 +20,8 @@ const sniffLen = 1024 const ( // SvgMimeType MIME type of SVG images. SvgMimeType = "image/svg+xml" + // PdfMimeType MIME type of PDF images. + PdfMimeType = "application/pdf" // ApplicationOctetStream MIME type of binary files. ApplicationOctetStream = "application/octet-stream" ) diff --git a/routers/common/repo.go b/routers/common/repo.go index b3cd749115fb1..501c5b0240703 100644 --- a/routers/common/repo.go +++ b/routers/common/repo.go @@ -66,37 +66,39 @@ func ServeData(ctx *context.Context, name string, size int64, reader io.Reader) st := typesniffer.DetectContentType(buf) - mappedMimeType := "" + mimeType := "" if setting.MimeTypeMap.Enabled { fileExtension := strings.ToLower(filepath.Ext(name)) - mappedMimeType = setting.MimeTypeMap.Map[fileExtension] + mimeType = setting.MimeTypeMap.Map[fileExtension] } + if st.IsText() || ctx.FormBool("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" } - if mappedMimeType == "" { - mappedMimeType = "text/plain" + if mimeType == "" { + mimeType = "text/plain" } - ctx.Resp.Header().Set("Content-Type", mappedMimeType+"; charset="+strings.ToLower(cs)) + ctx.Resp.Header().Set("Content-Type", mimeType+"; 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 mimeType == "" { + if st.IsSvgImage() { + mimeType = typesniffer.SvgMimeType + } else if st.IsPDF() { + mimeType = typesniffer.PdfMimeType + } else { + mimeType = typesniffer.ApplicationOctetStream + } } + ctx.Resp.Header().Set("Content-Type", mimeType) + ctx.Resp.Header().Set("X-Content-Type-Options", "nosniff") + + ctx.Resp.Header().Set("Access-Control-Expose-Headers", "Content-Disposition") 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() || st.IsPDF() { - ctx.Resp.Header().Set("Content-Security-Policy", "default-src 'none'; style-src 'unsafe-inline'; sandbox") - ctx.Resp.Header().Set("X-Content-Type-Options", "nosniff") - if st.IsSvgImage() { - ctx.Resp.Header().Set("Content-Type", typesniffer.SvgMimeType) - } else { - ctx.Resp.Header().Set("Content-Type", typesniffer.ApplicationOctetStream) - } - } + ctx.Resp.Header().Set("Content-Security-Policy", "default-src 'none'; style-src 'unsafe-inline'; sandbox") } else { ctx.Resp.Header().Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, name)) }