roulette/cmd/files.go

736 lines
14 KiB
Go
Raw Normal View History

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 (
"errors"
"fmt"
"image"
_ "image/gif"
_ "image/jpeg"
_ "image/png"
2023-09-10 17:16:50 +00:00
"math/big"
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"
"strconv"
"strings"
"sync"
"sync/atomic"
2022-09-08 15:57:59 +00:00
"time"
2022-10-29 15:32:01 +00:00
_ "golang.org/x/image/bmp"
_ "golang.org/x/image/webp"
"github.com/h2non/filetype"
2022-09-08 15:57:59 +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
}
var (
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",
}
)
type Dimensions struct {
width int
height int
}
type Files struct {
mutex sync.RWMutex
list map[string][]string
}
func (f *Files) Append(directory, path string) {
f.mutex.Lock()
f.list[directory] = append(f.list[directory], path)
f.mutex.Unlock()
}
type ScanStats struct {
2023-08-13 22:29:28 +00:00
filesMatched atomic.Uint32
filesSkipped atomic.Uint32
directoriesMatched atomic.Uint32
directoriesSkipped atomic.Uint32
}
type Path struct {
base string
number int
extension string
}
func (p *Path) increment() {
p.number = p.number + 1
}
func (p *Path) decrement() {
p.number = p.number - 1
}
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])
}
func imageDimensions(path string) (*Dimensions, error) {
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):
return &Dimensions{width: 0, height: 0}, nil
case err != nil:
return &Dimensions{}, err
}
return &Dimensions{width: myImage.Width, height: myImage.Height}, nil
}
func preparePath(path string) string {
if runtime.GOOS == "windows" {
return fmt.Sprintf("%s/%s", MediaPrefix, filepath.ToSlash(path))
}
return MediaPrefix + path
2023-01-19 18:07:15 +00:00
}
func appendPath(directory, path string, files *Files, stats *ScanStats, types *SupportedTypes, shouldCache bool) error {
if shouldCache {
supported, _, _, err := fileType(path, types)
if err != nil {
return err
}
2023-09-11 01:23:48 +00:00
if !supported {
return nil
}
}
files.Append(directory, path)
2023-08-13 22:29:28 +00:00
stats.filesMatched.Add(1)
return nil
}
func appendPaths(path string, files *Files, filters *Filters, stats *ScanStats, types *SupportedTypes) error {
shouldCache := cache && filters.IsEmpty()
absolutePath, err := filepath.Abs(path)
if err != nil {
return err
}
directory, filename := filepath.Split(absolutePath)
filename = strings.ToLower(filename)
if filters.HasExcludes() {
for i := 0; i < len(filters.excludes); i++ {
if strings.Contains(
filename,
filters.excludes[i],
) {
stats.filesSkipped.Add(1)
return nil
}
}
}
if filters.HasIncludes() {
for i := 0; i < len(filters.includes); i++ {
if strings.Contains(
filename,
filters.includes[i],
) {
err := appendPath(directory, path, files, stats, types, shouldCache)
if err != nil {
return err
}
return nil
}
}
stats.filesSkipped.Add(1)
return nil
}
err = appendPath(directory, path, files, stats, types, shouldCache)
if err != nil {
return err
}
return nil
}
func newFile(paths []string, filters *Filters, sortOrder string, Regexes *Regexes, index *Index, types *SupportedTypes) (string, error) {
filePath, err := pickFile(paths, filters, sortOrder, index, types)
if err != nil {
return "", nil
}
path, err := splitPath(filePath, Regexes)
if err != nil {
return "", err
}
path.number = 1
switch {
case sortOrder == "asc":
filePath, err = tryExtensions(path)
if err != nil {
return "", err
}
case sortOrder == "desc":
for {
path.increment()
filePath, err = tryExtensions(path)
2022-10-18 21:46:55 +00:00
if err != nil {
return "", err
}
if filePath == "" {
path.decrement()
filePath, err = tryExtensions(path)
if err != nil {
return "", err
}
2022-09-17 17:42:25 +00:00
break
}
}
}
return filePath, nil
}
func nextFile(filePath, sortOrder string, Regexes *Regexes) (string, error) {
path, err := splitPath(filePath, Regexes)
if err != nil {
return "", err
}
switch {
case sortOrder == "asc":
path.increment()
case sortOrder == "desc":
path.decrement()
default:
return "", nil
}
fileName, err := tryExtensions(path)
if err != nil {
return "", err
}
return fileName, err
}
func splitPath(path string, Regexes *Regexes) (*Path, error) {
p := Path{}
var err error
2022-10-18 21:46:55 +00:00
split := Regexes.filename.FindAllStringSubmatch(path, -1)
if len(split) < 1 || len(split[0]) < 3 {
return &Path{}, nil
}
p.base = split[0][1]
p.number, err = strconv.Atoi(split[0][2])
if err != nil {
return &Path{}, err
}
p.extension = split[0][3]
return &p, nil
}
func tryExtensions(p *Path) (string, error) {
var fileName string
for _, extension := range Extensions {
fileName = fmt.Sprintf("%s%.3d%s", p.base, p.number, extension)
exists, err := fileExists(fileName)
if err != nil {
return "", err
}
2022-10-18 21:46:55 +00:00
if exists {
return fileName, nil
}
}
return "", nil
}
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
}
}
func pathIsValid(filePath string, paths []string) bool {
var matchesPrefix = false
for i := 0; i < len(paths); i++ {
if strings.HasPrefix(filePath, paths[i]) {
matchesPrefix = true
}
}
2022-10-25 05:06:57 +00:00
switch {
case verbose && !matchesPrefix:
fmt.Printf("%s | Error: Failed to serve file outside specified path(s): %s\n",
time.Now().Format(LogDate),
filePath,
)
return false
2022-10-25 05:06:57 +00:00
case !matchesPrefix:
return false
default:
return true
}
}
func fileType(path string, types *SupportedTypes) (bool, *SupportedType, string, error) {
file, err := os.Open(path)
switch {
case errors.Is(err, os.ErrNotExist):
return false, nil, "", nil
case err != nil:
return false, nil, "", err
}
defer file.Close()
head := make([]byte, 261)
file.Read(head)
2023-09-11 02:02:03 +00:00
extension := filepath.Ext(path)
fileType := types.Type(extension)
2023-09-11 02:02:03 +00:00
isSupported := types.IsSupported(head)
2023-09-11 02:02:03 +00:00
if !isSupported {
return false, nil, "", nil
2023-09-11 02:02:03 +00:00
}
mimeType := (filetype.GetType(strings.TrimPrefix(extension, "."))).MIME.Value
return isSupported, fileType, mimeType, nil
}
func pathHasSupportedFiles(path string, types *SupportedTypes) (bool, error) {
2023-04-11 09:44:18 +00:00
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():
supported, _, _, err := fileType(p, types)
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
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
}
func scanPath(path string, files *Files, filters *Filters, stats *ScanStats, concurrency *Concurrency, types *SupportedTypes) error {
var wg sync.WaitGroup
err := filepath.WalkDir(path, func(p string, info os.DirEntry, err error) error {
if err != nil {
return err
}
2022-09-08 20:30:51 +00:00
switch {
case !recursive && info.IsDir() && p != path:
return filepath.SkipDir
case !info.IsDir():
wg.Add(1)
concurrency.fileScans <- 1
go func() {
defer func() {
<-concurrency.fileScans
2023-06-03 23:45:32 +00:00
wg.Done()
}()
path, err := normalizePath(p)
if err != nil {
fmt.Println(err)
}
err = appendPaths(path, files, filters, stats, types)
if err != nil {
fmt.Println(err)
}
}()
case info.IsDir():
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) {
// 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)
return filepath.SkipDir
}
stats.directoriesMatched.Add(1)
2022-09-08 17:12:58 +00:00
}
2022-09-08 17:12:58 +00:00
return err
})
wg.Wait()
if err != nil {
return err
}
2022-09-08 17:12:58 +00:00
return nil
2022-09-08 17:12:58 +00:00
}
func fileList(paths []string, filters *Filters, sort string, index *Index, types *SupportedTypes) ([]string, bool) {
if cache && filters.IsEmpty() && !index.IsEmpty() {
return index.Index(), true
}
var fileList []string
files := &Files{
mutex: sync.RWMutex{},
list: make(map[string][]string),
}
stats := &ScanStats{
2023-08-13 22:29:28 +00:00
filesMatched: atomic.Uint32{},
filesSkipped: atomic.Uint32{},
directoriesMatched: atomic.Uint32{},
directoriesSkipped: atomic.Uint32{},
}
2022-09-08 15:57:59 +00:00
concurrency := &Concurrency{
directoryScans: make(chan int, maxDirectoryScans),
fileScans: make(chan int, maxFileScans),
}
var wg sync.WaitGroup
startTime := time.Now()
for i := 0; i < len(paths); i++ {
wg.Add(1)
concurrency.directoryScans <- 1
go func(i int) {
defer func() {
<-concurrency.directoryScans
2023-06-03 23:45:32 +00:00
wg.Done()
}()
err := scanPath(paths[i], files, filters, stats, concurrency, types)
if err != nil {
fmt.Println(err)
}
}(i)
}
wg.Wait()
2022-09-08 17:12:58 +00:00
fileList = prepareDirectories(files, sort)
if stats.filesMatched.Load() < 1 {
return []string{}, false
}
if verbose {
fmt.Printf("%s | Indexed %d/%d files across %d/%d directories in %s\n",
time.Now().Format(LogDate),
stats.filesMatched.Load(),
stats.filesMatched.Load()+stats.filesSkipped.Load(),
stats.directoriesMatched.Load(),
stats.directoriesMatched.Load()+stats.directoriesSkipped.Load(),
time.Since(startTime),
)
}
if cache && filters.IsEmpty() {
index.setIndex(fileList)
}
return fileList, false
2022-09-08 20:30:51 +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 {
return []string{directory[0]}
} else {
return directory
}
}
func prepareDirectories(files *Files, sort string) []string {
directories := []string{}
keys := make([]string, len(files.list))
i := 0
for k := range files.list {
keys[i] = k
i++
}
if sort == "asc" || sort == "desc" {
for i := 0; i < len(keys); i++ {
directories = append(directories, prepareDirectory(files.list[keys[i]])...)
}
} else {
for i := 0; i < len(keys); i++ {
directories = append(directories, files.list[keys[i]]...)
}
}
return directories
}
func pickFile(args []string, filters *Filters, sort string, index *Index, types *SupportedTypes) (string, error) {
fileList, fromCache := fileList(args, filters, sort, index, types)
fileCount := len(fileList)
2023-04-10 20:53:01 +00:00
if fileCount < 1 {
return "", ErrNoMediaFound
}
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
}
for i := 0; i < fileCount; i++ {
2023-09-10 17:16:50 +00:00
if val >= fileCount {
val = 0
} else {
2023-09-10 17:16:50 +00:00
val++
}
2023-09-10 17:16:50 +00:00
filePath := fileList[val]
if !fromCache {
supported, _, _, err := fileType(filePath, types)
if err != nil {
return "", err
}
2023-09-11 01:23:48 +00:00
if supported {
return filePath, nil
}
continue
}
return filePath, nil
}
return "", ErrNoMediaFound
}
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
}
func normalizePaths(args []string, types *SupportedTypes) ([]string, error) {
var paths []string
2023-05-12 04:22:31 +00:00
var pathList strings.Builder
pathList.WriteString("Paths:\n")
for i := 0; i < len(args); i++ {
path, err := normalizePath(args[i])
2022-10-25 05:06:57 +00:00
if err != nil {
return nil, err
2022-10-25 05:06:57 +00:00
}
pathMatches := (args[i] == path)
hasSupportedFiles, err := pathHasSupportedFiles(path, types)
2023-04-11 09:44:18 +00:00
if err != nil {
return nil, err
}
var addPath bool = false
switch {
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
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
case pathMatches && !hasSupportedFiles:
2023-05-12 04:22:31 +00:00
pathList.WriteString(fmt.Sprintf("%s [No supported files found]\n", args[i]))
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-04-11 09:44:18 +00:00
if addPath {
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())
}
return paths, nil
2022-09-08 15:57:59 +00:00
}