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"
"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
}