From b7e0b1ad2562bfbd12741f1c940af33dc71103f8 Mon Sep 17 00:00:00 2001 From: MickaelK Date: Wed, 30 Jul 2025 15:43:29 +1000 Subject: [PATCH] feature (patch): static patches --- server/common/plugin.go | 8 ++--- server/ctrl/static.go | 67 +++++++++++++++++++++-------------------- server/model/plugin.go | 28 +++++++++++++++-- 3 files changed, 64 insertions(+), 39 deletions(-) diff --git a/server/common/plugin.go b/server/common/plugin.go index 87b30492..591df918 100644 --- a/server/common/plugin.go +++ b/server/common/plugin.go @@ -244,13 +244,13 @@ func (this Get) Middleware() []func(HandlerFunc) HandlerFunc { return middlewares } -var staticOverrides []fs.FS +var staticOverrides [][]byte -func (this Register) StaticPatch(folder fs.FS) { - staticOverrides = append(staticOverrides, folder) +func (this Register) StaticPatch(pathFile []byte) { + staticOverrides = append(staticOverrides, pathFile) } -func (this Get) StaticPatch() []fs.FS { +func (this Get) StaticPatch() [][]byte { return staticOverrides } diff --git a/server/ctrl/static.go b/server/ctrl/static.go index 3b6bb2bf..daa05918 100644 --- a/server/ctrl/static.go +++ b/server/ctrl/static.go @@ -275,48 +275,49 @@ func ServeBundle(ctx *App, res http.ResponseWriter, req *http.Request) { } func applyPatch(filePath string) (file *bytes.Buffer) { - origFile, err := WWWPublic.Open(filePath) - if err != nil { - Log.Debug("ctrl::static cannot open public file - %+v", err.Error()) - return nil - } var ( outputBuffer bytes.Buffer - outputInit bool + wasPatched bool ) - defer origFile.Close() - for _, patch := range Hooks.Get.StaticPatch() { - patchFile, err := patch.Open(strings.TrimPrefix(filePath, "/")) - if err != nil { - continue - } - patchFiles, _, err := gitdiff.Parse(patchFile) - patchFile.Close() - if err != nil { - Log.Debug("ctrl::static cannot parse patch file - %s", err.Error()) - break - } else if len(patchFiles) != 1 { - Log.Debug("ctrl::static unepected patch file size - must be 1, got %d", len(patchFiles)) - break - } - if !outputInit { - if _, err = outputBuffer.ReadFrom(origFile); err != nil { + for i, patch := range Hooks.Get.StaticPatch() { + if i == 0 { + origFile, err := WWWPublic.Open(filePath) + if err != nil { + Log.Debug("ctrl::static cannot open public file - %+v", err.Error()) + return nil + } + _, err = outputBuffer.ReadFrom(origFile) + origFile.Close() + if err != nil { + Log.Debug("ctrl::static cannot read from origFile - %s", err.Error()) return nil } - outputInit = true } - var patched bytes.Buffer - if err := gitdiff.Apply( - &patched, - bytes.NewReader(outputBuffer.Bytes()), - patchFiles[0], - ); err != nil { - Log.Debug("ctrl::static cannot apply patch - %s", err.Error()) + patchFiles, _, err := gitdiff.Parse(NewReadCloserFromBytes(patch)) + if err != nil { + Log.Debug("ctrl::static cannot parse patch file - %s", err.Error()) return nil } - outputBuffer = patched + for i := 0; i < len(patchFiles); i++ { + if patchFiles[i].NewName != patchFiles[i].OldName { + continue + } else if filePath != strings.TrimPrefix(patchFiles[i].NewName, "public") { + continue + } + var patched bytes.Buffer + if err := gitdiff.Apply( + &patched, + bytes.NewReader(outputBuffer.Bytes()), + patchFiles[i], + ); err != nil { + Log.Debug("ctrl::static cannot apply patch - %s", err.Error()) + return nil + } + outputBuffer = patched + wasPatched = true + } } - if outputInit { + if wasPatched { return &outputBuffer } return nil diff --git a/server/model/plugin.go b/server/model/plugin.go index 0001d11b..a9757783 100644 --- a/server/model/plugin.go +++ b/server/model/plugin.go @@ -35,12 +35,36 @@ func PluginDiscovery() error { if strings.HasSuffix(fname, ".zip") == false { continue } - name, modules, err := InitModule(entry.Name()) + name, impl, err := InitModule(entry.Name()) if err != nil { Log.Error("could not initialise module name=%s err=%s", entry.Name(), err.Error()) continue } - PLUGINS[name] = modules + for i := 0; i < len(impl.Modules); i++ { + switch impl.Modules[i]["type"] { + case "css": + f, err := GetPluginFile(name, impl.Modules[i]["entrypoint"]) + if err != nil { + return err + } + b, err := io.ReadAll(f) + if err != nil { + return err + } + Hooks.Register.CSS(string(b)) + case "patch": + f, err := GetPluginFile(name, impl.Modules[i]["entrypoint"]) + if err != nil { + return err + } + b, err := io.ReadAll(f) + if err != nil { + return err + } + Hooks.Register.StaticPatch(b) + } + } + PLUGINS[name] = impl } return nil }