mirror of
https://github.com/mickael-kerjean/filestash
synced 2025-12-06 08:22:24 +01:00
feature (patch): static patches
This commit is contained in:
parent
33dc1b5876
commit
b7e0b1ad25
3 changed files with 64 additions and 39 deletions
|
|
@ -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
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue