commit inicial
This commit is contained in:
commit
92b0a902ca
20 changed files with 2464 additions and 0 deletions
76
state/state.go
Normal file
76
state/state.go
Normal file
|
|
@ -0,0 +1,76 @@
|
|||
package state
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"goreader/gallery"
|
||||
"maps"
|
||||
"slices"
|
||||
|
||||
_ "golang.org/x/image/webp"
|
||||
)
|
||||
|
||||
type State struct {
|
||||
Port string
|
||||
Root string
|
||||
CacheDir string
|
||||
Galleries []gallery.Gallery
|
||||
GalleryNames []string
|
||||
UniqueTags map[gallery.Tag]int
|
||||
UniqueArtists map[string]int
|
||||
UniqueGroups map[string]int
|
||||
UniqueParodies map[string]int
|
||||
TagKeys []gallery.Tag
|
||||
Filtered []gallery.Tag
|
||||
Done bool
|
||||
}
|
||||
|
||||
func (s *State) AddGallery(g gallery.Gallery) error {
|
||||
s.Galleries = append(s.Galleries, g)
|
||||
s.GalleryNames = append(s.GalleryNames, g.Name())
|
||||
slices.SortFunc(s.Galleries, func(a, b gallery.Gallery) int {
|
||||
return a.CTime.Compare(b.CTime)
|
||||
|
||||
})
|
||||
for _, tag := range g.Tags() {
|
||||
s.UniqueTags[tag]++
|
||||
}
|
||||
for _, artist := range g.Artists() {
|
||||
s.UniqueArtists[artist]++
|
||||
}
|
||||
for _, group := range g.Groups() {
|
||||
s.UniqueGroups[group]++
|
||||
}
|
||||
for _, parody := range g.Parodies() {
|
||||
s.UniqueParodies[parody]++
|
||||
}
|
||||
s.TagKeys = slices.Collect(maps.Keys(s.UniqueTags))
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *State) FilterGalleries() []gallery.Gallery {
|
||||
var ret []gallery.Gallery
|
||||
for _, g := range s.Galleries {
|
||||
galleryTags := g.Tags()
|
||||
count := 0
|
||||
for _, f := range s.Filtered {
|
||||
if i := slices.Index(galleryTags, f); i < 0 {
|
||||
break
|
||||
}
|
||||
count++
|
||||
}
|
||||
if len(s.Filtered) == count {
|
||||
ret = append(ret, g)
|
||||
}
|
||||
}
|
||||
return ret
|
||||
}
|
||||
|
||||
func (s *State) FindTitle(uuid string) (gallery.Gallery, error) {
|
||||
index := slices.IndexFunc(s.Galleries, func(g gallery.Gallery) bool {
|
||||
return g.Uuid() == uuid
|
||||
})
|
||||
if index == -1 {
|
||||
return gallery.Gallery{}, fmt.Errorf("gallery not found")
|
||||
}
|
||||
return s.Galleries[index], nil
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue