Replace uint/uint16/uint32 values with int, declare errors as variables
This commit is contained in:
parent
a1d041dedc
commit
935021e464
|
@ -16,8 +16,9 @@ import (
|
|||
)
|
||||
|
||||
var (
|
||||
ErrIncorrectRefreshInterval = errors.New("refresh interval must be a duration string >= 500ms")
|
||||
ErrNoMediaFound = errors.New("no supported media formats found which match all criteria")
|
||||
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")
|
||||
)
|
||||
|
||||
func newErrorPage(title, body string) string {
|
||||
|
|
|
@ -323,7 +323,7 @@ func walkPath(path string, fileChannel chan<- string, fileScans chan int, stats
|
|||
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
|
||||
stats.directoriesSkipped <- directories + 1
|
||||
stats.filesSkipped <- files
|
||||
|
|
16
cmd/info.go
16
cmd/info.go
|
@ -36,8 +36,8 @@ func serveIndexHtml(args []string, cache *fileCache, paginate bool) httprouter.H
|
|||
startIndex = 0
|
||||
stopIndex = fileCount
|
||||
} else {
|
||||
startIndex = ((page - 1) * int(PageLength))
|
||||
stopIndex = (startIndex + int(PageLength))
|
||||
startIndex = ((page - 1) * PageLength)
|
||||
stopIndex = (startIndex + PageLength)
|
||||
}
|
||||
|
||||
if startIndex > (fileCount - 1) {
|
||||
|
@ -72,10 +72,10 @@ func serveIndexHtml(args []string, cache *fileCache, paginate bool) httprouter.H
|
|||
var firstPage int = 1
|
||||
var lastPage int
|
||||
|
||||
if fileCount%int(PageLength) == 0 {
|
||||
lastPage = fileCount / int(PageLength)
|
||||
if fileCount%PageLength == 0 {
|
||||
lastPage = fileCount / PageLength
|
||||
} else {
|
||||
lastPage = (fileCount / int(PageLength)) + 1
|
||||
lastPage = (fileCount / PageLength) + 1
|
||||
}
|
||||
|
||||
if paginate {
|
||||
|
@ -96,7 +96,7 @@ func serveIndexHtml(args []string, cache *fileCache, paginate bool) httprouter.H
|
|||
|
||||
nextPage := page + 1
|
||||
if nextPage > lastPage {
|
||||
nextPage = fileCount / int(PageLength)
|
||||
nextPage = fileCount / PageLength
|
||||
}
|
||||
|
||||
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
|
||||
stopIndex = fileCount
|
||||
} else {
|
||||
startIndex = ((page - 1) * int(PageLength))
|
||||
stopIndex = (startIndex + int(PageLength))
|
||||
startIndex = ((page - 1) * PageLength)
|
||||
stopIndex = (startIndex + PageLength)
|
||||
}
|
||||
|
||||
if startIndex > (fileCount - 1) {
|
||||
|
|
35
cmd/root.go
35
cmd/root.go
|
@ -5,14 +5,13 @@ Copyright © 2023 Seednode <seednode@seedno.de>
|
|||
package cmd
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"log"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
const (
|
||||
ReleaseVersion string = "0.93.0"
|
||||
ReleaseVersion string = "0.93.1"
|
||||
)
|
||||
|
||||
var (
|
||||
|
@ -30,12 +29,12 @@ var (
|
|||
Handlers bool
|
||||
Images bool
|
||||
Info bool
|
||||
MaxDirScans uint
|
||||
MaxFileScans uint
|
||||
MaxFileCount uint
|
||||
MinFileCount uint
|
||||
PageLength uint32
|
||||
Port uint16
|
||||
MaxDirScans int
|
||||
MaxFileScans int
|
||||
MaxFileCount int
|
||||
MinFileCount int
|
||||
PageLength int
|
||||
Port int
|
||||
Prefix string
|
||||
Profile bool
|
||||
Recursive bool
|
||||
|
@ -52,12 +51,12 @@ var (
|
|||
Short: "Serves random media from the specified directories.",
|
||||
Args: cobra.MinimumNArgs(1),
|
||||
PreRunE: func(cmd *cobra.Command, args []string) error {
|
||||
if MaxDirScans < 1 {
|
||||
return errors.New("max directory scan count must be a positive integer")
|
||||
if MaxDirScans < 1 || MaxFileScans < 1 {
|
||||
return ErrInvalidScanCount
|
||||
}
|
||||
|
||||
if MaxFileScans < 1 {
|
||||
return errors.New("max file scan count must be a positive integer")
|
||||
if Port < 1 || Port > 65535 {
|
||||
return ErrInvalidPort
|
||||
}
|
||||
|
||||
return nil
|
||||
|
@ -95,12 +94,12 @@ func init() {
|
|||
rootCmd.Flags().BoolVar(&Handlers, "handlers", false, "display registered handlers (for debugging)")
|
||||
rootCmd.Flags().BoolVar(&Images, "images", false, "enable support for image files")
|
||||
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().UintVar(&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().UintVar(&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().Uint16VarP(&Port, "port", "p", 8080, "port to listen on")
|
||||
rootCmd.Flags().IntVar(&MaxDirScans, "max-directory-scans", 32, "number of directories to scan at once")
|
||||
rootCmd.Flags().IntVar(&MaxFileScans, "max-file-scans", 256, "number of files to scan at once")
|
||||
rootCmd.Flags().IntVar(&MaxFileCount, "max-file-count", 1<<32-1, "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(&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)")
|
||||
rootCmd.Flags().BoolVar(&Profile, "profile", false, "register net/http/pprof handlers")
|
||||
rootCmd.Flags().BoolVarP(&Recursive, "recursive", "r", false, "recurse into subdirectories")
|
||||
|
|
|
@ -453,7 +453,7 @@ func ServePage(args []string) error {
|
|||
mux := httprouter.New()
|
||||
|
||||
srv := &http.Server{
|
||||
Addr: net.JoinHostPort(Bind, strconv.Itoa(int(Port))),
|
||||
Addr: net.JoinHostPort(Bind, strconv.Itoa(Port)),
|
||||
Handler: mux,
|
||||
IdleTimeout: 10 * time.Minute,
|
||||
ReadTimeout: 5 * time.Second,
|
||||
|
|
Loading…
Reference in New Issue