diff --git a/server/plugin/plg_backend_s3/config.go b/server/plugin/plg_backend_s3/config.go new file mode 100644 index 00000000..b7791c16 --- /dev/null +++ b/server/plugin/plg_backend_s3/config.go @@ -0,0 +1,26 @@ +package plg_backend_s3 + +import ( + . "github.com/mickael-kerjean/filestash/server/common" + "time" +) + +var ls_timeout func() time.Duration + +func init() { + ls_timeout = func() time.Duration { + return time.Duration(Config.Get("features.protection.ls_timeout").Schema(func(f *FormElement) *FormElement { + if f == nil { + f = &FormElement{} + } + f.Default = 2 + f.Name = "ls_timeout" + f.Type = "number" + f.Target = []string{} + f.Description = "failsafe timeout for listing files under a folder" + f.Placeholder = "Default: 2" + return f + }).Int()) * time.Second + } + ls_timeout() +} diff --git a/server/plugin/plg_backend_s3/index.go b/server/plugin/plg_backend_s3/index.go index 89ce4060..4292f142 100644 --- a/server/plugin/plg_backend_s3/index.go +++ b/server/plugin/plg_backend_s3/index.go @@ -19,6 +19,7 @@ import ( "os" "path/filepath" "strings" + "time" ) var S3Cache AppCache @@ -169,6 +170,7 @@ func (s S3Backend) Ls(path string) (files []os.FileInfo, err error) { } client := s3.New(s.createSession(p.bucket)) + startTime := time.Now() err = client.ListObjectsV2PagesWithContext( s.context, &s3.ListObjectsV2Input{ @@ -194,6 +196,11 @@ func (s S3Backend) Ls(path string) (files []os.FileInfo, err error) { FType: "directory", }) } + + if time.Since(startTime) > ls_timeout() { + Log.Debug("plg_backend_s3::ls timeout triggered after getting %d files", len(files)) + return false + } return aws.BoolValue(objs.IsTruncated) }) return files, err