Added Get() and Set() functions for file index
This commit is contained in:
parent
fcdb81a9b7
commit
340b637baf
30
cmd/files.go
30
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)
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -10,7 +10,7 @@ import (
|
|||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var Version = "0.32.0"
|
||||
var Version = "0.32.1"
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(versionCmd)
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue