Rewrite filewalk functions, add (incomplete) to logging when -v|--verbose is passed and a broken pipe error is received
This commit is contained in:
parent
823bc2095d
commit
173e1528be
60
cmd/files.go
60
cmd/files.go
|
@ -13,7 +13,6 @@ import (
|
||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"runtime"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
@ -94,16 +93,30 @@ func kill(path string, cache *fileCache) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func preparePath(path string) string {
|
func split(path string, regexes *regexes) (*splitPath, error) {
|
||||||
if runtime.GOOS == "windows" {
|
p := splitPath{}
|
||||||
return fmt.Sprintf("%s/%s", mediaPrefix, filepath.ToSlash(path))
|
var err error
|
||||||
|
|
||||||
|
split := regexes.filename.FindAllStringSubmatch(path, -1)
|
||||||
|
|
||||||
|
if len(split) < 1 || len(split[0]) < 3 {
|
||||||
|
return &splitPath{}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return mediaPrefix + path
|
p.base = split[0][1]
|
||||||
|
|
||||||
|
p.number, err = strconv.Atoi(split[0][2])
|
||||||
|
if err != nil {
|
||||||
|
return &splitPath{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func newFile(paths []string, filters *filters, sortOrder string, regexes *regexes, cache *fileCache, formats *types.Types) (string, error) {
|
p.extension = split[0][3]
|
||||||
path, err := pickFile(paths, filters, sortOrder, cache, formats)
|
|
||||||
|
return &p, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func newFile(list []string, sortOrder string, regexes *regexes, formats *types.Types) (string, error) {
|
||||||
|
path, err := pickFile(list)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
@ -169,28 +182,6 @@ 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
|
||||||
|
|
||||||
|
@ -297,7 +288,7 @@ func pathCount(path string) (int, int, error) {
|
||||||
return files, directories, nil
|
return files, directories, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func scanPath(path string, fileChannel chan<- string, fileScans chan int, stats *scanStatsChannels, formats *types.Types) error {
|
func walkPath(path string, fileChannel chan<- string, fileScans chan int, stats *scanStatsChannels, formats *types.Types) error {
|
||||||
var wg sync.WaitGroup
|
var wg sync.WaitGroup
|
||||||
|
|
||||||
errorChannel := make(chan error)
|
errorChannel := make(chan error)
|
||||||
|
@ -409,7 +400,7 @@ func scanPaths(paths []string, sort string, cache *fileCache, formats *types.Typ
|
||||||
<-directoryScans
|
<-directoryScans
|
||||||
}()
|
}()
|
||||||
|
|
||||||
err := scanPath(paths[i], fileChannel, fileScans, statsChannels, formats)
|
err := walkPath(paths[i], fileChannel, fileScans, statsChannels, formats)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
errorChannel <- err
|
errorChannel <- err
|
||||||
|
|
||||||
|
@ -501,12 +492,7 @@ func fileList(paths []string, filters *filters, sort string, cache *fileCache, f
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func pickFile(args []string, filters *filters, sort string, cache *fileCache, formats *types.Types) (string, error) {
|
func pickFile(list []string) (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.90.4"
|
ReleaseVersion string = "0.91.0"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
|
47
cmd/web.go
47
cmd/web.go
|
@ -19,6 +19,7 @@ import (
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/julienschmidt/httprouter"
|
"github.com/julienschmidt/httprouter"
|
||||||
|
@ -40,6 +41,14 @@ 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
|
||||||
|
@ -96,7 +105,19 @@ func serveStaticFile(paths []string, cache *fileCache, errorChannel chan<- error
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
written, _ := w.Write(buf)
|
var status string
|
||||||
|
|
||||||
|
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 {
|
||||||
|
@ -119,12 +140,13 @@ func serveStaticFile(paths []string, cache *fileCache, errorChannel chan<- error
|
||||||
}
|
}
|
||||||
|
|
||||||
if Verbose {
|
if Verbose {
|
||||||
fmt.Printf("%s | Serve: %s (%s) to %s in %s\n",
|
fmt.Printf("%s | Serve: %s (%s) to %s in %s%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,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -152,10 +174,10 @@ func serveRoot(paths []string, regexes *regexes, cache *fileCache, formats *type
|
||||||
|
|
||||||
_, refreshInterval := refreshInterval(r)
|
_, refreshInterval := refreshInterval(r)
|
||||||
|
|
||||||
var filePath string
|
var path string
|
||||||
|
|
||||||
if refererUri != "" {
|
if refererUri != "" {
|
||||||
filePath, err = nextFile(strippedRefererUri, sortOrder, regexes, formats)
|
path, err = nextFile(strippedRefererUri, sortOrder, regexes, formats)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
errorChannel <- err
|
errorChannel <- err
|
||||||
|
|
||||||
|
@ -165,6 +187,15 @@ 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 {
|
||||||
|
@ -173,14 +204,14 @@ func serveRoot(paths []string, regexes *regexes, cache *fileCache, formats *type
|
||||||
default:
|
default:
|
||||||
}
|
}
|
||||||
|
|
||||||
if filePath != "" {
|
if path != "" {
|
||||||
break loop
|
break loop
|
||||||
}
|
}
|
||||||
|
|
||||||
filePath, err = newFile(paths, filters, sortOrder, regexes, cache, formats)
|
path, err = newFile(list, sortOrder, regexes, formats)
|
||||||
switch {
|
switch {
|
||||||
case err != nil && err == ErrNoMediaFound:
|
case err != nil && err == ErrNoMediaFound:
|
||||||
notFound(w, r, filePath)
|
notFound(w, r, path)
|
||||||
|
|
||||||
return
|
return
|
||||||
case err != nil:
|
case err != nil:
|
||||||
|
@ -197,7 +228,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(filePath),
|
preparePath(path),
|
||||||
queryParams,
|
queryParams,
|
||||||
)
|
)
|
||||||
http.Redirect(w, r, newUrl, RedirectStatusCode)
|
http.Redirect(w, r, newUrl, RedirectStatusCode)
|
||||||
|
|
Loading…
Reference in New Issue