mirror of
https://github.com/mickael-kerjean/filestash.git
synced 2025-10-29 17:18:43 +08:00
improve (speed): support for brotli compression
This commit is contained in:
@ -78,21 +78,36 @@ func AboutHandler(ctx App, res http.ResponseWriter, req *http.Request) {
|
||||
|
||||
func ServeFile(res http.ResponseWriter, req *http.Request, filePath string) {
|
||||
zFilePath := filePath + ".gz"
|
||||
bFilePath := filePath + ".br"
|
||||
|
||||
etagNormal := hashFile(filePath, 10)
|
||||
etagGzip := hashFile(zFilePath, 10)
|
||||
etagBr := hashFile(bFilePath, 10)
|
||||
|
||||
if req.Header.Get("If-None-Match") != "" {
|
||||
browserTag := req.Header.Get("If-None-Match")
|
||||
if browserTag == etagNormal {
|
||||
res.WriteHeader(http.StatusNotModified)
|
||||
return
|
||||
} else if browserTag == etagBr {
|
||||
res.WriteHeader(http.StatusNotModified)
|
||||
return
|
||||
} else if browserTag == etagGzip {
|
||||
res.WriteHeader(http.StatusNotModified)
|
||||
return
|
||||
}
|
||||
}
|
||||
head := res.Header()
|
||||
if strings.Contains(req.Header.Get("Accept-Encoding"), "gzip") {
|
||||
acceptEncoding := req.Header.Get("Accept-Encoding")
|
||||
if strings.Contains(acceptEncoding, "br") {
|
||||
if file, err := os.OpenFile(bFilePath, os.O_RDONLY, os.ModePerm); err == nil {
|
||||
head.Set("Content-Encoding", "br")
|
||||
head.Set("Etag", etagBr)
|
||||
io.Copy(res, file)
|
||||
file.Close()
|
||||
return
|
||||
}
|
||||
} else if strings.Contains(acceptEncoding, "gzip") {
|
||||
if file, err := os.OpenFile(zFilePath, os.O_RDONLY, os.ModePerm); err == nil {
|
||||
head.Set("Content-Encoding", "gzip")
|
||||
head.Set("Etag", etagGzip)
|
||||
|
||||
@ -82,6 +82,13 @@ if (process.env.NODE_ENV === 'production') {
|
||||
threshold: 0,
|
||||
minRatio: 0.8
|
||||
}));
|
||||
config.plugins.push(new CompressionPlugin({
|
||||
asset: "[path].br[query]",
|
||||
algorithm: "brotliCompress",
|
||||
test: /\.js$|\.json$|\.html$|\.svg|\.ico$/,
|
||||
threshold: 0,
|
||||
minRatio: 0.8
|
||||
}));
|
||||
} else {
|
||||
config.devtool = '#inline-source-map';
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user