mirror of
https://github.com/celogeek/piwigo-cli.git
synced 2025-05-25 02:02:37 +02:00
prepare upload tree
This commit is contained in:
parent
c05ce69886
commit
b0d7754e59
@ -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
|
||||
}
|
||||
|
@ -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
|
||||
|
53
internal/piwigocli/images_upload_tree.go
Normal file
53
internal/piwigocli/images_upload_tree.go
Normal 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
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user