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"
|
2022-10-28 23:32:03 +00:00
|
|
|
"image"
|
|
|
|
_ "image/gif"
|
|
|
|
_ "image/jpeg"
|
|
|
|
_ "image/png"
|
2023-09-10 17:16:50 +00:00
|
|
|
"math/big"
|
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
|
|
|
|
2022-10-29 15:32:01 +00:00
|
|
|
_ "golang.org/x/image/bmp"
|
2022-10-28 23:32:03 +00:00
|
|
|
_ "golang.org/x/image/webp"
|
|
|
|
|
2022-09-10 00:00:02 +00:00
|
|
|
"github.com/h2non/filetype"
|
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
|
|
|
|
)
|
|
|
|
|
|
|
|
type Concurrency struct {
|
|
|
|
directoryScans chan int
|
|
|
|
fileScans chan int
|
|
|
|
}
|
|
|
|
|
2022-10-20 19:22:01 +00:00
|
|
|
var (
|
2023-09-11 01:29:11 +00:00
|
|
|
ErrNoMediaFound = errors.New("no supported media formats found which match all criteria")
|
2023-09-11 02:02:03 +00:00
|
|
|
Extensions = [12]string{
|
|
|
|
".bmp",
|
|
|
|
".gif",
|
|
|
|
".jpeg",
|
|
|
|
".jpg",
|
|
|
|
".mp3",
|
|
|
|
".mp4",
|
|
|
|
".ogg",
|
|
|
|
".ogv",
|
|
|
|
".png",
|
|
|
|
".wav",
|
|
|
|
".webm",
|
|
|
|
".webp",
|
|
|
|
}
|
2022-10-20 19:22:01 +00:00
|
|
|
)
|
|
|
|
|
2022-11-10 05:17:19 +00:00
|
|
|
type Dimensions struct {
|
2023-01-27 16:06:10 +00:00
|
|
|
width int
|
|
|
|
height int
|
2022-11-10 05:17:19 +00:00
|
|
|
}
|
|
|
|
|
2022-10-20 19:22:01 +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-01-25 01:06: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-01-25 01:06:15 +00:00
|
|
|
type ScanStats struct {
|
2023-08-13 22:29:28 +00:00
|
|
|
filesMatched atomic.Uint32
|
|
|
|
filesSkipped atomic.Uint32
|
|
|
|
directoriesMatched atomic.Uint32
|
|
|
|
directoriesSkipped atomic.Uint32
|
2023-08-02 18:29:37 +00:00
|
|
|
}
|
|
|
|
|
2023-01-25 01:06:15 +00:00
|
|
|
type Path struct {
|
2023-01-27 16:06:10 +00:00
|
|
|
base string
|
|
|
|
number int
|
|
|
|
extension string
|
2023-01-21 04:42:44 +00:00
|
|
|
}
|
|
|
|
|
2023-01-27 19:17:13 +00:00
|
|
|
func (p *Path) increment() {
|
2023-01-27 16:06:10 +00:00
|
|
|
p.number = p.number + 1
|
2023-01-21 04:42:44 +00:00
|
|
|
}
|
|
|
|
|
2023-01-27 19:17:13 +00:00
|
|
|
func (p *Path) decrement() {
|
2023-01-27 16:06:10 +00:00
|
|
|
p.number = p.number - 1
|
2023-01-25 01:06:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func contains(s []string, e string) bool {
|
|
|
|
for _, a := range s {
|
|
|
|
if a == e {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
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])
|
|
|
|
}
|
|
|
|
|
2023-01-27 16:06:10 +00:00
|
|
|
func imageDimensions(path string) (*Dimensions, error) {
|
2023-01-25 01:06:15 +00:00
|
|
|
file, err := os.Open(path)
|
|
|
|
switch {
|
|
|
|
case errors.Is(err, os.ErrNotExist):
|
|
|
|
return &Dimensions{}, nil
|
|
|
|
case err != nil:
|
|
|
|
return &Dimensions{}, err
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
myImage, _, err := image.DecodeConfig(file)
|
|
|
|
switch {
|
|
|
|
case errors.Is(err, image.ErrFormat):
|
2023-01-27 16:06:10 +00:00
|
|
|
return &Dimensions{width: 0, height: 0}, nil
|
2023-01-25 01:06:15 +00:00
|
|
|
case err != nil:
|
|
|
|
return &Dimensions{}, err
|
|
|
|
}
|
|
|
|
|
2023-01-27 16:06:10 +00:00
|
|
|
return &Dimensions{width: myImage.Width, height: myImage.Height}, nil
|
2023-01-25 01:06:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func preparePath(path string) string {
|
2023-09-11 01:29:11 +00:00
|
|
|
path = MediaPrefix + path
|
2023-06-03 18:29:49 +00:00
|
|
|
|
2023-01-25 01:06:15 +00:00
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
path = fmt.Sprintf("/%s", filepath.ToSlash(path))
|
|
|
|
}
|
|
|
|
|
|
|
|
return path
|
2023-01-19 18:07:15 +00:00
|
|
|
}
|
|
|
|
|
2023-01-21 16:27:35 +00:00
|
|
|
func appendPath(directory, path string, files *Files, stats *ScanStats, shouldCache bool) error {
|
|
|
|
if shouldCache {
|
2023-09-11 02:02:03 +00:00
|
|
|
supported, _, _, err := fileType(path)
|
2022-10-28 22:19:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-09-11 01:23:48 +00:00
|
|
|
if !supported {
|
2022-10-28 22:19:04 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-25 01:06: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-01-19 18:07:15 +00:00
|
|
|
func appendPaths(path string, files *Files, filters *Filters, stats *ScanStats) error {
|
2023-01-27 19:17:13 +00:00
|
|
|
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)
|
|
|
|
|
2022-10-23 18:08:08 +00:00
|
|
|
if filters.HasExcludes() {
|
2023-01-27 16:06:10 +00:00
|
|
|
for i := 0; i < len(filters.excludes); i++ {
|
2022-10-20 00:27:11 +00:00
|
|
|
if strings.Contains(
|
2022-10-20 15:38:32 +00:00
|
|
|
filename,
|
2023-01-27 16:06:10 +00:00
|
|
|
filters.excludes[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
|
|
|
|
2022-10-23 18:08:08 +00:00
|
|
|
if filters.HasIncludes() {
|
2023-01-27 16:06:10 +00:00
|
|
|
for i := 0; i < len(filters.includes); i++ {
|
2022-10-20 00:27:11 +00:00
|
|
|
if strings.Contains(
|
2022-10-20 15:38:32 +00:00
|
|
|
filename,
|
2023-01-27 16:06:10 +00:00
|
|
|
filters.includes[i],
|
2022-10-20 00:27:11 +00:00
|
|
|
) {
|
2023-01-21 16:27:35 +00:00
|
|
|
err := appendPath(directory, path, files, stats, 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-01-21 16:27:35 +00:00
|
|
|
err = appendPath(directory, path, files, stats, 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-01-27 16:06:10 +00:00
|
|
|
func newFile(paths []string, filters *Filters, sortOrder string, Regexes *Regexes, index *Index) (string, error) {
|
2023-01-21 04:42:44 +00:00
|
|
|
filePath, err := pickFile(paths, filters, sortOrder, index)
|
2022-11-05 16:00:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", nil
|
|
|
|
}
|
2022-09-18 18:07:46 +00:00
|
|
|
|
2023-01-27 16:06:10 +00:00
|
|
|
path, err := splitPath(filePath, Regexes)
|
2022-09-18 18:07:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2023-01-27 16:06:10 +00:00
|
|
|
path.number = 1
|
2022-10-18 18:42:32 +00:00
|
|
|
|
2022-11-05 16:00:42 +00:00
|
|
|
switch {
|
|
|
|
case sortOrder == "asc":
|
|
|
|
filePath, err = tryExtensions(path)
|
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-01-27 19:17:13 +00:00
|
|
|
path.increment()
|
2022-10-18 18:42:32 +00:00
|
|
|
|
2022-11-05 16:00:42 +00:00
|
|
|
filePath, err = tryExtensions(path)
|
2022-10-18 21:46:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2022-10-18 18:42:32 +00:00
|
|
|
|
2022-11-05 16:00:42 +00:00
|
|
|
if filePath == "" {
|
2023-01-27 19:17:13 +00:00
|
|
|
path.decrement()
|
2022-09-18 18:07:46 +00:00
|
|
|
|
2022-11-05 16:00:42 +00:00
|
|
|
filePath, err = tryExtensions(path)
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-11-05 16:00:42 +00:00
|
|
|
return filePath, nil
|
2022-10-18 21:40:13 +00:00
|
|
|
}
|
|
|
|
|
2023-01-27 16:06:10 +00:00
|
|
|
func nextFile(filePath, sortOrder string, Regexes *Regexes) (string, error) {
|
|
|
|
path, err := splitPath(filePath, 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-01-27 19:17:13 +00:00
|
|
|
path.increment()
|
2022-11-05 16:00:42 +00:00
|
|
|
case sortOrder == "desc":
|
2023-01-27 19:17:13 +00:00
|
|
|
path.decrement()
|
2022-11-05 16:00:42 +00:00
|
|
|
default:
|
|
|
|
return "", nil
|
|
|
|
}
|
2022-10-18 21:40:13 +00:00
|
|
|
|
2022-11-05 16:17:31 +00:00
|
|
|
fileName, err := tryExtensions(path)
|
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-01-27 16:06:10 +00:00
|
|
|
func splitPath(path string, Regexes *Regexes) (*Path, error) {
|
2023-01-25 01:06:15 +00:00
|
|
|
p := Path{}
|
|
|
|
var err error
|
2022-10-18 21:46:55 +00:00
|
|
|
|
2023-01-27 16:06:10 +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 {
|
|
|
|
return &Path{}, nil
|
|
|
|
}
|
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 {
|
|
|
|
return &Path{}, err
|
|
|
|
}
|
|
|
|
|
2023-01-27 16:06:10 +00:00
|
|
|
p.extension = split[0][3]
|
2023-01-25 01:06:15 +00:00
|
|
|
|
|
|
|
return &p, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func tryExtensions(p *Path) (string, error) {
|
|
|
|
var fileName string
|
|
|
|
|
2023-01-27 19:17:13 +00:00
|
|
|
for _, extension := range Extensions {
|
2023-01-27 16:06:10 +00:00
|
|
|
fileName = fmt.Sprintf("%s%.3d%s", p.base, p.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
|
|
|
}
|
|
|
|
|
2022-10-18 18:42:32 +00:00
|
|
|
func pathIsValid(filePath string, paths []string) bool {
|
|
|
|
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++ {
|
|
|
|
if strings.HasPrefix(filePath, paths[i]) {
|
|
|
|
matchesPrefix = true
|
|
|
|
}
|
|
|
|
}
|
2022-10-20 22:55:25 +00:00
|
|
|
|
2022-10-25 05:06:57 +00:00
|
|
|
switch {
|
2023-01-27 19:17:13 +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-01-25 01:06:15 +00:00
|
|
|
time.Now().Format(LogDate),
|
2022-10-20 22:55:25 +00:00
|
|
|
filePath,
|
|
|
|
)
|
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-11 02:02:03 +00:00
|
|
|
func fileType(path string) (bool, string, string, error) {
|
2023-01-25 01:06:15 +00:00
|
|
|
file, err := os.Open(path)
|
|
|
|
switch {
|
|
|
|
case errors.Is(err, os.ErrNotExist):
|
2023-09-11 01:41:08 +00:00
|
|
|
return false, "", "", nil
|
2023-01-25 01:06:15 +00:00
|
|
|
case err != nil:
|
2023-09-11 01:41:08 +00:00
|
|
|
return false, "", "", err
|
2023-01-24 18:03:26 +00:00
|
|
|
}
|
2023-01-25 01:06:15 +00:00
|
|
|
defer file.Close()
|
2022-09-09 22:21:03 +00:00
|
|
|
|
2023-01-25 01:06:15 +00:00
|
|
|
head := make([]byte, 261)
|
|
|
|
file.Read(head)
|
2023-01-24 18:03:26 +00:00
|
|
|
|
2023-09-11 02:02:03 +00:00
|
|
|
extension := filepath.Ext(path)
|
|
|
|
|
|
|
|
isSupported := false
|
|
|
|
|
|
|
|
for _, e := range Extensions {
|
|
|
|
if e == extension {
|
|
|
|
isSupported = true
|
|
|
|
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !isSupported {
|
|
|
|
return false, "", "", nil
|
|
|
|
}
|
2023-09-11 01:41:08 +00:00
|
|
|
|
2023-09-11 02:02:03 +00:00
|
|
|
fileType := filetype.GetType(strings.TrimPrefix(extension, "."))
|
2023-09-11 01:41:08 +00:00
|
|
|
|
2023-05-09 01:05:10 +00:00
|
|
|
switch {
|
2023-09-11 02:02:03 +00:00
|
|
|
case filetype.IsAudio(head) && audio:
|
|
|
|
return true, "audio", fileType.MIME.Value, nil
|
2023-05-09 01:05:10 +00:00
|
|
|
case filetype.IsImage(head):
|
2023-09-11 01:41:08 +00:00
|
|
|
return true, "image", fileType.MIME.Value, nil
|
2023-09-11 02:02:03 +00:00
|
|
|
case filetype.IsVideo(head) && video:
|
2023-09-11 01:41:08 +00:00
|
|
|
return true, "video", fileType.MIME.Value, nil
|
2023-05-09 01:05:10 +00:00
|
|
|
default:
|
2023-09-11 01:41:08 +00:00
|
|
|
return false, "", "", nil
|
2023-05-09 01:05:10 +00:00
|
|
|
}
|
2023-01-24 18:03:26 +00:00
|
|
|
}
|
|
|
|
|
2023-04-11 09:44:18 +00:00
|
|
|
func pathHasSupportedFiles(path string) (bool, error) {
|
|
|
|
hasSupportedFiles := make(chan bool, 1)
|
|
|
|
|
|
|
|
err := filepath.WalkDir(path, func(p string, info os.DirEntry, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case !recursive && info.IsDir() && p != path:
|
|
|
|
return filepath.SkipDir
|
|
|
|
case !info.IsDir():
|
2023-09-11 02:02:03 +00:00
|
|
|
supported, _, _, err := fileType(p)
|
2023-04-11 09:44:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-09-11 01:23:48 +00:00
|
|
|
if supported {
|
2023-04-11 09:44:18 +00:00
|
|
|
hasSupportedFiles <- true
|
|
|
|
return filepath.SkipAll
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-hasSupportedFiles:
|
|
|
|
return true, nil
|
|
|
|
default:
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-13 22:29:28 +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-01-27 16:06:10 +00:00
|
|
|
func scanPath(path string, files *Files, filters *Filters, stats *ScanStats, concurrency *Concurrency) 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-01-27 19:17:13 +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)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = appendPaths(path, files, filters, stats)
|
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-08-13 22:29:28 +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-01-27 16:06:10 +00:00
|
|
|
func fileList(paths []string, filters *Filters, sort string, index *Index) ([]string, bool) {
|
2023-01-27 19:17:13 +00:00
|
|
|
if cache && filters.IsEmpty() && !index.IsEmpty() {
|
2023-01-27 16:06:10 +00:00
|
|
|
return index.Index(), 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-01-25 01:06: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-01-25 01:06:15 +00:00
|
|
|
stats := &ScanStats{
|
2023-08-13 22:29:28 +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-01-25 01:06: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-01-27 16:06:10 +00:00
|
|
|
err := scanPath(paths[i], files, filters, stats, concurrency)
|
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-01-27 19:17:13 +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-01-25 01:06: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-01-27 19:17:13 +00:00
|
|
|
if cache && filters.IsEmpty() {
|
2023-01-27 16:06:10 +00:00
|
|
|
index.setIndex(fileList)
|
2023-01-25 01:06:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return fileList, false
|
2022-09-08 20:30:51 +00:00
|
|
|
}
|
|
|
|
|
2023-02-18 19:38:18 +00:00
|
|
|
func cleanFilename(filename string) string {
|
|
|
|
return filename[:len(filename)-(len(filepath.Ext(filename))+3)]
|
|
|
|
}
|
|
|
|
|
|
|
|
func prepareDirectory(directory []string) []string {
|
|
|
|
_, first := filepath.Split(directory[0])
|
|
|
|
first = cleanFilename(first)
|
|
|
|
|
|
|
|
_, last := filepath.Split(directory[len(directory)-1])
|
|
|
|
last = cleanFilename(last)
|
|
|
|
|
|
|
|
if first == last {
|
2023-02-18 20:08:11 +00:00
|
|
|
return []string{directory[0]}
|
2023-02-18 19:38:18 +00:00
|
|
|
} else {
|
|
|
|
return directory
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-25 01:06: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-02-18 19:38:18 +00:00
|
|
|
if sort == "asc" || sort == "desc" {
|
|
|
|
for i := 0; i < len(keys); i++ {
|
2023-02-18 20:08:11 +00:00
|
|
|
directories = append(directories, prepareDirectory(files.list[keys[i]])...)
|
2023-02-18 19:38:18 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for i := 0; i < len(keys); i++ {
|
2023-02-18 20:08:11 +00:00
|
|
|
directories = append(directories, files.list[keys[i]]...)
|
2023-02-18 19:38:18 +00:00
|
|
|
}
|
2023-01-25 01:06:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return directories
|
|
|
|
}
|
|
|
|
|
|
|
|
func pickFile(args []string, filters *Filters, sort string, index *Index) (string, error) {
|
2023-01-27 16:06:10 +00:00
|
|
|
fileList, fromCache := fileList(args, filters, sort, index)
|
2023-01-25 01:06:15 +00:00
|
|
|
|
|
|
|
fileCount := len(fileList)
|
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-10 17:16:50 +00:00
|
|
|
r, err := rand.Int(rand.Reader, big.NewInt(int64(fileCount-2)))
|
|
|
|
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-10 17:16:50 +00:00
|
|
|
if val >= fileCount {
|
|
|
|
val = 0
|
2023-01-25 01:06:15 +00:00
|
|
|
} else {
|
2023-09-10 17:16:50 +00:00
|
|
|
val++
|
2023-01-25 01:06:15 +00:00
|
|
|
}
|
|
|
|
|
2023-09-10 17:16:50 +00:00
|
|
|
filePath := fileList[val]
|
2023-01-25 01:06:15 +00:00
|
|
|
|
|
|
|
if !fromCache {
|
2023-09-11 02:02:03 +00:00
|
|
|
supported, _, _, err := fileType(filePath)
|
2023-01-25 01:06:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2023-09-11 01:23:48 +00:00
|
|
|
if supported {
|
2023-01-25 01:06:15 +00:00
|
|
|
return filePath, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
return filePath, nil
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
path, err := filepath.EvalSymlinks(path)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
absolutePath, err := filepath.Abs(path)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return absolutePath, nil
|
|
|
|
}
|
|
|
|
|
2023-01-25 01:06:15 +00:00
|
|
|
func normalizePaths(args []string) ([]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-04-11 12:19:06 +00:00
|
|
|
hasSupportedFiles, err := pathHasSupportedFiles(path)
|
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
|
|
|
}
|