feature (plugin): multi patch plugin

before this, we couldn't have multiple override plugin pointing to the
same file
This commit is contained in:
MickaelK 2025-05-13 00:51:29 +10:00
parent d5628fd03d
commit 2adad52308

View file

@ -463,13 +463,23 @@ func ServeBundle(ctx *App, res http.ResponseWriter, req *http.Request) {
} }
func applyPatch(filePath string) (file *bytes.Buffer) { 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
)
defer origFile.Close()
for _, patch := range Hooks.Get.StaticPatch() { for _, patch := range Hooks.Get.StaticPatch() {
patchFile, err := patch.Open(strings.TrimPrefix(filePath, "/")) patchFile, err := patch.Open(strings.TrimPrefix(filePath, "/"))
if err != nil { if err != nil {
continue continue
} }
defer patchFile.Close()
patchFiles, _, err := gitdiff.Parse(patchFile) patchFiles, _, err := gitdiff.Parse(patchFile)
patchFile.Close()
if err != nil { if err != nil {
Log.Debug("ctrl::static cannot parse patch file - %s", err.Error()) Log.Debug("ctrl::static cannot parse patch file - %s", err.Error())
break break
@ -477,27 +487,25 @@ func applyPatch(filePath string) (file *bytes.Buffer) {
Log.Debug("ctrl::static unepected patch file size - must be 1, got %d", len(patchFiles)) Log.Debug("ctrl::static unepected patch file size - must be 1, got %d", len(patchFiles))
break break
} }
origFile, err := WWWPublic.Open(filePath) if !outputInit {
if err != nil { if _, err = outputBuffer.ReadFrom(origFile); err != nil {
Log.Debug("ctrl::static cannot open public file - %+v", err.Error()) return nil
continue
} }
originalBuffer, err := io.ReadAll(origFile) outputInit = true
if err != nil {
Log.Debug("ctrl::static cannot read public file - %+v", err.Error())
continue
} }
var output bytes.Buffer var patched bytes.Buffer
origFile.Close()
if err := gitdiff.Apply( if err := gitdiff.Apply(
&output, &patched,
bytes.NewReader(originalBuffer), bytes.NewReader(outputBuffer.Bytes()),
patchFiles[0], patchFiles[0],
); err != nil { ); err != nil {
Log.Debug("ctrl::static cannot apply patch - %s", err.Error()) Log.Debug("ctrl::static cannot apply patch - %s", err.Error())
break return nil
} }
return &output outputBuffer = patched
}
if outputInit {
return &outputBuffer
} }
return nil return nil
} }