From 340b637baf284cdc9a7bc0f748d9dce4cb935ea1 Mon Sep 17 00:00:00 2001 From: Seednode Date: Sat, 21 Jan 2023 09:35:23 -0600 Subject: [PATCH] Added Get() and Set() functions for file index --- cmd/files.go | 30 +++++++++++++++++++++++++----- cmd/version.go | 2 +- cmd/web.go | 4 ++-- 3 files changed, 28 insertions(+), 8 deletions(-) diff --git a/cmd/files.go b/cmd/files.go index d7e75ea..74e9d8d 100644 --- a/cmd/files.go +++ b/cmd/files.go @@ -40,6 +40,20 @@ type Index struct { 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{} @@ -53,6 +67,14 @@ func (i *Index) GenerateCache(args []string) error { 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 @@ -570,8 +592,8 @@ func prepareDirectories(files *Files, sort string) []string { func pickFile(args []string, filters *Filters, sort string, index *Index) (string, error) { var fileList []string - if Cache && filters.IsEmpty() && len(index.List) != 0 { - fileList = index.List + if Cache && filters.IsEmpty() && !index.IsEmpty() { + fileList = index.Get() } else { files := &Files{ List: make(map[string][]string), @@ -605,9 +627,7 @@ func pickFile(args []string, filters *Filters, sort string, index *Index) (strin fileList = prepareDirectories(files, sort) if Cache { - index.Mutex.Lock() - index.List = append(index.List, fileList...) - index.Mutex.Unlock() + index.Set(fileList) } } diff --git a/cmd/version.go b/cmd/version.go index 736e063..bb96528 100644 --- a/cmd/version.go +++ b/cmd/version.go @@ -10,7 +10,7 @@ import ( "github.com/spf13/cobra" ) -var Version = "0.32.0" +var Version = "0.32.1" func init() { rootCmd.AddCommand(versionCmd) diff --git a/cmd/web.go b/cmd/web.go index a335cb0..82d9154 100644 --- a/cmd/web.go +++ b/cmd/web.go @@ -486,7 +486,7 @@ func ServePage(args []string) error { return err } - http.Handle("/clear_cache", serveCacheClearHandler(args, index)) + http.Handle("/_/clear_cache", serveCacheClearHandler(args, index)) } stats := &ServeStats{ @@ -502,7 +502,7 @@ func ServePage(args []string) error { http.HandleFunc("/favicon.ico", doNothing) if Debug { - http.Handle("/stats", serveStatsHandler(args, stats)) + http.Handle("/_/stats", serveStatsHandler(args, stats)) } err = http.ListenAndServe(":"+strconv.FormatInt(int64(Port), 10), nil)