Moved struct and method declarations to the appropriate source files

This commit is contained in:
Seednode 2023-01-21 16:08:59 -06:00
parent 7c40d62496
commit 3bf3c5b5ed
3 changed files with 104 additions and 103 deletions

View File

@ -5,14 +5,12 @@ Copyright © 2023 Seednode <seednode@seedno.de>
package cmd package cmd
import ( import (
"encoding/json"
"errors" "errors"
"fmt" "fmt"
"image" "image"
_ "image/gif" _ "image/gif"
_ "image/jpeg" _ "image/jpeg"
_ "image/png" _ "image/png"
"sort"
"math/rand" "math/rand"
"os" "os"
@ -35,46 +33,6 @@ var (
extensions = [6]string{".jpg", ".jpeg", ".png", ".gif", ".webp", ".bmp"} extensions = [6]string{".jpg", ".jpeg", ".png", ".gif", ".webp", ".bmp"}
) )
type Index struct {
Mutex sync.RWMutex
List []string
}
func (i *Index) Get() []string {
i.Mutex.RLock()
val := i.List
i.Mutex.RUnlock()
return val
}
func (i *Index) Set(val []string) {
i.Mutex.Lock()
i.List = val
i.Mutex.Unlock()
}
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
}
func (i *Index) IsEmpty() bool {
i.Mutex.RLock()
length := len(i.List)
i.Mutex.RUnlock()
return length == 0
}
type Dimensions struct { type Dimensions struct {
Width int Width int
Height int Height int
@ -125,65 +83,6 @@ func (s *ScanStats) GetDirectoriesMatched() uint64 {
return atomic.LoadUint64(&s.DirectoriesMatched) return atomic.LoadUint64(&s.DirectoriesMatched)
} }
type ServeStats struct {
Mutex sync.RWMutex
List []string
Count map[string]uint64
Size map[string]string
Times map[string][]string
}
type TimesServed struct {
File string
Served uint64
Size string
Times []string
}
func (s *ServeStats) IncrementCounter(image string, timestamp time.Time, filesize string) {
s.Mutex.Lock()
s.Count[image]++
s.Times[image] = append(s.Times[image], timestamp.Format(LogDate))
_, exists := s.Size[image]
if !exists {
s.Size[image] = filesize
}
if !contains(s.List, image) {
s.List = append(s.List, image)
}
s.Mutex.Unlock()
}
func (s *ServeStats) ListImages() ([]byte, error) {
s.Mutex.RLock()
sortedList := s.List
sort.SliceStable(sortedList, func(p, q int) bool {
return sortedList[p] < sortedList[q]
})
a := []TimesServed{}
for _, image := range s.List {
a = append(a, TimesServed{image, s.Count[image], s.Size[image], s.Times[image]})
}
s.Mutex.RUnlock()
r, err := json.MarshalIndent(a, "", " ")
if err != nil {
return []byte{}, err
}
return r, nil
}
type Path struct { type Path struct {
Base string Base string
Number int Number int
@ -538,7 +437,8 @@ func getFileList(paths []string, filters *Filters, sort string, index *Index) ([
var fileList []string var fileList []string
files := &Files{ files := &Files{
List: make(map[string][]string), Mutex: sync.Mutex{},
List: make(map[string][]string),
} }
stats := &ScanStats{ stats := &ScanStats{

View File

@ -10,7 +10,7 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
var Version = "0.33.0" var Version = "0.33.1"
func init() { func init() {
rootCmd.AddCommand(versionCmd) rootCmd.AddCommand(versionCmd)

View File

@ -5,6 +5,7 @@ Copyright © 2023 Seednode <seednode@seedno.de>
package cmd package cmd
import ( import (
"encoding/json"
"fmt" "fmt"
"io" "io"
"log" "log"
@ -15,6 +16,7 @@ import (
"path/filepath" "path/filepath"
"regexp" "regexp"
"runtime" "runtime"
"sort"
"strconv" "strconv"
"strings" "strings"
"sync" "sync"
@ -60,6 +62,105 @@ func (f *Filters) GetExcludes() string {
return strings.Join(f.Excludes, ",") return strings.Join(f.Excludes, ",")
} }
type Index struct {
Mutex sync.RWMutex
List []string
}
func (i *Index) Get() []string {
i.Mutex.RLock()
val := i.List
i.Mutex.RUnlock()
return val
}
func (i *Index) Set(val []string) {
i.Mutex.Lock()
i.List = val
i.Mutex.Unlock()
}
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
}
func (i *Index) IsEmpty() bool {
i.Mutex.RLock()
length := len(i.List)
i.Mutex.RUnlock()
return length == 0
}
type ServeStats struct {
Mutex sync.RWMutex
List []string
Count map[string]uint64
Size map[string]string
Times map[string][]string
}
func (s *ServeStats) IncrementCounter(image string, timestamp time.Time, filesize string) {
s.Mutex.Lock()
s.Count[image]++
s.Times[image] = append(s.Times[image], timestamp.Format(LogDate))
_, exists := s.Size[image]
if !exists {
s.Size[image] = filesize
}
if !contains(s.List, image) {
s.List = append(s.List, image)
}
s.Mutex.Unlock()
}
func (s *ServeStats) ListImages() ([]byte, error) {
s.Mutex.RLock()
sortedList := s.List
sort.SliceStable(sortedList, func(p, q int) bool {
return sortedList[p] < sortedList[q]
})
a := []TimesServed{}
for _, image := range s.List {
a = append(a, TimesServed{image, s.Count[image], s.Size[image], s.Times[image]})
}
s.Mutex.RUnlock()
r, err := json.MarshalIndent(a, "", " ")
if err != nil {
return []byte{}, err
}
return r, nil
}
type TimesServed struct {
File string
Served uint64
Size string
Times []string
}
func notFound(w http.ResponseWriter, r *http.Request, filePath string) error { func notFound(w http.ResponseWriter, r *http.Request, filePath string) error {
startTime := time.Now() startTime := time.Now()