diff --git a/cmd/piwigo-cli/images_list.go b/cmd/piwigo-cli/images_list.go index 04aae67..1626005 100644 --- a/cmd/piwigo-cli/images_list.go +++ b/cmd/piwigo-cli/images_list.go @@ -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 diff --git a/internal/piwigo/categories.go b/internal/piwigo/categories.go index 1982833..575a8ff 100644 --- a/internal/piwigo/categories.go +++ b/internal/piwigo/categories.go @@ -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 } } diff --git a/internal/piwigo/files.go b/internal/piwigo/files.go index 15f5350..041bb2f 100644 --- a/internal/piwigo/files.go +++ b/internal/piwigo/files.go @@ -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, "'", `\'`)}, diff --git a/internal/piwigo/piwigotools/categories.go b/internal/piwigo/piwigotools/categories.go index d10e03d..4c0ea34 100644 --- a/internal/piwigo/piwigotools/categories.go +++ b/internal/piwigo/piwigotools/categories.go @@ -1,6 +1,6 @@ package piwigotools -type Categories []Category +type Categories []*Category type Category struct { Id int `json:"id"` diff --git a/internal/piwigo/piwigotools/tree.go b/internal/tree/tree.go similarity index 97% rename from internal/piwigo/piwigotools/tree.go rename to internal/tree/tree.go index c66aaaa..f4f07c0 100644 --- a/internal/piwigo/piwigotools/tree.go +++ b/internal/tree/tree.go @@ -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{}