2019-02-25 23:36:18 +01:00
|
|
|
package app
|
|
|
|
|
|
|
|
import (
|
2019-02-25 23:51:18 +01:00
|
|
|
"errors"
|
2019-02-26 22:50:33 +01:00
|
|
|
"fmt"
|
2019-03-01 21:36:16 +01:00
|
|
|
"git.haefelfinger.net/piwigo/DirectoriesToAlbums/internal/pkg/localFileStructure"
|
|
|
|
"git.haefelfinger.net/piwigo/DirectoriesToAlbums/internal/pkg/piwigo/category"
|
2019-03-02 00:21:01 +01:00
|
|
|
"github.com/sirupsen/logrus"
|
2019-02-26 22:50:33 +01:00
|
|
|
"path/filepath"
|
2019-02-25 23:36:18 +01:00
|
|
|
"sort"
|
|
|
|
)
|
|
|
|
|
2019-02-27 22:13:23 +01:00
|
|
|
func getAllCategoriesFromServer(context *appContext) (map[string]*category.PiwigoCategory, error) {
|
2019-02-25 23:36:18 +01:00
|
|
|
logrus.Debugln("Starting GetAllCategories")
|
|
|
|
categories, err := category.GetAllCategories(context.Piwigo)
|
|
|
|
return categories, err
|
|
|
|
}
|
|
|
|
|
2019-02-27 22:13:23 +01:00
|
|
|
func synchronizeCategories(context *appContext, filesystemNodes map[string]*localFileStructure.FilesystemNode, existingCategories map[string]*category.PiwigoCategory) error {
|
2019-02-25 23:36:18 +01:00
|
|
|
logrus.Infoln("Synchronizing categories...")
|
|
|
|
|
|
|
|
missingCategories := findMissingCategories(filesystemNodes, existingCategories)
|
|
|
|
|
2019-02-26 22:55:45 +01:00
|
|
|
if len(missingCategories) == 0 {
|
|
|
|
logrus.Infof("No categories missing!")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-02-26 22:50:33 +01:00
|
|
|
return createMissingCategories(context, missingCategories, existingCategories)
|
2019-02-25 23:36:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func findMissingCategories(fileSystem map[string]*localFileStructure.FilesystemNode, existingCategories map[string]*category.PiwigoCategory) []string {
|
2019-02-27 23:26:18 +01:00
|
|
|
missingCategories := make([]string, 0, len(fileSystem))
|
2019-02-25 23:36:18 +01:00
|
|
|
|
|
|
|
for _, file := range fileSystem {
|
|
|
|
if !file.IsDir {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
_, exists := existingCategories[file.Key]
|
|
|
|
|
2019-02-26 00:00:21 +01:00
|
|
|
if exists {
|
|
|
|
logrus.Debugf("Found existing category %s", file.Key)
|
|
|
|
} else {
|
2019-02-25 23:36:18 +01:00
|
|
|
logrus.Infof("Missing category detected %s", file.Key)
|
|
|
|
missingCategories = append(missingCategories, file.Key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return missingCategories
|
|
|
|
}
|
|
|
|
|
2019-02-27 22:13:23 +01:00
|
|
|
func createMissingCategories(context *appContext, missingCategories []string, existingCategories map[string]*category.PiwigoCategory) error {
|
2019-02-25 23:36:18 +01:00
|
|
|
// 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)
|
|
|
|
|
2019-02-26 22:55:45 +01:00
|
|
|
logrus.Infof("Creating %d categories", len(missingCategories))
|
|
|
|
|
2019-02-26 22:50:33 +01:00
|
|
|
for _, categoryKey := range missingCategories {
|
|
|
|
logrus.Infof("Creating category %s", categoryKey)
|
|
|
|
|
|
|
|
name, parentId, err := getNameAndParentId(categoryKey, existingCategories)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-02-25 23:51:18 +01:00
|
|
|
|
|
|
|
// create category on piwigo
|
2019-02-26 22:50:33 +01:00
|
|
|
id, err := category.CreateCategory(context.Piwigo, parentId, name)
|
|
|
|
if err != nil {
|
|
|
|
return errors.New(fmt.Sprintf("Could not create category on piwigo: %s", err))
|
|
|
|
}
|
2019-02-25 23:51:18 +01:00
|
|
|
|
2019-02-26 22:50:33 +01:00
|
|
|
newCategory := category.PiwigoCategory{Id: id, Name: name, ParentId: parentId, Key: categoryKey}
|
|
|
|
logrus.Println(newCategory)
|
|
|
|
existingCategories[newCategory.Key] = &newCategory
|
|
|
|
}
|
2019-02-25 23:51:18 +01:00
|
|
|
|
2019-02-26 22:51:42 +01:00
|
|
|
return nil
|
2019-02-26 22:50:33 +01:00
|
|
|
}
|
2019-02-25 23:51:18 +01:00
|
|
|
|
2019-02-26 22:50:33 +01:00
|
|
|
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
|
|
|
|
}
|
2019-02-25 23:51:18 +01:00
|
|
|
|
2019-02-26 22:50:33 +01:00
|
|
|
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))
|
2019-02-25 23:36:18 +01:00
|
|
|
}
|
|
|
|
|
2019-02-26 22:50:33 +01:00
|
|
|
parentId := parent.Id
|
|
|
|
logrus.Debugf("Found parent %s with id %d", parentKey, parentId)
|
|
|
|
|
|
|
|
return name, parentId, nil
|
2019-02-25 23:36:18 +01:00
|
|
|
}
|