Compare commits
3 Commits
9b6d9464e6
...
ebb5e48d9a
Author | SHA1 | Date |
---|---|---|
Seednode | ebb5e48d9a | |
Seednode | 9e458aa0bb | |
Seednode | d568d10e78 |
46
cmd/files.go
46
cmd/files.go
|
@ -24,10 +24,9 @@ import (
|
|||
"seedno.de/seednode/roulette/types"
|
||||
)
|
||||
|
||||
type regexes struct {
|
||||
alphanumeric *regexp.Regexp
|
||||
filename *regexp.Regexp
|
||||
}
|
||||
var (
|
||||
filename = regexp.MustCompile(`(.+?)([0-9]*)(\..+)`)
|
||||
)
|
||||
|
||||
type scanStats struct {
|
||||
filesMatched chan int
|
||||
|
@ -79,14 +78,14 @@ func kill(path string, index *fileIndex) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func newFile(list []string, sortOrder string, regexes *regexes, formats types.Types) (string, error) {
|
||||
func newFile(list []string, sortOrder string, formats types.Types) (string, error) {
|
||||
path, err := pickFile(list)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
if sortOrder == "asc" || sortOrder == "desc" {
|
||||
splitPath, err := split(path, regexes)
|
||||
splitPath, err := split(path)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
@ -125,8 +124,8 @@ func newFile(list []string, sortOrder string, regexes *regexes, formats types.Ty
|
|||
return path, nil
|
||||
}
|
||||
|
||||
func nextFile(filePath, sortOrder string, regexes *regexes, formats types.Types) (string, error) {
|
||||
splitPath, err := split(filePath, regexes)
|
||||
func nextFile(filePath, sortOrder string, formats types.Types) (string, error) {
|
||||
splitPath, err := split(filePath)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
@ -241,10 +240,6 @@ 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() {
|
||||
|
@ -265,11 +260,13 @@ func walkPath(path string, fileChannel chan<- string, wg1 *sync.WaitGroup, stats
|
|||
var skipDir = false
|
||||
|
||||
for _, node := range nodes {
|
||||
if Ignore && !node.IsDir() && node.Name() == IgnoreFile {
|
||||
if !node.IsDir() {
|
||||
files++
|
||||
|
||||
if Ignore && node.Name() == IgnoreFile {
|
||||
skipDir = true
|
||||
}
|
||||
|
||||
files++
|
||||
}
|
||||
}
|
||||
|
||||
var skipFiles = false
|
||||
|
@ -297,18 +294,19 @@ 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)
|
||||
}()
|
||||
|
||||
case !node.IsDir() && !skipFiles:
|
||||
path, err := normalizePath(fullPath)
|
||||
if err != nil {
|
||||
|
||||
switch {
|
||||
case err != nil:
|
||||
errorChannel <- err
|
||||
|
||||
stats.filesSkipped <- 1
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
if formats.Validate(path) || Fallback {
|
||||
case formats.Validate(path) || Fallback:
|
||||
fileChannel <- path
|
||||
|
||||
stats.filesMatched <- 1
|
||||
|
@ -421,6 +419,8 @@ 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)
|
||||
}
|
||||
|
|
|
@ -6,6 +6,7 @@ package cmd
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"math"
|
||||
"os"
|
||||
"regexp"
|
||||
|
@ -17,7 +18,7 @@ import (
|
|||
|
||||
const (
|
||||
AllowedCharacters string = `^[A-z0-9.\-_]+$`
|
||||
ReleaseVersion string = "5.0.2"
|
||||
ReleaseVersion string = "5.0.4"
|
||||
)
|
||||
|
||||
var (
|
||||
|
@ -167,8 +168,6 @@ func init() {
|
|||
|
||||
rootCmd.Flags().SetInterspersed(true)
|
||||
|
||||
rootCmd.MarkFlagsMutuallyExclusive("debug", "exit-on-error")
|
||||
|
||||
rootCmd.MarkFlagsOneRequired(RequiredArgs...)
|
||||
|
||||
rootCmd.SetHelpCommand(&cobra.Command{
|
||||
|
@ -180,4 +179,6 @@ func init() {
|
|||
rootCmd.SilenceErrors = true
|
||||
|
||||
rootCmd.Version = ReleaseVersion
|
||||
|
||||
log.SetFlags(0)
|
||||
}
|
||||
|
|
14
cmd/sort.go
14
cmd/sort.go
|
@ -38,8 +38,8 @@ func (splitPath *splitPath) decrement() string {
|
|||
return fmt.Sprintf("%0*d", len(splitPath.number), asInt-1)
|
||||
}
|
||||
|
||||
func split(path string, regexes *regexes) (*splitPath, error) {
|
||||
split := regexes.filename.FindAllStringSubmatch(path, -1)
|
||||
func split(path string) (*splitPath, error) {
|
||||
split := filename.FindAllStringSubmatch(path, -1)
|
||||
|
||||
if len(split) < 1 || len(split[0]) < 3 {
|
||||
return &splitPath{}, nil
|
||||
|
@ -54,8 +54,8 @@ func split(path string, regexes *regexes) (*splitPath, error) {
|
|||
return p, nil
|
||||
}
|
||||
|
||||
func getRange(path string, regexes *regexes, index *fileIndex) (string, string, error) {
|
||||
splitPath, err := split(path, regexes)
|
||||
func getRange(path string, index *fileIndex) (string, string, error) {
|
||||
splitPath, err := split(path)
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
|
@ -70,7 +70,7 @@ func getRange(path string, regexes *regexes, index *fileIndex) (string, string,
|
|||
|
||||
Loop:
|
||||
for _, val := range list {
|
||||
splitVal, err := split(val, regexes)
|
||||
splitVal, err := split(val)
|
||||
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, regexes *regexes, formats types.Types) (string, error) {
|
||||
split, err := split(path, regexes)
|
||||
func paginateSorted(path, first, last, queryParams string, formats types.Types) (string, error) {
|
||||
split, err := split(path)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
|
|
@ -21,7 +21,7 @@ func sortOrder(r *http.Request) string {
|
|||
return ""
|
||||
}
|
||||
|
||||
func splitQueryParams(query string, regexes *regexes) []string {
|
||||
func splitQueryParams(query string) []string {
|
||||
results := []string{}
|
||||
|
||||
if query == "" {
|
||||
|
|
58
cmd/web.go
58
cmd/web.go
|
@ -6,16 +6,13 @@ package cmd
|
|||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"net"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"runtime"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
@ -171,7 +168,7 @@ func serveStaticFile(paths []string, index *fileIndex, errorChannel chan<- error
|
|||
}
|
||||
}
|
||||
|
||||
func serveRoot(paths []string, regexes *regexes, index *fileIndex, formats types.Types, errorChannel chan<- error) httprouter.Handle {
|
||||
func serveRoot(paths []string, 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 {
|
||||
|
@ -185,8 +182,8 @@ func serveRoot(paths []string, regexes *regexes, index *fileIndex, formats types
|
|||
strippedRefererUri := strings.TrimPrefix(refererUri, Prefix+mediaPrefix)
|
||||
|
||||
filters := &filters{
|
||||
included: splitQueryParams(r.URL.Query().Get("include"), regexes),
|
||||
excluded: splitQueryParams(r.URL.Query().Get("exclude"), regexes),
|
||||
included: splitQueryParams(r.URL.Query().Get("include")),
|
||||
excluded: splitQueryParams(r.URL.Query().Get("exclude")),
|
||||
}
|
||||
|
||||
sortOrder := sortOrder(r)
|
||||
|
@ -196,7 +193,7 @@ func serveRoot(paths []string, regexes *regexes, index *fileIndex, formats types
|
|||
var path string
|
||||
|
||||
if refererUri != "" {
|
||||
path, err = nextFile(strippedRefererUri, sortOrder, regexes, formats)
|
||||
path, err = nextFile(strippedRefererUri, sortOrder, formats)
|
||||
if err != nil {
|
||||
errorChannel <- err
|
||||
|
||||
|
@ -220,7 +217,7 @@ func serveRoot(paths []string, regexes *regexes, index *fileIndex, formats types
|
|||
break loop
|
||||
}
|
||||
|
||||
path, err = newFile(list, sortOrder, regexes, formats)
|
||||
path, err = newFile(list, sortOrder, formats)
|
||||
switch {
|
||||
case path == "":
|
||||
noFiles(w, r)
|
||||
|
@ -251,13 +248,13 @@ func serveRoot(paths []string, regexes *regexes, index *fileIndex, formats types
|
|||
}
|
||||
}
|
||||
|
||||
func serveMedia(paths []string, regexes *regexes, index *fileIndex, formats types.Types, errorChannel chan<- error) httprouter.Handle {
|
||||
func serveMedia(paths []string, 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"), regexes),
|
||||
excluded: splitQueryParams(r.URL.Query().Get("exclude"), regexes),
|
||||
included: splitQueryParams(r.URL.Query().Get("include")),
|
||||
excluded: splitQueryParams(r.URL.Query().Get("exclude")),
|
||||
}
|
||||
|
||||
sortOrder := sortOrder(r)
|
||||
|
@ -347,7 +344,7 @@ func serveMedia(paths []string, regexes *regexes, index *fileIndex, formats type
|
|||
var first, last string
|
||||
|
||||
if Index && sortOrder != "" {
|
||||
first, last, err = getRange(path, regexes, index)
|
||||
first, last, err = getRange(path, index)
|
||||
if err != nil {
|
||||
errorChannel <- err
|
||||
|
||||
|
@ -358,7 +355,7 @@ func serveMedia(paths []string, regexes *regexes, index *fileIndex, formats type
|
|||
}
|
||||
|
||||
if Index && !DisableButtons && sortOrder != "" {
|
||||
paginate, err := paginateSorted(path, first, last, queryParams, regexes, formats)
|
||||
paginate, err := paginateSorted(path, first, last, queryParams, formats)
|
||||
if err != nil {
|
||||
errorChannel <- err
|
||||
|
||||
|
@ -478,8 +475,6 @@ func redirectRoot() httprouter.Handle {
|
|||
}
|
||||
|
||||
func ServePage(args []string) error {
|
||||
log.SetFlags(0)
|
||||
|
||||
timeZone := os.Getenv("TZ")
|
||||
if timeZone != "" {
|
||||
var err error
|
||||
|
@ -541,11 +536,6 @@ func ServePage(args []string) error {
|
|||
return ErrNoMediaFound
|
||||
}
|
||||
|
||||
regexes := ®exes{
|
||||
filename: regexp.MustCompile(`(.+?)([0-9]*)(\..+)`),
|
||||
alphanumeric: regexp.MustCompile(`^[A-z0-9]*$`),
|
||||
}
|
||||
|
||||
if !strings.HasSuffix(Prefix, "/") {
|
||||
Prefix = Prefix + "/"
|
||||
}
|
||||
|
@ -573,30 +563,20 @@ func ServePage(args []string) error {
|
|||
|
||||
go func() {
|
||||
for err := range errorChannel {
|
||||
prefix := "ERROR"
|
||||
|
||||
switch {
|
||||
case errors.Is(err, os.ErrNotExist) && Debug:
|
||||
prefix = "DEBUG"
|
||||
case errors.Is(err, os.ErrNotExist):
|
||||
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):
|
||||
continue
|
||||
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())
|
||||
default:
|
||||
fmt.Printf("%s | ERROR: %v\n", time.Now().Format(logDate), err)
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
registerHandler(mux, Prefix, serveRoot(paths, regexes, index, formats, errorChannel))
|
||||
registerHandler(mux, Prefix, serveRoot(paths, index, formats, errorChannel))
|
||||
|
||||
Prefix = strings.TrimSuffix(Prefix, "/")
|
||||
|
||||
|
@ -608,7 +588,7 @@ func ServePage(args []string) error {
|
|||
|
||||
registerHandler(mux, Prefix+"/favicon.ico", serveFavicons(errorChannel))
|
||||
|
||||
registerHandler(mux, Prefix+mediaPrefix+"/*media", serveMedia(paths, regexes, index, formats, errorChannel))
|
||||
registerHandler(mux, Prefix+mediaPrefix+"/*media", serveMedia(paths, index, formats, errorChannel))
|
||||
|
||||
registerHandler(mux, Prefix+sourcePrefix+"/*static", serveStaticFile(paths, index, errorChannel))
|
||||
|
||||
|
|
Loading…
Reference in New Issue