diff --git a/README.md b/README.md index 8904b17..cc46419 100644 --- a/README.md +++ b/README.md @@ -60,7 +60,6 @@ Available Commands: version Print version Flags: - -c, --count display number of files/directories matched/skipped -f, --filter enable filtering via query parameters -h, --help help for roulette -p, --port uint16 port to listen on (default 8080) diff --git a/cmd/files.go b/cmd/files.go index c61fd35..3f05859 100644 --- a/cmd/files.go +++ b/cmd/files.go @@ -35,6 +35,10 @@ type Stats struct { DirectoriesMatched uint64 } +func (s *Stats) GetFilesTotal() uint64 { + return atomic.LoadUint64(&s.FilesMatched) + atomic.LoadUint64(&s.FilesSkipped) +} + func (s *Stats) IncrementFilesMatched() { atomic.AddUint64(&s.FilesMatched, 1) } @@ -283,10 +287,12 @@ func pathIsValid(filePath string, paths []string) bool { matchesPrefix = true } } - if !matchesPrefix { - if Verbose { - fmt.Printf("%v Failed to serve file outside specified path(s): %v\n", time.Now().Format(LOGDATE), filePath) - } + + if Verbose && !matchesPrefix { + fmt.Printf("%v | Error: Failed to serve file outside specified path(s): %v\n", + time.Now().Format(LOGDATE), + filePath, + ) return false } @@ -428,10 +434,11 @@ func pickFile(args []string, filters *Filters, sort string) (string, error) { getFileList(args, &files, filters, &stats, &concurrency) runTime := time.Since(startTime) - if Count { - fmt.Printf("Scanned %v files (skipped %v) across %v directories in %v\n", + if Verbose { + fmt.Printf("%v | Scanned %v/%v files across %v directories in %v\n", + time.Now().Format(LOGDATE), stats.GetFilesMatched(), - stats.GetFilesSkipped(), + stats.GetFilesTotal(), stats.GetDirectoriesMatched(), runTime, ) diff --git a/cmd/root.go b/cmd/root.go index 5ccfa4b..e9fe99a 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -24,7 +24,6 @@ type Concurrency struct { FileScans chan int } -var Count bool var Filter bool var Port uint16 var Recursive bool @@ -52,7 +51,6 @@ func Execute() { } func init() { - rootCmd.Flags().BoolVarP(&Count, "count", "c", false, "display number of files/directories matched/skipped") rootCmd.Flags().BoolVarP(&Filter, "filter", "f", false, "enable filtering via query parameters") rootCmd.Flags().Uint16VarP(&Port, "port", "p", 8080, "port to listen on") rootCmd.Flags().BoolVarP(&Recursive, "recursive", "r", false, "recurse into subdirectories") diff --git a/cmd/version.go b/cmd/version.go index 76fabfd..916351e 100644 --- a/cmd/version.go +++ b/cmd/version.go @@ -10,7 +10,7 @@ import ( "github.com/spf13/cobra" ) -var Version = "0.17.2" +var Version = "0.17.3" func init() { rootCmd.AddCommand(versionCmd) diff --git a/cmd/web.go b/cmd/web.go index 8e15579..2d7bba2 100644 --- a/cmd/web.go +++ b/cmd/web.go @@ -195,11 +195,7 @@ func serveStaticFile(w http.ResponseWriter, r *http.Request, paths []string) err return nil } - var startTime time.Time - if Verbose { - startTime = time.Now() - fmt.Printf("%v Serving file: %v", startTime.Format(LOGDATE), filePath) - } + startTime := time.Now() buf, err := os.ReadFile(filePath) if err != nil { @@ -209,7 +205,12 @@ func serveStaticFile(w http.ResponseWriter, r *http.Request, paths []string) err w.Write(buf) if Verbose { - fmt.Printf(" (Finished in %v)\n", time.Since(startTime).Round(time.Microsecond)) + fmt.Printf("%v | Served \"%v\" to %v in %v\n", + startTime.Format(LOGDATE), + filePath, + r.RemoteAddr, + time.Since(startTime).Round(time.Microsecond), + ) } return nil @@ -406,6 +407,17 @@ func serveHtmlHandler(paths []string) appHandler { default: filePath := r.URL.Path + exists, err := fileExists(filePath) + if err != nil { + return err + } + + if !exists { + http.NotFound(w, r) + + return nil + } + image, err := isImage(filePath) if err != nil { return err