@@ -13,7 +13,7 @@ import (
13
13
"strings"
14
14
"time"
15
15
16
- "code.gitea.io/gitea/modules/charset"
16
+ charsetModule "code.gitea.io/gitea/modules/charset"
17
17
"code.gitea.io/gitea/modules/context"
18
18
"code.gitea.io/gitea/modules/git"
19
19
"code.gitea.io/gitea/modules/httpcache"
@@ -62,19 +62,19 @@ func ServeData(ctx *context.Context, filePath string, size int64, reader io.Read
62
62
}
63
63
64
64
fileName := path .Base (filePath )
65
- st := typesniffer .DetectContentType (buf )
66
- isPlain := st .IsText () || ctx .FormBool ("render" )
65
+ sniffedType := typesniffer .DetectContentType (buf )
66
+ isPlain := sniffedType .IsText () || ctx .FormBool ("render" )
67
67
mimeType := ""
68
- cs := ""
68
+ charset := ""
69
69
70
70
if setting .MimeTypeMap .Enabled {
71
71
fileExtension := strings .ToLower (filepath .Ext (fileName ))
72
72
mimeType = setting .MimeTypeMap .Map [fileExtension ]
73
73
}
74
74
75
75
if mimeType == "" {
76
- if st .IsBrowsableBinaryType () {
77
- mimeType = st .GetMimeType ()
76
+ if sniffedType .IsBrowsableBinaryType () {
77
+ mimeType = sniffedType .GetMimeType ()
78
78
} else if isPlain {
79
79
mimeType = "text/plain"
80
80
} else {
@@ -83,24 +83,24 @@ func ServeData(ctx *context.Context, filePath string, size int64, reader io.Read
83
83
}
84
84
85
85
if isPlain {
86
- cs , err = charset .DetectEncoding (buf )
86
+ charset , err = charsetModule .DetectEncoding (buf )
87
87
if err != nil {
88
88
log .Error ("Detect raw file %s charset failed: %v, using by default utf-8" , filePath , err )
89
- cs = "utf-8"
89
+ charset = "utf-8"
90
90
}
91
91
}
92
92
93
- if cs != "" {
94
- ctx .Resp .Header ().Set ("Content-Type" , mimeType + "; charset=" + strings .ToLower (cs ))
93
+ if charset != "" {
94
+ ctx .Resp .Header ().Set ("Content-Type" , mimeType + "; charset=" + strings .ToLower (charset ))
95
95
} else {
96
96
ctx .Resp .Header ().Set ("Content-Type" , mimeType )
97
97
}
98
98
ctx .Resp .Header ().Set ("X-Content-Type-Options" , "nosniff" )
99
99
100
100
// serve types that can present a security risk with CSP
101
- if st .IsSvgImage () {
101
+ if sniffedType .IsSvgImage () {
102
102
ctx .Resp .Header ().Set ("Content-Security-Policy" , "default-src 'none'; style-src 'unsafe-inline'; sandbox" )
103
- } else if st .IsPDF () {
103
+ } else if sniffedType .IsPDF () {
104
104
// no sandbox attribute for pdf as it breaks rendering in at least safari. this
105
105
// should generally be safe as scripts inside PDF can not escape the PDF document
106
106
// see https://bugs.chromium.org/p/chromium/issues/detail?id=413851 for more discussion
0 commit comments