2022-09-08 15:57:59 +00:00
|
|
|
/*
|
|
|
|
Copyright © 2022 Seednode <seednode@seedno.de>
|
|
|
|
*/
|
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2022-09-09 22:21:03 +00:00
|
|
|
"errors"
|
2022-09-17 16:07:37 +00:00
|
|
|
"fmt"
|
2022-09-08 15:57:59 +00:00
|
|
|
"math/rand"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2022-09-17 16:07:37 +00:00
|
|
|
"regexp"
|
|
|
|
"strconv"
|
2022-09-24 22:17:56 +00:00
|
|
|
"strings"
|
2022-09-08 15:57:59 +00:00
|
|
|
"time"
|
2022-09-10 00:00:02 +00:00
|
|
|
|
|
|
|
"github.com/h2non/filetype"
|
2022-09-08 15:57:59 +00:00
|
|
|
)
|
|
|
|
|
2022-10-20 16:17:40 +00:00
|
|
|
type Stats struct {
|
|
|
|
FilesMatched uint64
|
|
|
|
FilesSkipped uint64
|
|
|
|
DirectoriesMatched uint64
|
|
|
|
DirectoriesSkipped uint64
|
|
|
|
}
|
|
|
|
|
2022-10-18 21:40:13 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-09-24 23:59:10 +00:00
|
|
|
var (
|
|
|
|
ErrNoImagesFound = fmt.Errorf("no supported image formats found")
|
|
|
|
)
|
|
|
|
|
2022-10-20 16:17:40 +00:00
|
|
|
func appendPaths(m map[string][]string, path string, filters *Filters, stats *Stats) (map[string][]string, error) {
|
2022-09-24 23:59:10 +00:00
|
|
|
absolutePath, err := filepath.Abs(path)
|
|
|
|
if err != nil {
|
2022-09-25 01:28:34 +00:00
|
|
|
return nil, 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)
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case filters.HasIncludes() && !filters.HasExcludes():
|
|
|
|
for i := 0; i < len(filters.Includes); i++ {
|
|
|
|
if strings.Contains(
|
|
|
|
filename,
|
|
|
|
filters.Includes[i],
|
|
|
|
) {
|
|
|
|
m[directory] = append(m[directory], path)
|
2022-10-20 16:17:40 +00:00
|
|
|
stats.FilesMatched += 1
|
2022-10-20 15:38:32 +00:00
|
|
|
|
|
|
|
return m, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-20 16:17:40 +00:00
|
|
|
stats.FilesSkipped += 1
|
|
|
|
|
2022-10-20 15:38:32 +00:00
|
|
|
return m, nil
|
|
|
|
case !filters.HasIncludes() && filters.HasExcludes():
|
|
|
|
for i := 0; i < len(filters.Excludes); i++ {
|
|
|
|
if strings.Contains(
|
|
|
|
filename,
|
|
|
|
filters.Excludes[i],
|
|
|
|
) {
|
2022-10-20 16:17:40 +00:00
|
|
|
stats.FilesSkipped += 1
|
|
|
|
|
2022-10-20 15:38:32 +00:00
|
|
|
return m, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-25 01:28:34 +00:00
|
|
|
m[directory] = append(m[directory], path)
|
2022-10-20 16:17:40 +00:00
|
|
|
stats.FilesMatched += 1
|
2022-10-20 15:38:32 +00:00
|
|
|
|
|
|
|
return m, nil
|
|
|
|
case filters.HasIncludes() && filters.HasExcludes():
|
2022-10-20 00:27:11 +00:00
|
|
|
for i := 0; i < len(filters.Excludes); i++ {
|
|
|
|
if strings.Contains(
|
2022-10-20 15:38:32 +00:00
|
|
|
filename,
|
|
|
|
filters.Excludes[i],
|
2022-10-20 00:27:11 +00:00
|
|
|
) {
|
2022-10-20 16:17:40 +00:00
|
|
|
stats.FilesSkipped += 1
|
|
|
|
|
2022-10-20 00:27:11 +00:00
|
|
|
return m, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < len(filters.Includes); i++ {
|
|
|
|
if strings.Contains(
|
2022-10-20 15:38:32 +00:00
|
|
|
filename,
|
|
|
|
filters.Includes[i],
|
2022-10-20 00:27:11 +00:00
|
|
|
) {
|
|
|
|
m[directory] = append(m[directory], path)
|
2022-10-20 16:17:40 +00:00
|
|
|
stats.FilesMatched += 1
|
2022-10-20 00:27:11 +00:00
|
|
|
|
2022-10-20 15:38:32 +00:00
|
|
|
return m, nil
|
2022-10-20 00:27:11 +00:00
|
|
|
}
|
|
|
|
}
|
2022-09-24 23:59:10 +00:00
|
|
|
|
2022-10-20 16:17:40 +00:00
|
|
|
stats.FilesSkipped += 1
|
|
|
|
|
2022-10-20 15:38:32 +00:00
|
|
|
return m, nil
|
|
|
|
default:
|
|
|
|
m[directory] = append(m[directory], path)
|
2022-10-20 16:17:40 +00:00
|
|
|
stats.FilesMatched += 1
|
2022-10-20 15:38:32 +00:00
|
|
|
|
|
|
|
return m, nil
|
|
|
|
}
|
2022-09-24 22:17:56 +00:00
|
|
|
}
|
|
|
|
|
2022-10-20 00:41:42 +00:00
|
|
|
func getFirstFile(p *Path) (string, error) {
|
2022-10-18 21:40:13 +00:00
|
|
|
p.Number = 1
|
2022-09-18 18:07:46 +00:00
|
|
|
|
2022-10-18 21:40:13 +00:00
|
|
|
fileName, err := tryExtensions(p)
|
2022-09-18 18:07:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2022-10-18 18:42:32 +00:00
|
|
|
return fileName, nil
|
|
|
|
}
|
|
|
|
|
2022-10-20 00:41:42 +00:00
|
|
|
func getLastFile(p *Path) (string, error) {
|
2022-10-18 18:42:32 +00:00
|
|
|
var fileName string
|
|
|
|
var err error
|
|
|
|
|
2022-10-18 21:40:13 +00:00
|
|
|
p.Number = 1
|
|
|
|
|
2022-10-18 18:42:32 +00:00
|
|
|
for {
|
2022-10-18 21:40:13 +00:00
|
|
|
p.Increment()
|
|
|
|
|
|
|
|
fileName, err = tryExtensions(p)
|
2022-10-18 18:42:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if fileName == "" {
|
2022-10-18 21:40:13 +00:00
|
|
|
p.Decrement()
|
|
|
|
|
|
|
|
fileName, err = tryExtensions(p)
|
2022-10-18 21:46:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2022-10-18 18:42:32 +00:00
|
|
|
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-18 18:07:46 +00:00
|
|
|
return fileName, nil
|
|
|
|
}
|
|
|
|
|
2022-10-20 00:41:42 +00:00
|
|
|
func getNextFile(p *Path) (string, error) {
|
2022-10-18 21:40:13 +00:00
|
|
|
p.Increment()
|
2022-09-17 17:42:25 +00:00
|
|
|
|
2022-10-18 21:40:13 +00:00
|
|
|
fileName, err := tryExtensions(p)
|
2022-09-17 16:07:37 +00:00
|
|
|
if err != nil {
|
2022-09-17 17:42:25 +00:00
|
|
|
return "", err
|
2022-09-17 16:07:37 +00:00
|
|
|
}
|
|
|
|
|
2022-10-18 21:40:13 +00:00
|
|
|
return fileName, err
|
|
|
|
}
|
|
|
|
|
2022-10-20 00:41:42 +00:00
|
|
|
func getPreviousFile(p *Path) (string, error) {
|
2022-10-18 21:40:13 +00:00
|
|
|
p.Decrement()
|
|
|
|
|
|
|
|
fileName, err := tryExtensions(p)
|
2022-09-17 16:07:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2022-10-18 18:42:32 +00:00
|
|
|
return fileName, err
|
|
|
|
}
|
|
|
|
|
2022-10-20 00:41:42 +00:00
|
|
|
func splitPath(path string) (*Path, error) {
|
2022-10-18 21:46:55 +00:00
|
|
|
p := Path{}
|
|
|
|
var err error
|
|
|
|
|
2022-10-18 18:42:32 +00:00
|
|
|
re := regexp.MustCompile(`(.+)([0-9]{3})(\..+)`)
|
|
|
|
|
|
|
|
split := re.FindAllStringSubmatch(path, -1)
|
|
|
|
|
|
|
|
if len(split) < 1 || len(split[0]) < 3 {
|
2022-10-20 00:41:42 +00:00
|
|
|
return &Path{}, nil
|
2022-09-17 16:07:37 +00:00
|
|
|
}
|
|
|
|
|
2022-10-18 21:40:13 +00:00
|
|
|
p.Base = split[0][1]
|
2022-10-18 21:46:55 +00:00
|
|
|
|
2022-10-18 21:40:13 +00:00
|
|
|
p.Number, err = strconv.Atoi(split[0][2])
|
2022-10-18 18:42:32 +00:00
|
|
|
if err != nil {
|
2022-10-20 00:41:42 +00:00
|
|
|
return &Path{}, err
|
2022-09-26 17:31:45 +00:00
|
|
|
}
|
2022-10-18 21:46:55 +00:00
|
|
|
|
2022-10-18 21:40:13 +00:00
|
|
|
p.Extension = split[0][3]
|
2022-09-26 17:31:45 +00:00
|
|
|
|
2022-10-20 00:41:42 +00:00
|
|
|
return &p, nil
|
2022-09-26 17:31:45 +00:00
|
|
|
}
|
|
|
|
|
2022-10-20 00:41:42 +00:00
|
|
|
func tryExtensions(p *Path) (string, error) {
|
2022-10-18 21:40:13 +00:00
|
|
|
extensions := [6]string{p.Extension, ".jpg", ".jpeg", ".png", ".gif", ".webp"}
|
2022-10-18 18:42:32 +00:00
|
|
|
|
|
|
|
var fileName string
|
|
|
|
|
|
|
|
for _, i := range extensions {
|
2022-10-18 21:40:13 +00:00
|
|
|
fileName = fmt.Sprintf("%v%.3d%v", p.Base, p.Number, i)
|
2022-10-18 18:42:32 +00:00
|
|
|
|
|
|
|
exists, err := fileExists(fileName)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
2022-09-26 17:31:45 +00:00
|
|
|
}
|
|
|
|
|
2022-10-18 18:42:32 +00:00
|
|
|
if exists {
|
|
|
|
return fileName, nil
|
|
|
|
}
|
2022-09-26 17:31:45 +00:00
|
|
|
}
|
|
|
|
|
2022-10-18 18:42:32 +00:00
|
|
|
return "", nil
|
2022-09-26 17:31:45 +00:00
|
|
|
}
|
|
|
|
|
2022-10-18 18:42:32 +00:00
|
|
|
func fileExists(path string) (bool, error) {
|
2022-09-25 01:28:34 +00:00
|
|
|
_, err := os.Stat(path)
|
|
|
|
switch {
|
|
|
|
case err == nil:
|
2022-09-17 16:07:37 +00:00
|
|
|
return true, nil
|
2022-09-25 01:28:34 +00:00
|
|
|
case errors.Is(err, os.ErrNotExist):
|
2022-09-17 16:07:37 +00:00
|
|
|
return false, nil
|
2022-09-25 01:28:34 +00:00
|
|
|
default:
|
2022-09-17 16:07:37 +00:00
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-18 18:42:32 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !matchesPrefix {
|
|
|
|
if Verbose {
|
|
|
|
fmt.Printf("%v Failed to serve file outside specified path(s): %v\n", time.Now().Format(LOGDATE), filePath)
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2022-09-26 17:31:45 +00:00
|
|
|
func isImage(path string) (bool, error) {
|
2022-09-09 22:21:03 +00:00
|
|
|
file, err := os.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2022-09-10 18:09:39 +00:00
|
|
|
defer file.Close()
|
2022-09-09 22:21:03 +00:00
|
|
|
|
2022-09-10 00:00:02 +00:00
|
|
|
head := make([]byte, 261)
|
|
|
|
file.Read(head)
|
2022-09-09 22:21:03 +00:00
|
|
|
|
2022-09-26 17:31:45 +00:00
|
|
|
return filetype.IsImage(head), nil
|
2022-09-09 22:21:03 +00:00
|
|
|
}
|
|
|
|
|
2022-10-20 16:17:40 +00:00
|
|
|
func getFiles(m map[string][]string, path string, filters *Filters, stats *Stats) (map[string][]string, error) {
|
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-10-20 16:17:40 +00:00
|
|
|
if info.IsDir() && Filter && filters.HasExcludes() {
|
2022-10-20 15:38:32 +00:00
|
|
|
for i := 0; i < len(filters.Excludes); i++ {
|
|
|
|
if strings.Contains(
|
|
|
|
strings.ToLower(p),
|
|
|
|
strings.ToLower(filters.Excludes[i]),
|
|
|
|
) {
|
2022-10-20 16:17:40 +00:00
|
|
|
stats.DirectoriesSkipped += 1
|
|
|
|
|
2022-10-20 15:38:32 +00:00
|
|
|
return filepath.SkipDir
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-24 22:17:56 +00:00
|
|
|
switch {
|
2022-09-24 23:59:10 +00:00
|
|
|
case !Recursive && info.IsDir() && p != path:
|
2022-10-20 16:17:40 +00:00
|
|
|
stats.DirectoriesSkipped += 1
|
|
|
|
|
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 16:17:40 +00:00
|
|
|
m, err = appendPaths(m, p, filters, stats)
|
2022-09-24 22:17:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-10-20 16:17:40 +00:00
|
|
|
case info.IsDir():
|
|
|
|
stats.DirectoriesMatched += 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-09-08 18:12:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-09-08 17:12:58 +00:00
|
|
|
|
2022-09-25 01:28:34 +00:00
|
|
|
return m, nil
|
2022-09-08 17:12:58 +00:00
|
|
|
}
|
|
|
|
|
2022-10-20 16:17:40 +00:00
|
|
|
func getFileList(paths []string, filters *Filters, stats *Stats) (map[string][]string, error) {
|
2022-09-25 01:28:34 +00:00
|
|
|
fileMap := map[string][]string{}
|
|
|
|
var err error
|
2022-09-08 15:57:59 +00:00
|
|
|
|
2022-09-16 18:45:33 +00:00
|
|
|
for i := 0; i < len(paths); i++ {
|
2022-10-20 16:17:40 +00:00
|
|
|
fileMap, err = getFiles(fileMap, paths[i], filters, stats)
|
2022-09-24 23:59:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2022-09-08 17:12:58 +00:00
|
|
|
}
|
2022-09-25 01:28:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return fileMap, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func cleanFilename(filename string) string {
|
2022-09-26 17:31:45 +00:00
|
|
|
return filename[:len(filename)-(len(filepath.Ext(filename))+3)]
|
2022-09-25 01:28:34 +00:00
|
|
|
}
|
2022-09-24 23:59:10 +00:00
|
|
|
|
2022-09-25 01:28:34 +00:00
|
|
|
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 {
|
2022-10-20 00:27:11 +00:00
|
|
|
return append([]string{}, directory[0])
|
2022-09-25 01:28:34 +00:00
|
|
|
} else {
|
|
|
|
return directory
|
2022-09-08 15:57:59 +00:00
|
|
|
}
|
2022-09-25 01:28:34 +00:00
|
|
|
}
|
2022-09-08 15:57:59 +00:00
|
|
|
|
2022-10-20 00:27:11 +00:00
|
|
|
func prepareDirectories(m map[string][]string, sort string) []string {
|
2022-09-25 01:28:34 +00:00
|
|
|
directories := []string{}
|
|
|
|
|
|
|
|
keys := make([]string, len(m))
|
|
|
|
|
|
|
|
i := 0
|
|
|
|
for k := range m {
|
|
|
|
keys[i] = k
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
|
2022-10-20 00:27:11 +00:00
|
|
|
if sort == "asc" || sort == "desc" {
|
2022-09-25 01:28:34 +00:00
|
|
|
for i := 0; i < len(keys); i++ {
|
|
|
|
directories = append(directories, prepareDirectory(m[keys[i]])...)
|
|
|
|
}
|
2022-09-26 17:31:45 +00:00
|
|
|
} else {
|
2022-09-25 01:28:34 +00:00
|
|
|
for i := 0; i < len(keys); i++ {
|
|
|
|
directories = append(directories, m[keys[i]]...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return directories
|
2022-09-08 17:12:58 +00:00
|
|
|
}
|
2022-09-08 15:57:59 +00:00
|
|
|
|
2022-10-20 00:27:11 +00:00
|
|
|
func pickFile(args []string, filters *Filters, sort string) (string, error) {
|
2022-10-20 16:17:40 +00:00
|
|
|
stats := Stats{}
|
|
|
|
|
|
|
|
fileMap, err := getFileList(args, filters, &stats)
|
2022-09-16 18:45:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2022-10-20 16:17:40 +00:00
|
|
|
if Count {
|
|
|
|
fmt.Printf("Choosing from %v files (skipped %v) out of %v directories (skipped %v)\n",
|
|
|
|
stats.FilesMatched,
|
|
|
|
stats.FilesSkipped,
|
|
|
|
stats.DirectoriesMatched,
|
|
|
|
stats.DirectoriesSkipped,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-10-20 00:27:11 +00:00
|
|
|
fileList := prepareDirectories(fileMap, sort)
|
2022-09-25 01:54:51 +00:00
|
|
|
|
2022-09-26 17:31:45 +00:00
|
|
|
rand.Seed(time.Now().UnixNano())
|
|
|
|
|
2022-09-25 01:54:51 +00:00
|
|
|
rand.Shuffle(len(fileList), func(i, j int) { fileList[i], fileList[j] = fileList[j], fileList[i] })
|
2022-09-09 22:21:03 +00:00
|
|
|
|
|
|
|
for i := 0; i < len(fileList); i++ {
|
2022-09-10 18:09:39 +00:00
|
|
|
filePath := fileList[i]
|
2022-09-26 17:31:45 +00:00
|
|
|
isImage, err := isImage(filePath)
|
2022-09-09 22:21:03 +00:00
|
|
|
if err != nil {
|
2022-09-16 18:45:33 +00:00
|
|
|
return "", err
|
2022-09-09 22:21:03 +00:00
|
|
|
}
|
2022-10-18 21:46:55 +00:00
|
|
|
|
2022-09-09 22:21:03 +00:00
|
|
|
if isImage {
|
2022-09-16 18:45:33 +00:00
|
|
|
return filePath, nil
|
2022-09-09 22:21:03 +00:00
|
|
|
}
|
|
|
|
}
|
2022-09-08 17:12:58 +00:00
|
|
|
|
2022-09-24 23:59:10 +00:00
|
|
|
return "", ErrNoImagesFound
|
2022-09-08 20:30:51 +00:00
|
|
|
}
|
|
|
|
|
2022-09-09 19:28:57 +00:00
|
|
|
func normalizePaths(args []string) ([]string, error) {
|
2022-09-08 20:30:51 +00:00
|
|
|
var paths []string
|
|
|
|
|
|
|
|
for i := 0; i < len(args); i++ {
|
|
|
|
absolutePath, err := filepath.Abs(args[i])
|
|
|
|
if err != nil {
|
2022-09-09 19:28:57 +00:00
|
|
|
return nil, err
|
2022-09-08 20:30:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
paths = append(paths, absolutePath)
|
|
|
|
}
|
|
|
|
|
2022-09-09 19:28:57 +00:00
|
|
|
return paths, nil
|
2022-09-08 15:57:59 +00:00
|
|
|
}
|