diff --git a/internal/piwigo/categories.go b/internal/piwigo/categories.go index 0c7cba3..ee0c881 100644 --- a/internal/piwigo/categories.go +++ b/internal/piwigo/categories.go @@ -1,9 +1,15 @@ package piwigo import ( + "errors" + "fmt" "net/url" ) +type CategoriesResult struct { + Categories `json:"categories"` +} + type Categories []Category type Category struct { @@ -14,9 +20,7 @@ type Category struct { } func (p *Piwigo) Categories() (map[int]Category, error) { - var categories struct { - Categories `json:"categories"` - } + var categories CategoriesResult err := p.Post("pwg.categories.getList", &url.Values{ "fullname": []string{"true"}, @@ -41,3 +45,29 @@ func (c Categories) Names() []string { } 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 +} diff --git a/internal/piwigocli/images.go b/internal/piwigocli/images.go index 3b51009..047e030 100644 --- a/internal/piwigocli/images.go +++ b/internal/piwigocli/images.go @@ -1,8 +1,9 @@ package piwigocli type ImagesGroup struct { - Details ImagesDetailsCommand `command:"details" description:"Details of the images"` - Upload ImagesUploadCommand `command:"upload" description:"Upload of an images"` + Details ImagesDetailsCommand `command:"details" description:"Details of the 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 diff --git a/internal/piwigocli/images_upload_tree.go b/internal/piwigocli/images_upload_tree.go new file mode 100644 index 0000000..ae26dd7 --- /dev/null +++ b/internal/piwigocli/images_upload_tree.go @@ -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 +}