2019-03-23 22:40:56 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2019 Philipp Haefelfinger (http://www.haefelfinger.ch/). All Rights Reserved.
|
|
|
|
* This application is licensed under GPLv2. See the LICENSE file in the root directory of the project.
|
|
|
|
*/
|
|
|
|
|
2019-04-02 21:58:54 +02:00
|
|
|
package category
|
2019-02-25 23:36:18 +01:00
|
|
|
|
|
|
|
import (
|
2019-02-25 23:51:18 +01:00
|
|
|
"errors"
|
2019-02-26 22:50:33 +01:00
|
|
|
"fmt"
|
2019-04-02 23:26:55 +02:00
|
|
|
"git.haefelfinger.net/piwigo/PiwigoDirectoryUploader/internal/pkg/datastore"
|
2019-03-03 23:44:13 +01:00
|
|
|
"git.haefelfinger.net/piwigo/PiwigoDirectoryUploader/internal/pkg/localFileStructure"
|
2019-03-11 21:45:46 +01:00
|
|
|
"git.haefelfinger.net/piwigo/PiwigoDirectoryUploader/internal/pkg/piwigo"
|
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
|
|
|
)
|
|
|
|
|
2019-04-04 16:12:46 +02:00
|
|
|
func SynchronizeCategories(filesystemNodes map[string]*localFileStructure.FilesystemNode, piwigoApi piwigo.PiwigoCategoryApi, db datastore.CategoryProvider) error {
|
|
|
|
logrus.Debug("Entering SynchronizeCategories...")
|
|
|
|
defer logrus.Debug("Leaving SynchronizeCategories...")
|
|
|
|
|
|
|
|
err := updatePiwigoCategoriesFromServer(piwigoApi, db)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
logrus.Infoln("Adding missing categories to local db...")
|
|
|
|
err = addMissingPiwigoCategoriesToLocalDb(db, filesystemNodes)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return createMissingCategories(piwigoApi, db)
|
|
|
|
}
|
|
|
|
|
|
|
|
func addMissingPiwigoCategoriesToLocalDb(db datastore.CategoryProvider, fileSystemNodes map[string]*localFileStructure.FilesystemNode) error {
|
|
|
|
logrus.Debug("Entering addMissingPiwigoCategoriesToLocalDb...")
|
|
|
|
defer logrus.Debug("Leave addMissingPiwigoCategoriesToLocalDb...")
|
|
|
|
|
|
|
|
for _, file := range fileSystemNodes {
|
|
|
|
if !file.IsDir {
|
|
|
|
logrus.Tracef("%s: Skipping as no directory", file.Key)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := db.GetCategoryByKey(file.Key)
|
|
|
|
if err == nil {
|
|
|
|
logrus.Debugf("%s already exists.", file.Key)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err != datastore.ErrorRecordNotFound {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
logrus.Debugf("Creating missing category %s", file.Key)
|
|
|
|
category := datastore.CategoryData{
|
|
|
|
Key: file.Key,
|
|
|
|
Name: file.Name,
|
|
|
|
PiwigoParentId: 0,
|
|
|
|
PiwigoId: 0,
|
|
|
|
}
|
|
|
|
|
|
|
|
err = db.SaveCategory(category)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-04-04 15:39:32 +02:00
|
|
|
func updatePiwigoCategoriesFromServer(piwigoApi piwigo.PiwigoCategoryApi, db datastore.CategoryProvider) error {
|
|
|
|
logrus.Debug("Entering updatePiwigoCategoriesFromServer")
|
|
|
|
defer logrus.Debug("Leaving updatePiwigoCategoriesFromServer")
|
2019-04-04 08:54:35 +02:00
|
|
|
|
|
|
|
categories, err := piwigoApi.GetAllCategories()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, pwgcat := range categories {
|
2019-04-04 15:35:42 +02:00
|
|
|
dbcat, err := db.GetCategoryByPiwigoId(pwgcat.Id)
|
|
|
|
if err == datastore.ErrorRecordNotFound {
|
|
|
|
logrus.Debugf("Adding category %s", pwgcat.Key)
|
|
|
|
dbcat = datastore.CategoryData{
|
2019-04-04 15:39:32 +02:00
|
|
|
PiwigoId: pwgcat.Id,
|
2019-04-04 15:35:42 +02:00
|
|
|
}
|
|
|
|
} else if err != nil {
|
|
|
|
return err
|
2019-04-04 08:54:35 +02:00
|
|
|
}
|
|
|
|
|
2019-04-04 15:35:42 +02:00
|
|
|
if dbcat.Name == pwgcat.Name && dbcat.Key == pwgcat.Key && dbcat.PiwigoParentId == pwgcat.ParentId {
|
|
|
|
logrus.Debugf("No changes for category %s", dbcat.Key)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
dbcat.Name = pwgcat.Name
|
|
|
|
dbcat.Key = pwgcat.Key
|
|
|
|
dbcat.PiwigoParentId = pwgcat.ParentId
|
|
|
|
|
|
|
|
err = db.SaveCategory(dbcat)
|
2019-04-04 08:54:35 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2019-04-02 23:26:55 +02:00
|
|
|
}
|
|
|
|
|
2019-04-04 16:12:46 +02:00
|
|
|
func createMissingCategories(piwigoApi piwigo.PiwigoCategoryApi, db datastore.CategoryProvider) error {
|
|
|
|
logrus.Debug("Entering createMissingCategories...")
|
|
|
|
defer logrus.Debug("Leaving createMissingCategories...")
|
2019-02-25 23:36:18 +01:00
|
|
|
|
2019-04-04 16:12:46 +02:00
|
|
|
missingCategories, err := db.GetCategoriesToCreate()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-02-25 23:36:18 +01:00
|
|
|
}
|
|
|
|
|
2019-02-26 22:55:45 +01:00
|
|
|
logrus.Infof("Creating %d categories", len(missingCategories))
|
|
|
|
|
2019-04-04 16:12:46 +02:00
|
|
|
for _, category := range missingCategories {
|
|
|
|
logrus.Infof("Creating category %s", category.Key)
|
2019-02-26 22:50:33 +01:00
|
|
|
|
2019-04-04 16:12:46 +02:00
|
|
|
parentId, err := getParentId(category, db)
|
2019-02-26 22:50:33 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-02-25 23:51:18 +01:00
|
|
|
|
|
|
|
// create category on piwigo
|
2019-04-04 16:12:46 +02:00
|
|
|
id, err := piwigoApi.CreateCategory(parentId, category.Name)
|
2019-02-26 22:50:33 +01:00
|
|
|
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-04-04 16:12:46 +02:00
|
|
|
// update local category information
|
|
|
|
category.PiwigoId = id
|
|
|
|
category.PiwigoParentId = parentId
|
|
|
|
|
|
|
|
err = db.SaveCategory(category)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-02-26 22:50:33 +01:00
|
|
|
}
|
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-04-04 16:12:46 +02:00
|
|
|
func getParentId(category datastore.CategoryData, db datastore.CategoryProvider) (int, error) {
|
|
|
|
parentKey := filepath.Dir(category.Key)
|
|
|
|
if category.Name == parentKey {
|
|
|
|
logrus.Debugf("The category %s is a root category, there is no parent", category.Name)
|
|
|
|
return 0, 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
|
|
|
logrus.Debugf("Looking up parent with key %s", parentKey)
|
2019-04-04 16:12:46 +02:00
|
|
|
parentCategory, err := db.GetCategoryByKey(parentKey)
|
|
|
|
if err == datastore.ErrorRecordNotFound {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
2019-02-25 23:36:18 +01:00
|
|
|
}
|
|
|
|
|
2019-04-04 16:12:46 +02:00
|
|
|
return parentCategory.PiwigoId, nil
|
2019-02-25 23:36:18 +01:00
|
|
|
}
|