add enabled hook flag, call hook before getting file info

This commit is contained in:
benitogf 2021-10-18 15:11:51 +08:00
parent cb3c4e83c1
commit 8c0568b871
4 changed files with 15 additions and 9 deletions

1
.gitignore vendored
View File

@ -4,3 +4,4 @@ data/
node_modules/
.DS_Store
./tusd
cmd/tusd/tusd

View File

@ -80,7 +80,7 @@ func ParseFlags() {
flag.StringVar(&Flags.AzBlobAccessTier, "azure-blob-access-tier", "", "Blob access tier when uploading new files (possible values: archive, cool, hot, '')")
flag.StringVar(&Flags.AzObjectPrefix, "azure-object-prefix", "", "Prefix for Azure object names")
flag.StringVar(&Flags.AzEndpoint, "azure-endpoint", "", "Custom Endpoint to use for Azure BlockBlob Storage (requires azure-storage to be pass)")
flag.StringVar(&Flags.EnabledHooksString, "hooks-enabled-events", "pre-create,post-create,post-receive,post-terminate,post-finish", "Comma separated list of enabled hook events (e.g. post-create,post-finish). Leave empty to enable default events")
flag.StringVar(&Flags.EnabledHooksString, "hooks-enabled-events", "pre-get,pre-create,post-create,post-receive,post-terminate,post-finish", "Comma separated list of enabled hook events (e.g. post-create,post-finish). Leave empty to enable default events")
flag.StringVar(&Flags.FileHooksDir, "hooks-dir", "", "Directory to search for available hooks scripts")
flag.StringVar(&Flags.HttpHooksEndpoint, "hooks-http", "", "An HTTP endpoint to which hook events will be sent to")
flag.StringVar(&Flags.HttpHooksForwardHeaders, "hooks-http-forward-headers", "", "List of HTTP request headers to be forwarded from the client request to the hook endpoint")

View File

@ -104,7 +104,7 @@ func SetupPreHooks(config *handler.Config) error {
config.PreUploadCreateCallback = preCreateCallback
config.PreFinishResponseCallback = preFinishCallback
config.PreGetResponseCallback = preGetCallback
config.PreGetCallback = preGetCallback
return nil
}
@ -146,6 +146,8 @@ func invokeHookSync(typ hooks.HookType, info handler.HookEvent, captureOutput bo
logEv(stdout, "UploadFinished", "id", id, "size", strconv.FormatInt(size, 10))
case hooks.HookPostTerminate:
logEv(stdout, "UploadTerminated", "id", id)
case hooks.HookPreGet:
logEv(stdout, "PreGet", "id", id)
}
if hookHandler == nil {

View File

@ -719,6 +719,16 @@ func (handler *UnroutedHandler) GetFile(w http.ResponseWriter, r *http.Request)
return
}
// call the get hook before getting the file
if handler.config.PreGetCallback != nil {
if err := handler.config.PreGetCallback(newHookEvent(FileInfo{
ID: id,
}, r)); err != nil {
handler.sendError(w, r, err)
return
}
}
if handler.composer.UsesLocker {
lock, err := handler.lockUpload(id)
if err != nil {
@ -741,13 +751,6 @@ func (handler *UnroutedHandler) GetFile(w http.ResponseWriter, r *http.Request)
return
}
if handler.config.PreGetCallback != nil {
if err := handler.config.PreGetCallback(newHookEvent(info, r)); err != nil {
handler.sendError(w, r, err)
return
}
}
// Set headers before sending responses
w.Header().Set("Content-Length", strconv.FormatInt(info.Offset, 10))