made category create work
This commit is contained in:
parent
d80348053e
commit
669bb18cf4
@ -39,7 +39,7 @@ func Run() {
|
|||||||
logErrorAndExit(err, 4)
|
logErrorAndExit(err, 4)
|
||||||
}
|
}
|
||||||
|
|
||||||
err = synchronizeCategories(filesystemNodes, categories)
|
err = synchronizeCategories(context, filesystemNodes, categories)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logErrorAndExit(err, 5)
|
logErrorAndExit(err, 5)
|
||||||
}
|
}
|
||||||
|
@ -2,9 +2,11 @@ package app
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
"haefelfinger.net/piwigo/DirectoriesToAlbums/internal/pkg/localFileStructure"
|
"haefelfinger.net/piwigo/DirectoriesToAlbums/internal/pkg/localFileStructure"
|
||||||
"haefelfinger.net/piwigo/DirectoriesToAlbums/internal/pkg/piwigo/category"
|
"haefelfinger.net/piwigo/DirectoriesToAlbums/internal/pkg/piwigo/category"
|
||||||
|
"path/filepath"
|
||||||
"sort"
|
"sort"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -14,12 +16,12 @@ func getAllCategoriesFromServer(context *AppContext) (map[string]*category.Piwig
|
|||||||
return categories, err
|
return categories, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func synchronizeCategories(filesystemNodes map[string]*localFileStructure.FilesystemNode, existingCategories map[string]*category.PiwigoCategory) error {
|
func synchronizeCategories(context *AppContext, filesystemNodes map[string]*localFileStructure.FilesystemNode, existingCategories map[string]*category.PiwigoCategory) error {
|
||||||
logrus.Infoln("Synchronizing categories...")
|
logrus.Infoln("Synchronizing categories...")
|
||||||
|
|
||||||
missingCategories := findMissingCategories(filesystemNodes, existingCategories)
|
missingCategories := findMissingCategories(filesystemNodes, existingCategories)
|
||||||
|
|
||||||
return createMissingCategories(missingCategories, existingCategories)
|
return createMissingCategories(context, missingCategories, existingCategories)
|
||||||
}
|
}
|
||||||
|
|
||||||
func findMissingCategories(fileSystem map[string]*localFileStructure.FilesystemNode, existingCategories map[string]*category.PiwigoCategory) []string {
|
func findMissingCategories(fileSystem map[string]*localFileStructure.FilesystemNode, existingCategories map[string]*category.PiwigoCategory) []string {
|
||||||
@ -43,26 +45,49 @@ func findMissingCategories(fileSystem map[string]*localFileStructure.FilesystemN
|
|||||||
return missingCategories
|
return missingCategories
|
||||||
}
|
}
|
||||||
|
|
||||||
func createMissingCategories(missingCategories []string, existingCategories map[string]*category.PiwigoCategory) error {
|
func createMissingCategories(context *AppContext, missingCategories []string, existingCategories map[string]*category.PiwigoCategory) error {
|
||||||
|
|
||||||
// we sort them to make sure the categories gets created
|
// we sort them to make sure the categories gets created
|
||||||
// in the right order and we have the parent available while creating the children
|
// in the right order and we have the parent available while creating the children
|
||||||
sort.Strings(missingCategories)
|
sort.Strings(missingCategories)
|
||||||
|
|
||||||
for _, c := range missingCategories {
|
for _, categoryKey := range missingCategories {
|
||||||
logrus.Infof("Creating category %s",c)
|
logrus.Infof("Creating category %s", categoryKey)
|
||||||
|
|
||||||
|
name, parentId, err := getNameAndParentId(categoryKey, existingCategories)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
// create category on piwigo
|
// create category on piwigo
|
||||||
|
id, err := category.CreateCategory(context.Piwigo, parentId, name)
|
||||||
// build new map entry
|
if err != nil {
|
||||||
|
return errors.New(fmt.Sprintf("Could not create category on piwigo: %s", err))
|
||||||
// get parent entry by path
|
|
||||||
// set parent entry id
|
|
||||||
|
|
||||||
// calculate new map key
|
|
||||||
// add to existing map
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return errors.New("NOT IMPLEMENTED")
|
newCategory := category.PiwigoCategory{Id: id, Name: name, ParentId: parentId, Key: categoryKey}
|
||||||
|
logrus.Println(newCategory)
|
||||||
|
existingCategories[newCategory.Key] = &newCategory
|
||||||
|
}
|
||||||
|
|
||||||
|
return errors.New("createMissingCategories: NOT IMPLEMENTED")
|
||||||
|
}
|
||||||
|
|
||||||
|
func getNameAndParentId(category string, categories map[string]*category.PiwigoCategory) (string, int, error) {
|
||||||
|
parentKey := filepath.Dir(category)
|
||||||
|
_, name := filepath.Split(category)
|
||||||
|
if name == category {
|
||||||
|
logrus.Debugf("The category %s is a root category, there is no parent", name)
|
||||||
|
return name, 0, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
logrus.Debugf("Looking up parent with key %s", parentKey)
|
||||||
|
parent, exists := categories[parentKey]
|
||||||
|
if !exists {
|
||||||
|
return "", 0, errors.New(fmt.Sprintf("could not find parent with key %s", parentKey))
|
||||||
|
}
|
||||||
|
|
||||||
|
parentId := parent.Id
|
||||||
|
logrus.Debugf("Found parent %s with id %d", parentKey, parentId)
|
||||||
|
|
||||||
|
return name, parentId, nil
|
||||||
}
|
}
|
||||||
|
@ -29,11 +29,11 @@ func Login(context *piwigo.PiwigoContext) error {
|
|||||||
client := http.Client{Jar: context.Cookies}
|
client := http.Client{Jar: context.Cookies}
|
||||||
|
|
||||||
response, err := client.PostForm(context.Url, formData)
|
response, err := client.PostForm(context.Url, formData)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logrus.Errorf("The HTTP request failed with error %s", err)
|
logrus.Errorf("The HTTP request failed with error %s", err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
defer response.Body.Close()
|
||||||
|
|
||||||
var loginResponse LoginResponse
|
var loginResponse LoginResponse
|
||||||
if err := json.NewDecoder(response.Body).Decode(&loginResponse); err != nil {
|
if err := json.NewDecoder(response.Body).Decode(&loginResponse); err != nil {
|
||||||
@ -61,11 +61,11 @@ func Logout(context *piwigo.PiwigoContext) error {
|
|||||||
|
|
||||||
client := http.Client{Jar: context.Cookies}
|
client := http.Client{Jar: context.Cookies}
|
||||||
response, err := client.PostForm(context.Url, formData)
|
response, err := client.PostForm(context.Url, formData)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logrus.Errorln("The HTTP request failed with error %s", err)
|
logrus.Errorln("The HTTP request failed with error %s", err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
defer response.Body.Close()
|
||||||
|
|
||||||
var statusResponse LogoutResponse
|
var statusResponse LogoutResponse
|
||||||
if err := json.NewDecoder(response.Body).Decode(&statusResponse); err != nil {
|
if err := json.NewDecoder(response.Body).Decode(&statusResponse); err != nil {
|
||||||
@ -92,11 +92,11 @@ func GetStatus(context *piwigo.PiwigoContext) (*GetStatusResponse, error) {
|
|||||||
|
|
||||||
client := http.Client{Jar: context.Cookies}
|
client := http.Client{Jar: context.Cookies}
|
||||||
response, err := client.PostForm(context.Url, formData)
|
response, err := client.PostForm(context.Url, formData)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logrus.Errorln("The HTTP request failed with error %s\n", err)
|
logrus.Errorln("The HTTP request failed with error %s\n", err)
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
defer response.Body.Close()
|
||||||
|
|
||||||
var statusResponse GetStatusResponse
|
var statusResponse GetStatusResponse
|
||||||
if err := json.NewDecoder(response.Body).Decode(&statusResponse); err != nil {
|
if err := json.NewDecoder(response.Body).Decode(&statusResponse); err != nil {
|
||||||
|
49
internal/pkg/piwigo/category/create.go
Normal file
49
internal/pkg/piwigo/category/create.go
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
package category
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"github.com/sirupsen/logrus"
|
||||||
|
"haefelfinger.net/piwigo/DirectoriesToAlbums/internal/pkg/piwigo"
|
||||||
|
"net/http"
|
||||||
|
"net/url"
|
||||||
|
)
|
||||||
|
|
||||||
|
func CreateCategory(context *piwigo.PiwigoContext, parentId int, name string) (int, error) {
|
||||||
|
if context.Cookies == nil {
|
||||||
|
return 0, errors.New("Not logged in and no cookies found! Can not get the category list!")
|
||||||
|
}
|
||||||
|
|
||||||
|
formData := url.Values{}
|
||||||
|
formData.Set("method", "pwg.categories.add")
|
||||||
|
formData.Set("name", name)
|
||||||
|
|
||||||
|
// we only submit the parentid if there is one.
|
||||||
|
if parentId > 0 {
|
||||||
|
formData.Set("parent", fmt.Sprint(parentId))
|
||||||
|
}
|
||||||
|
|
||||||
|
client := http.Client{Jar: context.Cookies}
|
||||||
|
response, err := client.PostForm(context.Url, formData)
|
||||||
|
if err != nil {
|
||||||
|
logrus.Errorln("The HTTP request failed with error %s", err)
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
defer response.Body.Close()
|
||||||
|
|
||||||
|
var createResponse createCategoryResponse
|
||||||
|
if err := json.NewDecoder(response.Body).Decode(&createResponse); err != nil {
|
||||||
|
logrus.Errorln(err)
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if createResponse.Status != "ok" {
|
||||||
|
logrus.Errorf("Got state %s while loading categories", createResponse.Status)
|
||||||
|
return 0, errors.New("Could not create category")
|
||||||
|
}
|
||||||
|
|
||||||
|
logrus.Infof("Successfully got all categories from %s", context.Url)
|
||||||
|
|
||||||
|
return createResponse.Result.ID, nil
|
||||||
|
}
|
@ -22,11 +22,11 @@ func GetAllCategories(context *piwigo.PiwigoContext) (map[string]*PiwigoCategory
|
|||||||
|
|
||||||
client := http.Client{Jar: context.Cookies}
|
client := http.Client{Jar: context.Cookies}
|
||||||
response, err := client.PostForm(context.Url, formData)
|
response, err := client.PostForm(context.Url, formData)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logrus.Errorln("The HTTP request failed with error %s", err)
|
logrus.Errorln("The HTTP request failed with error %s", err)
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
defer response.Body.Close()
|
||||||
|
|
||||||
var statusResponse getCategoryListResponse
|
var statusResponse getCategoryListResponse
|
||||||
if err := json.NewDecoder(response.Body).Decode(&statusResponse); err != nil {
|
if err := json.NewDecoder(response.Body).Decode(&statusResponse); err != nil {
|
||||||
@ -51,7 +51,7 @@ func GetAllCategories(context *piwigo.PiwigoContext) (map[string]*PiwigoCategory
|
|||||||
func buildLookupMap(categories map[int]*PiwigoCategory) map[string]*PiwigoCategory {
|
func buildLookupMap(categories map[int]*PiwigoCategory) map[string]*PiwigoCategory {
|
||||||
categoryLookups := map[string]*PiwigoCategory{}
|
categoryLookups := map[string]*PiwigoCategory{}
|
||||||
for _, category := range categories {
|
for _, category := range categories {
|
||||||
logrus.Debugf("Existing category %s", category.Key)
|
logrus.Debugf("Loaded existing category %s", category.Key)
|
||||||
categoryLookups[category.Key] = category
|
categoryLookups[category.Key] = category
|
||||||
}
|
}
|
||||||
return categoryLookups
|
return categoryLookups
|
||||||
|
@ -30,3 +30,13 @@ type getCategoryListResponse struct {
|
|||||||
} `json:"categories"`
|
} `json:"categories"`
|
||||||
} `json:"result"`
|
} `json:"result"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type createCategoryResponse struct {
|
||||||
|
Status string `json:"stat"`
|
||||||
|
Err int `json:"err"`
|
||||||
|
Message string `json:"message"`
|
||||||
|
Result struct {
|
||||||
|
Info string `json:"info"`
|
||||||
|
ID int `json:"id"`
|
||||||
|
} `json:"result"`
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user