@@ -33,6 +33,7 @@ type ServeHeaderOptions struct {
3333 ContentLength * int64
3434 Disposition string // defaults to "attachment"
3535 Filename string
36+ CacheIsPublic bool
3637 CacheDuration time.Duration // defaults to 5 minutes
3738 LastModified time.Time
3839}
@@ -72,11 +73,11 @@ func ServeSetHeaders(w http.ResponseWriter, opts *ServeHeaderOptions) {
7273 header .Set ("Access-Control-Expose-Headers" , "Content-Disposition" )
7374 }
7475
75- duration := opts . CacheDuration
76- if duration == 0 {
77- duration = 5 * time . Minute
78- }
79- httpcache . SetCacheControlInHeader ( header , duration )
76+ httpcache . SetCacheControlInHeader ( header , & httpcache. CacheControlOptions {
77+ IsPublic : opts . CacheIsPublic ,
78+ MaxAge : opts . CacheDuration ,
79+ NoTransform : true ,
80+ } )
8081
8182 if ! opts .LastModified .IsZero () {
8283 // http.TimeFormat required a UTC time, refer to https://pkg.go.dev/net/http#TimeFormat
@@ -85,19 +86,15 @@ func ServeSetHeaders(w http.ResponseWriter, opts *ServeHeaderOptions) {
8586}
8687
8788// ServeData download file from io.Reader
88- func setServeHeadersByFile (r * http.Request , w http.ResponseWriter , filePath string , mineBuf []byte ) {
89+ func setServeHeadersByFile (r * http.Request , w http.ResponseWriter , mineBuf []byte , opts * ServeHeaderOptions ) {
8990 // do not set "Content-Length", because the length could only be set by callers, and it needs to support range requests
90- opts := & ServeHeaderOptions {
91- Filename : path .Base (filePath ),
92- }
93-
9491 sniffedType := typesniffer .DetectContentType (mineBuf )
9592
9693 // the "render" parameter came from year 2016: 638dd24c, it doesn't have clear meaning, so I think it could be removed later
9794 isPlain := sniffedType .IsText () || r .FormValue ("render" ) != ""
9895
9996 if setting .MimeTypeMap .Enabled {
100- fileExtension := strings .ToLower (filepath .Ext (filePath ))
97+ fileExtension := strings .ToLower (filepath .Ext (opts . Filename ))
10198 opts .ContentType = setting .MimeTypeMap .Map [fileExtension ]
10299 }
103100
@@ -114,7 +111,7 @@ func setServeHeadersByFile(r *http.Request, w http.ResponseWriter, filePath stri
114111 if isPlain {
115112 charset , err := charsetModule .DetectEncoding (mineBuf )
116113 if err != nil {
117- log .Error ("Detect raw file %s charset failed: %v, using by default utf-8" , filePath , err )
114+ log .Error ("Detect raw file %s charset failed: %v, using by default utf-8" , opts . Filename , err )
118115 charset = "utf-8"
119116 }
120117 opts .ContentTypeCharset = strings .ToLower (charset )
@@ -142,7 +139,7 @@ func setServeHeadersByFile(r *http.Request, w http.ResponseWriter, filePath stri
142139
143140const mimeDetectionBufferLen = 1024
144141
145- func ServeContentByReader (r * http.Request , w http.ResponseWriter , filePath string , size int64 , reader io.Reader ) {
142+ func ServeContentByReader (r * http.Request , w http.ResponseWriter , size int64 , reader io.Reader , opts * ServeHeaderOptions ) {
146143 buf := make ([]byte , mimeDetectionBufferLen )
147144 n , err := util .ReadAtMost (reader , buf )
148145 if err != nil {
@@ -152,7 +149,7 @@ func ServeContentByReader(r *http.Request, w http.ResponseWriter, filePath strin
152149 if n >= 0 {
153150 buf = buf [:n ]
154151 }
155- setServeHeadersByFile (r , w , filePath , buf )
152+ setServeHeadersByFile (r , w , buf , opts )
156153
157154 // reset the reader to the beginning
158155 reader = io .MultiReader (bytes .NewReader (buf ), reader )
@@ -215,7 +212,7 @@ func ServeContentByReader(r *http.Request, w http.ResponseWriter, filePath strin
215212 _ , _ = io .CopyN (w , reader , partialLength ) // just like http.ServeContent, not necessary to handle the error
216213}
217214
218- func ServeContentByReadSeeker (r * http.Request , w http.ResponseWriter , filePath string , modTime * time.Time , reader io.ReadSeeker ) {
215+ func ServeContentByReadSeeker (r * http.Request , w http.ResponseWriter , modTime * time.Time , reader io.ReadSeeker , opts * ServeHeaderOptions ) {
219216 buf := make ([]byte , mimeDetectionBufferLen )
220217 n , err := util .ReadAtMost (reader , buf )
221218 if err != nil {
@@ -229,9 +226,9 @@ func ServeContentByReadSeeker(r *http.Request, w http.ResponseWriter, filePath s
229226 if n >= 0 {
230227 buf = buf [:n ]
231228 }
232- setServeHeadersByFile (r , w , filePath , buf )
229+ setServeHeadersByFile (r , w , buf , opts )
233230 if modTime == nil {
234231 modTime = & time.Time {}
235232 }
236- http .ServeContent (w , r , path . Base ( filePath ) , * modTime , reader )
233+ http .ServeContent (w , r , opts . Filename , * modTime , reader )
237234}
0 commit comments