mirror of
https://github.com/mickael-kerjean/filestash
synced 2025-12-06 16:32:31 +01:00
feature (plugin): multi patch plugin
before this, we couldn't have multiple override plugin pointing to the same file
This commit is contained in:
parent
d5628fd03d
commit
2adad52308
1 changed files with 24 additions and 16 deletions
|
|
@ -463,13 +463,23 @@ 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
|
||||
)
|
||||
defer origFile.Close()
|
||||
for _, patch := range Hooks.Get.StaticPatch() {
|
||||
patchFile, err := patch.Open(strings.TrimPrefix(filePath, "/"))
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
defer patchFile.Close()
|
||||
patchFiles, _, err := gitdiff.Parse(patchFile)
|
||||
patchFile.Close()
|
||||
if err != nil {
|
||||
Log.Debug("ctrl::static cannot parse patch file - %s", err.Error())
|
||||
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))
|
||||
break
|
||||
}
|
||||
origFile, err := WWWPublic.Open(filePath)
|
||||
if err != nil {
|
||||
Log.Debug("ctrl::static cannot open public file - %+v", err.Error())
|
||||
continue
|
||||
if !outputInit {
|
||||
if _, err = outputBuffer.ReadFrom(origFile); err != nil {
|
||||
return nil
|
||||
}
|
||||
originalBuffer, err := io.ReadAll(origFile)
|
||||
if err != nil {
|
||||
Log.Debug("ctrl::static cannot read public file - %+v", err.Error())
|
||||
continue
|
||||
outputInit = true
|
||||
}
|
||||
var output bytes.Buffer
|
||||
origFile.Close()
|
||||
var patched bytes.Buffer
|
||||
if err := gitdiff.Apply(
|
||||
&output,
|
||||
bytes.NewReader(originalBuffer),
|
||||
&patched,
|
||||
bytes.NewReader(outputBuffer.Bytes()),
|
||||
patchFiles[0],
|
||||
); err != nil {
|
||||
Log.Debug("ctrl::static cannot apply patch - %s", err.Error())
|
||||
break
|
||||
return nil
|
||||
}
|
||||
return &output
|
||||
outputBuffer = patched
|
||||
}
|
||||
if outputInit {
|
||||
return &outputBuffer
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue