2022-09-08 15:57:59 +00:00
|
|
|
/*
|
2023-01-18 17:19:29 +00:00
|
|
|
Copyright © 2023 Seednode <seednode@seedno.de>
|
2022-09-08 15:57:59 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2022-09-09 22:21:03 +00:00
|
|
|
"errors"
|
2022-09-17 16:07:37 +00:00
|
|
|
"fmt"
|
2023-09-10 17:16:50 +00:00
|
|
|
"math/big"
|
2023-09-12 02:22:26 +00:00
|
|
|
"regexp"
|
2022-10-28 23:32:03 +00:00
|
|
|
|
2023-09-10 17:16:50 +00:00
|
|
|
"crypto/rand"
|
2022-09-08 15:57:59 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2022-10-23 21:29:58 +00:00
|
|
|
"runtime"
|
2022-09-17 16:07:37 +00:00
|
|
|
"strconv"
|
2022-09-24 22:17:56 +00:00
|
|
|
"strings"
|
2022-10-20 19:22:01 +00:00
|
|
|
"sync"
|
|
|
|
"sync/atomic"
|
2022-09-08 15:57:59 +00:00
|
|
|
"time"
|
2022-09-10 00:00:02 +00:00
|
|
|
|
2023-09-12 21:32:19 +00:00
|
|
|
"seedno.de/seednode/roulette/types"
|
2022-09-08 15:57:59 +00:00
|
|
|
)
|
|
|
|
|
2023-01-27 16:06:10 +00:00
|
|
|
type maxConcurrency int
|
|
|
|
|
|
|
|
const (
|
|
|
|
// avoid hitting default open file descriptor limits (1024)
|
|
|
|
maxDirectoryScans maxConcurrency = 32
|
|
|
|
maxFileScans maxConcurrency = 256
|
|
|
|
)
|
|
|
|
|
2023-09-13 14:26:15 +00:00
|
|
|
type regexes struct {
|
2023-09-12 02:22:26 +00:00
|
|
|
alphanumeric *regexp.Regexp
|
|
|
|
filename *regexp.Regexp
|
|
|
|
}
|
|
|
|
|
2023-09-13 14:26:15 +00:00
|
|
|
type concurrency struct {
|
2023-01-27 16:06:10 +00:00
|
|
|
directoryScans chan int
|
|
|
|
fileScans chan int
|
|
|
|
}
|
|
|
|
|
2023-09-13 14:26:15 +00:00
|
|
|
type files struct {
|
2023-02-18 19:00:08 +00:00
|
|
|
mutex sync.RWMutex
|
2023-01-27 16:06:10 +00:00
|
|
|
list map[string][]string
|
2022-10-20 19:22:01 +00:00
|
|
|
}
|
|
|
|
|
2023-09-13 14:26:15 +00:00
|
|
|
func (f *files) append(directory, path string) {
|
2023-01-27 16:06:10 +00:00
|
|
|
f.mutex.Lock()
|
|
|
|
f.list[directory] = append(f.list[directory], path)
|
|
|
|
f.mutex.Unlock()
|
2023-01-21 16:27:35 +00:00
|
|
|
}
|
|
|
|
|
2023-09-13 14:26:15 +00:00
|
|
|
type scanStats struct {
|
2023-09-12 21:46:58 +00:00
|
|
|
filesMatched atomic.Uint32
|
|
|
|
filesSkipped atomic.Uint32
|
|
|
|
directoriesMatched atomic.Uint32
|
|
|
|
directoriesSkipped atomic.Uint32
|
2023-08-02 18:29:37 +00:00
|
|
|
}
|
|
|
|
|
2023-09-13 14:26:15 +00:00
|
|
|
type splitPath struct {
|
2023-01-27 16:06:10 +00:00
|
|
|
base string
|
|
|
|
number int
|
|
|
|
extension string
|
2023-01-21 04:42:44 +00:00
|
|
|
}
|
|
|
|
|
2023-09-13 14:26:15 +00:00
|
|
|
func (splitPath *splitPath) increment() {
|
|
|
|
splitPath.number = splitPath.number + 1
|
2023-01-21 04:42:44 +00:00
|
|
|
}
|
|
|
|
|
2023-09-13 14:26:15 +00:00
|
|
|
func (splitPath *splitPath) decrement() {
|
|
|
|
splitPath.number = splitPath.number - 1
|
2023-01-25 01:06:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func humanReadableSize(bytes int) string {
|
|
|
|
const unit = 1000
|
|
|
|
|
|
|
|
if bytes < unit {
|
|
|
|
return fmt.Sprintf("%d B", bytes)
|
|
|
|
}
|
|
|
|
|
|
|
|
div, exp := int64(unit), 0
|
|
|
|
|
|
|
|
for n := bytes / unit; n >= unit; n /= unit {
|
|
|
|
div *= unit
|
|
|
|
exp++
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Sprintf("%.1f %cB",
|
|
|
|
float64(bytes)/float64(div), "KMGTPE"[exp])
|
|
|
|
}
|
|
|
|
|
|
|
|
func preparePath(path string) string {
|
|
|
|
if runtime.GOOS == "windows" {
|
2023-09-13 14:26:15 +00:00
|
|
|
return fmt.Sprintf("%s/%s", mediaPrefix, filepath.ToSlash(path))
|
2023-01-25 01:06:15 +00:00
|
|
|
}
|
|
|
|
|
2023-09-13 14:26:15 +00:00
|
|
|
return mediaPrefix + path
|
2023-01-19 18:07:15 +00:00
|
|
|
}
|
|
|
|
|
2023-09-13 14:26:15 +00:00
|
|
|
func appendPath(directory, path string, files *files, stats *scanStats, formats *types.Types, shouldCache bool) error {
|
2023-01-21 16:27:35 +00:00
|
|
|
if shouldCache {
|
2023-09-14 22:37:22 +00:00
|
|
|
format := formats.FileType(path)
|
|
|
|
if format == nil {
|
|
|
|
return nil
|
2022-10-28 22:19:04 +00:00
|
|
|
}
|
|
|
|
|
2023-09-14 22:37:22 +00:00
|
|
|
if !format.Validate(path) {
|
2022-10-28 22:19:04 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-13 14:26:15 +00:00
|
|
|
files.append(directory, path)
|
2022-10-28 22:19:04 +00:00
|
|
|
|
2023-08-13 22:29:28 +00:00
|
|
|
stats.filesMatched.Add(1)
|
2022-11-02 14:17:07 +00:00
|
|
|
|
2022-10-28 22:19:04 +00:00
|
|
|
return nil
|
2022-10-20 19:22:01 +00:00
|
|
|
}
|
|
|
|
|
2023-09-13 14:26:15 +00:00
|
|
|
func appendPaths(path string, files *files, filters *filters, stats *scanStats, formats *types.Types) error {
|
|
|
|
shouldCache := Cache && filters.isEmpty()
|
2023-01-21 16:27:35 +00:00
|
|
|
|
2022-09-24 23:59:10 +00:00
|
|
|
absolutePath, err := filepath.Abs(path)
|
|
|
|
if err != nil {
|
2022-10-20 19:22:01 +00:00
|
|
|
return err
|
2022-09-24 23:59:10 +00:00
|
|
|
}
|
|
|
|
|
2022-10-20 00:27:11 +00:00
|
|
|
directory, filename := filepath.Split(absolutePath)
|
2022-09-25 01:28:34 +00:00
|
|
|
|
2022-10-20 15:38:32 +00:00
|
|
|
filename = strings.ToLower(filename)
|
|
|
|
|
2023-09-13 14:26:15 +00:00
|
|
|
if filters.hasExcludes() {
|
|
|
|
for i := 0; i < len(filters.excluded); i++ {
|
2022-10-20 00:27:11 +00:00
|
|
|
if strings.Contains(
|
2022-10-20 15:38:32 +00:00
|
|
|
filename,
|
2023-09-13 14:26:15 +00:00
|
|
|
filters.excluded[i],
|
2022-10-20 00:27:11 +00:00
|
|
|
) {
|
2023-08-13 22:02:09 +00:00
|
|
|
stats.filesSkipped.Add(1)
|
2022-10-20 16:17:40 +00:00
|
|
|
|
2022-10-20 19:22:01 +00:00
|
|
|
return nil
|
2022-10-20 00:27:11 +00:00
|
|
|
}
|
|
|
|
}
|
2022-10-23 18:08:08 +00:00
|
|
|
}
|
2022-10-20 00:27:11 +00:00
|
|
|
|
2023-09-13 14:26:15 +00:00
|
|
|
if filters.hasIncludes() {
|
|
|
|
for i := 0; i < len(filters.included); i++ {
|
2022-10-20 00:27:11 +00:00
|
|
|
if strings.Contains(
|
2022-10-20 15:38:32 +00:00
|
|
|
filename,
|
2023-09-13 14:26:15 +00:00
|
|
|
filters.included[i],
|
2022-10-20 00:27:11 +00:00
|
|
|
) {
|
2023-09-12 21:32:19 +00:00
|
|
|
err := appendPath(directory, path, files, stats, formats, shouldCache)
|
2022-10-28 22:19:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-10-20 00:27:11 +00:00
|
|
|
|
2022-10-20 19:22:01 +00:00
|
|
|
return nil
|
2022-10-20 00:27:11 +00:00
|
|
|
}
|
|
|
|
}
|
2022-09-24 23:59:10 +00:00
|
|
|
|
2023-08-13 22:02:09 +00:00
|
|
|
stats.filesSkipped.Add(1)
|
2022-10-20 16:17:40 +00:00
|
|
|
|
2022-10-20 19:22:01 +00:00
|
|
|
return nil
|
2022-10-20 15:38:32 +00:00
|
|
|
}
|
2022-10-23 18:08:08 +00:00
|
|
|
|
2023-09-12 21:32:19 +00:00
|
|
|
err = appendPath(directory, path, files, stats, formats, shouldCache)
|
2022-10-28 22:19:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-10-23 18:08:08 +00:00
|
|
|
|
|
|
|
return nil
|
2022-09-24 22:17:56 +00:00
|
|
|
}
|
|
|
|
|
2023-09-14 01:45:28 +00:00
|
|
|
func newFile(paths []string, filters *filters, sortOrder string, regexes *regexes, cache *fileCache, formats *types.Types) (string, error) {
|
|
|
|
path, err := pickFile(paths, filters, sortOrder, cache, formats)
|
2022-11-05 16:00:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", nil
|
|
|
|
}
|
2022-09-18 18:07:46 +00:00
|
|
|
|
2023-09-13 14:26:15 +00:00
|
|
|
splitPath, err := split(path, regexes)
|
2022-09-18 18:07:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2023-09-13 14:26:15 +00:00
|
|
|
splitPath.number = 1
|
2022-10-18 18:42:32 +00:00
|
|
|
|
2022-11-05 16:00:42 +00:00
|
|
|
switch {
|
|
|
|
case sortOrder == "asc":
|
2023-09-13 14:26:15 +00:00
|
|
|
path, err = tryExtensions(splitPath, formats)
|
2022-10-18 18:42:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2022-11-05 16:00:42 +00:00
|
|
|
case sortOrder == "desc":
|
|
|
|
for {
|
2023-09-13 14:26:15 +00:00
|
|
|
splitPath.increment()
|
2022-10-18 18:42:32 +00:00
|
|
|
|
2023-09-13 14:26:15 +00:00
|
|
|
path, err = tryExtensions(splitPath, formats)
|
2022-10-18 21:46:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2022-10-18 18:42:32 +00:00
|
|
|
|
2023-09-13 14:26:15 +00:00
|
|
|
if path == "" {
|
|
|
|
splitPath.decrement()
|
2022-09-18 18:07:46 +00:00
|
|
|
|
2023-09-13 14:26:15 +00:00
|
|
|
path, err = tryExtensions(splitPath, formats)
|
2022-11-05 16:00:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2022-09-17 17:42:25 +00:00
|
|
|
|
2022-11-05 16:00:42 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2022-09-17 16:07:37 +00:00
|
|
|
}
|
|
|
|
|
2023-09-13 14:26:15 +00:00
|
|
|
return path, nil
|
2022-10-18 21:40:13 +00:00
|
|
|
}
|
|
|
|
|
2023-09-13 14:26:15 +00:00
|
|
|
func nextFile(path, sortOrder string, regexes *regexes, formats *types.Types) (string, error) {
|
|
|
|
splitPath, err := split(path, regexes)
|
2022-11-05 16:17:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2022-11-05 16:00:42 +00:00
|
|
|
switch {
|
|
|
|
case sortOrder == "asc":
|
2023-09-13 14:26:15 +00:00
|
|
|
splitPath.increment()
|
2022-11-05 16:00:42 +00:00
|
|
|
case sortOrder == "desc":
|
2023-09-13 14:26:15 +00:00
|
|
|
splitPath.decrement()
|
2022-11-05 16:00:42 +00:00
|
|
|
default:
|
|
|
|
return "", nil
|
|
|
|
}
|
2022-10-18 21:40:13 +00:00
|
|
|
|
2023-09-13 14:26:15 +00:00
|
|
|
fileName, err := tryExtensions(splitPath, formats)
|
2022-09-17 16:07:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2022-10-18 18:42:32 +00:00
|
|
|
return fileName, err
|
|
|
|
}
|
|
|
|
|
2023-09-13 14:26:15 +00:00
|
|
|
func split(path string, regexes *regexes) (*splitPath, error) {
|
|
|
|
p := splitPath{}
|
2023-01-25 01:06:15 +00:00
|
|
|
var err error
|
2022-10-18 21:46:55 +00:00
|
|
|
|
2023-09-13 14:26:15 +00:00
|
|
|
split := regexes.filename.FindAllStringSubmatch(path, -1)
|
2022-10-18 18:42:32 +00:00
|
|
|
|
2023-01-25 01:06:15 +00:00
|
|
|
if len(split) < 1 || len(split[0]) < 3 {
|
2023-09-13 14:26:15 +00:00
|
|
|
return &splitPath{}, nil
|
2023-01-25 01:06:15 +00:00
|
|
|
}
|
2022-09-17 16:07:37 +00:00
|
|
|
|
2023-01-27 16:06:10 +00:00
|
|
|
p.base = split[0][1]
|
2023-01-25 01:06:15 +00:00
|
|
|
|
2023-01-27 16:06:10 +00:00
|
|
|
p.number, err = strconv.Atoi(split[0][2])
|
2023-01-25 01:06:15 +00:00
|
|
|
if err != nil {
|
2023-09-13 14:26:15 +00:00
|
|
|
return &splitPath{}, err
|
2023-01-25 01:06:15 +00:00
|
|
|
}
|
|
|
|
|
2023-01-27 16:06:10 +00:00
|
|
|
p.extension = split[0][3]
|
2023-01-25 01:06:15 +00:00
|
|
|
|
|
|
|
return &p, nil
|
|
|
|
}
|
|
|
|
|
2023-09-13 14:26:15 +00:00
|
|
|
func tryExtensions(splitPath *splitPath, formats *types.Types) (string, error) {
|
2023-01-25 01:06:15 +00:00
|
|
|
var fileName string
|
|
|
|
|
2023-09-13 00:46:27 +00:00
|
|
|
for extension := range formats.Extensions {
|
2023-09-13 14:26:15 +00:00
|
|
|
fileName = fmt.Sprintf("%s%.3d%s", splitPath.base, splitPath.number, extension)
|
2023-01-25 01:06:15 +00:00
|
|
|
|
|
|
|
exists, err := fileExists(fileName)
|
2023-01-24 18:03:26 +00:00
|
|
|
if err != nil {
|
2023-01-25 01:06:15 +00:00
|
|
|
return "", err
|
2023-01-24 18:03:26 +00:00
|
|
|
}
|
2022-10-18 21:46:55 +00:00
|
|
|
|
2023-01-25 01:06:15 +00:00
|
|
|
if exists {
|
|
|
|
return fileName, nil
|
2023-01-24 18:03:26 +00:00
|
|
|
}
|
2022-09-26 17:31:45 +00:00
|
|
|
}
|
|
|
|
|
2023-01-25 01:06:15 +00:00
|
|
|
return "", nil
|
|
|
|
}
|
2022-09-26 17:31:45 +00:00
|
|
|
|
2023-01-25 01:06:15 +00:00
|
|
|
func fileExists(path string) (bool, error) {
|
|
|
|
_, err := os.Stat(path)
|
|
|
|
switch {
|
|
|
|
case err == nil:
|
|
|
|
return true, nil
|
|
|
|
case errors.Is(err, os.ErrNotExist):
|
|
|
|
return false, nil
|
|
|
|
default:
|
|
|
|
return false, err
|
|
|
|
}
|
2022-09-17 16:07:37 +00:00
|
|
|
}
|
|
|
|
|
2023-09-13 14:26:15 +00:00
|
|
|
func pathIsValid(path string, paths []string) bool {
|
2022-10-18 18:42:32 +00:00
|
|
|
var matchesPrefix = false
|
2022-10-20 22:12:29 +00:00
|
|
|
|
2022-10-18 18:42:32 +00:00
|
|
|
for i := 0; i < len(paths); i++ {
|
2023-09-13 14:26:15 +00:00
|
|
|
if strings.HasPrefix(path, paths[i]) {
|
2022-10-18 18:42:32 +00:00
|
|
|
matchesPrefix = true
|
|
|
|
}
|
|
|
|
}
|
2022-10-20 22:55:25 +00:00
|
|
|
|
2022-10-25 05:06:57 +00:00
|
|
|
switch {
|
2023-09-12 18:06:45 +00:00
|
|
|
case Verbose && !matchesPrefix:
|
2023-01-24 16:13:09 +00:00
|
|
|
fmt.Printf("%s | Error: Failed to serve file outside specified path(s): %s\n",
|
2023-09-13 14:26:15 +00:00
|
|
|
time.Now().Format(logDate),
|
|
|
|
path,
|
2022-10-20 22:55:25 +00:00
|
|
|
)
|
2022-10-18 18:42:32 +00:00
|
|
|
|
|
|
|
return false
|
2022-10-25 05:06:57 +00:00
|
|
|
case !matchesPrefix:
|
|
|
|
return false
|
|
|
|
default:
|
|
|
|
return true
|
2022-10-18 18:42:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-12 21:32:19 +00:00
|
|
|
func pathHasSupportedFiles(path string, formats *types.Types) (bool, error) {
|
2023-09-11 17:09:08 +00:00
|
|
|
hasRegisteredFiles := make(chan bool, 1)
|
2023-04-11 09:44:18 +00:00
|
|
|
|
|
|
|
err := filepath.WalkDir(path, func(p string, info os.DirEntry, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
switch {
|
2023-09-12 18:06:45 +00:00
|
|
|
case !Recursive && info.IsDir() && p != path:
|
2023-04-11 09:44:18 +00:00
|
|
|
return filepath.SkipDir
|
|
|
|
case !info.IsDir():
|
2023-09-14 22:37:22 +00:00
|
|
|
format := formats.FileType(p)
|
|
|
|
if format == nil {
|
|
|
|
return nil
|
2023-04-11 09:44:18 +00:00
|
|
|
}
|
|
|
|
|
2023-09-14 22:37:22 +00:00
|
|
|
if format.Validate(p) {
|
2023-09-11 17:09:08 +00:00
|
|
|
hasRegisteredFiles <- true
|
2023-09-14 22:37:22 +00:00
|
|
|
|
2023-04-11 09:44:18 +00:00
|
|
|
return filepath.SkipAll
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
2023-09-11 17:09:08 +00:00
|
|
|
case <-hasRegisteredFiles:
|
2023-04-11 09:44:18 +00:00
|
|
|
return true, nil
|
|
|
|
default:
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-12 21:46:58 +00:00
|
|
|
func pathCount(path string) (uint32, uint32, error) {
|
|
|
|
var directories uint32 = 0
|
|
|
|
var files uint32 = 0
|
2023-08-02 18:29:37 +00:00
|
|
|
|
|
|
|
nodes, err := os.ReadDir(path)
|
|
|
|
if err != nil {
|
|
|
|
return 0, 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, node := range nodes {
|
|
|
|
if node.IsDir() {
|
|
|
|
directories++
|
|
|
|
} else {
|
|
|
|
files++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return files, directories, nil
|
|
|
|
}
|
|
|
|
|
2023-09-13 14:26:15 +00:00
|
|
|
func scanPath(path string, files *files, filters *filters, stats *scanStats, concurrency *concurrency, formats *types.Types) error {
|
2022-10-20 19:22:01 +00:00
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
2022-09-08 18:12:50 +00:00
|
|
|
err := filepath.WalkDir(path, func(p string, info os.DirEntry, err error) error {
|
2022-09-24 23:59:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2022-09-08 18:12:50 +00:00
|
|
|
}
|
2022-09-08 20:30:51 +00:00
|
|
|
|
2022-09-24 22:17:56 +00:00
|
|
|
switch {
|
2023-09-12 18:06:45 +00:00
|
|
|
case !Recursive && info.IsDir() && p != path:
|
2022-09-24 23:59:10 +00:00
|
|
|
return filepath.SkipDir
|
2022-09-25 01:54:51 +00:00
|
|
|
case !info.IsDir():
|
2022-10-20 19:22:01 +00:00
|
|
|
wg.Add(1)
|
2023-01-27 16:06:10 +00:00
|
|
|
concurrency.fileScans <- 1
|
2022-10-20 19:22:01 +00:00
|
|
|
|
|
|
|
go func() {
|
|
|
|
defer func() {
|
2023-01-27 16:06:10 +00:00
|
|
|
<-concurrency.fileScans
|
2023-06-03 23:45:32 +00:00
|
|
|
|
2022-10-20 19:22:01 +00:00
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
|
2023-04-11 12:19:06 +00:00
|
|
|
path, err := normalizePath(p)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
|
|
|
|
2023-09-12 21:32:19 +00:00
|
|
|
err = appendPaths(path, files, filters, stats, formats)
|
2022-10-20 19:22:01 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
|
|
|
}()
|
2022-10-20 16:17:40 +00:00
|
|
|
case info.IsDir():
|
2023-08-02 18:29:37 +00:00
|
|
|
files, directories, err := pathCount(p)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
|
|
|
|
2023-09-12 18:06:45 +00:00
|
|
|
if files > 0 && (files < MinimumFileCount) || (files > MaximumFileCount) {
|
2023-08-02 18:29:37 +00:00
|
|
|
// This count will not otherwise include the parent directory itself, so increment by one
|
2023-08-13 22:29:28 +00:00
|
|
|
stats.directoriesSkipped.Add(directories + 1)
|
|
|
|
stats.filesSkipped.Add(files)
|
2023-08-02 18:29:37 +00:00
|
|
|
|
|
|
|
return filepath.SkipDir
|
|
|
|
}
|
|
|
|
|
2023-08-13 22:02:09 +00:00
|
|
|
stats.directoriesMatched.Add(1)
|
2022-09-08 17:12:58 +00:00
|
|
|
}
|
2022-09-24 22:17:56 +00:00
|
|
|
|
2022-09-08 17:12:58 +00:00
|
|
|
return err
|
|
|
|
})
|
2022-10-20 19:22:01 +00:00
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
|
2022-09-08 18:12:50 +00:00
|
|
|
if err != nil {
|
2022-10-20 19:22:01 +00:00
|
|
|
return err
|
2022-09-08 18:12:50 +00:00
|
|
|
}
|
2022-09-08 17:12:58 +00:00
|
|
|
|
2022-10-20 19:22:01 +00:00
|
|
|
return nil
|
2022-09-08 17:12:58 +00:00
|
|
|
}
|
|
|
|
|
2023-09-13 14:26:15 +00:00
|
|
|
func fileList(paths []string, filters *filters, sort string, cache *fileCache, formats *types.Types) ([]string, bool) {
|
|
|
|
if Cache && filters.isEmpty() && !cache.isEmpty() {
|
|
|
|
return cache.List(), true
|
2023-01-25 01:06:15 +00:00
|
|
|
}
|
2022-09-25 01:28:34 +00:00
|
|
|
|
2023-01-25 01:06:15 +00:00
|
|
|
var fileList []string
|
2022-09-25 01:28:34 +00:00
|
|
|
|
2023-09-13 14:26:15 +00:00
|
|
|
files := &files{
|
2023-02-18 19:00:08 +00:00
|
|
|
mutex: sync.RWMutex{},
|
2023-01-27 16:06:10 +00:00
|
|
|
list: make(map[string][]string),
|
2022-09-25 01:28:34 +00:00
|
|
|
}
|
|
|
|
|
2023-09-13 14:26:15 +00:00
|
|
|
stats := &scanStats{
|
2023-09-12 21:46:58 +00:00
|
|
|
filesMatched: atomic.Uint32{},
|
|
|
|
filesSkipped: atomic.Uint32{},
|
|
|
|
directoriesMatched: atomic.Uint32{},
|
|
|
|
directoriesSkipped: atomic.Uint32{},
|
2023-01-25 01:06:15 +00:00
|
|
|
}
|
2022-09-08 15:57:59 +00:00
|
|
|
|
2023-09-13 14:26:15 +00:00
|
|
|
concurrency := &concurrency{
|
2023-01-27 16:06:10 +00:00
|
|
|
directoryScans: make(chan int, maxDirectoryScans),
|
|
|
|
fileScans: make(chan int, maxFileScans),
|
2023-01-25 01:06:15 +00:00
|
|
|
}
|
2022-10-20 16:17:40 +00:00
|
|
|
|
2023-01-25 01:06:15 +00:00
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
|
|
|
startTime := time.Now()
|
|
|
|
|
|
|
|
for i := 0; i < len(paths); i++ {
|
|
|
|
wg.Add(1)
|
2023-01-27 16:06:10 +00:00
|
|
|
concurrency.directoryScans <- 1
|
2023-01-25 01:06:15 +00:00
|
|
|
|
|
|
|
go func(i int) {
|
|
|
|
defer func() {
|
2023-01-27 16:06:10 +00:00
|
|
|
<-concurrency.directoryScans
|
2023-06-03 23:45:32 +00:00
|
|
|
|
2023-01-25 01:06:15 +00:00
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
|
2023-09-12 21:32:19 +00:00
|
|
|
err := scanPath(paths[i], files, filters, stats, concurrency, formats)
|
2023-01-25 01:06:15 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
|
|
|
}(i)
|
2022-11-05 16:00:42 +00:00
|
|
|
}
|
2022-09-09 22:21:03 +00:00
|
|
|
|
2023-01-25 01:06:15 +00:00
|
|
|
wg.Wait()
|
2022-09-08 17:12:58 +00:00
|
|
|
|
2023-01-25 01:06:15 +00:00
|
|
|
fileList = prepareDirectories(files, sort)
|
|
|
|
|
2023-09-10 01:57:50 +00:00
|
|
|
if stats.filesMatched.Load() < 1 {
|
|
|
|
return []string{}, false
|
|
|
|
}
|
|
|
|
|
2023-09-12 18:06:45 +00:00
|
|
|
if Verbose {
|
2023-08-02 18:29:37 +00:00
|
|
|
fmt.Printf("%s | Indexed %d/%d files across %d/%d directories in %s\n",
|
2023-09-13 14:26:15 +00:00
|
|
|
time.Now().Format(logDate),
|
2023-08-13 22:02:09 +00:00
|
|
|
stats.filesMatched.Load(),
|
|
|
|
stats.filesMatched.Load()+stats.filesSkipped.Load(),
|
|
|
|
stats.directoriesMatched.Load(),
|
|
|
|
stats.directoriesMatched.Load()+stats.directoriesSkipped.Load(),
|
2023-01-25 01:06:15 +00:00
|
|
|
time.Since(startTime),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-09-13 14:26:15 +00:00
|
|
|
if Cache && filters.isEmpty() {
|
|
|
|
cache.set(fileList)
|
2023-01-25 01:06:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return fileList, false
|
2022-09-08 20:30:51 +00:00
|
|
|
}
|
|
|
|
|
2023-09-13 14:26:15 +00:00
|
|
|
func prepareDirectories(files *files, sort string) []string {
|
2023-02-18 20:08:11 +00:00
|
|
|
directories := []string{}
|
2023-01-25 01:06:15 +00:00
|
|
|
|
2023-01-27 16:06:10 +00:00
|
|
|
keys := make([]string, len(files.list))
|
2023-01-25 01:06:15 +00:00
|
|
|
|
2023-02-18 20:08:11 +00:00
|
|
|
i := 0
|
2023-01-27 16:06:10 +00:00
|
|
|
for k := range files.list {
|
2023-01-25 01:06:15 +00:00
|
|
|
keys[i] = k
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
|
2023-09-14 01:45:28 +00:00
|
|
|
for i := 0; i < len(keys); i++ {
|
|
|
|
directories = append(directories, files.list[keys[i]]...)
|
2023-01-25 01:06:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return directories
|
|
|
|
}
|
|
|
|
|
2023-09-13 14:26:15 +00:00
|
|
|
func pickFile(args []string, filters *filters, sort string, cache *fileCache, formats *types.Types) (string, error) {
|
|
|
|
fileList, fromCache := fileList(args, filters, sort, cache, formats)
|
2023-01-25 01:06:15 +00:00
|
|
|
|
|
|
|
fileCount := len(fileList)
|
2023-09-14 03:08:01 +00:00
|
|
|
|
2023-04-10 20:53:01 +00:00
|
|
|
if fileCount < 1 {
|
2023-09-11 01:29:11 +00:00
|
|
|
return "", ErrNoMediaFound
|
2023-01-25 01:06:15 +00:00
|
|
|
}
|
|
|
|
|
2023-09-14 03:08:01 +00:00
|
|
|
r, err := rand.Int(rand.Reader, big.NewInt(int64(fileCount)))
|
2023-09-10 17:16:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
val, err := strconv.Atoi(strconv.FormatInt(r.Int64(), 10))
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2023-01-25 01:06:15 +00:00
|
|
|
|
|
|
|
for i := 0; i < fileCount; i++ {
|
2023-09-14 03:08:01 +00:00
|
|
|
switch {
|
|
|
|
case val >= fileCount:
|
2023-09-10 17:16:50 +00:00
|
|
|
val = 0
|
2023-09-14 03:08:01 +00:00
|
|
|
case val < fileCount-1:
|
2023-09-10 17:16:50 +00:00
|
|
|
val++
|
2023-01-25 01:06:15 +00:00
|
|
|
}
|
|
|
|
|
2023-09-13 14:26:15 +00:00
|
|
|
path := fileList[val]
|
2023-01-25 01:06:15 +00:00
|
|
|
|
|
|
|
if !fromCache {
|
2023-09-14 22:37:22 +00:00
|
|
|
format := formats.FileType(path)
|
|
|
|
if format == nil {
|
|
|
|
return "", nil
|
2023-01-25 01:06:15 +00:00
|
|
|
}
|
|
|
|
|
2023-09-14 22:37:22 +00:00
|
|
|
if format.Validate(path) {
|
2023-09-13 14:26:15 +00:00
|
|
|
return path, nil
|
2023-01-25 01:06:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-09-13 14:26:15 +00:00
|
|
|
return path, nil
|
2023-01-25 01:06:15 +00:00
|
|
|
}
|
|
|
|
|
2023-09-11 01:29:11 +00:00
|
|
|
return "", ErrNoMediaFound
|
2023-01-25 01:06:15 +00:00
|
|
|
}
|
|
|
|
|
2023-04-11 12:19:06 +00:00
|
|
|
func normalizePath(path string) (string, error) {
|
2023-09-13 17:46:14 +00:00
|
|
|
homeDir, err := os.UserHomeDir()
|
2023-09-13 17:30:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if path == "~" {
|
2023-09-13 17:46:14 +00:00
|
|
|
path = homeDir
|
2023-09-13 17:30:24 +00:00
|
|
|
} else if strings.HasPrefix(path, "~/") {
|
2023-09-13 17:46:14 +00:00
|
|
|
path = filepath.Join(homeDir, path[2:])
|
2023-09-13 17:30:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
path, err = filepath.EvalSymlinks(path)
|
2023-04-11 12:19:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
absolutePath, err := filepath.Abs(path)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return absolutePath, nil
|
|
|
|
}
|
|
|
|
|
2023-09-14 01:45:28 +00:00
|
|
|
func validatePaths(args []string, formats *types.Types) ([]string, error) {
|
2023-05-31 18:01:24 +00:00
|
|
|
var paths []string
|
2023-01-25 01:06:15 +00:00
|
|
|
|
2023-05-12 04:22:31 +00:00
|
|
|
var pathList strings.Builder
|
|
|
|
pathList.WriteString("Paths:\n")
|
2023-01-25 01:06:15 +00:00
|
|
|
|
|
|
|
for i := 0; i < len(args); i++ {
|
2023-04-11 12:19:06 +00:00
|
|
|
path, err := normalizePath(args[i])
|
2022-10-25 05:06:57 +00:00
|
|
|
if err != nil {
|
2023-01-25 01:06:15 +00:00
|
|
|
return nil, err
|
2022-10-25 05:06:57 +00:00
|
|
|
}
|
|
|
|
|
2023-04-11 12:19:06 +00:00
|
|
|
pathMatches := (args[i] == path)
|
2023-01-25 01:06:15 +00:00
|
|
|
|
2023-09-12 21:32:19 +00:00
|
|
|
hasSupportedFiles, err := pathHasSupportedFiles(path, formats)
|
2023-04-11 09:44:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var addPath bool = false
|
|
|
|
|
|
|
|
switch {
|
2023-04-11 12:19:06 +00:00
|
|
|
case pathMatches && hasSupportedFiles:
|
2023-05-12 04:22:31 +00:00
|
|
|
pathList.WriteString(fmt.Sprintf("%s\n", args[i]))
|
2023-04-11 09:44:18 +00:00
|
|
|
addPath = true
|
2023-04-11 12:19:06 +00:00
|
|
|
case !pathMatches && hasSupportedFiles:
|
2023-05-12 04:22:31 +00:00
|
|
|
pathList.WriteString(fmt.Sprintf("%s (resolved to %s)\n", args[i], path))
|
2023-04-11 09:44:18 +00:00
|
|
|
addPath = true
|
2023-04-11 12:19:06 +00:00
|
|
|
case pathMatches && !hasSupportedFiles:
|
2023-05-12 04:22:31 +00:00
|
|
|
pathList.WriteString(fmt.Sprintf("%s [No supported files found]\n", args[i]))
|
2023-04-11 12:19:06 +00:00
|
|
|
case !pathMatches && !hasSupportedFiles:
|
2023-05-12 04:22:31 +00:00
|
|
|
pathList.WriteString(fmt.Sprintf("%s (resolved to %s) [No supported files found]\n", args[i], path))
|
2022-10-25 05:06:57 +00:00
|
|
|
}
|
2023-01-25 01:06:15 +00:00
|
|
|
|
2023-04-11 09:44:18 +00:00
|
|
|
if addPath {
|
2023-05-31 18:01:24 +00:00
|
|
|
paths = append(paths, path)
|
2023-04-11 09:44:18 +00:00
|
|
|
}
|
2022-09-08 20:30:51 +00:00
|
|
|
}
|
|
|
|
|
2023-05-12 04:22:31 +00:00
|
|
|
if len(paths) > 0 {
|
|
|
|
fmt.Println(pathList.String())
|
|
|
|
}
|
2023-01-25 01:06:15 +00:00
|
|
|
|
|
|
|
return paths, nil
|
2022-09-08 15:57:59 +00:00
|
|
|
}
|