prepare upload tree

This commit is contained in:
Celogeek 2021-12-26 13:55:48 +01:00
parent c05ce69886
commit b0d7754e59
Signed by: celogeek
GPG Key ID: E6B7BDCFC446233A
3 changed files with 89 additions and 5 deletions

View File

@ -1,9 +1,15 @@
package piwigo package piwigo
import ( import (
"errors"
"fmt"
"net/url" "net/url"
) )
type CategoriesResult struct {
Categories `json:"categories"`
}
type Categories []Category type Categories []Category
type Category struct { type Category struct {
@ -14,9 +20,7 @@ type Category struct {
} }
func (p *Piwigo) Categories() (map[int]Category, error) { func (p *Piwigo) Categories() (map[int]Category, error) {
var categories struct { var categories CategoriesResult
Categories `json:"categories"`
}
err := p.Post("pwg.categories.getList", &url.Values{ err := p.Post("pwg.categories.getList", &url.Values{
"fullname": []string{"true"}, "fullname": []string{"true"},
@ -41,3 +45,29 @@ func (c Categories) Names() []string {
} }
return names return names
} }
func (p *Piwigo) CategoriesId(catId int) (map[string]int, error) {
var categories CategoriesResult
err := p.Post("pwg.categories.getList", &url.Values{
"cat_id": []string{fmt.Sprint(catId)},
}, &categories)
if err != nil {
return nil, err
}
categoriesId := make(map[string]int)
ok := false
for _, category := range categories.Categories {
switch category.Id {
case catId:
ok = true
default:
categoriesId[category.Name] = category.Id
}
}
if !ok {
return nil, errors.New("category doesn't exists")
}
return categoriesId, nil
}

View File

@ -3,6 +3,7 @@ package piwigocli
type ImagesGroup struct { type ImagesGroup struct {
Details ImagesDetailsCommand `command:"details" description:"Details of the images"` Details ImagesDetailsCommand `command:"details" description:"Details of the images"`
Upload ImagesUploadCommand `command:"upload" description:"Upload of an images"` Upload ImagesUploadCommand `command:"upload" description:"Upload of an images"`
UploadTree ImagesUploadTreeCommand `command:"upload-tree" description:"Upload of a directory of images"`
} }
var imagesGroup ImagesGroup var imagesGroup ImagesGroup

View File

@ -0,0 +1,53 @@
package piwigocli
import (
"fmt"
"io/ioutil"
"path/filepath"
"github.com/celogeek/piwigo-cli/internal/piwigo"
)
type ImagesUploadTreeCommand struct {
Dirname string `short:"d" long:"dirname" description:"Directory to upload" required:"true"`
NbJobs int `short:"j" long:"jobs" description:"Number of jobs" default:"1"`
CategoryId int `short:"c" long:"category" description:"Category to upload the file" required:"true"`
}
func (c *ImagesUploadTreeCommand) Execute(args []string) error {
p := piwigo.Piwigo{}
if err := p.LoadConfig(); err != nil {
return err
}
_, err := p.Login()
if err != nil {
return err
}
rootPath, err := filepath.Abs(c.Dirname)
if err != nil {
return err
}
categoriesId, err := p.CategoriesId(c.CategoryId)
if err != nil {
return err
}
dirs, err := ioutil.ReadDir(rootPath)
if err != nil {
return err
}
for _, dir := range dirs {
if !dir.IsDir() {
continue
}
if _, ok := categoriesId[dir.Name()]; !ok {
fmt.Println("Creating", dir.Name(), "...")
}
}
return nil
}