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 (
|
2023-01-19 18:07:15 +00:00
|
|
|
"encoding/json"
|
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-01-20 02:31:02 +00:00
|
|
|
"sort"
|
2022-10-28 23:32:03 +00:00
|
|
|
|
2022-09-08 15:57:59 +00:00
|
|
|
"math/rand"
|
|
|
|
"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
|
|
|
)
|
|
|
|
|
2022-10-20 19:22:01 +00:00
|
|
|
var (
|
2022-11-05 16:00:42 +00:00
|
|
|
ErrNoImagesFound = fmt.Errorf("no supported image formats found which match all criteria")
|
2022-10-31 20:18:47 +00:00
|
|
|
extensions = [6]string{".jpg", ".jpeg", ".png", ".gif", ".webp", ".bmp"}
|
2022-10-20 19:22:01 +00:00
|
|
|
)
|
|
|
|
|
2023-01-21 04:42:44 +00:00
|
|
|
type Index struct {
|
|
|
|
Mutex sync.RWMutex
|
|
|
|
List []string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *Index) GenerateCache(args []string) error {
|
|
|
|
filters := &Filters{}
|
|
|
|
|
|
|
|
i.Mutex.Lock()
|
|
|
|
i.List = []string{}
|
|
|
|
i.Mutex.Unlock()
|
|
|
|
|
|
|
|
fmt.Printf("%v | Preparing image cache...\n", time.Now().Format(LogDate))
|
|
|
|
_, err := pickFile(args, filters, "", i)
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-11-10 05:17:19 +00:00
|
|
|
type Dimensions struct {
|
|
|
|
Width int
|
|
|
|
Height int
|
|
|
|
}
|
|
|
|
|
2022-10-20 19:22:01 +00:00
|
|
|
type Files struct {
|
|
|
|
Mutex sync.Mutex
|
|
|
|
List map[string][]string
|
|
|
|
}
|
|
|
|
|
2023-01-19 18:07:15 +00:00
|
|
|
type ScanStats struct {
|
2022-10-20 16:17:40 +00:00
|
|
|
FilesMatched uint64
|
|
|
|
FilesSkipped uint64
|
|
|
|
DirectoriesMatched uint64
|
2022-10-20 19:22:01 +00:00
|
|
|
}
|
|
|
|
|
2023-01-21 04:42:44 +00:00
|
|
|
func (s *ScanStats) GetFilesTotal() uint64 {
|
|
|
|
return atomic.LoadUint64(&s.FilesMatched) + atomic.LoadUint64(&s.FilesSkipped)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ScanStats) IncrementFilesMatched() {
|
|
|
|
atomic.AddUint64(&s.FilesMatched, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ScanStats) GetFilesMatched() uint64 {
|
|
|
|
return atomic.LoadUint64(&s.FilesMatched)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ScanStats) IncrementFilesSkipped() {
|
|
|
|
atomic.AddUint64(&s.FilesSkipped, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ScanStats) GetFilesSkipped() uint64 {
|
|
|
|
return atomic.LoadUint64(&s.FilesSkipped)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ScanStats) IncrementDirectoriesMatched() {
|
|
|
|
atomic.AddUint64(&s.DirectoriesMatched, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ScanStats) GetDirectoriesMatched() uint64 {
|
|
|
|
return atomic.LoadUint64(&s.DirectoriesMatched)
|
2023-01-19 18:07:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type ServeStats struct {
|
2023-01-21 04:42:44 +00:00
|
|
|
Mutex sync.RWMutex
|
|
|
|
List []string
|
|
|
|
Count map[string]uint64
|
|
|
|
Size map[string]string
|
|
|
|
Times map[string][]string
|
2023-01-19 18:07:15 +00:00
|
|
|
}
|
|
|
|
|
2023-01-21 04:42:44 +00:00
|
|
|
type TimesServed struct {
|
|
|
|
File string
|
|
|
|
Served uint64
|
|
|
|
Size string
|
|
|
|
Times []string
|
2023-01-19 18:07:15 +00:00
|
|
|
}
|
|
|
|
|
2023-01-21 00:14:07 +00:00
|
|
|
func (s *ServeStats) IncrementCounter(image string, timestamp time.Time, filesize string) {
|
2023-01-21 04:42:44 +00:00
|
|
|
s.Mutex.Lock()
|
2023-01-20 02:31:02 +00:00
|
|
|
|
2023-01-21 04:42:44 +00:00
|
|
|
s.Count[image]++
|
2023-01-19 18:07:15 +00:00
|
|
|
|
2023-01-21 04:42:44 +00:00
|
|
|
s.Times[image] = append(s.Times[image], timestamp.Format(LogDate))
|
2023-01-21 00:14:07 +00:00
|
|
|
|
2023-01-21 04:42:44 +00:00
|
|
|
_, exists := s.Size[image]
|
2023-01-21 00:14:07 +00:00
|
|
|
if !exists {
|
2023-01-21 04:42:44 +00:00
|
|
|
s.Size[image] = filesize
|
2023-01-21 00:14:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !contains(s.List, image) {
|
|
|
|
s.List = append(s.List, image)
|
2023-01-19 18:07:15 +00:00
|
|
|
}
|
2023-01-21 04:42:44 +00:00
|
|
|
|
|
|
|
s.Mutex.Unlock()
|
2023-01-19 18:07:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ServeStats) ListImages() ([]byte, error) {
|
2023-01-21 04:42:44 +00:00
|
|
|
s.Mutex.RLock()
|
2023-01-19 18:07:15 +00:00
|
|
|
|
2023-01-21 00:14:07 +00:00
|
|
|
sortedList := s.List
|
2023-01-20 02:31:02 +00:00
|
|
|
|
|
|
|
sort.SliceStable(sortedList, func(p, q int) bool {
|
|
|
|
return sortedList[p] < sortedList[q]
|
|
|
|
})
|
|
|
|
|
2023-01-21 04:42:44 +00:00
|
|
|
a := []TimesServed{}
|
|
|
|
|
2023-01-21 00:14:07 +00:00
|
|
|
for _, image := range s.List {
|
2023-01-21 04:42:44 +00:00
|
|
|
a = append(a, TimesServed{image, s.Count[image], s.Size[image], s.Times[image]})
|
2023-01-19 18:07:15 +00:00
|
|
|
}
|
|
|
|
|
2023-01-21 04:42:44 +00:00
|
|
|
s.Mutex.RUnlock()
|
|
|
|
|
2023-01-20 02:31:02 +00:00
|
|
|
r, err := json.MarshalIndent(a, "", " ")
|
2023-01-19 18:07:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return []byte{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return r, nil
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-01-19 18:07:15 +00:00
|
|
|
func contains(s []string, e string) bool {
|
|
|
|
for _, a := range s {
|
|
|
|
if a == e {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-10-25 05:11:55 +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])
|
|
|
|
}
|
|
|
|
|
2022-11-10 05:17:19 +00:00
|
|
|
func getImageDimensions(path string) (*Dimensions, error) {
|
2022-10-28 23:32:03 +00:00
|
|
|
file, err := os.Open(path)
|
2022-11-10 06:29:06 +00:00
|
|
|
switch {
|
|
|
|
case errors.Is(err, os.ErrNotExist):
|
2022-11-10 06:26:21 +00:00
|
|
|
return &Dimensions{}, nil
|
2022-11-10 06:29:06 +00:00
|
|
|
case err != nil:
|
2022-11-10 05:17:19 +00:00
|
|
|
return &Dimensions{}, err
|
2022-11-05 16:00:42 +00:00
|
|
|
}
|
2022-10-28 23:32:03 +00:00
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
myImage, _, err := image.DecodeConfig(file)
|
2022-11-10 06:29:06 +00:00
|
|
|
switch {
|
|
|
|
case errors.Is(err, image.ErrFormat):
|
2022-11-10 05:17:19 +00:00
|
|
|
return &Dimensions{Width: 0, Height: 0}, nil
|
2022-11-10 06:29:06 +00:00
|
|
|
case err != nil:
|
2022-11-10 05:17:19 +00:00
|
|
|
return &Dimensions{}, err
|
2022-10-28 23:32:03 +00:00
|
|
|
}
|
|
|
|
|
2022-11-10 05:17:19 +00:00
|
|
|
return &Dimensions{Width: myImage.Width, Height: myImage.Height}, nil
|
2022-10-28 23:32:03 +00:00
|
|
|
}
|
|
|
|
|
2022-10-23 21:16:40 +00:00
|
|
|
func preparePath(path string) string {
|
2022-10-23 21:29:58 +00:00
|
|
|
if runtime.GOOS == "windows" {
|
2022-10-23 21:47:23 +00:00
|
|
|
path = fmt.Sprintf("/%v", filepath.ToSlash(path))
|
2022-10-23 21:29:58 +00:00
|
|
|
}
|
2022-10-23 21:16:40 +00:00
|
|
|
|
|
|
|
return path
|
|
|
|
}
|
|
|
|
|
2023-01-19 18:07:15 +00:00
|
|
|
func appendPath(directory, path string, files *Files, stats *ScanStats) error {
|
2022-10-31 20:18:47 +00:00
|
|
|
// If caching, only check image types once, during the initial scan, to speed up future pickFile() calls
|
2022-10-28 22:19:04 +00:00
|
|
|
if Cache {
|
|
|
|
image, err := isImage(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !image {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-20 19:22:01 +00:00
|
|
|
files.Mutex.Lock()
|
|
|
|
files.List[directory] = append(files.List[directory], path)
|
2022-11-02 14:10:55 +00:00
|
|
|
files.Mutex.Unlock()
|
2022-10-28 22:19:04 +00:00
|
|
|
|
2022-11-02 14:17:07 +00:00
|
|
|
stats.IncrementFilesMatched()
|
|
|
|
|
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 {
|
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() {
|
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 19:22:01 +00:00
|
|
|
stats.IncrementFilesSkipped()
|
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() {
|
2022-10-20 00:27:11 +00:00
|
|
|
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
|
|
|
) {
|
2022-10-28 22:19:04 +00:00
|
|
|
err := appendPath(directory, path, files, stats)
|
|
|
|
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
|
|
|
|
2022-10-20 19:22:01 +00:00
|
|
|
stats.IncrementFilesSkipped()
|
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
|
|
|
|
2022-10-28 22:19:04 +00:00
|
|
|
err = appendPath(directory, path, files, stats)
|
|
|
|
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-21 04:42:44 +00:00
|
|
|
func getNewFile(paths []string, filters *Filters, sortOrder string, regexes *Regexes, index *Index) (string, error) {
|
|
|
|
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
|
|
|
|
2022-11-10 16:09:39 +00:00
|
|
|
path, err := splitPath(filePath, regexes)
|
2022-09-18 18:07:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2022-11-05 16:00:42 +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 {
|
|
|
|
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 == "" {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-11-10 16:09:39 +00:00
|
|
|
func getNextFile(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":
|
2022-11-05 16:17:31 +00:00
|
|
|
path.Increment()
|
2022-11-05 16:00:42 +00:00
|
|
|
case sortOrder == "desc":
|
2022-11-05 16:17:31 +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
|
|
|
|
}
|
|
|
|
|
2022-11-10 16:09:39 +00:00
|
|
|
func splitPath(path string, regexes *Regexes) (*Path, error) {
|
2022-10-18 21:46:55 +00:00
|
|
|
p := Path{}
|
|
|
|
var err error
|
|
|
|
|
2022-11-10 16:09:39 +00:00
|
|
|
split := regexes.Filename.FindAllStringSubmatch(path, -1)
|
2022-10-18 18:42:32 +00:00
|
|
|
|
|
|
|
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-20 19:50:42 +00:00
|
|
|
|
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 18:42:32 +00:00
|
|
|
var fileName string
|
|
|
|
|
2022-10-25 05:06:57 +00:00
|
|
|
for _, extension := range extensions {
|
|
|
|
fileName = fmt.Sprintf("%v%.3d%v", p.Base, p.Number, extension)
|
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
|
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 {
|
|
|
|
case Verbose && !matchesPrefix:
|
2022-10-20 22:55:25 +00:00
|
|
|
fmt.Printf("%v | Error: Failed to serve file outside specified path(s): %v\n",
|
2022-11-10 05:17:19 +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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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)
|
2022-11-10 06:29:06 +00:00
|
|
|
switch {
|
|
|
|
case errors.Is(err, os.ErrNotExist):
|
2022-11-10 06:26:21 +00:00
|
|
|
return false, nil
|
2022-11-10 06:29:06 +00:00
|
|
|
case err != nil:
|
2022-09-09 22:21:03 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-01-19 18:07:15 +00:00
|
|
|
func getFiles(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 {
|
2022-09-24 23:59:10 +00:00
|
|
|
case !Recursive && info.IsDir() && p != path:
|
|
|
|
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)
|
|
|
|
concurrency.FileScans <- 1
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
defer func() {
|
|
|
|
<-concurrency.FileScans
|
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
|
|
|
|
err = appendPaths(p, files, filters, stats)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
|
|
|
}()
|
2022-10-20 16:17:40 +00:00
|
|
|
case info.IsDir():
|
2022-10-20 19:22:01 +00:00
|
|
|
stats.IncrementDirectoriesMatched()
|
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-19 18:07:15 +00:00
|
|
|
func getFileList(paths []string, files *Files, filters *Filters, stats *ScanStats, concurrency *Concurrency) {
|
2022-10-20 19:22:01 +00:00
|
|
|
var wg sync.WaitGroup
|
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 19:22:01 +00:00
|
|
|
wg.Add(1)
|
|
|
|
concurrency.DirectoryScans <- 1
|
|
|
|
|
|
|
|
go func(i int) {
|
|
|
|
defer func() {
|
|
|
|
<-concurrency.DirectoryScans
|
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
|
|
|
|
err := getFiles(paths[i], files, filters, stats, concurrency)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
|
|
|
}(i)
|
2022-09-25 01:28:34 +00:00
|
|
|
}
|
|
|
|
|
2022-10-20 19:22:01 +00:00
|
|
|
wg.Wait()
|
2022-09-25 01:28:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 19:22:01 +00:00
|
|
|
func prepareDirectories(files *Files, sort string) []string {
|
2022-09-25 01:28:34 +00:00
|
|
|
directories := []string{}
|
|
|
|
|
2022-10-20 19:22:01 +00:00
|
|
|
keys := make([]string, len(files.List))
|
2022-09-25 01:28:34 +00:00
|
|
|
|
|
|
|
i := 0
|
2022-10-20 19:22:01 +00:00
|
|
|
for k := range files.List {
|
2022-09-25 01:28:34 +00:00
|
|
|
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++ {
|
2022-10-20 19:22:01 +00:00
|
|
|
directories = append(directories, prepareDirectory(files.List[keys[i]])...)
|
2022-09-25 01:28:34 +00:00
|
|
|
}
|
2022-09-26 17:31:45 +00:00
|
|
|
} else {
|
2022-09-25 01:28:34 +00:00
|
|
|
for i := 0; i < len(keys); i++ {
|
2022-10-20 19:22:01 +00:00
|
|
|
directories = append(directories, files.List[keys[i]]...)
|
2022-09-25 01:28:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return directories
|
2022-09-08 17:12:58 +00:00
|
|
|
}
|
2022-09-08 15:57:59 +00:00
|
|
|
|
2023-01-21 04:42:44 +00:00
|
|
|
func pickFile(args []string, filters *Filters, sort string, index *Index) (string, error) {
|
2022-10-28 22:19:04 +00:00
|
|
|
var fileList []string
|
2022-10-25 14:40:27 +00:00
|
|
|
|
2023-01-21 04:42:44 +00:00
|
|
|
if Cache && filters.IsEmpty() && len(index.List) != 0 {
|
|
|
|
fileList = index.List
|
2022-10-28 22:19:04 +00:00
|
|
|
} else {
|
2022-11-11 15:44:47 +00:00
|
|
|
files := &Files{
|
|
|
|
List: make(map[string][]string),
|
|
|
|
}
|
2022-10-28 22:19:04 +00:00
|
|
|
|
2023-01-19 18:07:15 +00:00
|
|
|
stats := &ScanStats{
|
2022-11-11 15:44:47 +00:00
|
|
|
FilesMatched: 0,
|
|
|
|
FilesSkipped: 0,
|
|
|
|
DirectoriesMatched: 0,
|
|
|
|
}
|
2022-10-28 22:19:04 +00:00
|
|
|
|
2022-11-11 15:44:47 +00:00
|
|
|
concurrency := &Concurrency{
|
|
|
|
DirectoryScans: make(chan int, maxDirectoryScans),
|
|
|
|
FileScans: make(chan int, maxFileScans),
|
|
|
|
}
|
2022-10-28 22:19:04 +00:00
|
|
|
|
|
|
|
startTime := time.Now()
|
2022-11-11 15:44:47 +00:00
|
|
|
getFileList(args, files, filters, stats, concurrency)
|
2022-10-28 22:19:04 +00:00
|
|
|
runTime := time.Since(startTime)
|
|
|
|
|
|
|
|
if Verbose {
|
|
|
|
fmt.Printf("%v | Scanned %v/%v files across %v directories in %v\n",
|
2022-11-10 05:17:19 +00:00
|
|
|
time.Now().Format(LogDate),
|
2022-10-28 22:19:04 +00:00
|
|
|
stats.GetFilesMatched(),
|
|
|
|
stats.GetFilesTotal(),
|
|
|
|
stats.GetDirectoriesMatched(),
|
|
|
|
runTime,
|
|
|
|
)
|
|
|
|
}
|
2022-10-20 19:22:01 +00:00
|
|
|
|
2022-11-11 15:44:47 +00:00
|
|
|
fileList = prepareDirectories(files, sort)
|
2022-09-16 18:45:33 +00:00
|
|
|
|
2022-10-28 22:19:04 +00:00
|
|
|
if Cache {
|
2023-01-21 04:42:44 +00:00
|
|
|
index.Mutex.Lock()
|
|
|
|
index.List = append(index.List, fileList...)
|
|
|
|
index.Mutex.Unlock()
|
2022-10-28 22:19:04 +00:00
|
|
|
}
|
2023-01-21 04:42:44 +00:00
|
|
|
|
2022-10-20 16:17:40 +00:00
|
|
|
}
|
|
|
|
|
2022-10-29 15:41:03 +00:00
|
|
|
fileCount := len(fileList)
|
2022-11-05 16:00:42 +00:00
|
|
|
if fileCount == 0 {
|
|
|
|
return "", ErrNoImagesFound
|
|
|
|
}
|
2022-09-09 22:21:03 +00:00
|
|
|
|
2022-10-29 15:41:03 +00:00
|
|
|
r := rand.Intn(fileCount - 1)
|
2022-10-29 04:07:25 +00:00
|
|
|
|
|
|
|
for i := 0; i < fileCount; i++ {
|
2022-10-29 15:41:03 +00:00
|
|
|
if r >= (fileCount - 1) {
|
2022-10-29 04:07:25 +00:00
|
|
|
r = 0
|
|
|
|
} else {
|
|
|
|
r++
|
|
|
|
}
|
|
|
|
|
|
|
|
filePath := fileList[r]
|
2022-10-18 21:46:55 +00:00
|
|
|
|
2022-10-28 22:19:04 +00:00
|
|
|
if !Cache {
|
|
|
|
isImage, err := isImage(filePath)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if isImage {
|
|
|
|
return filePath, nil
|
|
|
|
}
|
2022-10-29 15:41:03 +00:00
|
|
|
|
|
|
|
continue
|
2022-09-09 22:21:03 +00:00
|
|
|
}
|
2022-10-28 22:19:04 +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
|
|
|
|
|
2022-10-25 05:06:57 +00:00
|
|
|
fmt.Println("Paths:")
|
|
|
|
|
2022-09-08 20:30:51 +00:00
|
|
|
for i := 0; i < len(args); i++ {
|
2022-10-25 05:06:57 +00:00
|
|
|
path, err := filepath.EvalSymlinks(args[i])
|
2022-09-08 20:30:51 +00:00
|
|
|
if err != nil {
|
2022-09-09 19:28:57 +00:00
|
|
|
return nil, err
|
2022-09-08 20:30:51 +00:00
|
|
|
}
|
|
|
|
|
2022-10-25 05:06:57 +00:00
|
|
|
absolutePath, err := filepath.Abs(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if (args[i]) != absolutePath {
|
|
|
|
fmt.Printf("%v (resolved to %v)\n", args[i], absolutePath)
|
|
|
|
} else {
|
|
|
|
fmt.Printf("%v\n", args[i])
|
|
|
|
}
|
|
|
|
|
2022-09-08 20:30:51 +00:00
|
|
|
paths = append(paths, absolutePath)
|
|
|
|
}
|
|
|
|
|
2022-10-25 05:06:57 +00:00
|
|
|
fmt.Println()
|
|
|
|
|
2022-09-09 19:28:57 +00:00
|
|
|
return paths, nil
|
2022-09-08 15:57:59 +00:00
|
|
|
}
|