From 5dc218c60eb2910e21411e92cf9dfa875970f70d Mon Sep 17 00:00:00 2001 From: Seednode Date: Tue, 3 Oct 2023 15:00:32 -0500 Subject: [PATCH] Remove timestamp from error output, allow min/max file count value of 0 --- cmd/errors.go | 2 +- cmd/files.go | 80 ++++++++++++++++++++++++--------------------------- cmd/root.go | 6 ++-- cmd/web.go | 3 ++ 4 files changed, 45 insertions(+), 46 deletions(-) diff --git a/cmd/errors.go b/cmd/errors.go index a5e4803..718dbbe 100644 --- a/cmd/errors.go +++ b/cmd/errors.go @@ -17,7 +17,7 @@ import ( var ( ErrInvalidFileCountRange = errors.New("maximum file count limit must be greater than or equal to minimum file count limit") - ErrInvalidFileCountValue = errors.New("file count limits must be positive integers no greater than 2147483647") + ErrInvalidFileCountValue = errors.New("file count limits must be non-negative integers no greater than 2147483647") ErrInvalidPort = errors.New("listen port must be an integer between 1 and 65535 inclusive") ErrNoMediaFound = errors.New("no supported media formats found which match all criteria") ) diff --git a/cmd/files.go b/cmd/files.go index 634aff9..f3b07b1 100644 --- a/cmd/files.go +++ b/cmd/files.go @@ -7,6 +7,7 @@ package cmd import ( "errors" "fmt" + "io/fs" "math/big" "regexp" @@ -229,8 +230,6 @@ func hasSupportedFiles(path string, formats *types.Types) (bool, error) { } func walkPath(path string, fileChannel chan<- string, stats *scanStatsChannels, formats *types.Types) error { - var wg sync.WaitGroup - errorChannel := make(chan error) done := make(chan bool, 1) @@ -239,8 +238,7 @@ func walkPath(path string, fileChannel chan<- string, stats *scanStatsChannels, return err } - var directories = 0 - var files = 0 + var directories, files = 0, 0 for _, node := range nodes { if node.IsDir() { @@ -261,50 +259,48 @@ func walkPath(path string, fileChannel chan<- string, stats *scanStatsChannels, skipFiles = true } - for _, node := range nodes { - fullPath := filepath.Join(path, node.Name()) + var wg sync.WaitGroup - switch { - case node.IsDir() && Recursive: + wg.Add(1) + go func() { + defer wg.Done() + for _, node := range nodes { wg.Add(1) - go func() { - defer func() { - wg.Done() - }() - err := walkPath(fullPath, fileChannel, stats, formats) - if err != nil { - errorChannel <- err + go func(node fs.DirEntry) { + defer wg.Done() - return + fullPath := filepath.Join(path, node.Name()) + + switch { + case node.IsDir() && Recursive: + err := walkPath(fullPath, fileChannel, stats, formats) + if err != nil { + errorChannel <- err + + return + } + case !node.IsDir() && !skipFiles: + path, err := normalizePath(fullPath) + if err != nil { + errorChannel <- err + + return + } + + if formats.Validate(path) || Fallback { + fileChannel <- path + + stats.filesMatched <- 1 + + return + } + + stats.filesSkipped <- 1 } - }() - case !node.IsDir() && !skipFiles: - wg.Add(1) - - go func() { - defer func() { - wg.Done() - }() - path, err := normalizePath(fullPath) - if err != nil { - errorChannel <- err - - return - } - - if formats.Validate(path) || Fallback { - fileChannel <- path - - stats.filesMatched <- 1 - - return - } - - stats.filesSkipped <- 1 - }() + }(node) } - } + }() go func() { wg.Wait() diff --git a/cmd/root.go b/cmd/root.go index dba42c8..1415f08 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -12,7 +12,7 @@ import ( ) const ( - ReleaseVersion string = "2.3.2" + ReleaseVersion string = "2.4.0" ) var ( @@ -52,7 +52,7 @@ var ( Args: cobra.MinimumNArgs(1), PreRunE: func(cmd *cobra.Command, args []string) error { switch { - case MaxFileCount < 1 || MinFileCount < 1 || MaxFileCount > math.MaxInt32 || MinFileCount > math.MaxInt32: + case MaxFileCount < 0 || MinFileCount < 0 || MaxFileCount > math.MaxInt32 || MinFileCount > math.MaxInt32: return ErrInvalidFileCountValue case MinFileCount > MaxFileCount: return ErrInvalidFileCountRange @@ -97,7 +97,7 @@ func init() { rootCmd.Flags().StringVar(&IndexFile, "index-file", "", "path to optional persistent index file") rootCmd.Flags().BoolVarP(&Info, "info", "i", false, "expose informational endpoints") rootCmd.Flags().IntVar(&MaxFileCount, "max-file-count", math.MaxInt32, "skip directories with file counts above this value") - rootCmd.Flags().IntVar(&MinFileCount, "min-file-count", 1, "skip directories with file counts below this value") + rootCmd.Flags().IntVar(&MinFileCount, "min-file-count", 0, "skip directories with file counts below this value") rootCmd.Flags().IntVar(&PageLength, "page-length", 0, "pagination length for info pages") rootCmd.Flags().IntVarP(&Port, "port", "p", 8080, "port to listen on") rootCmd.Flags().StringVar(&Prefix, "prefix", "/", "root path for http handlers (for reverse proxying)") diff --git a/cmd/web.go b/cmd/web.go index 20c6f68..e553742 100644 --- a/cmd/web.go +++ b/cmd/web.go @@ -10,6 +10,7 @@ import ( "errors" "fmt" "io" + "log" "net" "net/http" "os" @@ -404,6 +405,8 @@ func redirectRoot() httprouter.Handle { } func ServePage(args []string) error { + log.SetFlags(0) + timeZone := os.Getenv("TZ") if timeZone != "" { var err error