Replace uint/uint16/uint32 values with int, declare errors as variables

This commit is contained in:
Seednode 2023-09-26 03:49:20 -05:00
parent a1d041dedc
commit 935021e464
5 changed files with 30 additions and 30 deletions

View File

@ -16,7 +16,8 @@ import (
) )
var ( var (
ErrIncorrectRefreshInterval = errors.New("refresh interval must be a duration string >= 500ms") ErrInvalidPort = errors.New("listen port must be an integer between 1 and 65535 inclusive")
ErrInvalidScanCount = errors.New("scan count must be a positive integer")
ErrNoMediaFound = errors.New("no supported media formats found which match all criteria") ErrNoMediaFound = errors.New("no supported media formats found which match all criteria")
) )

View File

@ -323,7 +323,7 @@ func walkPath(path string, fileChannel chan<- string, fileScans chan int, stats
errorChannel <- err errorChannel <- err
} }
if files > 0 && (files < int(MinFileCount)) || (files > int(MaxFileCount)) { if files > 0 && (files < MinFileCount) || (files > MaxFileCount) {
// This count will not otherwise include the parent directory itself, so increment by one // This count will not otherwise include the parent directory itself, so increment by one
stats.directoriesSkipped <- directories + 1 stats.directoriesSkipped <- directories + 1
stats.filesSkipped <- files stats.filesSkipped <- files

View File

@ -36,8 +36,8 @@ func serveIndexHtml(args []string, cache *fileCache, paginate bool) httprouter.H
startIndex = 0 startIndex = 0
stopIndex = fileCount stopIndex = fileCount
} else { } else {
startIndex = ((page - 1) * int(PageLength)) startIndex = ((page - 1) * PageLength)
stopIndex = (startIndex + int(PageLength)) stopIndex = (startIndex + PageLength)
} }
if startIndex > (fileCount - 1) { if startIndex > (fileCount - 1) {
@ -72,10 +72,10 @@ func serveIndexHtml(args []string, cache *fileCache, paginate bool) httprouter.H
var firstPage int = 1 var firstPage int = 1
var lastPage int var lastPage int
if fileCount%int(PageLength) == 0 { if fileCount%PageLength == 0 {
lastPage = fileCount / int(PageLength) lastPage = fileCount / PageLength
} else { } else {
lastPage = (fileCount / int(PageLength)) + 1 lastPage = (fileCount / PageLength) + 1
} }
if paginate { if paginate {
@ -96,7 +96,7 @@ func serveIndexHtml(args []string, cache *fileCache, paginate bool) httprouter.H
nextPage := page + 1 nextPage := page + 1
if nextPage > lastPage { if nextPage > lastPage {
nextPage = fileCount / int(PageLength) nextPage = fileCount / PageLength
} }
htmlBody.WriteString(fmt.Sprintf("<button onclick=\"window.location.href = '/html/%d';\">First</button>", htmlBody.WriteString(fmt.Sprintf("<button onclick=\"window.location.href = '/html/%d';\">First</button>",
@ -154,8 +154,8 @@ func serveIndexJson(args []string, index *fileCache, errorChannel chan<- error)
startIndex = 0 startIndex = 0
stopIndex = fileCount stopIndex = fileCount
} else { } else {
startIndex = ((page - 1) * int(PageLength)) startIndex = ((page - 1) * PageLength)
stopIndex = (startIndex + int(PageLength)) stopIndex = (startIndex + PageLength)
} }
if startIndex > (fileCount - 1) { if startIndex > (fileCount - 1) {

View File

@ -5,14 +5,13 @@ Copyright © 2023 Seednode <seednode@seedno.de>
package cmd package cmd
import ( import (
"errors"
"log" "log"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
const ( const (
ReleaseVersion string = "0.93.0" ReleaseVersion string = "0.93.1"
) )
var ( var (
@ -30,12 +29,12 @@ var (
Handlers bool Handlers bool
Images bool Images bool
Info bool Info bool
MaxDirScans uint MaxDirScans int
MaxFileScans uint MaxFileScans int
MaxFileCount uint MaxFileCount int
MinFileCount uint MinFileCount int
PageLength uint32 PageLength int
Port uint16 Port int
Prefix string Prefix string
Profile bool Profile bool
Recursive bool Recursive bool
@ -52,12 +51,12 @@ var (
Short: "Serves random media from the specified directories.", Short: "Serves random media from the specified directories.",
Args: cobra.MinimumNArgs(1), Args: cobra.MinimumNArgs(1),
PreRunE: func(cmd *cobra.Command, args []string) error { PreRunE: func(cmd *cobra.Command, args []string) error {
if MaxDirScans < 1 { if MaxDirScans < 1 || MaxFileScans < 1 {
return errors.New("max directory scan count must be a positive integer") return ErrInvalidScanCount
} }
if MaxFileScans < 1 { if Port < 1 || Port > 65535 {
return errors.New("max file scan count must be a positive integer") return ErrInvalidPort
} }
return nil return nil
@ -95,12 +94,12 @@ func init() {
rootCmd.Flags().BoolVar(&Handlers, "handlers", false, "display registered handlers (for debugging)") rootCmd.Flags().BoolVar(&Handlers, "handlers", false, "display registered handlers (for debugging)")
rootCmd.Flags().BoolVar(&Images, "images", false, "enable support for image files") rootCmd.Flags().BoolVar(&Images, "images", false, "enable support for image files")
rootCmd.Flags().BoolVarP(&Info, "info", "i", false, "expose informational endpoints") rootCmd.Flags().BoolVarP(&Info, "info", "i", false, "expose informational endpoints")
rootCmd.Flags().UintVar(&MaxDirScans, "max-directory-scans", 32, "number of directories to scan at once") rootCmd.Flags().IntVar(&MaxDirScans, "max-directory-scans", 32, "number of directories to scan at once")
rootCmd.Flags().UintVar(&MaxFileScans, "max-file-scans", 256, "number of files to scan at once") rootCmd.Flags().IntVar(&MaxFileScans, "max-file-scans", 256, "number of files to scan at once")
rootCmd.Flags().UintVar(&MaxFileCount, "max-file-count", 1<<32-1, "skip directories with file counts above this value") rootCmd.Flags().IntVar(&MaxFileCount, "max-file-count", 1<<32-1, "skip directories with file counts above this value")
rootCmd.Flags().UintVar(&MinFileCount, "min-file-count", 1, "skip directories with file counts below this value") rootCmd.Flags().IntVar(&MinFileCount, "min-file-count", 1, "skip directories with file counts below this value")
rootCmd.Flags().Uint32Var(&PageLength, "page-length", 0, "pagination length for info pages") rootCmd.Flags().IntVar(&PageLength, "page-length", 0, "pagination length for info pages")
rootCmd.Flags().Uint16VarP(&Port, "port", "p", 8080, "port to listen on") rootCmd.Flags().IntVarP(&Port, "port", "p", 8080, "port to listen on")
rootCmd.Flags().StringVar(&Prefix, "prefix", "/", "root path for http handlers (for reverse proxying)") rootCmd.Flags().StringVar(&Prefix, "prefix", "/", "root path for http handlers (for reverse proxying)")
rootCmd.Flags().BoolVar(&Profile, "profile", false, "register net/http/pprof handlers") rootCmd.Flags().BoolVar(&Profile, "profile", false, "register net/http/pprof handlers")
rootCmd.Flags().BoolVarP(&Recursive, "recursive", "r", false, "recurse into subdirectories") rootCmd.Flags().BoolVarP(&Recursive, "recursive", "r", false, "recurse into subdirectories")

View File

@ -453,7 +453,7 @@ func ServePage(args []string) error {
mux := httprouter.New() mux := httprouter.New()
srv := &http.Server{ srv := &http.Server{
Addr: net.JoinHostPort(Bind, strconv.Itoa(int(Port))), Addr: net.JoinHostPort(Bind, strconv.Itoa(Port)),
Handler: mux, Handler: mux,
IdleTimeout: 10 * time.Minute, IdleTimeout: 10 * time.Minute,
ReadTimeout: 5 * time.Second, ReadTimeout: 5 * time.Second,