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-09-24 22:17:56 +00:00
|
|
|
func containsCaseIntensitive(a string, b string) bool {
|
|
|
|
return strings.Contains(
|
|
|
|
strings.ToLower(a),
|
|
|
|
strings.ToLower(b),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-09-18 18:07:46 +00:00
|
|
|
func getFirstFile(path string) (string, error) {
|
2022-09-22 02:24:05 +00:00
|
|
|
re := regexp.MustCompile(`(.+)([0-9]{3})(\..+)`)
|
2022-09-18 18:07:46 +00:00
|
|
|
|
|
|
|
split := re.FindAllStringSubmatch(path, -1)
|
|
|
|
|
|
|
|
if len(split) < 1 || len(split[0]) < 3 {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
base := split[0][1]
|
|
|
|
number := 1
|
|
|
|
extension := split[0][3]
|
|
|
|
|
|
|
|
fileName := fmt.Sprintf("%v%.3d%v", base, number, extension)
|
|
|
|
|
|
|
|
nextFile, err := checkNextFile(fileName)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !nextFile {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return fileName, nil
|
|
|
|
}
|
|
|
|
|
2022-09-17 16:07:37 +00:00
|
|
|
func getNextFile(path string) (string, error) {
|
2022-09-22 02:24:05 +00:00
|
|
|
re := regexp.MustCompile(`(.+)([0-9]{3})(\..+)`)
|
2022-09-17 16:07:37 +00:00
|
|
|
|
|
|
|
split := re.FindAllStringSubmatch(path, -1)
|
|
|
|
|
2022-09-17 18:12:22 +00:00
|
|
|
if len(split) < 1 || len(split[0]) < 3 {
|
2022-09-17 17:42:25 +00:00
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
2022-09-17 16:07:37 +00:00
|
|
|
base := split[0][1]
|
|
|
|
number, err := strconv.Atoi(split[0][2])
|
|
|
|
if err != nil {
|
2022-09-17 17:42:25 +00:00
|
|
|
return "", err
|
2022-09-17 16:07:37 +00:00
|
|
|
}
|
|
|
|
extension := split[0][3]
|
|
|
|
|
|
|
|
incremented := number + 1
|
|
|
|
|
|
|
|
fileName := fmt.Sprintf("%v%.3d%v", base, incremented, extension)
|
|
|
|
|
|
|
|
nextFile, err := checkNextFile(fileName)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !nextFile {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return fileName, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkNextFile(path string) (bool, error) {
|
|
|
|
if _, err := os.Stat(path); err == nil {
|
|
|
|
return true, nil
|
|
|
|
} else if errors.Is(err, os.ErrNotExist) {
|
|
|
|
return false, nil
|
|
|
|
} else {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-09 22:21:03 +00:00
|
|
|
func checkIfImage(path string) (bool, error) {
|
|
|
|
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-10 00:00:02 +00:00
|
|
|
if filetype.IsImage(head) {
|
2022-09-09 22:21:03 +00:00
|
|
|
return true, nil
|
|
|
|
}
|
2022-09-10 00:00:02 +00:00
|
|
|
|
|
|
|
return false, nil
|
2022-09-09 22:21:03 +00:00
|
|
|
}
|
|
|
|
|
2022-09-08 18:12:50 +00:00
|
|
|
func getFiles(path string) ([]string, error) {
|
|
|
|
var paths []string
|
2022-09-08 15:57:59 +00:00
|
|
|
|
2022-09-08 18:12:50 +00:00
|
|
|
err := filepath.WalkDir(path, func(p string, info os.DirEntry, err error) error {
|
2022-09-24 22:17:56 +00:00
|
|
|
switch {
|
|
|
|
case info.IsDir() && p != path:
|
2022-09-08 18:12:50 +00:00
|
|
|
return filepath.SkipDir
|
2022-09-24 22:17:56 +00:00
|
|
|
case Filter != "":
|
|
|
|
absolutePath, err := filepath.Abs(p)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if containsCaseIntensitive(p, Filter) {
|
|
|
|
paths = append(paths, absolutePath)
|
|
|
|
}
|
|
|
|
default:
|
2022-09-08 20:30:51 +00:00
|
|
|
absolutePath, err := filepath.Abs(p)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
paths = append(paths, absolutePath)
|
2022-09-08 18:12:50 +00:00
|
|
|
}
|
2022-09-08 20:30:51 +00:00
|
|
|
|
2022-09-08 18:12:50 +00:00
|
|
|
return err
|
|
|
|
})
|
2022-09-08 15:57:59 +00:00
|
|
|
if err != nil {
|
2022-09-08 18:12:50 +00:00
|
|
|
return nil, err
|
2022-09-08 15:57:59 +00:00
|
|
|
}
|
|
|
|
|
2022-09-08 18:12:50 +00:00
|
|
|
return paths, nil
|
2022-09-08 15:57:59 +00:00
|
|
|
}
|
|
|
|
|
2022-09-08 17:12:58 +00:00
|
|
|
func getFilesRecursive(path string) ([]string, error) {
|
|
|
|
var paths []string
|
|
|
|
|
|
|
|
err := filepath.WalkDir(path, func(p string, info os.DirEntry, err error) error {
|
2022-09-24 22:17:56 +00:00
|
|
|
switch {
|
|
|
|
case Filter != "" && !info.IsDir():
|
2022-09-08 20:30:51 +00:00
|
|
|
absolutePath, err := filepath.Abs(p)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-09-24 22:17:56 +00:00
|
|
|
|
|
|
|
if containsCaseIntensitive(p, Filter) {
|
|
|
|
paths = append(paths, absolutePath)
|
|
|
|
}
|
|
|
|
case Filter == "" && !info.IsDir():
|
|
|
|
absolutePath, err := filepath.Abs(p)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-09-08 20:30:51 +00:00
|
|
|
paths = append(paths, absolutePath)
|
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-08 18:12:50 +00:00
|
|
|
return paths, nil
|
2022-09-08 17:12:58 +00:00
|
|
|
}
|
|
|
|
|
2022-09-16 18:45:33 +00:00
|
|
|
func getFileList(paths []string) ([]string, error) {
|
2022-09-08 17:12:58 +00:00
|
|
|
fileList := []string{}
|
2022-09-08 15:57:59 +00:00
|
|
|
|
2022-09-16 18:45:33 +00:00
|
|
|
for i := 0; i < len(paths); i++ {
|
2022-09-08 17:12:58 +00:00
|
|
|
if Recursive {
|
2022-09-16 18:45:33 +00:00
|
|
|
f, err := getFilesRecursive(paths[i])
|
2022-09-08 17:12:58 +00:00
|
|
|
if err != nil {
|
2022-09-08 18:12:50 +00:00
|
|
|
return nil, err
|
2022-09-08 17:12:58 +00:00
|
|
|
}
|
2022-09-08 20:30:51 +00:00
|
|
|
|
2022-09-08 17:12:58 +00:00
|
|
|
fileList = append(fileList, f...)
|
|
|
|
} else {
|
2022-09-16 18:45:33 +00:00
|
|
|
f, err := getFiles(paths[i])
|
2022-09-08 17:12:58 +00:00
|
|
|
if err != nil {
|
2022-09-08 18:12:50 +00:00
|
|
|
return nil, err
|
2022-09-08 17:12:58 +00:00
|
|
|
}
|
2022-09-08 20:30:51 +00:00
|
|
|
|
2022-09-08 17:12:58 +00:00
|
|
|
fileList = append(fileList, f...)
|
|
|
|
}
|
2022-09-08 15:57:59 +00:00
|
|
|
}
|
|
|
|
|
2022-09-08 18:12:50 +00:00
|
|
|
return fileList, nil
|
2022-09-08 17:12:58 +00:00
|
|
|
}
|
2022-09-08 15:57:59 +00:00
|
|
|
|
2022-09-16 18:45:33 +00:00
|
|
|
func pickFile(args []string) (string, error) {
|
|
|
|
fileList, err := getFileList(args)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2022-09-09 22:21:03 +00:00
|
|
|
rand.Seed(time.Now().UnixNano())
|
|
|
|
|
|
|
|
rand.Shuffle(len(fileList), func(i, j int) { fileList[i], fileList[j] = fileList[j], fileList[i] })
|
|
|
|
|
|
|
|
for i := 0; i < len(fileList); i++ {
|
2022-09-10 18:09:39 +00:00
|
|
|
filePath := fileList[i]
|
2022-09-09 22:21:03 +00:00
|
|
|
isImage, err := checkIfImage(filePath)
|
|
|
|
if err != nil {
|
2022-09-16 18:45:33 +00:00
|
|
|
return "", err
|
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-16 18:45:33 +00:00
|
|
|
err = errors.New("no images found")
|
2022-09-08 15:57:59 +00:00
|
|
|
|
2022-09-16 18:45:33 +00:00
|
|
|
return "", err
|
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
|
|
|
}
|