Compare commits
No commits in common. "e67752470da09416aec3d3cadd941b3602fc109e" and "64133afe3333840eb3c1a7f2ee850f6ac74cbc10" have entirely different histories.
e67752470d
...
64133afe33
61
README.md
61
README.md
|
@ -106,36 +106,37 @@ Usage:
|
|||
roulette <path> [path]... [flags]
|
||||
|
||||
Flags:
|
||||
-a, --all enable all supported file types
|
||||
--audio enable support for audio files
|
||||
-b, --bind string address to bind to (default "0.0.0.0")
|
||||
--case-sensitive use case-sensitive matching for filters
|
||||
--code enable support for source code files
|
||||
--code-theme string theme for source code syntax highlighting (default "solarized-dark256")
|
||||
--exit-on-error shut down webserver on error, instead of just printing the error
|
||||
--fallback serve files as application/octet-stream if no matching format is registered
|
||||
-f, --filter enable filtering
|
||||
--flash enable support for shockwave flash files (via ruffle.rs)
|
||||
--handlers display registered handlers (for debugging)
|
||||
-h, --help help for roulette
|
||||
--images enable support for image files
|
||||
--index generate index of supported file paths at startup
|
||||
--index-file string path to optional persistent index file
|
||||
-i, --info expose informational endpoints
|
||||
--max-file-count int skip directories with file counts above this value (default 2147483647)
|
||||
--min-file-count int skip directories with file counts below this value (default 1)
|
||||
--page-length int pagination length for info pages
|
||||
-p, --port int port to listen on (default 8080)
|
||||
--prefix string root path for http handlers (for reverse proxying) (default "/")
|
||||
--profile register net/http/pprof handlers
|
||||
-r, --recursive recurse into subdirectories
|
||||
--refresh enable automatic page refresh via query parameter
|
||||
--russian remove selected images after serving
|
||||
-s, --sort enable sorting
|
||||
--text enable support for text files
|
||||
-v, --verbose log accessed files and other information to stdout
|
||||
-V, --version display version and exit
|
||||
--video enable support for video files
|
||||
-a, --all enable all supported file types
|
||||
--audio enable support for audio files
|
||||
-b, --bind string address to bind to (default "0.0.0.0")
|
||||
--case-sensitive use case-sensitive matching for filters
|
||||
--code enable support for source code files
|
||||
--code-theme string theme for source code syntax highlighting (default "solarized-dark256")
|
||||
--exit-on-error shut down webserver on error, instead of just printing the error
|
||||
-f, --filter enable filtering
|
||||
--flash enable support for shockwave flash files (via ruffle.rs)
|
||||
--handlers display registered handlers (for debugging)
|
||||
-h, --help help for roulette
|
||||
--images enable support for image files
|
||||
-c, --index generate index of supported file paths at startup
|
||||
--index-file string path to optional persistent index file
|
||||
-i, --info expose informational endpoints
|
||||
--max-directory-scans int number of directories to scan at once (default 32)
|
||||
--max-file-count int skip directories with file counts above this value (default 2147483647)
|
||||
--max-file-scans int number of files to scan at once (default 256)
|
||||
--min-file-count int skip directories with file counts below this value (default 1)
|
||||
--page-length int pagination length for info pages
|
||||
-p, --port int port to listen on (default 8080)
|
||||
--prefix string root path for http handlers (for reverse proxying) (default "/")
|
||||
--profile register net/http/pprof handlers
|
||||
-r, --recursive recurse into subdirectories
|
||||
--refresh enable automatic page refresh via query parameter
|
||||
--russian remove selected images after serving
|
||||
-s, --sort enable sorting
|
||||
--text enable support for text files
|
||||
-v, --verbose log accessed files and other information to stdout
|
||||
-V, --version display version and exit
|
||||
--video enable support for video files
|
||||
```
|
||||
|
||||
## Building the Docker container
|
||||
|
|
|
@ -19,6 +19,7 @@ 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")
|
||||
ErrInvalidPort = errors.New("listen port must be an integer between 1 and 65535 inclusive")
|
||||
ErrInvalidScanCount = errors.New("maximum scan count must be a positive integer no greater than 2147483647")
|
||||
ErrNoMediaFound = errors.New("no supported media formats found which match all criteria")
|
||||
)
|
||||
|
||||
|
|
91
cmd/files.go
91
cmd/files.go
|
@ -226,20 +226,15 @@ 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)
|
||||
func pathCount(path string) (int, int, error) {
|
||||
var directories = 0
|
||||
var files = 0
|
||||
|
||||
nodes, err := os.ReadDir(path)
|
||||
if err != nil {
|
||||
return err
|
||||
return 0, 0, err
|
||||
}
|
||||
|
||||
var directories = 0
|
||||
var files = 0
|
||||
|
||||
for _, node := range nodes {
|
||||
if node.IsDir() {
|
||||
directories++
|
||||
|
@ -248,61 +243,65 @@ func walkPath(path string, fileChannel chan<- string, stats *scanStatsChannels,
|
|||
}
|
||||
}
|
||||
|
||||
var skipFiles = false
|
||||
return files, directories, nil
|
||||
}
|
||||
|
||||
if files <= MaxFileCount && files >= MinFileCount {
|
||||
stats.directoriesMatched <- 1
|
||||
} else {
|
||||
stats.filesSkipped <- files
|
||||
stats.directoriesSkipped <- 1
|
||||
func walkPath(path string, fileChannel chan<- string, fileScans chan int, stats *scanStatsChannels, formats *types.Types) error {
|
||||
var wg sync.WaitGroup
|
||||
|
||||
skipFiles = true
|
||||
}
|
||||
|
||||
for _, node := range nodes {
|
||||
fullPath := filepath.Join(path, node.Name())
|
||||
errorChannel := make(chan error)
|
||||
done := make(chan bool, 1)
|
||||
|
||||
filepath.WalkDir(path, func(p string, info os.DirEntry, err error) error {
|
||||
switch {
|
||||
case node.IsDir() && Recursive:
|
||||
case !Recursive && info.IsDir() && p != path:
|
||||
return filepath.SkipDir
|
||||
case !info.IsDir():
|
||||
wg.Add(1)
|
||||
fileScans <- 1
|
||||
|
||||
go func() {
|
||||
defer func() {
|
||||
wg.Done()
|
||||
<-fileScans
|
||||
}()
|
||||
err := walkPath(fullPath, fileChannel, stats, formats)
|
||||
if err != nil {
|
||||
errorChannel <- err
|
||||
|
||||
return
|
||||
}
|
||||
}()
|
||||
case !node.IsDir() && !skipFiles:
|
||||
wg.Add(1)
|
||||
|
||||
go func() {
|
||||
defer func() {
|
||||
wg.Done()
|
||||
}()
|
||||
path, err := normalizePath(fullPath)
|
||||
path, err := normalizePath(p)
|
||||
if err != nil {
|
||||
errorChannel <- err
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
if formats.Validate(path) || Fallback {
|
||||
fileChannel <- path
|
||||
|
||||
stats.filesMatched <- 1
|
||||
if !formats.Validate(path) {
|
||||
stats.filesSkipped <- 1
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
stats.filesSkipped <- 1
|
||||
fileChannel <- path
|
||||
|
||||
stats.filesMatched <- 1
|
||||
}()
|
||||
case info.IsDir():
|
||||
files, directories, err := pathCount(p)
|
||||
if err != nil {
|
||||
errorChannel <- err
|
||||
}
|
||||
|
||||
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
|
||||
|
||||
return filepath.SkipDir
|
||||
}
|
||||
|
||||
stats.directoriesMatched <- 1
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
|
||||
go func() {
|
||||
wg.Wait()
|
||||
|
@ -312,8 +311,8 @@ func walkPath(path string, fileChannel chan<- string, stats *scanStatsChannels,
|
|||
Poll:
|
||||
for {
|
||||
select {
|
||||
case err := <-errorChannel:
|
||||
return err
|
||||
case e := <-errorChannel:
|
||||
return e
|
||||
case <-done:
|
||||
break Poll
|
||||
}
|
||||
|
@ -327,6 +326,8 @@ func scanPaths(paths []string, sort string, index *fileIndex, formats *types.Typ
|
|||
|
||||
fileChannel := make(chan string)
|
||||
errorChannel := make(chan error)
|
||||
directoryScans := make(chan int, MaxDirScans)
|
||||
fileScans := make(chan int, MaxFileScans)
|
||||
done := make(chan bool, 1)
|
||||
|
||||
stats := &scanStats{
|
||||
|
@ -349,13 +350,15 @@ func scanPaths(paths []string, sort string, index *fileIndex, formats *types.Typ
|
|||
|
||||
for i := 0; i < len(paths); i++ {
|
||||
wg.Add(1)
|
||||
directoryScans <- 1
|
||||
|
||||
go func(i int) {
|
||||
defer func() {
|
||||
wg.Done()
|
||||
<-directoryScans
|
||||
}()
|
||||
err := walkPath(paths[i], fileChannel, statsChannels, formats)
|
||||
|
||||
err := walkPath(paths[i], fileChannel, fileScans, statsChannels, formats)
|
||||
if err != nil {
|
||||
errorChannel <- err
|
||||
|
||||
|
|
10
cmd/root.go
10
cmd/root.go
|
@ -12,7 +12,7 @@ import (
|
|||
)
|
||||
|
||||
const (
|
||||
ReleaseVersion string = "2.3.0"
|
||||
ReleaseVersion string = "2.1.1"
|
||||
)
|
||||
|
||||
var (
|
||||
|
@ -23,7 +23,6 @@ var (
|
|||
Code bool
|
||||
CodeTheme string
|
||||
ExitOnError bool
|
||||
Fallback bool
|
||||
Filtering bool
|
||||
Flash bool
|
||||
Handlers bool
|
||||
|
@ -31,6 +30,8 @@ var (
|
|||
Index bool
|
||||
IndexFile string
|
||||
Info bool
|
||||
MaxDirScans int
|
||||
MaxFileScans int
|
||||
MaxFileCount int
|
||||
MinFileCount int
|
||||
PageLength int
|
||||
|
@ -52,6 +53,8 @@ var (
|
|||
Args: cobra.MinimumNArgs(1),
|
||||
PreRunE: func(cmd *cobra.Command, args []string) error {
|
||||
switch {
|
||||
case MaxDirScans < 1 || MaxFileScans < 1 || MaxDirScans > math.MaxInt32 || MaxFileScans > math.MaxInt32:
|
||||
return ErrInvalidScanCount
|
||||
case MaxFileCount < 1 || MinFileCount < 1 || MaxFileCount > math.MaxInt32 || MinFileCount > math.MaxInt32:
|
||||
return ErrInvalidFileCountValue
|
||||
case MinFileCount > MaxFileCount:
|
||||
|
@ -88,7 +91,6 @@ func init() {
|
|||
rootCmd.Flags().BoolVar(&Code, "code", false, "enable support for source code files")
|
||||
rootCmd.Flags().StringVar(&CodeTheme, "code-theme", "solarized-dark256", "theme for source code syntax highlighting")
|
||||
rootCmd.Flags().BoolVar(&ExitOnError, "exit-on-error", false, "shut down webserver on error, instead of just printing the error")
|
||||
rootCmd.Flags().BoolVar(&Fallback, "fallback", false, "serve files as application/octet-stream if no matching format is registered")
|
||||
rootCmd.Flags().BoolVarP(&Filtering, "filter", "f", false, "enable filtering")
|
||||
rootCmd.Flags().BoolVar(&Flash, "flash", false, "enable support for shockwave flash files (via ruffle.rs)")
|
||||
rootCmd.Flags().BoolVar(&Handlers, "handlers", false, "display registered handlers (for debugging)")
|
||||
|
@ -96,6 +98,8 @@ func init() {
|
|||
rootCmd.Flags().BoolVar(&Index, "index", false, "generate index of supported file paths at startup")
|
||||
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(&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", 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(&PageLength, "page-length", 0, "pagination length for info pages")
|
||||
|
|
31
cmd/web.go
31
cmd/web.go
|
@ -41,12 +41,12 @@ const (
|
|||
timeout time.Duration = 10 * time.Second
|
||||
)
|
||||
|
||||
func preparePath(prefix, path string) string {
|
||||
func preparePath(path string) string {
|
||||
if runtime.GOOS == "windows" {
|
||||
return fmt.Sprintf("%s/%s", prefix, filepath.ToSlash(path))
|
||||
return fmt.Sprintf("%s/%s", mediaPrefix, filepath.ToSlash(path))
|
||||
}
|
||||
|
||||
return prefix + path
|
||||
return mediaPrefix + path
|
||||
}
|
||||
|
||||
func serveStaticFile(paths []string, index *fileIndex, errorChannel chan<- error) httprouter.Handle {
|
||||
|
@ -228,7 +228,7 @@ func serveRoot(paths []string, regexes *regexes, index *fileIndex, formats *type
|
|||
newUrl := fmt.Sprintf("http://%s%s%s%s",
|
||||
r.Host,
|
||||
Prefix,
|
||||
preparePath(mediaPrefix, path),
|
||||
preparePath(path),
|
||||
queryParams,
|
||||
)
|
||||
http.Redirect(w, r, newUrl, redirectStatusCode)
|
||||
|
@ -266,28 +266,9 @@ func serveMedia(paths []string, regexes *regexes, index *fileIndex, formats *typ
|
|||
|
||||
format := formats.FileType(path)
|
||||
if format == nil {
|
||||
if Fallback {
|
||||
w.Header().Add("Content-Type", "application/octet-stream")
|
||||
serverError(w, r, nil)
|
||||
|
||||
_, refreshInterval := refreshInterval(r)
|
||||
|
||||
// redirect to static url for file
|
||||
newUrl := fmt.Sprintf("http://%s%s%s%s",
|
||||
r.Host,
|
||||
Prefix,
|
||||
preparePath(sourcePrefix, path),
|
||||
generateQueryParams(filters, sortOrder, refreshInterval),
|
||||
)
|
||||
|
||||
http.Redirect(w, r, newUrl, redirectStatusCode)
|
||||
|
||||
return
|
||||
} else {
|
||||
notFound(w, r, path)
|
||||
|
||||
return
|
||||
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
if !format.Validate(path) {
|
||||
|
|
Loading…
Reference in New Issue