feature (plugin): handle compression in opener plugin

This commit is contained in:
MickaelK 2025-02-18 13:44:19 +11:00
parent 2c1edd85e9
commit f584807fac

View file

@ -4,6 +4,7 @@ import (
"io" "io"
"net/http" "net/http"
"os" "os"
"strings"
. "github.com/mickael-kerjean/filestash/server/common" . "github.com/mickael-kerjean/filestash/server/common"
"github.com/mickael-kerjean/filestash/server/model" "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) { func PluginStaticHandler(ctx *App, res http.ResponseWriter, req *http.Request) {
path := mux.Vars(req)["path"] path := mux.Vars(req)["path"]
mtype := GetMimeType(path) mtype := GetMimeType(path)
file, err := model.GetPluginFile(mux.Vars(req)["name"], path) staticConfig := []struct {
if err != nil { ContentType string
SendErrorResult(res, err) FileExt string
return }{
} {"br", ".br"},
defer file.Close() {"gzip", ".gz"},
res.Header().Set("Content-Type", mtype) {"", ""},
_, err = io.Copy(res, file) }
if err != nil {
SendErrorResult(res, err) 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 return
} }
SendErrorResult(res, err)
return
} }