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

View File

@ -6,7 +6,6 @@ package cmd
import (
"fmt"
"log"
"math"
"os"
"regexp"
@ -18,7 +17,7 @@ import (
const (
AllowedCharacters string = `^[A-z0-9.\-_]+$`
ReleaseVersion string = "5.0.4"
ReleaseVersion string = "5.0.2"
)
var (
@ -168,6 +167,8 @@ func init() {
rootCmd.Flags().SetInterspersed(true)
rootCmd.MarkFlagsMutuallyExclusive("debug", "exit-on-error")
rootCmd.MarkFlagsOneRequired(RequiredArgs...)
rootCmd.SetHelpCommand(&cobra.Command{
@ -179,6 +180,4 @@ func init() {
rootCmd.SilenceErrors = true
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)
}
func split(path string) (*splitPath, error) {
split := filename.FindAllStringSubmatch(path, -1)
func split(path string, regexes *regexes) (*splitPath, error) {
split := regexes.filename.FindAllStringSubmatch(path, -1)
if len(split) < 1 || len(split[0]) < 3 {
return &splitPath{}, nil
@ -54,8 +54,8 @@ func split(path string) (*splitPath, error) {
return p, nil
}
func getRange(path string, index *fileIndex) (string, string, error) {
splitPath, err := split(path)
func getRange(path string, regexes *regexes, index *fileIndex) (string, string, error) {
splitPath, err := split(path, regexes)
if err != nil {
return "", "", err
}
@ -70,7 +70,7 @@ func getRange(path string, index *fileIndex) (string, string, error) {
Loop:
for _, val := range list {
splitVal, err := split(val)
splitVal, err := split(val, regexes)
if err != nil {
return "", "", err
}
@ -94,8 +94,8 @@ func pathUrlEscape(path string) string {
return strings.Replace(path, `'`, `%27`, -1)
}
func paginateSorted(path, first, last, queryParams string, formats types.Types) (string, error) {
split, err := split(path)
func paginateSorted(path, first, last, queryParams string, regexes *regexes, formats types.Types) (string, error) {
split, err := split(path, regexes)
if err != nil {
return "", err
}

View File

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

View File

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