Compare commits

..

No commits in common. "ebb5e48d9a309db49e829eec12bdf88762a29db0" and "9b6d9464e613b3c143acf09adbf43f2e1be24134" have entirely different histories.

5 changed files with 75 additions and 56 deletions

View File

@ -24,9 +24,10 @@ import (
"seedno.de/seednode/roulette/types" "seedno.de/seednode/roulette/types"
) )
var ( type regexes struct {
filename = regexp.MustCompile(`(.+?)([0-9]*)(\..+)`) alphanumeric *regexp.Regexp
) filename *regexp.Regexp
}
type scanStats struct { type scanStats struct {
filesMatched chan int filesMatched chan int
@ -78,14 +79,14 @@ func kill(path string, index *fileIndex) error {
return nil return nil
} }
func newFile(list []string, sortOrder string, formats types.Types) (string, error) { func newFile(list []string, sortOrder string, regexes *regexes, formats types.Types) (string, error) {
path, err := pickFile(list) path, err := pickFile(list)
if err != nil { if err != nil {
return "", err return "", err
} }
if sortOrder == "asc" || sortOrder == "desc" { if sortOrder == "asc" || sortOrder == "desc" {
splitPath, err := split(path) splitPath, err := split(path, regexes)
if err != nil { if err != nil {
return "", err return "", err
} }
@ -124,8 +125,8 @@ func newFile(list []string, sortOrder string, formats types.Types) (string, erro
return path, nil return path, nil
} }
func nextFile(filePath, sortOrder string, formats types.Types) (string, error) { func nextFile(filePath, sortOrder string, regexes *regexes, formats types.Types) (string, error) {
splitPath, err := split(filePath) splitPath, err := split(filePath, regexes)
if err != nil { if err != nil {
return "", err return "", err
} }
@ -240,6 +241,10 @@ func hasSupportedFiles(path string, formats types.Types) (bool, error) {
} }
func walkPath(path string, fileChannel chan<- string, wg1 *sync.WaitGroup, stats *scanStats, limit chan struct{}, formats types.Types, errorChannel chan<- error) { func walkPath(path string, fileChannel chan<- string, wg1 *sync.WaitGroup, stats *scanStats, limit chan struct{}, formats types.Types, errorChannel chan<- error) {
defer func() {
wg1.Done()
}()
limit <- struct{}{} limit <- struct{}{}
defer func() { defer func() {
@ -260,13 +265,11 @@ func walkPath(path string, fileChannel chan<- string, wg1 *sync.WaitGroup, stats
var skipDir = false var skipDir = false
for _, node := range nodes { for _, node := range nodes {
if !node.IsDir() { if Ignore && !node.IsDir() && node.Name() == IgnoreFile {
files++ skipDir = true
if Ignore && node.Name() == IgnoreFile {
skipDir = true
}
} }
files++
} }
var skipFiles = false var skipFiles = false
@ -294,19 +297,18 @@ func walkPath(path string, fileChannel chan<- string, wg1 *sync.WaitGroup, stats
case node.IsDir() && Recursive: case node.IsDir() && Recursive:
wg1.Add(1) wg1.Add(1)
go func() { walkPath(fullPath, fileChannel, wg1, stats, limit, formats, errorChannel)
defer wg1.Done()
walkPath(fullPath, fileChannel, wg1, stats, limit, formats, errorChannel)
}()
case !node.IsDir() && !skipFiles: case !node.IsDir() && !skipFiles:
path, err := normalizePath(fullPath) path, err := normalizePath(fullPath)
if err != nil {
switch {
case err != nil:
errorChannel <- err errorChannel <- err
case formats.Validate(path) || Fallback:
stats.filesSkipped <- 1
return
}
if formats.Validate(path) || Fallback {
fileChannel <- path fileChannel <- path
stats.filesMatched <- 1 stats.filesMatched <- 1
@ -419,8 +421,6 @@ func scanPaths(paths []string, sort string, index *fileIndex, formats types.Type
wg1.Add(1) wg1.Add(1)
go func(i int) { go func(i int) {
defer wg1.Done()
walkPath(paths[i], fileChannel, &wg1, stats, limit, formats, errorChannel) walkPath(paths[i], fileChannel, &wg1, stats, limit, formats, errorChannel)
}(i) }(i)
} }

View File

@ -6,7 +6,6 @@ package cmd
import ( import (
"fmt" "fmt"
"log"
"math" "math"
"os" "os"
"regexp" "regexp"
@ -18,7 +17,7 @@ import (
const ( const (
AllowedCharacters string = `^[A-z0-9.\-_]+$` AllowedCharacters string = `^[A-z0-9.\-_]+$`
ReleaseVersion string = "5.0.4" ReleaseVersion string = "5.0.2"
) )
var ( var (
@ -168,6 +167,8 @@ func init() {
rootCmd.Flags().SetInterspersed(true) rootCmd.Flags().SetInterspersed(true)
rootCmd.MarkFlagsMutuallyExclusive("debug", "exit-on-error")
rootCmd.MarkFlagsOneRequired(RequiredArgs...) rootCmd.MarkFlagsOneRequired(RequiredArgs...)
rootCmd.SetHelpCommand(&cobra.Command{ rootCmd.SetHelpCommand(&cobra.Command{
@ -179,6 +180,4 @@ func init() {
rootCmd.SilenceErrors = true rootCmd.SilenceErrors = true
rootCmd.Version = ReleaseVersion rootCmd.Version = ReleaseVersion
log.SetFlags(0)
} }

View File

@ -38,8 +38,8 @@ func (splitPath *splitPath) decrement() string {
return fmt.Sprintf("%0*d", len(splitPath.number), asInt-1) return fmt.Sprintf("%0*d", len(splitPath.number), asInt-1)
} }
func split(path string) (*splitPath, error) { func split(path string, regexes *regexes) (*splitPath, error) {
split := filename.FindAllStringSubmatch(path, -1) split := regexes.filename.FindAllStringSubmatch(path, -1)
if len(split) < 1 || len(split[0]) < 3 { if len(split) < 1 || len(split[0]) < 3 {
return &splitPath{}, nil return &splitPath{}, nil
@ -54,8 +54,8 @@ func split(path string) (*splitPath, error) {
return p, nil return p, nil
} }
func getRange(path string, index *fileIndex) (string, string, error) { func getRange(path string, regexes *regexes, index *fileIndex) (string, string, error) {
splitPath, err := split(path) splitPath, err := split(path, regexes)
if err != nil { if err != nil {
return "", "", err return "", "", err
} }
@ -70,7 +70,7 @@ func getRange(path string, index *fileIndex) (string, string, error) {
Loop: Loop:
for _, val := range list { for _, val := range list {
splitVal, err := split(val) splitVal, err := split(val, regexes)
if err != nil { if err != nil {
return "", "", err return "", "", err
} }
@ -94,8 +94,8 @@ func pathUrlEscape(path string) string {
return strings.Replace(path, `'`, `%27`, -1) return strings.Replace(path, `'`, `%27`, -1)
} }
func paginateSorted(path, first, last, queryParams string, formats types.Types) (string, error) { func paginateSorted(path, first, last, queryParams string, regexes *regexes, formats types.Types) (string, error) {
split, err := split(path) split, err := split(path, regexes)
if err != nil { if err != nil {
return "", err return "", err
} }

View File

@ -21,7 +21,7 @@ func sortOrder(r *http.Request) string {
return "" return ""
} }
func splitQueryParams(query string) []string { func splitQueryParams(query string, regexes *regexes) []string {
results := []string{} results := []string{}
if query == "" { if query == "" {

View File

@ -6,13 +6,16 @@ package cmd
import ( import (
"bytes" "bytes"
"context"
"errors" "errors"
"fmt" "fmt"
"io" "io"
"log"
"net" "net"
"net/http" "net/http"
"os" "os"
"path/filepath" "path/filepath"
"regexp"
"runtime" "runtime"
"strconv" "strconv"
"strings" "strings"
@ -168,7 +171,7 @@ func serveStaticFile(paths []string, index *fileIndex, errorChannel chan<- error
} }
} }
func serveRoot(paths []string, index *fileIndex, formats types.Types, errorChannel chan<- error) httprouter.Handle { func serveRoot(paths []string, regexes *regexes, index *fileIndex, formats types.Types, errorChannel chan<- error) httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) { return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
refererUri, err := stripQueryParams(refererToUri(r.Referer())) refererUri, err := stripQueryParams(refererToUri(r.Referer()))
if err != nil { if err != nil {
@ -182,8 +185,8 @@ func serveRoot(paths []string, index *fileIndex, formats types.Types, errorChann
strippedRefererUri := strings.TrimPrefix(refererUri, Prefix+mediaPrefix) strippedRefererUri := strings.TrimPrefix(refererUri, Prefix+mediaPrefix)
filters := &filters{ filters := &filters{
included: splitQueryParams(r.URL.Query().Get("include")), included: splitQueryParams(r.URL.Query().Get("include"), regexes),
excluded: splitQueryParams(r.URL.Query().Get("exclude")), excluded: splitQueryParams(r.URL.Query().Get("exclude"), regexes),
} }
sortOrder := sortOrder(r) sortOrder := sortOrder(r)
@ -193,7 +196,7 @@ func serveRoot(paths []string, index *fileIndex, formats types.Types, errorChann
var path string var path string
if refererUri != "" { if refererUri != "" {
path, err = nextFile(strippedRefererUri, sortOrder, formats) path, err = nextFile(strippedRefererUri, sortOrder, regexes, formats)
if err != nil { if err != nil {
errorChannel <- err errorChannel <- err
@ -217,7 +220,7 @@ func serveRoot(paths []string, index *fileIndex, formats types.Types, errorChann
break loop break loop
} }
path, err = newFile(list, sortOrder, formats) path, err = newFile(list, sortOrder, regexes, formats)
switch { switch {
case path == "": case path == "":
noFiles(w, r) noFiles(w, r)
@ -248,13 +251,13 @@ func serveRoot(paths []string, index *fileIndex, formats types.Types, errorChann
} }
} }
func serveMedia(paths []string, index *fileIndex, formats types.Types, errorChannel chan<- error) httprouter.Handle { func serveMedia(paths []string, regexes *regexes, index *fileIndex, formats types.Types, errorChannel chan<- error) httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) { return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
startTime := time.Now() startTime := time.Now()
filters := &filters{ filters := &filters{
included: splitQueryParams(r.URL.Query().Get("include")), included: splitQueryParams(r.URL.Query().Get("include"), regexes),
excluded: splitQueryParams(r.URL.Query().Get("exclude")), excluded: splitQueryParams(r.URL.Query().Get("exclude"), regexes),
} }
sortOrder := sortOrder(r) sortOrder := sortOrder(r)
@ -344,7 +347,7 @@ func serveMedia(paths []string, index *fileIndex, formats types.Types, errorChan
var first, last string var first, last string
if Index && sortOrder != "" { if Index && sortOrder != "" {
first, last, err = getRange(path, index) first, last, err = getRange(path, regexes, index)
if err != nil { if err != nil {
errorChannel <- err errorChannel <- err
@ -355,7 +358,7 @@ func serveMedia(paths []string, index *fileIndex, formats types.Types, errorChan
} }
if Index && !DisableButtons && sortOrder != "" { if Index && !DisableButtons && sortOrder != "" {
paginate, err := paginateSorted(path, first, last, queryParams, formats) paginate, err := paginateSorted(path, first, last, queryParams, regexes, formats)
if err != nil { if err != nil {
errorChannel <- err errorChannel <- err
@ -475,6 +478,8 @@ func redirectRoot() httprouter.Handle {
} }
func ServePage(args []string) error { func ServePage(args []string) error {
log.SetFlags(0)
timeZone := os.Getenv("TZ") timeZone := os.Getenv("TZ")
if timeZone != "" { if timeZone != "" {
var err error var err error
@ -536,6 +541,11 @@ func ServePage(args []string) error {
return ErrNoMediaFound return ErrNoMediaFound
} }
regexes := &regexes{
filename: regexp.MustCompile(`(.+?)([0-9]*)(\..+)`),
alphanumeric: regexp.MustCompile(`^[A-z0-9]*$`),
}
if !strings.HasSuffix(Prefix, "/") { if !strings.HasSuffix(Prefix, "/") {
Prefix = Prefix + "/" Prefix = Prefix + "/"
} }
@ -563,20 +573,30 @@ func ServePage(args []string) error {
go func() { go func() {
for err := range errorChannel { for err := range errorChannel {
prefix := "ERROR"
switch { switch {
case ExitOnError: case errors.Is(err, os.ErrNotExist) && Debug:
fmt.Printf("%s | FATAL: %v\n", time.Now().Format(logDate), err) prefix = "DEBUG"
case Debug && errors.Is(err, os.ErrNotExist) || errors.Is(err, os.ErrPermission): case errors.Is(err, os.ErrNotExist):
fmt.Printf("%s | DEBUG: %v\n", time.Now().Format(logDate), err)
case errors.Is(err, os.ErrNotExist) || errors.Is(err, os.ErrPermission):
continue continue
default: case errors.Is(err, os.ErrPermission) && Debug:
fmt.Printf("%s | ERROR: %v\n", time.Now().Format(logDate), err) prefix = "DEBUG"
case errors.Is(err, os.ErrPermission):
continue
}
fmt.Printf("%s | %s: %v\n", time.Now().Format(logDate), prefix, err)
if ExitOnError {
fmt.Printf("%s | %s: Shutting down...\n", time.Now().Format(logDate), prefix)
srv.Shutdown(context.Background())
} }
} }
}() }()
registerHandler(mux, Prefix, serveRoot(paths, index, formats, errorChannel)) registerHandler(mux, Prefix, serveRoot(paths, regexes, index, formats, errorChannel))
Prefix = strings.TrimSuffix(Prefix, "/") Prefix = strings.TrimSuffix(Prefix, "/")
@ -588,7 +608,7 @@ func ServePage(args []string) error {
registerHandler(mux, Prefix+"/favicon.ico", serveFavicons(errorChannel)) registerHandler(mux, Prefix+"/favicon.ico", serveFavicons(errorChannel))
registerHandler(mux, Prefix+mediaPrefix+"/*media", serveMedia(paths, index, formats, errorChannel)) registerHandler(mux, Prefix+mediaPrefix+"/*media", serveMedia(paths, regexes, index, formats, errorChannel))
registerHandler(mux, Prefix+sourcePrefix+"/*static", serveStaticFile(paths, index, errorChannel)) registerHandler(mux, Prefix+sourcePrefix+"/*static", serveStaticFile(paths, index, errorChannel))