From 669bb18cf4edb5438981dbaaf287a01687d63f93 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20H=C3=A4felfinger?= Date: Tue, 26 Feb 2019 22:50:33 +0100 Subject: [PATCH] made category create work --- internal/app/app.go | 2 +- internal/app/category.go | 55 ++++++++++++++----- .../localFileStructure/filesystemScanner.go | 2 +- .../piwigo/authentication/authentication.go | 6 +- internal/pkg/piwigo/category/create.go | 49 +++++++++++++++++ internal/pkg/piwigo/category/query.go | 4 +- internal/pkg/piwigo/category/types.go | 10 ++++ 7 files changed, 106 insertions(+), 22 deletions(-) create mode 100644 internal/pkg/piwigo/category/create.go diff --git a/internal/app/app.go b/internal/app/app.go index 469c333..07cc54a 100644 --- a/internal/app/app.go +++ b/internal/app/app.go @@ -39,7 +39,7 @@ func Run() { logErrorAndExit(err, 4) } - err = synchronizeCategories(filesystemNodes, categories) + err = synchronizeCategories(context, filesystemNodes, categories) if err != nil { logErrorAndExit(err, 5) } diff --git a/internal/app/category.go b/internal/app/category.go index ecfcbd5..611c8df 100644 --- a/internal/app/category.go +++ b/internal/app/category.go @@ -2,9 +2,11 @@ package app import ( "errors" + "fmt" "github.com/sirupsen/logrus" "haefelfinger.net/piwigo/DirectoriesToAlbums/internal/pkg/localFileStructure" "haefelfinger.net/piwigo/DirectoriesToAlbums/internal/pkg/piwigo/category" + "path/filepath" "sort" ) @@ -14,12 +16,12 @@ func getAllCategoriesFromServer(context *AppContext) (map[string]*category.Piwig 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...") 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 { @@ -43,26 +45,49 @@ func findMissingCategories(fileSystem map[string]*localFileStructure.FilesystemN 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 // in the right order and we have the parent available while creating the children sort.Strings(missingCategories) - for _, c := range missingCategories { - logrus.Infof("Creating category %s",c) + for _, categoryKey := range missingCategories { + logrus.Infof("Creating category %s", categoryKey) + + name, parentId, err := getNameAndParentId(categoryKey, existingCategories) + if err != nil { + return err + } // create category on piwigo + id, err := category.CreateCategory(context.Piwigo, parentId, name) + if err != nil { + return errors.New(fmt.Sprintf("Could not create category on piwigo: %s", err)) + } - // build new map entry - - // get parent entry by path - // set parent entry id - - // calculate new map key - // add to existing map - + newCategory := category.PiwigoCategory{Id: id, Name: name, ParentId: parentId, Key: categoryKey} + logrus.Println(newCategory) + existingCategories[newCategory.Key] = &newCategory } - return errors.New("NOT IMPLEMENTED") + 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 } diff --git a/internal/pkg/localFileStructure/filesystemScanner.go b/internal/pkg/localFileStructure/filesystemScanner.go index f08c274..da6f22a 100644 --- a/internal/pkg/localFileStructure/filesystemScanner.go +++ b/internal/pkg/localFileStructure/filesystemScanner.go @@ -17,7 +17,7 @@ func ScanLocalFileStructure(path string) (map[string]*FilesystemNode, error) { logrus.Infof("Scanning %s for images...", fullPathRoot) fileMap := make(map[string]*FilesystemNode) - fullPathReplace := fmt.Sprintf("%s%c",fullPathRoot, os.PathSeparator) + fullPathReplace := fmt.Sprintf("%s%c", fullPathRoot, os.PathSeparator) numberOfDirectories := 0 numberOfImages := 0 diff --git a/internal/pkg/piwigo/authentication/authentication.go b/internal/pkg/piwigo/authentication/authentication.go index cc4e5f6..ae613e5 100644 --- a/internal/pkg/piwigo/authentication/authentication.go +++ b/internal/pkg/piwigo/authentication/authentication.go @@ -29,11 +29,11 @@ func Login(context *piwigo.PiwigoContext) error { client := http.Client{Jar: context.Cookies} response, err := client.PostForm(context.Url, formData) - if err != nil { logrus.Errorf("The HTTP request failed with error %s", err) return err } + defer response.Body.Close() var loginResponse LoginResponse 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} response, err := client.PostForm(context.Url, formData) - if err != nil { logrus.Errorln("The HTTP request failed with error %s", err) return err } + defer response.Body.Close() var statusResponse LogoutResponse 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} response, err := client.PostForm(context.Url, formData) - if err != nil { logrus.Errorln("The HTTP request failed with error %s\n", err) return nil, err } + defer response.Body.Close() var statusResponse GetStatusResponse if err := json.NewDecoder(response.Body).Decode(&statusResponse); err != nil { diff --git a/internal/pkg/piwigo/category/create.go b/internal/pkg/piwigo/category/create.go new file mode 100644 index 0000000..9ac6041 --- /dev/null +++ b/internal/pkg/piwigo/category/create.go @@ -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 +} diff --git a/internal/pkg/piwigo/category/query.go b/internal/pkg/piwigo/category/query.go index 586af08..656412f 100644 --- a/internal/pkg/piwigo/category/query.go +++ b/internal/pkg/piwigo/category/query.go @@ -22,11 +22,11 @@ func GetAllCategories(context *piwigo.PiwigoContext) (map[string]*PiwigoCategory 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 nil, err } + defer response.Body.Close() var statusResponse getCategoryListResponse 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 { categoryLookups := map[string]*PiwigoCategory{} for _, category := range categories { - logrus.Debugf("Existing category %s", category.Key) + logrus.Debugf("Loaded existing category %s", category.Key) categoryLookups[category.Key] = category } return categoryLookups diff --git a/internal/pkg/piwigo/category/types.go b/internal/pkg/piwigo/category/types.go index fa58409..7fe878e 100644 --- a/internal/pkg/piwigo/category/types.go +++ b/internal/pkg/piwigo/category/types.go @@ -30,3 +30,13 @@ type getCategoryListResponse struct { } `json:"categories"` } `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"` +}