fix: before and after hook on upload event (Fixs #2721 #2626 #2600)

This commit is contained in:
kangkang 2023-10-15 20:26:01 +08:00
parent bd3c1941ff
commit d36bf89d93
5 changed files with 58 additions and 2 deletions

View File

@ -91,6 +91,7 @@ export async function post(url, content = "", overwrite = false, onupload) {
return useResourcesApi return useResourcesApi
? postResources(url, content, overwrite, onupload) ? postResources(url, content, overwrite, onupload)
: postTus(url, content, overwrite, onupload); : postTus(url, content, overwrite, onupload);
} }
async function postResources(url, content = "", overwrite = false, 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]; 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) { export function getDownloadURL(file, inline) {
const params = { const params = {
...(inline && { inline: "true" }), ...(inline && { inline: "true" }),

View File

@ -60,7 +60,9 @@ const actions = {
context.commit("addJob", item); context.commit("addJob", item);
context.dispatch("processUploads"); 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("setProgress", { id: item.id, loaded: item.file.size });
context.commit("removeJob", item.id); context.commit("removeJob", item.id);
context.dispatch("processUploads"); context.dispatch("processUploads");

View File

@ -38,6 +38,16 @@ var resourceGetHandler = withUser(func(w http.ResponseWriter, r *http.Request, d
file.Listing.ApplySort() file.Listing.ApplySort()
return renderJSON(w, r, file) 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 != "" { if checksum := r.URL.Query().Get("checksum"); checksum != "" {
err := file.Checksum(checksum) err := file.Checksum(checksum)

View File

@ -59,7 +59,12 @@ func tusPostHandler() handleFunc {
if err := openFile.Close(); err != nil { if err := openFile.Close(); err != nil {
return errToStatus(err), err 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 return http.StatusCreated, nil
}) })
} }

View File

@ -17,6 +17,39 @@ type Runner struct {
*settings.Settings *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. // 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 { func (r *Runner) RunHook(fn func() error, evt, path, dst string, user *users.User) error {
path = user.FullPath(path) path = user.FullPath(path)