Compare commits
No commits in common. "173e1528be116f7f8d9520f27eb732bda4f35fa5" and "16d1428a52f9ed51d7058b24a0fc20a98641f8f4" have entirely different histories.
173e1528be
...
16d1428a52
158
cmd/files.go
158
cmd/files.go
|
@ -13,6 +13,7 @@ import (
|
||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"runtime"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
@ -34,6 +35,11 @@ type regexes struct {
|
||||||
filename *regexp.Regexp
|
filename *regexp.Regexp
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type concurrency struct {
|
||||||
|
directoryScans chan int
|
||||||
|
fileScans chan int
|
||||||
|
}
|
||||||
|
|
||||||
type scanStats struct {
|
type scanStats struct {
|
||||||
filesMatched int
|
filesMatched int
|
||||||
filesSkipped int
|
filesSkipped int
|
||||||
|
@ -41,13 +47,6 @@ type scanStats struct {
|
||||||
directoriesSkipped int
|
directoriesSkipped int
|
||||||
}
|
}
|
||||||
|
|
||||||
type scanStatsChannels struct {
|
|
||||||
filesMatched chan int
|
|
||||||
filesSkipped chan int
|
|
||||||
directoriesMatched chan int
|
|
||||||
directoriesSkipped chan int
|
|
||||||
}
|
|
||||||
|
|
||||||
type splitPath struct {
|
type splitPath struct {
|
||||||
base string
|
base string
|
||||||
number int
|
number int
|
||||||
|
@ -93,30 +92,16 @@ func kill(path string, cache *fileCache) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func split(path string, regexes *regexes) (*splitPath, error) {
|
func preparePath(path string) string {
|
||||||
p := splitPath{}
|
if runtime.GOOS == "windows" {
|
||||||
var err error
|
return fmt.Sprintf("%s/%s", mediaPrefix, filepath.ToSlash(path))
|
||||||
|
|
||||||
split := regexes.filename.FindAllStringSubmatch(path, -1)
|
|
||||||
|
|
||||||
if len(split) < 1 || len(split[0]) < 3 {
|
|
||||||
return &splitPath{}, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
p.base = split[0][1]
|
return mediaPrefix + path
|
||||||
|
|
||||||
p.number, err = strconv.Atoi(split[0][2])
|
|
||||||
if err != nil {
|
|
||||||
return &splitPath{}, err
|
|
||||||
}
|
|
||||||
|
|
||||||
p.extension = split[0][3]
|
|
||||||
|
|
||||||
return &p, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func newFile(list []string, sortOrder string, regexes *regexes, formats *types.Types) (string, error) {
|
func newFile(paths []string, filters *filters, sortOrder string, regexes *regexes, cache *fileCache, formats *types.Types) (string, error) {
|
||||||
path, err := pickFile(list)
|
path, err := pickFile(paths, filters, sortOrder, cache, formats)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
@ -182,6 +167,28 @@ func nextFile(path, sortOrder string, regexes *regexes, formats *types.Types) (s
|
||||||
return fileName, err
|
return fileName, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func split(path string, regexes *regexes) (*splitPath, error) {
|
||||||
|
p := splitPath{}
|
||||||
|
var err error
|
||||||
|
|
||||||
|
split := regexes.filename.FindAllStringSubmatch(path, -1)
|
||||||
|
|
||||||
|
if len(split) < 1 || len(split[0]) < 3 {
|
||||||
|
return &splitPath{}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
p.base = split[0][1]
|
||||||
|
|
||||||
|
p.number, err = strconv.Atoi(split[0][2])
|
||||||
|
if err != nil {
|
||||||
|
return &splitPath{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
p.extension = split[0][3]
|
||||||
|
|
||||||
|
return &p, nil
|
||||||
|
}
|
||||||
|
|
||||||
func tryExtensions(splitPath *splitPath, formats *types.Types) (string, error) {
|
func tryExtensions(splitPath *splitPath, formats *types.Types) (string, error) {
|
||||||
var fileName string
|
var fileName string
|
||||||
|
|
||||||
|
@ -288,42 +295,49 @@ func pathCount(path string) (int, int, error) {
|
||||||
return files, directories, nil
|
return files, directories, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func walkPath(path string, fileChannel chan<- string, fileScans chan int, stats *scanStatsChannels, formats *types.Types) error {
|
func scanPath(path string, fileChannel chan<- string, statChannel chan<- *scanStats, errorChannel chan<- error, concurrency *concurrency, formats *types.Types) {
|
||||||
var wg sync.WaitGroup
|
var wg sync.WaitGroup
|
||||||
|
|
||||||
errorChannel := make(chan error)
|
stats := &scanStats{
|
||||||
done := make(chan bool, 1)
|
filesMatched: 0,
|
||||||
|
filesSkipped: 0,
|
||||||
|
directoriesMatched: 0,
|
||||||
|
directoriesSkipped: 0,
|
||||||
|
}
|
||||||
|
|
||||||
|
err := filepath.WalkDir(path, func(p string, info os.DirEntry, err error) error {
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
filepath.WalkDir(path, func(p string, info os.DirEntry, err error) error {
|
|
||||||
switch {
|
switch {
|
||||||
case !Recursive && info.IsDir() && p != path:
|
case !Recursive && info.IsDir() && p != path:
|
||||||
return filepath.SkipDir
|
return filepath.SkipDir
|
||||||
case !info.IsDir():
|
case !info.IsDir():
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
fileScans <- 1
|
concurrency.fileScans <- 1
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
defer func() {
|
defer func() {
|
||||||
|
<-concurrency.fileScans
|
||||||
|
|
||||||
wg.Done()
|
wg.Done()
|
||||||
<-fileScans
|
|
||||||
}()
|
}()
|
||||||
|
|
||||||
path, err := normalizePath(p)
|
path, err := normalizePath(p)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
errorChannel <- err
|
errorChannel <- err
|
||||||
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if !formats.Validate(path) {
|
if !formats.Validate(path) {
|
||||||
stats.filesSkipped <- 1
|
stats.filesSkipped = stats.filesSkipped + 1
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
fileChannel <- path
|
fileChannel <- path
|
||||||
|
|
||||||
stats.filesMatched <- 1
|
stats.filesMatched = stats.filesMatched + 1
|
||||||
}()
|
}()
|
||||||
case info.IsDir():
|
case info.IsDir():
|
||||||
files, directories, err := pathCount(p)
|
files, directories, err := pathCount(p)
|
||||||
|
@ -333,43 +347,33 @@ func walkPath(path string, fileChannel chan<- string, fileScans chan int, stats
|
||||||
|
|
||||||
if files > 0 && (files < int(MinimumFileCount)) || (files > int(MaximumFileCount)) {
|
if files > 0 && (files < int(MinimumFileCount)) || (files > int(MaximumFileCount)) {
|
||||||
// This count will not otherwise include the parent directory itself, so increment by one
|
// This count will not otherwise include the parent directory itself, so increment by one
|
||||||
stats.directoriesSkipped <- directories + 1
|
stats.directoriesSkipped = stats.directoriesSkipped + directories + 1
|
||||||
stats.filesSkipped <- files
|
stats.filesSkipped = stats.filesSkipped + files
|
||||||
|
|
||||||
return filepath.SkipDir
|
return filepath.SkipDir
|
||||||
}
|
}
|
||||||
|
|
||||||
stats.directoriesMatched <- 1
|
stats.directoriesMatched = stats.directoriesMatched + 1
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return err
|
||||||
})
|
})
|
||||||
|
|
||||||
go func() {
|
wg.Wait()
|
||||||
wg.Wait()
|
|
||||||
done <- true
|
|
||||||
}()
|
|
||||||
|
|
||||||
Poll:
|
statChannel <- stats
|
||||||
for {
|
|
||||||
select {
|
if err != nil {
|
||||||
case e := <-errorChannel:
|
errorChannel <- err
|
||||||
return e
|
|
||||||
case <-done:
|
|
||||||
break Poll
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func scanPaths(paths []string, sort string, cache *fileCache, formats *types.Types) ([]string, error) {
|
func scanPaths(paths []string, sort string, cache *fileCache, formats *types.Types) ([]string, error) {
|
||||||
var list []string
|
var list []string
|
||||||
|
|
||||||
fileChannel := make(chan string)
|
fileChannel := make(chan string)
|
||||||
|
statChannel := make(chan *scanStats)
|
||||||
errorChannel := make(chan error)
|
errorChannel := make(chan error)
|
||||||
directoryScans := make(chan int, maxDirectoryScans)
|
|
||||||
fileScans := make(chan int, maxFileScans)
|
|
||||||
done := make(chan bool, 1)
|
done := make(chan bool, 1)
|
||||||
|
|
||||||
stats := &scanStats{
|
stats := &scanStats{
|
||||||
|
@ -379,11 +383,9 @@ func scanPaths(paths []string, sort string, cache *fileCache, formats *types.Typ
|
||||||
directoriesSkipped: 0,
|
directoriesSkipped: 0,
|
||||||
}
|
}
|
||||||
|
|
||||||
statsChannels := &scanStatsChannels{
|
concurrency := &concurrency{
|
||||||
filesMatched: make(chan int),
|
directoryScans: make(chan int, maxDirectoryScans),
|
||||||
filesSkipped: make(chan int),
|
fileScans: make(chan int, maxFileScans),
|
||||||
directoriesMatched: make(chan int),
|
|
||||||
directoriesSkipped: make(chan int),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var wg sync.WaitGroup
|
var wg sync.WaitGroup
|
||||||
|
@ -392,20 +394,16 @@ func scanPaths(paths []string, sort string, cache *fileCache, formats *types.Typ
|
||||||
|
|
||||||
for i := 0; i < len(paths); i++ {
|
for i := 0; i < len(paths); i++ {
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
directoryScans <- 1
|
concurrency.directoryScans <- 1
|
||||||
|
|
||||||
go func(i int) {
|
go func(i int) {
|
||||||
defer func() {
|
defer func() {
|
||||||
|
<-concurrency.directoryScans
|
||||||
|
|
||||||
wg.Done()
|
wg.Done()
|
||||||
<-directoryScans
|
|
||||||
}()
|
}()
|
||||||
|
|
||||||
err := walkPath(paths[i], fileChannel, fileScans, statsChannels, formats)
|
scanPath(paths[i], fileChannel, statChannel, errorChannel, concurrency, formats)
|
||||||
if err != nil {
|
|
||||||
errorChannel <- err
|
|
||||||
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}(i)
|
}(i)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -419,14 +417,11 @@ Poll:
|
||||||
select {
|
select {
|
||||||
case p := <-fileChannel:
|
case p := <-fileChannel:
|
||||||
list = append(list, p)
|
list = append(list, p)
|
||||||
case s := <-statsChannels.filesMatched:
|
case s := <-statChannel:
|
||||||
stats.filesMatched = stats.filesMatched + s
|
stats.filesMatched = stats.filesMatched + s.filesMatched
|
||||||
case s := <-statsChannels.filesSkipped:
|
stats.filesSkipped = stats.filesSkipped + s.filesSkipped
|
||||||
stats.filesSkipped = stats.filesSkipped + s
|
stats.directoriesMatched = stats.directoriesMatched + s.directoriesMatched
|
||||||
case s := <-statsChannels.directoriesMatched:
|
stats.directoriesSkipped = stats.directoriesSkipped + s.directoriesSkipped
|
||||||
stats.directoriesMatched = stats.directoriesMatched + s
|
|
||||||
case s := <-statsChannels.directoriesSkipped:
|
|
||||||
stats.directoriesSkipped = stats.directoriesSkipped + s
|
|
||||||
case e := <-errorChannel:
|
case e := <-errorChannel:
|
||||||
return []string{}, e
|
return []string{}, e
|
||||||
case <-done:
|
case <-done:
|
||||||
|
@ -492,7 +487,12 @@ func fileList(paths []string, filters *filters, sort string, cache *fileCache, f
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func pickFile(list []string) (string, error) {
|
func pickFile(args []string, filters *filters, sort string, cache *fileCache, formats *types.Types) (string, error) {
|
||||||
|
list, err := fileList(args, filters, sort, cache, formats)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
fileCount := len(list)
|
fileCount := len(list)
|
||||||
|
|
||||||
if fileCount < 1 {
|
if fileCount < 1 {
|
||||||
|
|
|
@ -11,7 +11,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
ReleaseVersion string = "0.91.0"
|
ReleaseVersion string = "0.90.3"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
|
47
cmd/web.go
47
cmd/web.go
|
@ -19,7 +19,6 @@ import (
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"syscall"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/julienschmidt/httprouter"
|
"github.com/julienschmidt/httprouter"
|
||||||
|
@ -41,14 +40,6 @@ const (
|
||||||
timeout time.Duration = 10 * time.Second
|
timeout time.Duration = 10 * time.Second
|
||||||
)
|
)
|
||||||
|
|
||||||
func preparePath(path string) string {
|
|
||||||
if runtime.GOOS == "windows" {
|
|
||||||
return fmt.Sprintf("%s/%s", mediaPrefix, filepath.ToSlash(path))
|
|
||||||
}
|
|
||||||
|
|
||||||
return mediaPrefix + path
|
|
||||||
}
|
|
||||||
|
|
||||||
func serveStaticFile(paths []string, cache *fileCache, errorChannel chan<- error) httprouter.Handle {
|
func serveStaticFile(paths []string, cache *fileCache, 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) {
|
||||||
prefix := Prefix + sourcePrefix
|
prefix := Prefix + sourcePrefix
|
||||||
|
@ -105,19 +96,7 @@ func serveStaticFile(paths []string, cache *fileCache, errorChannel chan<- error
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var status string
|
written, _ := w.Write(buf)
|
||||||
|
|
||||||
written, err := w.Write(buf)
|
|
||||||
switch {
|
|
||||||
case errors.Is(err, syscall.EPIPE):
|
|
||||||
status = " (incomplete)"
|
|
||||||
case err != nil:
|
|
||||||
errorChannel <- err
|
|
||||||
|
|
||||||
serverError(w, r, nil)
|
|
||||||
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
refererUri, err := stripQueryParams(refererToUri(r.Referer()))
|
refererUri, err := stripQueryParams(refererToUri(r.Referer()))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -140,13 +119,12 @@ func serveStaticFile(paths []string, cache *fileCache, errorChannel chan<- error
|
||||||
}
|
}
|
||||||
|
|
||||||
if Verbose {
|
if Verbose {
|
||||||
fmt.Printf("%s | Serve: %s (%s) to %s in %s%s\n",
|
fmt.Printf("%s | Serve: %s (%s) to %s in %s\n",
|
||||||
startTime.Format(logDate),
|
startTime.Format(logDate),
|
||||||
filePath,
|
filePath,
|
||||||
humanReadableSize(written),
|
humanReadableSize(written),
|
||||||
realIP(r),
|
realIP(r),
|
||||||
time.Since(startTime).Round(time.Microsecond),
|
time.Since(startTime).Round(time.Microsecond),
|
||||||
status,
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -174,10 +152,10 @@ func serveRoot(paths []string, regexes *regexes, cache *fileCache, formats *type
|
||||||
|
|
||||||
_, refreshInterval := refreshInterval(r)
|
_, refreshInterval := refreshInterval(r)
|
||||||
|
|
||||||
var path string
|
var filePath string
|
||||||
|
|
||||||
if refererUri != "" {
|
if refererUri != "" {
|
||||||
path, err = nextFile(strippedRefererUri, sortOrder, regexes, formats)
|
filePath, err = nextFile(strippedRefererUri, sortOrder, regexes, formats)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
errorChannel <- err
|
errorChannel <- err
|
||||||
|
|
||||||
|
@ -187,15 +165,6 @@ func serveRoot(paths []string, regexes *regexes, cache *fileCache, formats *type
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
list, err := fileList(paths, filters, sortOrder, cache, formats)
|
|
||||||
if err != nil {
|
|
||||||
errorChannel <- err
|
|
||||||
|
|
||||||
serverError(w, r, nil)
|
|
||||||
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
loop:
|
loop:
|
||||||
for timeout := time.After(timeout); ; {
|
for timeout := time.After(timeout); ; {
|
||||||
select {
|
select {
|
||||||
|
@ -204,14 +173,14 @@ func serveRoot(paths []string, regexes *regexes, cache *fileCache, formats *type
|
||||||
default:
|
default:
|
||||||
}
|
}
|
||||||
|
|
||||||
if path != "" {
|
if filePath != "" {
|
||||||
break loop
|
break loop
|
||||||
}
|
}
|
||||||
|
|
||||||
path, err = newFile(list, sortOrder, regexes, formats)
|
filePath, err = newFile(paths, filters, sortOrder, regexes, cache, formats)
|
||||||
switch {
|
switch {
|
||||||
case err != nil && err == ErrNoMediaFound:
|
case err != nil && err == ErrNoMediaFound:
|
||||||
notFound(w, r, path)
|
notFound(w, r, filePath)
|
||||||
|
|
||||||
return
|
return
|
||||||
case err != nil:
|
case err != nil:
|
||||||
|
@ -228,7 +197,7 @@ func serveRoot(paths []string, regexes *regexes, cache *fileCache, formats *type
|
||||||
newUrl := fmt.Sprintf("http://%s%s%s%s",
|
newUrl := fmt.Sprintf("http://%s%s%s%s",
|
||||||
r.Host,
|
r.Host,
|
||||||
Prefix,
|
Prefix,
|
||||||
preparePath(path),
|
preparePath(filePath),
|
||||||
queryParams,
|
queryParams,
|
||||||
)
|
)
|
||||||
http.Redirect(w, r, newUrl, RedirectStatusCode)
|
http.Redirect(w, r, newUrl, RedirectStatusCode)
|
||||||
|
|
BIN
default.pgo
BIN
default.pgo
Binary file not shown.
Loading…
Reference in New Issue