From aa8e6b7eab2a870e478545dbbbfa9d1f7b8cfc98 Mon Sep 17 00:00:00 2001 From: Aleksandr Tihomirov Date: Sat, 7 Jan 2017 17:59:55 +0200 Subject: [PATCH] Add handling for file count limit --- cmd/gowo/cmd/watch.go | 7 +++++-- helpers.go | 13 +++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/cmd/gowo/cmd/watch.go b/cmd/gowo/cmd/watch.go index 3d5b90e..998f797 100644 --- a/cmd/gowo/cmd/watch.go +++ b/cmd/gowo/cmd/watch.go @@ -23,14 +23,14 @@ package cmd import ( "log" + "sync" "time" "github.com/spf13/cobra" "github.com/spf13/viper" - "sync" - "github.com/fsnotify/fsnotify" + "github.com/whats-this/owo.go" ) var queue = make(map[string]struct{}) @@ -67,6 +67,9 @@ var watchCmd = &cobra.Command{ var names []string queueLock.Lock() for name := range queue { + if len(names) == owo.FileCountLimit { + break + } names = append(names, name) delete(queue, name) } diff --git a/helpers.go b/helpers.go index b69bfa0..e7a5959 100644 --- a/helpers.go +++ b/helpers.go @@ -11,6 +11,10 @@ import ( // FilesToNamedReaders Converts a list of file names to named readers. func FilesToNamedReaders(names []string) (files []NamedReader, err error) { + if len(names) > FileCountLimit { + err = ErrTooManyFiles{len(names)} + return + } files = make([]NamedReader, len(names)) var file *os.File var stat os.FileInfo @@ -73,3 +77,12 @@ func humanateBytes(s uint64, base float64, sizes []string) string { return fmt.Sprintf(f, val, suffix) } + +// ErrTooManyFiles thrown when file count in upload exceeds filecount limit +type ErrTooManyFiles struct { + Count int +} + +func (e ErrTooManyFiles) Error() string { + return fmt.Sprintf("[pre-flight] Too many files (%d > %d)", e.Count, FileCountLimit) +}