From d36bf89d931ec718206d6ff6eed499192b1fdb4e Mon Sep 17 00:00:00 2001 From: kangkang Date: Sun, 15 Oct 2023 20:26:01 +0800 Subject: [PATCH] fix: before and after hook on upload event (Fixs #2721 #2626 #2600) --- frontend/src/api/files.js | 6 +++++ frontend/src/store/modules/upload.js | 4 +++- http/resource.go | 10 +++++++++ http/tus_handlers.go | 7 +++++- runner/runner.go | 33 ++++++++++++++++++++++++++++ 5 files changed, 58 insertions(+), 2 deletions(-) diff --git a/frontend/src/api/files.js b/frontend/src/api/files.js index 1c2ea80a..85a010e8 100644 --- a/frontend/src/api/files.js +++ b/frontend/src/api/files.js @@ -91,6 +91,7 @@ export async function post(url, content = "", overwrite = false, onupload) { return useResourcesApi ? postResources(url, content, overwrite, onupload) : postTus(url, content, overwrite, onupload); + } async function postResources(url, content = "", overwrite = false, onupload) { @@ -163,6 +164,11 @@ export async function checksum(url, algo) { return (await data.json()).checksums[algo]; } +export async function runHook(url) { + const data = await resourceAction(`${url}?runhookafter=true`, "GET"); + return await data.json(); +} + export function getDownloadURL(file, inline) { const params = { ...(inline && { inline: "true" }), diff --git a/frontend/src/store/modules/upload.js b/frontend/src/store/modules/upload.js index 836b59ab..374189e5 100644 --- a/frontend/src/store/modules/upload.js +++ b/frontend/src/store/modules/upload.js @@ -60,7 +60,9 @@ const actions = { context.commit("addJob", item); context.dispatch("processUploads"); }, - finishUpload: (context, item) => { + finishUpload: async (context, item) => { + await api.runHook(item.path).catch(Vue.prototype.$showError); + context.commit("setProgress", { id: item.id, loaded: item.file.size }); context.commit("removeJob", item.id); context.dispatch("processUploads"); diff --git a/http/resource.go b/http/resource.go index 3a12538a..4c3d3580 100644 --- a/http/resource.go +++ b/http/resource.go @@ -38,6 +38,16 @@ var resourceGetHandler = withUser(func(w http.ResponseWriter, r *http.Request, d file.Listing.ApplySort() return renderJSON(w, r, file) } + if runhookafter := r.URL.Query().Get("runhookafter"); runhookafter != "" { + err := d.RunHookAfter(func() error { + return nil + }, "upload", r.URL.Path, "", d.user) + if err != nil { + return http.StatusInternalServerError, err + } + // do not waste bandwidth if we just want the run hook + file.Content = "" + } if checksum := r.URL.Query().Get("checksum"); checksum != "" { err := file.Checksum(checksum) diff --git a/http/tus_handlers.go b/http/tus_handlers.go index 15003cce..7ef4fb12 100644 --- a/http/tus_handlers.go +++ b/http/tus_handlers.go @@ -59,7 +59,12 @@ func tusPostHandler() handleFunc { if err := openFile.Close(); err != nil { return errToStatus(err), err } - + err = d.RunHookBefore(func() error { + return nil + }, "upload", r.URL.Path, "", d.user) + if err != nil { + return errToStatus(err), err + } return http.StatusCreated, nil }) } diff --git a/runner/runner.go b/runner/runner.go index 2dbafa5c..f292382f 100644 --- a/runner/runner.go +++ b/runner/runner.go @@ -17,6 +17,39 @@ type Runner struct { *settings.Settings } +func (r *Runner) RunHookBefore(fn func() error, evt, path, dst string, user *users.User) error { + path = user.FullPath(path) + dst = user.FullPath(dst) + + if r.Enabled { + if val, ok := r.Commands["before_"+evt]; ok { + for _, command := range val { + err := r.exec(command, "before_"+evt, path, dst, user) + if err != nil { + return err + } + } + } + } + return nil +} +func (r *Runner) RunHookAfter(fn func() error, evt, path, dst string, user *users.User) error { + path = user.FullPath(path) + dst = user.FullPath(dst) + if r.Enabled { + if val, ok := r.Commands["after_"+evt]; ok { + for _, command := range val { + err := r.exec(command, "after_"+evt, path, dst, user) + if err != nil { + return err + } + } + } + } + + return nil +} + // RunHook runs the hooks for the before and after event. func (r *Runner) RunHook(fn func() error, evt, path, dst string, user *users.User) error { path = user.FullPath(path)