diff --git a/README.md b/README.md index b6a9619..bd71a17 100644 --- a/README.md +++ b/README.md @@ -110,6 +110,8 @@ Usage of tusd: Use Google Cloud Storage with this bucket as storage backend (requires the GCS_SERVICE_ACCOUNT_FILE environment variable to be set) -hooks-dir string Directory to search for available hooks scripts + -hooks-enabled-events string + Comma separated list of enabled hook events (e.g. post-create,post-finish). Leave empty to enable all events -hooks-http string An HTTP endpoint to which hook events will be sent to -hooks-http-backoff int diff --git a/cmd/tusd/cli/flags.go b/cmd/tusd/cli/flags.go index e39ddbd..8af3a47 100644 --- a/cmd/tusd/cli/flags.go +++ b/cmd/tusd/cli/flags.go @@ -4,6 +4,8 @@ import ( "flag" "path/filepath" "strings" + + "github.com/tus/tusd/cmd/tusd/cli/hooks" ) var Flags struct { @@ -20,12 +22,14 @@ var Flags struct { S3Endpoint string GCSBucket string GCSObjectPrefix string + EnabledHooksString string FileHooksDir string HttpHooksEndpoint string HttpHooksRetry int HttpHooksBackoff int HooksStopUploadCode int PluginHookPath string + EnabledHooks []hooks.HookType ShowVersion bool ExposeMetrics bool MetricsPath string @@ -50,6 +54,7 @@ func ParseFlags() { flag.StringVar(&Flags.S3Endpoint, "s3-endpoint", "", "Endpoint to use S3 compatible implementations like minio (requires s3-bucket to be pass)") flag.StringVar(&Flags.GCSBucket, "gcs-bucket", "", "Use Google Cloud Storage with this bucket as storage backend (requires the GCS_SERVICE_ACCOUNT_FILE environment variable to be set)") flag.StringVar(&Flags.GCSObjectPrefix, "gcs-object-prefix", "", "Prefix for GCS object names (can't contain underscore character)") + flag.StringVar(&Flags.EnabledHooksString, "hooks-enabled-events", "", "Comma separated list of enabled hook events (e.g. post-create,post-finish). Leave empty to enable all 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.IntVar(&Flags.HttpHooksRetry, "hooks-http-retry", 3, "Number of times to retry on a 500 or network timeout") @@ -61,9 +66,10 @@ func ParseFlags() { flag.StringVar(&Flags.MetricsPath, "metrics-path", "/metrics", "Path under which the metrics endpoint will be accessible") flag.BoolVar(&Flags.BehindProxy, "behind-proxy", false, "Respect X-Forwarded-* and similar headers which may be set by proxies") flag.BoolVar(&Flags.VerboseOutput, "verbose", true, "Enable verbose logging output") - flag.Parse() + SetEnabledHooks() + if Flags.FileHooksDir != "" { Flags.FileHooksDir, _ = filepath.Abs(Flags.FileHooksDir) Flags.FileHooksInstalled = true @@ -89,3 +95,30 @@ func ParseFlags() { "Please remove underscore from the value", Flags.GCSObjectPrefix) } } + +func SetEnabledHooks() { + if Flags.EnabledHooksString != "" { + slc := strings.Split(Flags.EnabledHooksString, ",") + + for i, h := range slc { + slc[i] = strings.TrimSpace(h) + + if !hookTypeInSlice(hooks.HookType(h), hooks.AvailableHooks) { + stderr.Fatalf("Unknown hook event type in -hooks-enabled-events flag: %s", h) + } + + Flags.EnabledHooks = append(Flags.EnabledHooks, hooks.HookType(h)) + } + } + + if len(Flags.EnabledHooks) == 0 { + Flags.EnabledHooks = hooks.AvailableHooks + } + + var enabledHooksString []string + for _, h := range Flags.EnabledHooks { + enabledHooksString = append(enabledHooksString, string(h)) + } + + stdout.Printf("Enabled hook events: %s", strings.Join(enabledHooksString, ", ")) +} diff --git a/cmd/tusd/cli/hooks.go b/cmd/tusd/cli/hooks.go index 6c36651..faa91f6 100644 --- a/cmd/tusd/cli/hooks.go +++ b/cmd/tusd/cli/hooks.go @@ -10,6 +10,15 @@ import ( var hookHandler hooks.HookHandler = nil +func hookTypeInSlice(a hooks.HookType, list []hooks.HookType) bool { + for _, b := range list { + if b == a { + return true + } + } + return false +} + type hookDataStore struct { tusd.DataStore } @@ -62,7 +71,6 @@ func SetupPreHooks(composer *tusd.StoreComposer) error { composer.UseCore(hookDataStore{ DataStore: composer.Core, }) - return nil } @@ -91,6 +99,10 @@ func invokeHookAsync(typ hooks.HookType, info tusd.FileInfo) { } func invokeHookSync(typ hooks.HookType, info tusd.FileInfo, captureOutput bool) ([]byte, error) { + if !hookTypeInSlice(typ, Flags.EnabledHooks) { + return nil, nil + } + switch typ { case hooks.HookPostFinish: logEv(stdout, "UploadFinished", "id", info.ID, "size", strconv.FormatInt(info.Size, 10)) diff --git a/cmd/tusd/cli/hooks/hooks.go b/cmd/tusd/cli/hooks/hooks.go index 81a7ed4..9e97492 100644 --- a/cmd/tusd/cli/hooks/hooks.go +++ b/cmd/tusd/cli/hooks/hooks.go @@ -19,6 +19,8 @@ const ( HookPreCreate HookType = "pre-create" ) +var AvailableHooks []HookType = []HookType{HookPreCreate, HookPostCreate, HookPostReceive, HookPostTerminate, HookPostFinish} + type hookDataStore struct { tusd.DataStore } diff --git a/docs/hooks.md b/docs/hooks.md index b43e117..3fa9323 100644 --- a/docs/hooks.md +++ b/docs/hooks.md @@ -45,6 +45,9 @@ This event will be triggered after an upload has been terminated, meaning that t This event will be triggered for every running upload to indicate its current progress. It will be emitted whenever the server has received more data from the client but at most every second. The offset property will be set to the number of bytes which have been transfered to the server, at the time in total. Please be aware that this number may be higher than the number of bytes which have been stored by the data store! +## Whitelisting Hook Events + +The `--hooks-enabled-events` option for the tusd binary works as a whitelist for hook events and takes a comma separated list of hook events (for instance: `pre-create,post-create`). This can be useful to limit the number of hook executions and save resources if you are only interested in some events. If the `--hooks-enabled-events` option is omitted, all hook events are enabled. ## File Hooks ### The Hook Directory