From 654f9b56340b3621de7e4ee0f9af7ef66849dddc Mon Sep 17 00:00:00 2001 From: celogeek <65178+celogeek@users.noreply.github.com> Date: Wed, 29 Dec 2021 16:26:11 +0100 Subject: [PATCH] reorganize --- internal/piwigo/categories.go | 54 +++++------ internal/piwigo/derivatives.go | 9 -- internal/piwigo/files.go | 44 +++++---- internal/piwigo/helper.go | 96 ------------------- internal/piwigo/images.go | 19 ---- internal/piwigo/info.go | 8 -- internal/piwigo/login.go | 14 +-- internal/piwigo/methods.go | 45 --------- internal/piwigo/piwigo.go | 7 -- .../piwigo/{ => piwigotools}/active_plugin.go | 2 +- internal/piwigo/piwigotools/base64.go | 53 ++++++++++ internal/piwigo/piwigotools/categories.go | 18 ++++ internal/piwigo/piwigotools/dump.go | 14 +++ internal/piwigo/{ => piwigotools}/exif.go | 2 +- .../{ => piwigotools}/file_to_upload.go | 7 +- .../{ => piwigotools}/file_to_upload_stat.go | 2 +- internal/piwigo/piwigotools/image_details.go | 23 +++++ internal/piwigo/piwigotools/md5.go | 24 +++++ internal/piwigo/{ => piwigotools}/tags.go | 2 +- .../{time.go => piwigotools/time_result.go} | 2 +- .../{ => piwigotools}/upload_file_type.go | 2 +- internal/piwigo/post.go | 11 ++- internal/piwigocli/categories_list.go | 15 +-- internal/piwigocli/general.go | 9 +- internal/piwigocli/images.go | 2 +- internal/piwigocli/images_details.go | 9 +- internal/piwigocli/images_list.go | 5 +- internal/piwigocli/images_upload.go | 5 +- internal/piwigocli/images_upload_tree.go | 7 +- internal/piwigocli/method_details.go | 4 +- internal/piwigocli/method_list.go | 43 ++++++++- internal/piwigocli/method_try.go | 23 ++++- 32 files changed, 291 insertions(+), 289 deletions(-) delete mode 100644 internal/piwigo/derivatives.go delete mode 100644 internal/piwigo/helper.go delete mode 100644 internal/piwigo/images.go delete mode 100644 internal/piwigo/info.go delete mode 100644 internal/piwigo/methods.go rename internal/piwigo/{ => piwigotools}/active_plugin.go (97%) create mode 100644 internal/piwigo/piwigotools/base64.go create mode 100644 internal/piwigo/piwigotools/categories.go create mode 100644 internal/piwigo/piwigotools/dump.go rename internal/piwigo/{ => piwigotools}/exif.go (97%) rename internal/piwigo/{ => piwigotools}/file_to_upload.go (87%) rename internal/piwigo/{ => piwigotools}/file_to_upload_stat.go (98%) create mode 100644 internal/piwigo/piwigotools/image_details.go create mode 100644 internal/piwigo/piwigotools/md5.go rename internal/piwigo/{ => piwigotools}/tags.go (97%) rename internal/piwigo/{time.go => piwigotools/time_result.go} (99%) rename internal/piwigo/{ => piwigotools}/upload_file_type.go (97%) diff --git a/internal/piwigo/categories.go b/internal/piwigo/categories.go index ee0c881..1982833 100644 --- a/internal/piwigo/categories.go +++ b/internal/piwigo/categories.go @@ -4,66 +4,56 @@ import ( "errors" "fmt" "net/url" + + "github.com/celogeek/piwigo-cli/internal/piwigo/piwigotools" ) type CategoriesResult struct { - Categories `json:"categories"` + Categories piwigotools.Categories `json:"categories"` } -type Categories []Category +func (p *Piwigo) Categories() (piwigotools.Categories, error) { + var result CategoriesResult -type Category struct { - Id int `json:"id"` - Name string `json:"name"` - ImagesCount int `json:"nb_images"` - Url string `json:"url"` -} - -func (p *Piwigo) Categories() (map[int]Category, error) { - var categories CategoriesResult - - err := p.Post("pwg.categories.getList", &url.Values{ + if err := p.Post("pwg.categories.getList", &url.Values{ "fullname": []string{"true"}, "recursive": []string{"true"}, - }, &categories) + }, &result); err != nil { + return nil, err + } + return result.Categories, nil +} + +func (p *Piwigo) CategoryFromId() (map[int]piwigotools.Category, error) { + categories, err := p.Categories() if err != nil { return nil, err } - - result := map[int]Category{} - - for _, category := range categories.Categories { + result := map[int]piwigotools.Category{} + for _, category := range categories { result[category.Id] = category } return result, nil } -func (c Categories) Names() []string { - names := []string{} - for _, category := range c { - names = append(names, category.Name) - } - return names -} - -func (p *Piwigo) CategoriesId(catId int) (map[string]int, error) { - var categories CategoriesResult +func (p *Piwigo) CategoryFromName(catId int) (map[string]piwigotools.Category, error) { + var results CategoriesResult err := p.Post("pwg.categories.getList", &url.Values{ "cat_id": []string{fmt.Sprint(catId)}, - }, &categories) + }, &results) if err != nil { return nil, err } - categoriesId := make(map[string]int) + categoriesId := map[string]piwigotools.Category{} ok := false - for _, category := range categories.Categories { + for _, category := range results.Categories { switch category.Id { case catId: ok = true default: - categoriesId[category.Name] = category.Id + categoriesId[category.Name] = category } } if !ok { diff --git a/internal/piwigo/derivatives.go b/internal/piwigo/derivatives.go deleted file mode 100644 index dfe3257..0000000 --- a/internal/piwigo/derivatives.go +++ /dev/null @@ -1,9 +0,0 @@ -package piwigo - -type Derivatives map[string]Derivative - -type Derivative struct { - Height int `json:"height"` - Width int `json:"width"` - Url string `json:"url"` -} diff --git a/internal/piwigo/files.go b/internal/piwigo/files.go index d7c8138..f5b4d61 100644 --- a/internal/piwigo/files.go +++ b/internal/piwigo/files.go @@ -8,9 +8,15 @@ import ( "strings" "sync" + "github.com/celogeek/piwigo-cli/internal/piwigo/piwigotools" "golang.org/x/text/unicode/norm" ) +type FileUploadResult struct { + ImageId int `json:"image_id"` + Url string `json:"url"` +} + func (p *Piwigo) FileExists(md5 string) bool { var resp map[string]*string @@ -19,11 +25,10 @@ func (p *Piwigo) FileExists(md5 string) bool { }, &resp); err != nil { return false } - return resp[md5] != nil } -func (p *Piwigo) CheckUploadFile(file *FileToUpload, stat *FileToUploadStat) (err error) { +func (p *Piwigo) CheckUploadFile(file *piwigotools.FileToUpload, stat *piwigotools.FileToUploadStat) (err error) { if !file.Checked() { if file.MD5() == "" { stat.Fail() @@ -46,13 +51,13 @@ func (p *Piwigo) CheckUploadFile(file *FileToUpload, stat *FileToUploadStat) (er return nil } -func (p *Piwigo) Upload(file *FileToUpload, stat *FileToUploadStat, nbJobs int, hasVideoJS bool) { +func (p *Piwigo) Upload(file *piwigotools.FileToUpload, stat *piwigotools.FileToUploadStat, nbJobs int, hasVideoJS bool) { err := p.CheckUploadFile(file, stat) if err != nil { return } wg := &sync.WaitGroup{} - chunks, err := Base64Chunker(file.FullPath()) + chunks, err := piwigotools.Base64Chunker(file.FullPath()) if err != nil { stat.Error("Base64Chunker", file.FullPath(), err) return @@ -70,7 +75,7 @@ func (p *Piwigo) Upload(file *FileToUpload, stat *FileToUploadStat, nbJobs int, // lock this process for committing the file - exif, _ := Exif(file.FullPath()) + exif, _ := piwigotools.Exif(file.FullPath()) var resp *FileUploadResult data := &url.Values{} data.Set("original_sum", file.MD5()) @@ -109,7 +114,7 @@ func (p *Piwigo) Upload(file *FileToUpload, stat *FileToUploadStat, nbJobs int, stat.Done() } -func (p *Piwigo) UploadChunk(file *FileToUpload, chunks chan *Base64ChunkResult, wg *sync.WaitGroup, stat *FileToUploadStat, ok *bool) { +func (p *Piwigo) UploadChunk(file *piwigotools.FileToUpload, chunks chan *piwigotools.Base64Chunk, wg *sync.WaitGroup, stat *piwigotools.FileToUploadStat, ok *bool) { defer wg.Done() for chunk := range chunks { var err error @@ -139,9 +144,9 @@ func (p *Piwigo) ScanTree( rootPath string, parentCategoryId int, level int, - filter *UploadFileType, - stat *FileToUploadStat, - files chan *FileToUpload, + filter *piwigotools.UploadFileType, + stat *piwigotools.FileToUploadStat, + files chan *piwigotools.FileToUpload, ) { if level == 0 { defer close(files) @@ -152,7 +157,7 @@ func (p *Piwigo) ScanTree( return } - categoriesId, err := p.CategoriesId(parentCategoryId) + categoryFromName, err := p.CategoryFromName(parentCategoryId) if err != nil { stat.Error("ScanTree CategoriesId", rootPath, err) return @@ -168,26 +173,23 @@ func (p *Piwigo) ScanTree( switch dir.IsDir() { case true: // Directory dirname := norm.NFC.String(dir.Name()) - categoryId, ok := categoriesId[dirname] + category, ok := categoryFromName[dirname] if !ok { - var resp struct { - Id int `json:"id"` - } + category = piwigotools.Category{} p.mu.Lock() err = p.Post("pwg.categories.add", &url.Values{ "name": []string{strings.ReplaceAll(dirname, "'", `\'`)}, "parent": []string{fmt.Sprint(parentCategoryId)}, - }, &resp) + }, &category) p.mu.Unlock() if err != nil { stat.Error("ScanTree Categories Add", rootPath, err) return } - categoryId = resp.Id } - p.ScanTree(filepath.Join(rootPath, dirname), categoryId, level+1, filter, stat, files) + p.ScanTree(filepath.Join(rootPath, dirname), category.Id, level+1, filter, stat, files) case false: // File - file := &FileToUpload{ + file := &piwigotools.FileToUpload{ Dir: rootPath, Name: dir.Name(), CategoryId: parentCategoryId, @@ -202,7 +204,7 @@ func (p *Piwigo) ScanTree( } -func (p *Piwigo) CheckFiles(filesToCheck chan *FileToUpload, files chan *FileToUpload, stat *FileToUploadStat, nbJobs int) { +func (p *Piwigo) CheckFiles(filesToCheck chan *piwigotools.FileToUpload, files chan *piwigotools.FileToUpload, stat *piwigotools.FileToUploadStat, nbJobs int) { defer close(files) wg := &sync.WaitGroup{} @@ -224,8 +226,8 @@ func (p *Piwigo) CheckFiles(filesToCheck chan *FileToUpload, files chan *FileToU } func (p *Piwigo) UploadFiles( - files chan *FileToUpload, - stat *FileToUploadStat, + files chan *piwigotools.FileToUpload, + stat *piwigotools.FileToUploadStat, hasVideoJS bool, nbJobs int, nbJobsChunk int, diff --git a/internal/piwigo/helper.go b/internal/piwigo/helper.go deleted file mode 100644 index f1a57d8..0000000 --- a/internal/piwigo/helper.go +++ /dev/null @@ -1,96 +0,0 @@ -package piwigo - -import ( - "bytes" - "crypto/md5" - "encoding/base64" - "encoding/json" - "errors" - "fmt" - "io" - "net/url" - "os" - "strings" -) - -var CHUNK_SIZE int64 = 1 * 1024 * 1024 -var CHUNK_BUFF_SIZE int64 = 32 * 1024 -var CHUNK_BUFF_COUNT = CHUNK_SIZE / CHUNK_BUFF_SIZE - -func DumpResponse(v interface{}) (err error) { - b, err := json.MarshalIndent(v, "", " ") - if err == nil { - fmt.Println(string(b)) - } - return -} - -func ArgsToForm(args []string) (*url.Values, error) { - params := &url.Values{} - for _, arg := range args { - r := strings.SplitN(arg, "=", 2) - if len(r) != 2 { - return nil, errors.New("args should be key=value") - } - params.Add(r[0], strings.ReplaceAll(r[1], "'", `\'`)) - } - return params, nil -} - -func Md5File(filename string) (result string, err error) { - file, err := os.Open(filename) - if err != nil { - return - } - defer file.Close() - - hash := md5.New() - _, err = io.Copy(hash, file) - if err != nil { - return - } - result = fmt.Sprintf("%x", hash.Sum(nil)) - return -} - -type Base64ChunkResult struct { - Position int64 - Size int64 - Buffer bytes.Buffer -} - -func Base64Chunker(filename string) (out chan *Base64ChunkResult, err error) { - f, err := os.Open(filename) - if err != nil { - return - } - - out = make(chan *Base64ChunkResult, 8) - go func() { - b := make([]byte, CHUNK_BUFF_SIZE) - defer f.Close() - defer close(out) - ok := false - for position := int64(0); !ok; position += 1 { - bf := &Base64ChunkResult{ - Position: position, - } - b64 := base64.NewEncoder(base64.StdEncoding, &bf.Buffer) - for i := int64(0); i < CHUNK_BUFF_COUNT; i++ { - n, _ := f.Read(b) - if n == 0 { - ok = true - break - } - bf.Size += int64(n) - b64.Write(b[:n]) - } - b64.Close() - if bf.Size > 0 { - out <- bf - } - } - }() - - return -} diff --git a/internal/piwigo/images.go b/internal/piwigo/images.go deleted file mode 100644 index 3066b97..0000000 --- a/internal/piwigo/images.go +++ /dev/null @@ -1,19 +0,0 @@ -package piwigo - -type ImagesDetails struct { - Id int `json:"id"` - Md5 string `json:"md5sum"` - Name string `json:"name"` - DateAvailable TimeResult `json:"date_available"` - DateCreation TimeResult `json:"date_creation"` - LastModified TimeResult `json:"lastmodified"` - Width int `json:"width"` - Height int `json:"height"` - Url string `json:"page_url"` - ImageUrl string `json:"element_url"` - Filename string `json:"file"` - Filesize int64 `json:"filesize"` - Categories Categories `json:"categories"` - Tags Tags `json:"tags"` - Derivatives Derivatives `json:"derivatives"` -} diff --git a/internal/piwigo/info.go b/internal/piwigo/info.go deleted file mode 100644 index a38f023..0000000 --- a/internal/piwigo/info.go +++ /dev/null @@ -1,8 +0,0 @@ -package piwigo - -type Infos []Info - -type Info struct { - Name string `json:"name"` - Value interface{} `json:"value"` -} diff --git a/internal/piwigo/login.go b/internal/piwigo/login.go index 88e02be..e88d657 100644 --- a/internal/piwigo/login.go +++ b/internal/piwigo/login.go @@ -3,15 +3,17 @@ package piwigo import ( "errors" "net/url" + + "github.com/celogeek/piwigo-cli/internal/piwigo/piwigotools" ) type StatusResponse struct { - User string `json:"username"` - Role string `json:"status"` - Version string `json:"version"` - Token string `json:"pwg_token"` - UploadFileType UploadFileType `json:"upload_file_types"` - Plugins ActivePlugin `json:"plugins"` + User string `json:"username"` + Role string `json:"status"` + Version string `json:"version"` + Token string `json:"pwg_token"` + UploadFileType piwigotools.UploadFileType `json:"upload_file_types"` + Plugins piwigotools.ActivePlugin `json:"plugins"` } func (p *Piwigo) GetStatus() (*StatusResponse, error) { diff --git a/internal/piwigo/methods.go b/internal/piwigo/methods.go deleted file mode 100644 index 1702807..0000000 --- a/internal/piwigo/methods.go +++ /dev/null @@ -1,45 +0,0 @@ -package piwigo - -import ( - "encoding/json" -) - -type Methods []string - -type MethodParams []MethodParam - -type MethodParam struct { - Name string `json:"name"` - Optional bool `json:"optional"` - Type string `json:"type"` - AcceptArray bool `json:"acceptArray"` - DefaultValue interface{} `json:"defaultValue"` - MaxValue interface{} `json:"maxValue"` - Info string `json:"info"` -} - -type MethodOptions struct { - Admin bool `json:"admin_only"` - PostOnly bool `json:"post_only"` -} - -func (j *MethodOptions) UnmarshalJSON(data []byte) error { - var r interface{} - if err := json.Unmarshal(data, &r); err != nil { - return err - } - - switch r := r.(type) { - case map[string]interface{}: - j.Admin, _ = r["admin_only"].(bool) - j.PostOnly, _ = r["post_only"].(bool) - } - return nil -} - -type MethodDetails struct { - Name string `json:"name"` - Description string `json:"description"` - Options MethodOptions `json:"options"` - Parameters MethodParams `json:"params"` -} diff --git a/internal/piwigo/piwigo.go b/internal/piwigo/piwigo.go index 7bb4e31..774d945 100644 --- a/internal/piwigo/piwigo.go +++ b/internal/piwigo/piwigo.go @@ -10,10 +10,3 @@ type Piwigo struct { mu sync.Mutex } - -type PiwigoResult struct { - Stat string `json:"stat"` - Err int `json:"err"` - ErrMessage string `json:"message"` - Result interface{} `json:"result"` -} diff --git a/internal/piwigo/active_plugin.go b/internal/piwigo/piwigotools/active_plugin.go similarity index 97% rename from internal/piwigo/active_plugin.go rename to internal/piwigo/piwigotools/active_plugin.go index 4474486..1ceea37 100644 --- a/internal/piwigo/active_plugin.go +++ b/internal/piwigo/piwigotools/active_plugin.go @@ -1,4 +1,4 @@ -package piwigo +package piwigotools import ( "encoding/json" diff --git a/internal/piwigo/piwigotools/base64.go b/internal/piwigo/piwigotools/base64.go new file mode 100644 index 0000000..ff40a67 --- /dev/null +++ b/internal/piwigo/piwigotools/base64.go @@ -0,0 +1,53 @@ +package piwigotools + +import ( + "bytes" + "encoding/base64" + "os" +) + +var CHUNK_SIZE int64 = 1 * 1024 * 1024 +var CHUNK_BUFF_SIZE int64 = 32 * 1024 +var CHUNK_BUFF_COUNT = CHUNK_SIZE / CHUNK_BUFF_SIZE + +type Base64Chunk struct { + Position int64 + Size int64 + Buffer bytes.Buffer +} + +func Base64Chunker(filename string) (out chan *Base64Chunk, err error) { + f, err := os.Open(filename) + if err != nil { + return + } + + out = make(chan *Base64Chunk, 8) + go func() { + b := make([]byte, CHUNK_BUFF_SIZE) + defer f.Close() + defer close(out) + ok := false + for position := int64(0); !ok; position += 1 { + bf := &Base64Chunk{ + Position: position, + } + b64 := base64.NewEncoder(base64.StdEncoding, &bf.Buffer) + for i := int64(0); i < CHUNK_BUFF_COUNT; i++ { + n, _ := f.Read(b) + if n == 0 { + ok = true + break + } + bf.Size += int64(n) + b64.Write(b[:n]) + } + b64.Close() + if bf.Size > 0 { + out <- bf + } + } + }() + + return +} diff --git a/internal/piwigo/piwigotools/categories.go b/internal/piwigo/piwigotools/categories.go new file mode 100644 index 0000000..d10e03d --- /dev/null +++ b/internal/piwigo/piwigotools/categories.go @@ -0,0 +1,18 @@ +package piwigotools + +type Categories []Category + +type Category struct { + Id int `json:"id"` + Name string `json:"name"` + ImagesCount int `json:"nb_images"` + Url string `json:"url"` +} + +func (c *Categories) Names() []string { + names := []string{} + for _, category := range *c { + names = append(names, category.Name) + } + return names +} diff --git a/internal/piwigo/piwigotools/dump.go b/internal/piwigo/piwigotools/dump.go new file mode 100644 index 0000000..6ec10dd --- /dev/null +++ b/internal/piwigo/piwigotools/dump.go @@ -0,0 +1,14 @@ +package piwigotools + +import ( + "encoding/json" + "fmt" +) + +func DumpResponse(v interface{}) (err error) { + b, err := json.MarshalIndent(v, "", " ") + if err == nil { + fmt.Println(string(b)) + } + return +} diff --git a/internal/piwigo/exif.go b/internal/piwigo/piwigotools/exif.go similarity index 97% rename from internal/piwigo/exif.go rename to internal/piwigo/piwigotools/exif.go index dc91102..472c025 100644 --- a/internal/piwigo/exif.go +++ b/internal/piwigo/piwigotools/exif.go @@ -1,4 +1,4 @@ -package piwigo +package piwigotools import ( "fmt" diff --git a/internal/piwigo/file_to_upload.go b/internal/piwigo/piwigotools/file_to_upload.go similarity index 87% rename from internal/piwigo/file_to_upload.go rename to internal/piwigo/piwigotools/file_to_upload.go index baebc4c..ebda35e 100644 --- a/internal/piwigo/file_to_upload.go +++ b/internal/piwigo/piwigotools/file_to_upload.go @@ -1,4 +1,4 @@ -package piwigo +package piwigotools import ( "os" @@ -6,11 +6,6 @@ import ( "strings" ) -type FileUploadResult struct { - ImageId int `json:"image_id"` - Url string `json:"url"` -} - type FileToUpload struct { Dir string Name string diff --git a/internal/piwigo/file_to_upload_stat.go b/internal/piwigo/piwigotools/file_to_upload_stat.go similarity index 98% rename from internal/piwigo/file_to_upload_stat.go rename to internal/piwigo/piwigotools/file_to_upload_stat.go index d68bee7..2ce42d0 100644 --- a/internal/piwigo/file_to_upload_stat.go +++ b/internal/piwigo/piwigotools/file_to_upload_stat.go @@ -1,4 +1,4 @@ -package piwigo +package piwigotools import ( "fmt" diff --git a/internal/piwigo/piwigotools/image_details.go b/internal/piwigo/piwigotools/image_details.go new file mode 100644 index 0000000..42391c4 --- /dev/null +++ b/internal/piwigo/piwigotools/image_details.go @@ -0,0 +1,23 @@ +package piwigotools + +type ImageDetails struct { + Id int `json:"id"` + Md5 string `json:"md5sum"` + Name string `json:"name"` + DateAvailable TimeResult `json:"date_available"` + DateCreation TimeResult `json:"date_creation"` + LastModified TimeResult `json:"lastmodified"` + Width int `json:"width"` + Height int `json:"height"` + Url string `json:"page_url"` + ImageUrl string `json:"element_url"` + Filename string `json:"file"` + Filesize int64 `json:"filesize"` + Categories Categories `json:"categories"` + Tags Tags `json:"tags"` + Derivatives map[string]struct { + Height int `json:"height"` + Width int `json:"width"` + Url string `json:"url"` + } `json:"derivatives"` +} diff --git a/internal/piwigo/piwigotools/md5.go b/internal/piwigo/piwigotools/md5.go new file mode 100644 index 0000000..ebfd562 --- /dev/null +++ b/internal/piwigo/piwigotools/md5.go @@ -0,0 +1,24 @@ +package piwigotools + +import ( + "crypto/md5" + "fmt" + "io" + "os" +) + +func Md5File(filename string) (result string, err error) { + file, err := os.Open(filename) + if err != nil { + return + } + defer file.Close() + + hash := md5.New() + _, err = io.Copy(hash, file) + if err != nil { + return + } + result = fmt.Sprintf("%x", hash.Sum(nil)) + return +} diff --git a/internal/piwigo/tags.go b/internal/piwigo/piwigotools/tags.go similarity index 97% rename from internal/piwigo/tags.go rename to internal/piwigo/piwigotools/tags.go index 4410a96..7d13fdc 100644 --- a/internal/piwigo/tags.go +++ b/internal/piwigo/piwigotools/tags.go @@ -1,4 +1,4 @@ -package piwigo +package piwigotools import ( "fmt" diff --git a/internal/piwigo/time.go b/internal/piwigo/piwigotools/time_result.go similarity index 99% rename from internal/piwigo/time.go rename to internal/piwigo/piwigotools/time_result.go index ce23c81..6e71e83 100644 --- a/internal/piwigo/time.go +++ b/internal/piwigo/piwigotools/time_result.go @@ -1,4 +1,4 @@ -package piwigo +package piwigotools import ( "fmt" diff --git a/internal/piwigo/upload_file_type.go b/internal/piwigo/piwigotools/upload_file_type.go similarity index 97% rename from internal/piwigo/upload_file_type.go rename to internal/piwigo/piwigotools/upload_file_type.go index 600d8f1..0ff2006 100644 --- a/internal/piwigo/upload_file_type.go +++ b/internal/piwigo/piwigotools/upload_file_type.go @@ -1,4 +1,4 @@ -package piwigo +package piwigotools import ( "encoding/json" diff --git a/internal/piwigo/post.go b/internal/piwigo/post.go index 30e1840..5f8fbb5 100644 --- a/internal/piwigo/post.go +++ b/internal/piwigo/post.go @@ -8,8 +8,17 @@ import ( "net/url" "os" "strings" + + "github.com/celogeek/piwigo-cli/internal/piwigo/piwigotools" ) +type PiwigoResult struct { + Stat string `json:"stat"` + Err int `json:"err"` + ErrMessage string `json:"message"` + Result interface{} `json:"result"` +} + func (p *Piwigo) BuildUrl(method string) (string, error) { Url, err := url.Parse(p.Url) @@ -89,7 +98,7 @@ func (p *Piwigo) Post(method string, form *url.Values, resp interface{}) error { return err } - DumpResponse(RawResult) + piwigotools.DumpResponse(RawResult) } if Result.Stat != "ok" { diff --git a/internal/piwigocli/categories_list.go b/internal/piwigocli/categories_list.go index 5214148..e8b78f6 100644 --- a/internal/piwigocli/categories_list.go +++ b/internal/piwigocli/categories_list.go @@ -1,7 +1,6 @@ package piwigocli import ( - "net/url" "os" "regexp" @@ -13,10 +12,6 @@ type CategoriesListCommand struct { Filter string `short:"x" long:"filter" description:"Regexp filter"` } -type GetCategoriesListResponse struct { - Categories piwigo.Categories `json:"categories"` -} - func (c *CategoriesListCommand) Execute(args []string) error { p := piwigo.Piwigo{} if err := p.LoadConfig(); err != nil { @@ -28,12 +23,8 @@ func (c *CategoriesListCommand) Execute(args []string) error { return err } - var resp GetCategoriesListResponse - - if err := p.Post("pwg.categories.getList", &url.Values{ - "recursive": []string{"true"}, - "fullname": []string{"true"}, - }, &resp); err != nil { + categories, err := p.Categories() + if err != nil { return err } @@ -44,7 +35,7 @@ func (c *CategoriesListCommand) Execute(args []string) error { t := table.NewWriter() t.AppendHeader(table.Row{"Id", "Name", "Images", "Url"}) - for _, category := range resp.Categories { + for _, category := range categories { if filter.MatchString(category.Name) { t.AppendRow(table.Row{ category.Id, diff --git a/internal/piwigocli/general.go b/internal/piwigocli/general.go index f7e1c63..ee27ea0 100644 --- a/internal/piwigocli/general.go +++ b/internal/piwigocli/general.go @@ -12,7 +12,14 @@ type GetInfosCommand struct { } type GetInfosResponse struct { - Infos piwigo.Infos `json:"infos"` + Infos Infos `json:"infos"` +} + +type Infos []Info + +type Info struct { + Name string `json:"name"` + Value interface{} `json:"value"` } var getInfosCommand GetInfosCommand diff --git a/internal/piwigocli/images.go b/internal/piwigocli/images.go index 2a53de9..2c729c0 100644 --- a/internal/piwigocli/images.go +++ b/internal/piwigocli/images.go @@ -2,7 +2,7 @@ package piwigocli type ImagesGroup struct { List ImagesListCommand `command:"list" description:"List of images"` - Details ImagesDetailsCommand `command:"details" description:"Details of the images"` + Details ImageDetailsCommand `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"` } diff --git a/internal/piwigocli/images_details.go b/internal/piwigocli/images_details.go index 1fe2bd8..4917f45 100644 --- a/internal/piwigocli/images_details.go +++ b/internal/piwigocli/images_details.go @@ -7,16 +7,15 @@ import ( "strings" "github.com/celogeek/piwigo-cli/internal/piwigo" + "github.com/celogeek/piwigo-cli/internal/piwigo/piwigotools" "github.com/jedib0t/go-pretty/v6/table" ) -type ImagesDetailsCommand struct { +type ImageDetailsCommand struct { Id string `short:"i" long:"id" description:"ID of the images" required:"true"` } -type GetImagesDetailsResponse piwigo.ImagesDetails - -func (c *ImagesDetailsCommand) Execute(args []string) error { +func (c *ImageDetailsCommand) Execute(args []string) error { p := piwigo.Piwigo{} if err := p.LoadConfig(); err != nil { return err @@ -27,7 +26,7 @@ func (c *ImagesDetailsCommand) Execute(args []string) error { return err } - var resp GetImagesDetailsResponse + var resp piwigotools.ImageDetails if err := p.Post("pwg.images.getInfo", &url.Values{ "image_id": []string{c.Id}, }, &resp); err != nil { diff --git a/internal/piwigocli/images_list.go b/internal/piwigocli/images_list.go index a90b471..43cc3c0 100644 --- a/internal/piwigocli/images_list.go +++ b/internal/piwigocli/images_list.go @@ -8,6 +8,7 @@ import ( "strings" "github.com/celogeek/piwigo-cli/internal/piwigo" + "github.com/celogeek/piwigo-cli/internal/piwigo/piwigotools" "github.com/schollz/progressbar/v3" ) @@ -19,7 +20,7 @@ type ImagesListCommand struct { } type ImagesListResult struct { - Images []*piwigo.ImagesDetails `json:"images"` + Images []*piwigotools.ImageDetails `json:"images"` Paging struct { Count int `json:"count"` Page int `json:"page"` @@ -39,7 +40,7 @@ func (c *ImagesListCommand) Execute(args []string) error { return err } - categories, err := p.Categories() + categories, err := p.CategoryFromId() if err != nil { return err } diff --git a/internal/piwigocli/images_upload.go b/internal/piwigocli/images_upload.go index ab4f322..eb43009 100644 --- a/internal/piwigocli/images_upload.go +++ b/internal/piwigocli/images_upload.go @@ -6,6 +6,7 @@ import ( "strings" "github.com/celogeek/piwigo-cli/internal/piwigo" + "github.com/celogeek/piwigo-cli/internal/piwigo/piwigotools" "github.com/schollz/progressbar/v3" ) @@ -33,13 +34,13 @@ func (c *ImagesUploadCommand) Execute(args []string) error { _, hasVideoJS := status.Plugins["piwigo-videojs"] - file := &piwigo.FileToUpload{ + file := &piwigotools.FileToUpload{ Dir: filepath.Dir(c.Filename), Name: filepath.Base(c.Filename), CategoryId: c.CategoryId, } - stat := &piwigo.FileToUploadStat{ + stat := &piwigotools.FileToUploadStat{ Progress: progressbar.DefaultBytes(1, "..."), } defer stat.Close() diff --git a/internal/piwigocli/images_upload_tree.go b/internal/piwigocli/images_upload_tree.go index 16afacd..f3a3782 100644 --- a/internal/piwigocli/images_upload_tree.go +++ b/internal/piwigocli/images_upload_tree.go @@ -2,6 +2,7 @@ package piwigocli import ( "github.com/celogeek/piwigo-cli/internal/piwigo" + "github.com/celogeek/piwigo-cli/internal/piwigo/piwigotools" "github.com/schollz/progressbar/v3" ) @@ -23,13 +24,13 @@ func (c *ImagesUploadTreeCommand) Execute(args []string) error { } _, hasVideoJS := status.Plugins["piwigo-videojs"] - stat := &piwigo.FileToUploadStat{ + stat := &piwigotools.FileToUploadStat{ Progress: progressbar.DefaultBytes(1, "..."), } defer stat.Close() - filesToCheck := make(chan *piwigo.FileToUpload, 1000) - files := make(chan *piwigo.FileToUpload, 1000) + filesToCheck := make(chan *piwigotools.FileToUpload, 1000) + files := make(chan *piwigotools.FileToUpload, 1000) go p.ScanTree(c.Dirname, c.CategoryId, 0, &status.UploadFileType, stat, filesToCheck) go p.CheckFiles(filesToCheck, files, stat, 8) diff --git a/internal/piwigocli/method_details.go b/internal/piwigocli/method_details.go index 7ba96a2..c202662 100644 --- a/internal/piwigocli/method_details.go +++ b/internal/piwigocli/method_details.go @@ -14,8 +14,6 @@ type MethodDetailsCommand struct { MethodName string `short:"m" long:"method-name" description:"Method name to details"` } -type MethodDetailsResult piwigo.MethodDetails - func (c *MethodDetailsCommand) Execute(args []string) error { p := piwigo.Piwigo{} if err := p.LoadConfig(); err != nil { @@ -27,7 +25,7 @@ func (c *MethodDetailsCommand) Execute(args []string) error { return err } - var result MethodDetailsResult + var result MethodDetails if err := p.Post("reflection.getMethodDetails", &url.Values{ "methodName": []string{c.MethodName}, diff --git a/internal/piwigocli/method_list.go b/internal/piwigocli/method_list.go index cc16a30..6c097dc 100644 --- a/internal/piwigocli/method_list.go +++ b/internal/piwigocli/method_list.go @@ -1,6 +1,7 @@ package piwigocli import ( + "encoding/json" "os" "regexp" @@ -13,7 +14,47 @@ type MethodListCommand struct { } type MethodListResult struct { - Methods piwigo.Methods `json:"methods"` + Methods Methods `json:"methods"` +} + +type Methods []string + +type MethodParams []MethodParam + +type MethodParam struct { + Name string `json:"name"` + Optional bool `json:"optional"` + Type string `json:"type"` + AcceptArray bool `json:"acceptArray"` + DefaultValue interface{} `json:"defaultValue"` + MaxValue interface{} `json:"maxValue"` + Info string `json:"info"` +} + +type MethodOptions struct { + Admin bool `json:"admin_only"` + PostOnly bool `json:"post_only"` +} + +func (j *MethodOptions) UnmarshalJSON(data []byte) error { + var r interface{} + if err := json.Unmarshal(data, &r); err != nil { + return err + } + + switch r := r.(type) { + case map[string]interface{}: + j.Admin, _ = r["admin_only"].(bool) + j.PostOnly, _ = r["post_only"].(bool) + } + return nil +} + +type MethodDetails struct { + Name string `json:"name"` + Description string `json:"description"` + Options MethodOptions `json:"options"` + Parameters MethodParams `json:"params"` } func (c *MethodListCommand) Execute(args []string) error { diff --git a/internal/piwigocli/method_try.go b/internal/piwigocli/method_try.go index 57aa082..e0d4e1b 100644 --- a/internal/piwigocli/method_try.go +++ b/internal/piwigocli/method_try.go @@ -1,7 +1,12 @@ package piwigocli import ( + "errors" + "net/url" + "strings" + "github.com/celogeek/piwigo-cli/internal/piwigo" + "github.com/celogeek/piwigo-cli/internal/piwigo/piwigotools" ) type MethodTryCommand struct { @@ -20,19 +25,31 @@ func (c *MethodTryCommand) Execute(args []string) error { } var result interface{} - params, err := piwigo.ArgsToForm(args) + params, err := ArgsToForm(args) if err != nil { return err } if err := p.Post(c.MethodName, params, &result); err != nil { - piwigo.DumpResponse(params) + piwigotools.DumpResponse(params) return err } - piwigo.DumpResponse(map[string]interface{}{ + piwigotools.DumpResponse(map[string]interface{}{ "params": params, "result": result, }) return nil } + +func ArgsToForm(args []string) (*url.Values, error) { + params := &url.Values{} + for _, arg := range args { + r := strings.SplitN(arg, "=", 2) + if len(r) != 2 { + return nil, errors.New("args should be key=value") + } + params.Add(r[0], strings.ReplaceAll(r[1], "'", `\'`)) + } + return params, nil +}