mirror of
https://github.com/mickael-kerjean/filestash
synced 2025-12-06 16:32:31 +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
|
return middlewares
|
||||||
}
|
}
|
||||||
|
|
||||||
var staticOverrides []fs.FS
|
var staticOverrides [][]byte
|
||||||
|
|
||||||
func (this Register) StaticPatch(folder fs.FS) {
|
func (this Register) StaticPatch(pathFile []byte) {
|
||||||
staticOverrides = append(staticOverrides, folder)
|
staticOverrides = append(staticOverrides, pathFile)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this Get) StaticPatch() []fs.FS {
|
func (this Get) StaticPatch() [][]byte {
|
||||||
return staticOverrides
|
return staticOverrides
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -275,48 +275,49 @@ func ServeBundle(ctx *App, res http.ResponseWriter, req *http.Request) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func applyPatch(filePath string) (file *bytes.Buffer) {
|
func applyPatch(filePath string) (file *bytes.Buffer) {
|
||||||
|
var (
|
||||||
|
outputBuffer bytes.Buffer
|
||||||
|
wasPatched bool
|
||||||
|
)
|
||||||
|
for i, patch := range Hooks.Get.StaticPatch() {
|
||||||
|
if i == 0 {
|
||||||
origFile, err := WWWPublic.Open(filePath)
|
origFile, err := WWWPublic.Open(filePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
Log.Debug("ctrl::static cannot open public file - %+v", err.Error())
|
Log.Debug("ctrl::static cannot open public file - %+v", err.Error())
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
var (
|
_, err = outputBuffer.ReadFrom(origFile)
|
||||||
outputBuffer bytes.Buffer
|
origFile.Close()
|
||||||
outputInit bool
|
|
||||||
)
|
|
||||||
defer origFile.Close()
|
|
||||||
for _, patch := range Hooks.Get.StaticPatch() {
|
|
||||||
patchFile, err := patch.Open(strings.TrimPrefix(filePath, "/"))
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
continue
|
Log.Debug("ctrl::static cannot read from origFile - %s", err.Error())
|
||||||
}
|
|
||||||
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 {
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
outputInit = true
|
}
|
||||||
|
patchFiles, _, err := gitdiff.Parse(NewReadCloserFromBytes(patch))
|
||||||
|
if err != nil {
|
||||||
|
Log.Debug("ctrl::static cannot parse patch file - %s", err.Error())
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
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
|
var patched bytes.Buffer
|
||||||
if err := gitdiff.Apply(
|
if err := gitdiff.Apply(
|
||||||
&patched,
|
&patched,
|
||||||
bytes.NewReader(outputBuffer.Bytes()),
|
bytes.NewReader(outputBuffer.Bytes()),
|
||||||
patchFiles[0],
|
patchFiles[i],
|
||||||
); err != nil {
|
); err != nil {
|
||||||
Log.Debug("ctrl::static cannot apply patch - %s", err.Error())
|
Log.Debug("ctrl::static cannot apply patch - %s", err.Error())
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
outputBuffer = patched
|
outputBuffer = patched
|
||||||
|
wasPatched = true
|
||||||
}
|
}
|
||||||
if outputInit {
|
}
|
||||||
|
if wasPatched {
|
||||||
return &outputBuffer
|
return &outputBuffer
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
|
|
||||||
|
|
@ -35,12 +35,36 @@ func PluginDiscovery() error {
|
||||||
if strings.HasSuffix(fname, ".zip") == false {
|
if strings.HasSuffix(fname, ".zip") == false {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
name, modules, err := InitModule(entry.Name())
|
name, impl, err := InitModule(entry.Name())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
Log.Error("could not initialise module name=%s err=%s", entry.Name(), err.Error())
|
Log.Error("could not initialise module name=%s err=%s", entry.Name(), err.Error())
|
||||||
continue
|
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
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue