diff --git a/cmd/files.go b/cmd/files.go index 0de04fb..5ed815b 100644 --- a/cmd/files.go +++ b/cmd/files.go @@ -5,14 +5,12 @@ Copyright © 2023 Seednode package cmd import ( - "encoding/json" "errors" "fmt" "image" _ "image/gif" _ "image/jpeg" _ "image/png" - "sort" "math/rand" "os" @@ -35,46 +33,6 @@ var ( 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 { Width int Height int @@ -125,65 +83,6 @@ func (s *ScanStats) GetDirectoriesMatched() uint64 { 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 { Base string Number int @@ -538,7 +437,8 @@ func getFileList(paths []string, filters *Filters, sort string, index *Index) ([ var fileList []string files := &Files{ - List: make(map[string][]string), + Mutex: sync.Mutex{}, + List: make(map[string][]string), } stats := &ScanStats{ diff --git a/cmd/version.go b/cmd/version.go index 942a440..389844a 100644 --- a/cmd/version.go +++ b/cmd/version.go @@ -10,7 +10,7 @@ import ( "github.com/spf13/cobra" ) -var Version = "0.33.0" +var Version = "0.33.1" func init() { rootCmd.AddCommand(versionCmd) diff --git a/cmd/web.go b/cmd/web.go index 82d9154..7cb083e 100644 --- a/cmd/web.go +++ b/cmd/web.go @@ -5,6 +5,7 @@ Copyright © 2023 Seednode package cmd import ( + "encoding/json" "fmt" "io" "log" @@ -15,6 +16,7 @@ import ( "path/filepath" "regexp" "runtime" + "sort" "strconv" "strings" "sync" @@ -60,6 +62,105 @@ func (f *Filters) GetExcludes() string { 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 { startTime := time.Now()