All methods that might mutate state changed to unexported, and all fields set to unexported by default
This commit is contained in:
parent
2a6c6b48d4
commit
635df29fbc
164
cmd/files.go
164
cmd/files.go
|
@ -28,73 +28,86 @@ import (
|
||||||
"github.com/h2non/filetype"
|
"github.com/h2non/filetype"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
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 (
|
var (
|
||||||
ErrNoImagesFound = fmt.Errorf("no supported image formats found which match all criteria")
|
ErrNoImagesFound = fmt.Errorf("no supported image formats found which match all criteria")
|
||||||
extensions = [6]string{".jpg", ".jpeg", ".png", ".gif", ".webp", ".bmp"}
|
extensions = [6]string{".jpg", ".jpeg", ".png", ".gif", ".webp", ".bmp"}
|
||||||
)
|
)
|
||||||
|
|
||||||
type Dimensions struct {
|
type Dimensions struct {
|
||||||
Width int
|
width int
|
||||||
Height int
|
height int
|
||||||
}
|
}
|
||||||
|
|
||||||
type Files struct {
|
type Files struct {
|
||||||
Mutex sync.Mutex
|
mutex sync.Mutex
|
||||||
List map[string][]string
|
list map[string][]string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *Files) Append(directory, path string) {
|
func (f *Files) Append(directory, path string) {
|
||||||
f.Mutex.Lock()
|
f.mutex.Lock()
|
||||||
f.List[directory] = append(f.List[directory], path)
|
f.list[directory] = append(f.list[directory], path)
|
||||||
f.Mutex.Unlock()
|
f.mutex.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
type ScanStats struct {
|
type ScanStats struct {
|
||||||
FilesMatched uint64
|
filesMatched uint64
|
||||||
FilesSkipped uint64
|
filesSkipped uint64
|
||||||
DirectoriesMatched uint64
|
directoriesMatched uint64
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *ScanStats) GetFilesTotal() uint64 {
|
func (s *ScanStats) FilesTotal() uint64 {
|
||||||
return atomic.LoadUint64(&s.FilesMatched) + atomic.LoadUint64(&s.FilesSkipped)
|
return atomic.LoadUint64(&s.filesMatched) + atomic.LoadUint64(&s.filesSkipped)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *ScanStats) IncrementFilesMatched() {
|
func (s *ScanStats) incrementFilesMatched() {
|
||||||
atomic.AddUint64(&s.FilesMatched, 1)
|
atomic.AddUint64(&s.filesMatched, 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *ScanStats) GetFilesMatched() uint64 {
|
func (s *ScanStats) FilesMatched() uint64 {
|
||||||
return atomic.LoadUint64(&s.FilesMatched)
|
return atomic.LoadUint64(&s.filesMatched)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *ScanStats) IncrementFilesSkipped() {
|
func (s *ScanStats) incrementFilesSkipped() {
|
||||||
atomic.AddUint64(&s.FilesSkipped, 1)
|
atomic.AddUint64(&s.filesSkipped, 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *ScanStats) GetFilesSkipped() uint64 {
|
func (s *ScanStats) FilesSkipped() uint64 {
|
||||||
return atomic.LoadUint64(&s.FilesSkipped)
|
return atomic.LoadUint64(&s.filesSkipped)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *ScanStats) IncrementDirectoriesMatched() {
|
func (s *ScanStats) incrementDirectoriesMatched() {
|
||||||
atomic.AddUint64(&s.DirectoriesMatched, 1)
|
atomic.AddUint64(&s.directoriesMatched, 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *ScanStats) GetDirectoriesMatched() uint64 {
|
func (s *ScanStats) DirectoriesMatched() uint64 {
|
||||||
return atomic.LoadUint64(&s.DirectoriesMatched)
|
return atomic.LoadUint64(&s.directoriesMatched)
|
||||||
}
|
}
|
||||||
|
|
||||||
type Path struct {
|
type Path struct {
|
||||||
Base string
|
base string
|
||||||
Number int
|
number int
|
||||||
Extension string
|
extension string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Path) Increment() {
|
func (p *Path) Increment() {
|
||||||
p.Number = p.Number + 1
|
p.number = p.number + 1
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Path) Decrement() {
|
func (p *Path) Decrement() {
|
||||||
p.Number = p.Number - 1
|
p.number = p.number - 1
|
||||||
}
|
}
|
||||||
|
|
||||||
func contains(s []string, e string) bool {
|
func contains(s []string, e string) bool {
|
||||||
|
@ -124,7 +137,7 @@ func humanReadableSize(bytes int) string {
|
||||||
float64(bytes)/float64(div), "KMGTPE"[exp])
|
float64(bytes)/float64(div), "KMGTPE"[exp])
|
||||||
}
|
}
|
||||||
|
|
||||||
func getImageDimensions(path string) (*Dimensions, error) {
|
func imageDimensions(path string) (*Dimensions, error) {
|
||||||
file, err := os.Open(path)
|
file, err := os.Open(path)
|
||||||
switch {
|
switch {
|
||||||
case errors.Is(err, os.ErrNotExist):
|
case errors.Is(err, os.ErrNotExist):
|
||||||
|
@ -137,12 +150,12 @@ func getImageDimensions(path string) (*Dimensions, error) {
|
||||||
myImage, _, err := image.DecodeConfig(file)
|
myImage, _, err := image.DecodeConfig(file)
|
||||||
switch {
|
switch {
|
||||||
case errors.Is(err, image.ErrFormat):
|
case errors.Is(err, image.ErrFormat):
|
||||||
return &Dimensions{Width: 0, Height: 0}, nil
|
return &Dimensions{width: 0, height: 0}, nil
|
||||||
case err != nil:
|
case err != nil:
|
||||||
return &Dimensions{}, err
|
return &Dimensions{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return &Dimensions{Width: myImage.Width, Height: myImage.Height}, nil
|
return &Dimensions{width: myImage.Width, height: myImage.Height}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func preparePath(path string) string {
|
func preparePath(path string) string {
|
||||||
|
@ -154,7 +167,6 @@ func preparePath(path string) string {
|
||||||
}
|
}
|
||||||
|
|
||||||
func appendPath(directory, path string, files *Files, stats *ScanStats, shouldCache bool) error {
|
func appendPath(directory, path string, files *Files, stats *ScanStats, shouldCache bool) error {
|
||||||
// If caching, only check image types once, during the initial scan, to speed up future pickFile() calls
|
|
||||||
if shouldCache {
|
if shouldCache {
|
||||||
image, err := isImage(path)
|
image, err := isImage(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -168,7 +180,7 @@ func appendPath(directory, path string, files *Files, stats *ScanStats, shouldCa
|
||||||
|
|
||||||
files.Append(directory, path)
|
files.Append(directory, path)
|
||||||
|
|
||||||
stats.IncrementFilesMatched()
|
stats.incrementFilesMatched()
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -186,12 +198,12 @@ func appendPaths(path string, files *Files, filters *Filters, stats *ScanStats)
|
||||||
filename = strings.ToLower(filename)
|
filename = strings.ToLower(filename)
|
||||||
|
|
||||||
if filters.HasExcludes() {
|
if filters.HasExcludes() {
|
||||||
for i := 0; i < len(filters.Excludes); i++ {
|
for i := 0; i < len(filters.excludes); i++ {
|
||||||
if strings.Contains(
|
if strings.Contains(
|
||||||
filename,
|
filename,
|
||||||
filters.Excludes[i],
|
filters.excludes[i],
|
||||||
) {
|
) {
|
||||||
stats.IncrementFilesSkipped()
|
stats.incrementFilesSkipped()
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -199,10 +211,10 @@ func appendPaths(path string, files *Files, filters *Filters, stats *ScanStats)
|
||||||
}
|
}
|
||||||
|
|
||||||
if filters.HasIncludes() {
|
if filters.HasIncludes() {
|
||||||
for i := 0; i < len(filters.Includes); i++ {
|
for i := 0; i < len(filters.includes); i++ {
|
||||||
if strings.Contains(
|
if strings.Contains(
|
||||||
filename,
|
filename,
|
||||||
filters.Includes[i],
|
filters.includes[i],
|
||||||
) {
|
) {
|
||||||
err := appendPath(directory, path, files, stats, shouldCache)
|
err := appendPath(directory, path, files, stats, shouldCache)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -213,7 +225,7 @@ func appendPaths(path string, files *Files, filters *Filters, stats *ScanStats)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
stats.IncrementFilesSkipped()
|
stats.incrementFilesSkipped()
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -226,18 +238,18 @@ func appendPaths(path string, files *Files, filters *Filters, stats *ScanStats)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func getNewFile(paths []string, filters *Filters, sortOrder string, regexes *Regexes, index *Index) (string, error) {
|
func newFile(paths []string, filters *Filters, sortOrder string, Regexes *Regexes, index *Index) (string, error) {
|
||||||
filePath, err := pickFile(paths, filters, sortOrder, index)
|
filePath, err := pickFile(paths, filters, sortOrder, index)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", nil
|
return "", nil
|
||||||
}
|
}
|
||||||
|
|
||||||
path, err := splitPath(filePath, regexes)
|
path, err := splitPath(filePath, Regexes)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
path.Number = 1
|
path.number = 1
|
||||||
|
|
||||||
switch {
|
switch {
|
||||||
case sortOrder == "asc":
|
case sortOrder == "asc":
|
||||||
|
@ -270,8 +282,8 @@ func getNewFile(paths []string, filters *Filters, sortOrder string, regexes *Reg
|
||||||
return filePath, nil
|
return filePath, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func getNextFile(filePath, sortOrder string, regexes *Regexes) (string, error) {
|
func nextFile(filePath, sortOrder string, Regexes *Regexes) (string, error) {
|
||||||
path, err := splitPath(filePath, regexes)
|
path, err := splitPath(filePath, Regexes)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
@ -293,25 +305,25 @@ func getNextFile(filePath, sortOrder string, regexes *Regexes) (string, error) {
|
||||||
return fileName, err
|
return fileName, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func splitPath(path string, regexes *Regexes) (*Path, error) {
|
func splitPath(path string, Regexes *Regexes) (*Path, error) {
|
||||||
p := Path{}
|
p := Path{}
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
split := regexes.Filename.FindAllStringSubmatch(path, -1)
|
split := Regexes.filename.FindAllStringSubmatch(path, -1)
|
||||||
|
|
||||||
if len(split) < 1 || len(split[0]) < 3 {
|
if len(split) < 1 || len(split[0]) < 3 {
|
||||||
return &Path{}, nil
|
return &Path{}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
p.Base = split[0][1]
|
p.base = split[0][1]
|
||||||
|
|
||||||
p.Number, err = strconv.Atoi(split[0][2])
|
p.number, err = strconv.Atoi(split[0][2])
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return &Path{}, err
|
return &Path{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
p.Extension = split[0][3]
|
p.extension = split[0][3]
|
||||||
|
|
||||||
return &p, nil
|
return &p, nil
|
||||||
}
|
}
|
||||||
|
@ -320,7 +332,7 @@ func tryExtensions(p *Path) (string, error) {
|
||||||
var fileName string
|
var fileName string
|
||||||
|
|
||||||
for _, extension := range extensions {
|
for _, extension := range extensions {
|
||||||
fileName = fmt.Sprintf("%s%.3d%s", p.Base, p.Number, extension)
|
fileName = fmt.Sprintf("%s%.3d%s", p.base, p.number, extension)
|
||||||
|
|
||||||
exists, err := fileExists(fileName)
|
exists, err := fileExists(fileName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -387,7 +399,7 @@ func isImage(path string) (bool, error) {
|
||||||
return filetype.IsImage(head), nil
|
return filetype.IsImage(head), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func getFiles(path string, files *Files, filters *Filters, stats *ScanStats, concurrency *Concurrency) error {
|
func scanPath(path string, files *Files, filters *Filters, stats *ScanStats, concurrency *Concurrency) error {
|
||||||
var wg sync.WaitGroup
|
var wg sync.WaitGroup
|
||||||
|
|
||||||
err := filepath.WalkDir(path, func(p string, info os.DirEntry, err error) error {
|
err := filepath.WalkDir(path, func(p string, info os.DirEntry, err error) error {
|
||||||
|
@ -400,11 +412,11 @@ func getFiles(path string, files *Files, filters *Filters, stats *ScanStats, con
|
||||||
return filepath.SkipDir
|
return filepath.SkipDir
|
||||||
case !info.IsDir():
|
case !info.IsDir():
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
concurrency.FileScans <- 1
|
concurrency.fileScans <- 1
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
defer func() {
|
defer func() {
|
||||||
<-concurrency.FileScans
|
<-concurrency.fileScans
|
||||||
wg.Done()
|
wg.Done()
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
@ -414,7 +426,7 @@ func getFiles(path string, files *Files, filters *Filters, stats *ScanStats, con
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
case info.IsDir():
|
case info.IsDir():
|
||||||
stats.IncrementDirectoriesMatched()
|
stats.incrementDirectoriesMatched()
|
||||||
}
|
}
|
||||||
|
|
||||||
return err
|
return err
|
||||||
|
@ -429,27 +441,27 @@ func getFiles(path string, files *Files, filters *Filters, stats *ScanStats, con
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func getFileList(paths []string, filters *Filters, sort string, index *Index) ([]string, bool) {
|
func fileList(paths []string, filters *Filters, sort string, index *Index) ([]string, bool) {
|
||||||
if Cache && filters.IsEmpty() && !index.IsEmpty() {
|
if Cache && filters.IsEmpty() && !index.IsEmpty() {
|
||||||
return index.Get(), true
|
return index.Index(), true
|
||||||
}
|
}
|
||||||
|
|
||||||
var fileList []string
|
var fileList []string
|
||||||
|
|
||||||
files := &Files{
|
files := &Files{
|
||||||
Mutex: sync.Mutex{},
|
mutex: sync.Mutex{},
|
||||||
List: make(map[string][]string),
|
list: make(map[string][]string),
|
||||||
}
|
}
|
||||||
|
|
||||||
stats := &ScanStats{
|
stats := &ScanStats{
|
||||||
FilesMatched: 0,
|
filesMatched: 0,
|
||||||
FilesSkipped: 0,
|
filesSkipped: 0,
|
||||||
DirectoriesMatched: 0,
|
directoriesMatched: 0,
|
||||||
}
|
}
|
||||||
|
|
||||||
concurrency := &Concurrency{
|
concurrency := &Concurrency{
|
||||||
DirectoryScans: make(chan int, maxDirectoryScans),
|
directoryScans: make(chan int, maxDirectoryScans),
|
||||||
FileScans: make(chan int, maxFileScans),
|
fileScans: make(chan int, maxFileScans),
|
||||||
}
|
}
|
||||||
|
|
||||||
var wg sync.WaitGroup
|
var wg sync.WaitGroup
|
||||||
|
@ -458,15 +470,15 @@ func getFileList(paths []string, filters *Filters, sort string, index *Index) ([
|
||||||
|
|
||||||
for i := 0; i < len(paths); i++ {
|
for i := 0; i < len(paths); i++ {
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
concurrency.DirectoryScans <- 1
|
concurrency.directoryScans <- 1
|
||||||
|
|
||||||
go func(i int) {
|
go func(i int) {
|
||||||
defer func() {
|
defer func() {
|
||||||
<-concurrency.DirectoryScans
|
<-concurrency.directoryScans
|
||||||
wg.Done()
|
wg.Done()
|
||||||
}()
|
}()
|
||||||
|
|
||||||
err := getFiles(paths[i], files, filters, stats, concurrency)
|
err := scanPath(paths[i], files, filters, stats, concurrency)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
}
|
}
|
||||||
|
@ -480,15 +492,15 @@ func getFileList(paths []string, filters *Filters, sort string, index *Index) ([
|
||||||
if Verbose {
|
if Verbose {
|
||||||
fmt.Printf("%s | Indexed %d/%d files across %d directories in %s\n",
|
fmt.Printf("%s | Indexed %d/%d files across %d directories in %s\n",
|
||||||
time.Now().Format(LogDate),
|
time.Now().Format(LogDate),
|
||||||
stats.GetFilesMatched(),
|
stats.FilesMatched(),
|
||||||
stats.GetFilesTotal(),
|
stats.FilesTotal(),
|
||||||
stats.GetDirectoriesMatched(),
|
stats.DirectoriesMatched(),
|
||||||
time.Since(startTime),
|
time.Since(startTime),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
if Cache && filters.IsEmpty() {
|
if Cache && filters.IsEmpty() {
|
||||||
index.Set(fileList)
|
index.setIndex(fileList)
|
||||||
}
|
}
|
||||||
|
|
||||||
return fileList, false
|
return fileList, false
|
||||||
|
@ -515,21 +527,21 @@ func prepareDirectory(directory []string) []string {
|
||||||
func prepareDirectories(files *Files, sort string) []string {
|
func prepareDirectories(files *Files, sort string) []string {
|
||||||
directories := []string{}
|
directories := []string{}
|
||||||
|
|
||||||
keys := make([]string, len(files.List))
|
keys := make([]string, len(files.list))
|
||||||
|
|
||||||
i := 0
|
i := 0
|
||||||
for k := range files.List {
|
for k := range files.list {
|
||||||
keys[i] = k
|
keys[i] = k
|
||||||
i++
|
i++
|
||||||
}
|
}
|
||||||
|
|
||||||
if sort == "asc" || sort == "desc" {
|
if sort == "asc" || sort == "desc" {
|
||||||
for i := 0; i < len(keys); i++ {
|
for i := 0; i < len(keys); i++ {
|
||||||
directories = append(directories, prepareDirectory(files.List[keys[i]])...)
|
directories = append(directories, prepareDirectory(files.list[keys[i]])...)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
for i := 0; i < len(keys); i++ {
|
for i := 0; i < len(keys); i++ {
|
||||||
directories = append(directories, files.List[keys[i]]...)
|
directories = append(directories, files.list[keys[i]]...)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -537,7 +549,7 @@ func prepareDirectories(files *Files, sort string) []string {
|
||||||
}
|
}
|
||||||
|
|
||||||
func pickFile(args []string, filters *Filters, sort string, index *Index) (string, error) {
|
func pickFile(args []string, filters *Filters, sort string, index *Index) (string, error) {
|
||||||
fileList, fromCache := getFileList(args, filters, sort, index)
|
fileList, fromCache := fileList(args, filters, sort, index)
|
||||||
|
|
||||||
fileCount := len(fileList)
|
fileCount := len(fileList)
|
||||||
if fileCount == 0 {
|
if fileCount == 0 {
|
||||||
|
|
13
cmd/root.go
13
cmd/root.go
|
@ -11,19 +11,6 @@ import (
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
|
||||||
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 Cache bool
|
var Cache bool
|
||||||
var Debug bool
|
var Debug bool
|
||||||
var Filter bool
|
var Filter bool
|
||||||
|
|
|
@ -10,7 +10,7 @@ import (
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
|
||||||
var Version = "0.33.4"
|
var Version = "0.34.0"
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
rootCmd.AddCommand(versionCmd)
|
rootCmd.AddCommand(versionCmd)
|
||||||
|
|
182
cmd/web.go
182
cmd/web.go
|
@ -32,14 +32,14 @@ const (
|
||||||
)
|
)
|
||||||
|
|
||||||
type Regexes struct {
|
type Regexes struct {
|
||||||
Alphanumeric *regexp.Regexp
|
alphanumeric *regexp.Regexp
|
||||||
Filename *regexp.Regexp
|
filename *regexp.Regexp
|
||||||
Units *regexp.Regexp
|
units *regexp.Regexp
|
||||||
}
|
}
|
||||||
|
|
||||||
type Filters struct {
|
type Filters struct {
|
||||||
Includes []string
|
includes []string
|
||||||
Excludes []string
|
excludes []string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *Filters) IsEmpty() bool {
|
func (f *Filters) IsEmpty() bool {
|
||||||
|
@ -47,99 +47,99 @@ func (f *Filters) IsEmpty() bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *Filters) HasIncludes() bool {
|
func (f *Filters) HasIncludes() bool {
|
||||||
return len(f.Includes) != 0
|
return len(f.includes) != 0
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *Filters) GetIncludes() string {
|
func (f *Filters) Includes() string {
|
||||||
return strings.Join(f.Includes, ",")
|
return strings.Join(f.includes, ",")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *Filters) HasExcludes() bool {
|
func (f *Filters) HasExcludes() bool {
|
||||||
return len(f.Excludes) != 0
|
return len(f.excludes) != 0
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *Filters) GetExcludes() string {
|
func (f *Filters) Excludes() string {
|
||||||
return strings.Join(f.Excludes, ",")
|
return strings.Join(f.excludes, ",")
|
||||||
}
|
}
|
||||||
|
|
||||||
type Index struct {
|
type Index struct {
|
||||||
Mutex sync.RWMutex
|
mutex sync.RWMutex
|
||||||
List []string
|
list []string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *Index) Get() []string {
|
func (i *Index) Index() []string {
|
||||||
i.Mutex.RLock()
|
i.mutex.RLock()
|
||||||
val := i.List
|
val := i.list
|
||||||
i.Mutex.RUnlock()
|
i.mutex.RUnlock()
|
||||||
|
|
||||||
return val
|
return val
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *Index) Set(val []string) {
|
func (i *Index) setIndex(val []string) {
|
||||||
i.Mutex.Lock()
|
i.mutex.Lock()
|
||||||
i.List = val
|
i.list = val
|
||||||
i.Mutex.Unlock()
|
i.mutex.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *Index) GenerateCache(args []string) {
|
func (i *Index) generateCache(args []string) {
|
||||||
i.Mutex.Lock()
|
i.mutex.Lock()
|
||||||
i.List = []string{}
|
i.list = []string{}
|
||||||
i.Mutex.Unlock()
|
i.mutex.Unlock()
|
||||||
|
|
||||||
getFileList(args, &Filters{}, "", i)
|
fileList(args, &Filters{}, "", i)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *Index) IsEmpty() bool {
|
func (i *Index) IsEmpty() bool {
|
||||||
i.Mutex.RLock()
|
i.mutex.RLock()
|
||||||
length := len(i.List)
|
length := len(i.list)
|
||||||
i.Mutex.RUnlock()
|
i.mutex.RUnlock()
|
||||||
|
|
||||||
return length == 0
|
return length == 0
|
||||||
}
|
}
|
||||||
|
|
||||||
type ServeStats struct {
|
type ServeStats struct {
|
||||||
Mutex sync.RWMutex
|
mutex sync.RWMutex
|
||||||
List []string
|
list []string
|
||||||
Count map[string]uint64
|
count map[string]uint64
|
||||||
Size map[string]string
|
size map[string]string
|
||||||
Times map[string][]string
|
times map[string][]string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *ServeStats) IncrementCounter(image string, timestamp time.Time, filesize string) {
|
func (s *ServeStats) incrementCounter(image string, timestamp time.Time, filesize string) {
|
||||||
s.Mutex.Lock()
|
s.mutex.Lock()
|
||||||
|
|
||||||
s.Count[image]++
|
s.count[image]++
|
||||||
|
|
||||||
s.Times[image] = append(s.Times[image], timestamp.Format(LogDate))
|
s.times[image] = append(s.times[image], timestamp.Format(LogDate))
|
||||||
|
|
||||||
_, exists := s.Size[image]
|
_, exists := s.size[image]
|
||||||
if !exists {
|
if !exists {
|
||||||
s.Size[image] = filesize
|
s.size[image] = filesize
|
||||||
}
|
}
|
||||||
|
|
||||||
if !contains(s.List, image) {
|
if !contains(s.list, image) {
|
||||||
s.List = append(s.List, image)
|
s.list = append(s.list, image)
|
||||||
}
|
}
|
||||||
|
|
||||||
s.Mutex.Unlock()
|
s.mutex.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *ServeStats) ListImages() ([]byte, error) {
|
func (s *ServeStats) ListImages() ([]byte, error) {
|
||||||
s.Mutex.RLock()
|
s.mutex.RLock()
|
||||||
|
|
||||||
sortedList := s.List
|
sortedList := s.list
|
||||||
|
|
||||||
sort.SliceStable(sortedList, func(p, q int) bool {
|
sort.SliceStable(sortedList, func(p, q int) bool {
|
||||||
return sortedList[p] < sortedList[q]
|
return sortedList[p] < sortedList[q]
|
||||||
})
|
})
|
||||||
|
|
||||||
a := []TimesServed{}
|
a := []timesServed{}
|
||||||
|
|
||||||
for _, image := range s.List {
|
for _, image := range s.list {
|
||||||
a = append(a, TimesServed{image, s.Count[image], s.Size[image], s.Times[image]})
|
a = append(a, timesServed{image, s.count[image], s.size[image], s.times[image]})
|
||||||
}
|
}
|
||||||
|
|
||||||
s.Mutex.RUnlock()
|
s.mutex.RUnlock()
|
||||||
|
|
||||||
r, err := json.MarshalIndent(a, "", " ")
|
r, err := json.MarshalIndent(a, "", " ")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -149,7 +149,7 @@ func (s *ServeStats) ListImages() ([]byte, error) {
|
||||||
return r, nil
|
return r, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
type TimesServed struct {
|
type timesServed struct {
|
||||||
File string
|
File string
|
||||||
Served uint64
|
Served uint64
|
||||||
Size string
|
Size string
|
||||||
|
@ -184,10 +184,10 @@ func notFound(w http.ResponseWriter, r *http.Request, filePath string) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func getRefreshInterval(r *http.Request, regexes *Regexes) (int64, string) {
|
func refreshInterval(r *http.Request, Regexes *Regexes) (int64, string) {
|
||||||
refreshInterval := r.URL.Query().Get("refresh")
|
refreshInterval := r.URL.Query().Get("refresh")
|
||||||
|
|
||||||
if !regexes.Units.MatchString(refreshInterval) {
|
if !Regexes.units.MatchString(refreshInterval) {
|
||||||
return 0, "0ms"
|
return 0, "0ms"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -201,7 +201,7 @@ func getRefreshInterval(r *http.Request, regexes *Regexes) (int64, string) {
|
||||||
return durationInMs, refreshInterval
|
return durationInMs, refreshInterval
|
||||||
}
|
}
|
||||||
|
|
||||||
func getSortOrder(r *http.Request) string {
|
func sortOrder(r *http.Request) string {
|
||||||
sortOrder := r.URL.Query().Get("sort")
|
sortOrder := r.URL.Query().Get("sort")
|
||||||
if sortOrder == "asc" || sortOrder == "desc" {
|
if sortOrder == "asc" || sortOrder == "desc" {
|
||||||
return sortOrder
|
return sortOrder
|
||||||
|
@ -210,7 +210,7 @@ func getSortOrder(r *http.Request) string {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
func splitQueryParams(query string, regexes *Regexes) []string {
|
func splitQueryParams(query string, Regexes *Regexes) []string {
|
||||||
results := []string{}
|
results := []string{}
|
||||||
|
|
||||||
if query == "" {
|
if query == "" {
|
||||||
|
@ -220,7 +220,7 @@ func splitQueryParams(query string, regexes *Regexes) []string {
|
||||||
params := strings.Split(query, ",")
|
params := strings.Split(query, ",")
|
||||||
|
|
||||||
for i := 0; i < len(params); i++ {
|
for i := 0; i < len(params); i++ {
|
||||||
if regexes.Alphanumeric.MatchString(params[i]) {
|
if Regexes.alphanumeric.MatchString(params[i]) {
|
||||||
results = append(results, strings.ToLower(params[i]))
|
results = append(results, strings.ToLower(params[i]))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -238,12 +238,12 @@ func generateQueryParams(filters *Filters, sortOrder, refreshInterval string) st
|
||||||
if Filter {
|
if Filter {
|
||||||
queryParams.WriteString("include=")
|
queryParams.WriteString("include=")
|
||||||
if filters.HasIncludes() {
|
if filters.HasIncludes() {
|
||||||
queryParams.WriteString(filters.GetIncludes())
|
queryParams.WriteString(filters.Includes())
|
||||||
}
|
}
|
||||||
|
|
||||||
queryParams.WriteString("&exclude=")
|
queryParams.WriteString("&exclude=")
|
||||||
if filters.HasExcludes() {
|
if filters.HasExcludes() {
|
||||||
queryParams.WriteString(filters.GetExcludes())
|
queryParams.WriteString(filters.Excludes())
|
||||||
}
|
}
|
||||||
|
|
||||||
hasParams = true
|
hasParams = true
|
||||||
|
@ -309,7 +309,7 @@ func refererToUri(referer string) string {
|
||||||
return "/" + parts[3]
|
return "/" + parts[3]
|
||||||
}
|
}
|
||||||
|
|
||||||
func getRealIp(r *http.Request) string {
|
func realIP(r *http.Request) string {
|
||||||
remoteAddr := strings.SplitAfter(r.RemoteAddr, ":")
|
remoteAddr := strings.SplitAfter(r.RemoteAddr, ":")
|
||||||
|
|
||||||
if len(remoteAddr) < 1 {
|
if len(remoteAddr) < 1 {
|
||||||
|
@ -331,14 +331,14 @@ func getRealIp(r *http.Request) string {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func serveHtml(w http.ResponseWriter, r *http.Request, filePath string, dimensions *Dimensions, filters *Filters, regexes *Regexes) error {
|
func serveHtml(w http.ResponseWriter, r *http.Request, filePath string, dimensions *Dimensions, filters *Filters, Regexes *Regexes) error {
|
||||||
fileName := filepath.Base(filePath)
|
fileName := filepath.Base(filePath)
|
||||||
|
|
||||||
w.Header().Add("Content-Type", "text/html")
|
w.Header().Add("Content-Type", "text/html")
|
||||||
|
|
||||||
sortOrder := getSortOrder(r)
|
sortOrder := sortOrder(r)
|
||||||
|
|
||||||
refreshTimer, refreshInterval := getRefreshInterval(r, regexes)
|
refreshTimer, refreshInterval := refreshInterval(r, Regexes)
|
||||||
|
|
||||||
queryParams := generateQueryParams(filters, sortOrder, refreshInterval)
|
queryParams := generateQueryParams(filters, sortOrder, refreshInterval)
|
||||||
|
|
||||||
|
@ -350,8 +350,8 @@ func serveHtml(w http.ResponseWriter, r *http.Request, filePath string, dimensio
|
||||||
htmlBody.WriteString(`position:absolute;top:50%;left:50%;transform:translate(-50%,-50%);}</style>`)
|
htmlBody.WriteString(`position:absolute;top:50%;left:50%;transform:translate(-50%,-50%);}</style>`)
|
||||||
htmlBody.WriteString(fmt.Sprintf(`<title>%s (%dx%d)</title>`,
|
htmlBody.WriteString(fmt.Sprintf(`<title>%s (%dx%d)</title>`,
|
||||||
fileName,
|
fileName,
|
||||||
dimensions.Width,
|
dimensions.width,
|
||||||
dimensions.Height))
|
dimensions.height))
|
||||||
htmlBody.WriteString(`</head><body>`)
|
htmlBody.WriteString(`</head><body>`)
|
||||||
if refreshInterval != "0ms" {
|
if refreshInterval != "0ms" {
|
||||||
htmlBody.WriteString(fmt.Sprintf("<script>window.onload = function(){setInterval(function(){window.location.href = '/%s';}, %d);};</script>",
|
htmlBody.WriteString(fmt.Sprintf("<script>window.onload = function(){setInterval(function(){window.location.href = '/%s';}, %d);};</script>",
|
||||||
|
@ -361,8 +361,8 @@ func serveHtml(w http.ResponseWriter, r *http.Request, filePath string, dimensio
|
||||||
htmlBody.WriteString(fmt.Sprintf(`<a href="/%s"><img src="%s" width="%d" height="%d" alt="Roulette selected: %s"></a>`,
|
htmlBody.WriteString(fmt.Sprintf(`<a href="/%s"><img src="%s" width="%d" height="%d" alt="Roulette selected: %s"></a>`,
|
||||||
queryParams,
|
queryParams,
|
||||||
generateFilePath(filePath),
|
generateFilePath(filePath),
|
||||||
dimensions.Width,
|
dimensions.width,
|
||||||
dimensions.Height,
|
dimensions.height,
|
||||||
fileName))
|
fileName))
|
||||||
htmlBody.WriteString(`</body></html>`)
|
htmlBody.WriteString(`</body></html>`)
|
||||||
|
|
||||||
|
@ -418,13 +418,13 @@ func serveStaticFile(w http.ResponseWriter, r *http.Request, paths []string, sta
|
||||||
startTime.Format(LogDate),
|
startTime.Format(LogDate),
|
||||||
filePath,
|
filePath,
|
||||||
fileSize,
|
fileSize,
|
||||||
getRealIp(r),
|
realIP(r),
|
||||||
time.Since(startTime).Round(time.Microsecond),
|
time.Since(startTime).Round(time.Microsecond),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
if Debug {
|
if Debug {
|
||||||
stats.IncrementCounter(filePath, startTime, fileSize)
|
stats.incrementCounter(filePath, startTime, fileSize)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
@ -432,7 +432,7 @@ func serveStaticFile(w http.ResponseWriter, r *http.Request, paths []string, sta
|
||||||
|
|
||||||
func serveCacheClearHandler(args []string, index *Index) http.HandlerFunc {
|
func serveCacheClearHandler(args []string, index *Index) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
index.GenerateCache(args)
|
index.generateCache(args)
|
||||||
|
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
w.Header().Set("Content-Type", "text/plain")
|
w.Header().Set("Content-Type", "text/plain")
|
||||||
|
@ -458,7 +458,7 @@ func serveStatsHandler(args []string, stats *ServeStats) http.HandlerFunc {
|
||||||
fmt.Printf("%s | Served statistics page (%s) to %s in %s\n",
|
fmt.Printf("%s | Served statistics page (%s) to %s in %s\n",
|
||||||
startTime.Format(LogDate),
|
startTime.Format(LogDate),
|
||||||
humanReadableSize(len(response)),
|
humanReadableSize(len(response)),
|
||||||
getRealIp(r),
|
realIP(r),
|
||||||
time.Since(startTime).Round(time.Microsecond),
|
time.Since(startTime).Round(time.Microsecond),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@ -474,7 +474,7 @@ func serveStaticFileHandler(paths []string, stats *ServeStats) http.HandlerFunc
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func serveHtmlHandler(paths []string, regexes *Regexes, index *Index) http.HandlerFunc {
|
func serveHtmlHandler(paths []string, Regexes *Regexes, index *Index) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
refererUri, err := stripQueryParams(refererToUri(r.Referer()))
|
refererUri, err := stripQueryParams(refererToUri(r.Referer()))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -482,27 +482,27 @@ func serveHtmlHandler(paths []string, regexes *Regexes, index *Index) http.Handl
|
||||||
}
|
}
|
||||||
|
|
||||||
filters := &Filters{
|
filters := &Filters{
|
||||||
Includes: splitQueryParams(r.URL.Query().Get("include"), regexes),
|
includes: splitQueryParams(r.URL.Query().Get("include"), Regexes),
|
||||||
Excludes: splitQueryParams(r.URL.Query().Get("exclude"), regexes),
|
excludes: splitQueryParams(r.URL.Query().Get("exclude"), Regexes),
|
||||||
}
|
}
|
||||||
|
|
||||||
sortOrder := getSortOrder(r)
|
sortOrder := sortOrder(r)
|
||||||
|
|
||||||
_, refreshInterval := getRefreshInterval(r, regexes)
|
_, refreshInterval := refreshInterval(r, Regexes)
|
||||||
|
|
||||||
if r.URL.Path == "/" {
|
if r.URL.Path == "/" {
|
||||||
var filePath string
|
var filePath string
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
if refererUri != "" {
|
if refererUri != "" {
|
||||||
filePath, err = getNextFile(refererUri, sortOrder, regexes)
|
filePath, err = nextFile(refererUri, sortOrder, Regexes)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if filePath == "" {
|
if filePath == "" {
|
||||||
filePath, err = getNewFile(paths, filters, sortOrder, regexes, index)
|
filePath, err = newFile(paths, filters, sortOrder, Regexes, index)
|
||||||
switch {
|
switch {
|
||||||
case err != nil && err == ErrNoImagesFound:
|
case err != nil && err == ErrNoImagesFound:
|
||||||
notFound(w, r, filePath)
|
notFound(w, r, filePath)
|
||||||
|
@ -548,12 +548,12 @@ func serveHtmlHandler(paths []string, regexes *Regexes, index *Index) http.Handl
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
dimensions, err := getImageDimensions(filePath)
|
dimensions, err := imageDimensions(filePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
err = serveHtml(w, r, filePath, dimensions, filters, regexes)
|
err = serveHtml(w, r, filePath, dimensions, filters, Regexes)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
@ -571,34 +571,34 @@ func ServePage(args []string) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
regexes := &Regexes{
|
Regexes := &Regexes{
|
||||||
Filename: regexp.MustCompile(`(.+)([0-9]{3})(\..+)`),
|
filename: regexp.MustCompile(`(.+)([0-9]{3})(\..+)`),
|
||||||
Alphanumeric: regexp.MustCompile(`^[a-zA-Z0-9]*$`),
|
alphanumeric: regexp.MustCompile(`^[a-zA-Z0-9]*$`),
|
||||||
Units: regexp.MustCompile(`^[0-9]+(ns|us|µs|ms|s|m|h)$`),
|
units: regexp.MustCompile(`^[0-9]+(ns|us|µs|ms|s|m|h)$`),
|
||||||
}
|
}
|
||||||
|
|
||||||
rand.Seed(time.Now().UnixNano())
|
rand.Seed(time.Now().UnixNano())
|
||||||
|
|
||||||
index := &Index{
|
index := &Index{
|
||||||
Mutex: sync.RWMutex{},
|
mutex: sync.RWMutex{},
|
||||||
List: []string{},
|
list: []string{},
|
||||||
}
|
}
|
||||||
|
|
||||||
if Cache {
|
if Cache {
|
||||||
index.GenerateCache(args)
|
index.generateCache(args)
|
||||||
|
|
||||||
http.Handle("/_/clear_cache", serveCacheClearHandler(args, index))
|
http.Handle("/_/clear_cache", serveCacheClearHandler(args, index))
|
||||||
}
|
}
|
||||||
|
|
||||||
stats := &ServeStats{
|
stats := &ServeStats{
|
||||||
Mutex: sync.RWMutex{},
|
mutex: sync.RWMutex{},
|
||||||
List: []string{},
|
list: []string{},
|
||||||
Count: make(map[string]uint64),
|
count: make(map[string]uint64),
|
||||||
Size: make(map[string]string),
|
size: make(map[string]string),
|
||||||
Times: make(map[string][]string),
|
times: make(map[string][]string),
|
||||||
}
|
}
|
||||||
|
|
||||||
http.Handle("/", serveHtmlHandler(paths, regexes, index))
|
http.Handle("/", serveHtmlHandler(paths, Regexes, index))
|
||||||
http.Handle(Prefix+"/", http.StripPrefix(Prefix, serveStaticFileHandler(paths, stats)))
|
http.Handle(Prefix+"/", http.StripPrefix(Prefix, serveStaticFileHandler(paths, stats)))
|
||||||
http.HandleFunc("/favicon.ico", doNothing)
|
http.HandleFunc("/favicon.ico", doNothing)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue