@ -64,16 +64,26 @@ func ServeData(ctx *context.Context, name string, size int64, reader io.Reader)
st := typesniffer . DetectContentType ( buf )
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" ) {
if st . IsText ( ) || ctx . QueryBool ( "render" ) {
cs , err := charset . DetectEncoding ( buf )
cs , err := charset . DetectEncoding ( buf )
if err != nil {
if err != nil {
log . Error ( "Detect raw file %s charset failed: %v, using by default utf-8" , name , err )
log . Error ( "Detect raw file %s charset failed: %v, using by default utf-8" , name , err )
cs = "utf-8"
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 {
} else {
ctx . Resp . Header ( ) . Set ( "Access-Control-Expose-Headers" , "Content-Disposition" )
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 ( ) ) {
if ( st . IsImage ( ) || st . IsPDF ( ) ) && ( setting . UI . SVG . Enabled || ! st . IsSvgImage ( ) ) {
ctx . Resp . Header ( ) . Set ( "Content-Disposition" , fmt . Sprintf ( ` inline; filename="%s" ` , name ) )
ctx . Resp . Header ( ) . Set ( "Content-Disposition" , fmt . Sprintf ( ` inline; filename="%s" ` , name ) )
if st . IsSvgImage ( ) {
if st . IsSvgImage ( ) {
@ -83,12 +93,6 @@ func ServeData(ctx *context.Context, name string, size int64, reader io.Reader)
}
}
} else {
} else {
ctx . Resp . Header ( ) . Set ( "Content-Disposition" , fmt . Sprintf ( ` attachment; filename="%s" ` , name ) )
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 )
}
}
}
}
}
}