move tree, simplify categories

This commit is contained in:
Celogeek 2021-12-31 13:23:28 +01:00
parent ed52dd691c
commit 222fce0e3f
Signed by: celogeek
GPG Key ID: E6B7BDCFC446233A
5 changed files with 26 additions and 19 deletions

View File

@ -5,10 +5,10 @@ import (
"net/url"
"path/filepath"
"regexp"
"strings"
"github.com/celogeek/piwigo-cli/internal/piwigo"
"github.com/celogeek/piwigo-cli/internal/piwigo/piwigotools"
"github.com/celogeek/piwigo-cli/internal/tree"
"github.com/schollz/progressbar/v3"
)
@ -51,7 +51,7 @@ func (c *ImagesListCommand) Execute(args []string) error {
if !ok {
return fmt.Errorf("category doesn't exists")
}
rootCatName = rootCat.Name + " / "
rootCatName = rootCat.Name
}
filter := regexp.MustCompile("")
@ -59,7 +59,7 @@ func (c *ImagesListCommand) Execute(args []string) error {
filter = regexp.MustCompile("(?i)" + c.Filter)
}
rootTree := piwigotools.NewTree()
rootTree := tree.New()
bar := progressbar.Default(1, "listing")
for page := 0; ; page++ {
@ -80,9 +80,11 @@ func (c *ImagesListCommand) Execute(args []string) error {
for _, image := range resp.Images {
for _, cat := range image.Categories {
filename := filepath.Join(
strings.ReplaceAll(categories[cat.Id].Name[len(rootCatName):], " / ", "/"),
image.Filename,
filename, _ := filepath.Rel(rootCatName,
filepath.Join(
categories[cat.Id].Name,
image.Filename,
),
)
if !filter.MatchString(filename) {
continue

View File

@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"net/url"
"strings"
"github.com/celogeek/piwigo-cli/internal/piwigo/piwigotools"
)
@ -21,22 +22,25 @@ func (p *Piwigo) Categories() (piwigotools.Categories, error) {
}, &result); err != nil {
return nil, err
}
for _, category := range result.Categories {
category.Name = strings.ReplaceAll(category.Name, " / ", "/")
}
return result.Categories, nil
}
func (p *Piwigo) CategoryFromId() (map[int]piwigotools.Category, error) {
func (p *Piwigo) CategoryFromId() (map[int]*piwigotools.Category, error) {
categories, err := p.Categories()
if err != nil {
return nil, err
}
result := map[int]piwigotools.Category{}
result := map[int]*piwigotools.Category{}
for _, category := range categories {
result[category.Id] = category
}
return result, nil
}
func (p *Piwigo) CategoryFromName(catId int) (map[string]piwigotools.Category, error) {
func (p *Piwigo) CategoryFromName(catId int) (map[string]*piwigotools.Category, error) {
var results CategoriesResult
err := p.Post("pwg.categories.getList", &url.Values{
@ -46,13 +50,14 @@ func (p *Piwigo) CategoryFromName(catId int) (map[string]piwigotools.Category, e
return nil, err
}
categoriesId := map[string]piwigotools.Category{}
categoriesId := map[string]*piwigotools.Category{}
ok := false
for _, category := range results.Categories {
switch category.Id {
case catId:
ok = true
default:
category.Name = strings.ReplaceAll(category.Name, " / ", "/")
categoriesId[category.Name] = category
}
}

View File

@ -178,7 +178,7 @@ func (p *Piwigo) ScanTree(
dirname := norm.NFC.String(dir.Name())
category, ok := categoryFromName[dirname]
if !ok {
category = piwigotools.Category{}
category = &piwigotools.Category{}
p.mu.Lock()
err = p.Post("pwg.categories.add", &url.Values{
"name": []string{strings.ReplaceAll(dirname, "'", `\'`)},

View File

@ -1,6 +1,6 @@
package piwigotools
type Categories []Category
type Categories []*Category
type Category struct {
Id int `json:"id"`

View File

@ -1,4 +1,4 @@
package piwigotools
package tree
import (
"path/filepath"
@ -13,17 +13,17 @@ type Tree interface {
TreeView() chan string
}
type node struct {
Name string
Nodes map[string]*node
}
func NewTree() Tree {
func New() Tree {
return &node{
Name: ".",
}
}
type node struct {
Name string
Nodes map[string]*node
}
func (t *node) Add(name string) Tree {
if t.Nodes == nil {
t.Nodes = map[string]*node{}