diff --git a/server/ctrl/plugin.go b/server/ctrl/plugin.go index 28757ddf..f21c2425 100644 --- a/server/ctrl/plugin.go +++ b/server/ctrl/plugin.go @@ -4,6 +4,7 @@ import ( "io" "net/http" "os" + "strings" . "github.com/mickael-kerjean/filestash/server/common" "github.com/mickael-kerjean/filestash/server/model" @@ -42,16 +43,36 @@ func PluginExportHandler(ctx *App, res http.ResponseWriter, req *http.Request) { func PluginStaticHandler(ctx *App, res http.ResponseWriter, req *http.Request) { path := mux.Vars(req)["path"] mtype := GetMimeType(path) - file, err := model.GetPluginFile(mux.Vars(req)["name"], path) - if err != nil { - SendErrorResult(res, err) - return - } - defer file.Close() - res.Header().Set("Content-Type", mtype) - _, err = io.Copy(res, file) - if err != nil { - SendErrorResult(res, err) + staticConfig := []struct { + ContentType string + FileExt string + }{ + {"br", ".br"}, + {"gzip", ".gz"}, + {"", ""}, + } + + var file io.ReadCloser + var err error + head := res.Header() + acceptEncoding := req.Header.Get("Accept-Encoding") + for _, cfg := range staticConfig { + if strings.Contains(acceptEncoding, cfg.ContentType) == false { + continue + } + file, err = model.GetPluginFile(mux.Vars(req)["name"], path+cfg.FileExt) + if err != nil { + continue + } + head.Set("Content-Type", mtype) + if cfg.ContentType != "" { + head.Set("Content-Encoding", cfg.ContentType) + } + io.Copy(res, file) + file.Close() return } + + SendErrorResult(res, err) + return }