From 6df316fe19e5e9ef24e7e8ed08fa0a2aa12a147d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20H=C3=A4felfinger?= Date: Mon, 25 Feb 2019 23:36:18 +0100 Subject: [PATCH] restructured the app code a bit updated errorhandling to make exit codes only in app.go --- internal/app/app.go | 53 ++++------------- internal/app/category.go | 57 +++++++++++++++++++ internal/app/images.go | 17 ++++++ .../localFileStructure/filesystemScanner.go | 17 +++++- internal/pkg/matcher/category.go | 27 --------- internal/pkg/piwigo/category/query.go | 1 - 6 files changed, 99 insertions(+), 73 deletions(-) create mode 100644 internal/app/category.go create mode 100644 internal/app/images.go delete mode 100644 internal/pkg/matcher/category.go diff --git a/internal/app/app.go b/internal/app/app.go index 7a45465..752fc2e 100644 --- a/internal/app/app.go +++ b/internal/app/app.go @@ -6,10 +6,8 @@ import ( "fmt" "github.com/sirupsen/logrus" "haefelfinger.net/piwigo/DirectoriesToAlbums/internal/pkg/localFileStructure" - "haefelfinger.net/piwigo/DirectoriesToAlbums/internal/pkg/matcher" "haefelfinger.net/piwigo/DirectoriesToAlbums/internal/pkg/piwigo" "haefelfinger.net/piwigo/DirectoriesToAlbums/internal/pkg/piwigo/authentication" - "haefelfinger.net/piwigo/DirectoriesToAlbums/internal/pkg/piwigo/category" "os" ) @@ -31,58 +29,27 @@ func Run() { os.Exit(2) } - filesystemNodes := scanLocalDirectories(context) - categories := getAllCategoriesFromServer(context) - - synchronizeCategories(filesystemNodes, categories) - - - findMissingImages() - uploadImages() - - _ = authentication.Logout(context.Piwigo) -} - -func synchronizeCategories(filesystemNodes map[string]*localFileStructure.FilesystemNode, categories map[string]*category.PiwigoCategory) { - missingCategories := findMissingCategories(filesystemNodes, categories) - createMissingCategories(missingCategories) -} - -func scanLocalDirectories(context *AppContext) map[string]*localFileStructure.FilesystemNode { - fileNodes, err := localFileStructure.ScanLocalFileStructure(context.LocalRootPath) + filesystemNodes, err := localFileStructure.ScanLocalFileStructure(context.LocalRootPath) if err != nil { os.Exit(3) } - return fileNodes -} -func getAllCategoriesFromServer(context *AppContext) map[string]*category.PiwigoCategory { - - categories, err := category.GetAllCategories(context.Piwigo) + categories, err := getAllCategoriesFromServer(context) if err != nil { os.Exit(4) } - return categories -} - -func findMissingCategories(fileSystem map[string]*localFileStructure.FilesystemNode, categories map[string]*category.PiwigoCategory) []string { - return matcher.FindMissingCategories(fileSystem, categories) -} - -func createMissingCategories(categories []string) { - logrus.Warnln("Creating missing albums (NotImplemented)") - for _, c := range categories { - logrus.Debug(c) + err = synchronizeCategories(filesystemNodes, categories) + if err != nil { + os.Exit(5) } -} -func findMissingImages() { - logrus.Warnln("Finding missing images (NotImplemented)") -} + err = synchronizeImages() + if err != nil { + os.Exit(6) + } -func uploadImages() { - logrus.Warnln("Uploading missing images (NotImplemented)") + _ = authentication.Logout(context.Piwigo) } func configureContext() (*AppContext, error) { diff --git a/internal/app/category.go b/internal/app/category.go new file mode 100644 index 0000000..c1eac45 --- /dev/null +++ b/internal/app/category.go @@ -0,0 +1,57 @@ +package app + +import ( + "github.com/sirupsen/logrus" + "haefelfinger.net/piwigo/DirectoriesToAlbums/internal/pkg/localFileStructure" + "haefelfinger.net/piwigo/DirectoriesToAlbums/internal/pkg/piwigo/category" + "sort" +) + +func getAllCategoriesFromServer(context *AppContext) (map[string]*category.PiwigoCategory, error) { + logrus.Debugln("Starting GetAllCategories") + categories, err := category.GetAllCategories(context.Piwigo) + return categories, err +} + +func synchronizeCategories(filesystemNodes map[string]*localFileStructure.FilesystemNode, existingCategories map[string]*category.PiwigoCategory) error { + logrus.Infoln("Synchronizing categories...") + + missingCategories := findMissingCategories(filesystemNodes, existingCategories) + + return createMissingCategories(missingCategories, existingCategories) +} + +func findMissingCategories(fileSystem map[string]*localFileStructure.FilesystemNode, existingCategories map[string]*category.PiwigoCategory) []string { + missingCategories := []string{} + + for _, file := range fileSystem { + if !file.IsDir { + continue + } + + _, exists := existingCategories[file.Key] + + if !exists { + logrus.Infof("Missing category detected %s", file.Key) + missingCategories = append(missingCategories, file.Key) + } else { + logrus.Debugf("Found existing category %s", file.Key) + } + } + + return missingCategories +} + +func createMissingCategories(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) + + logrus.Warnln("Creating missing albums (NotImplemented)") + for _, c := range missingCategories { + logrus.Debug(c) + } + + return nil +} diff --git a/internal/app/images.go b/internal/app/images.go new file mode 100644 index 0000000..fafe1c1 --- /dev/null +++ b/internal/app/images.go @@ -0,0 +1,17 @@ +package app + +import "github.com/sirupsen/logrus" + +func synchronizeImages() error { + findMissingImages() + uploadImages() + return nil +} + +func findMissingImages() { + logrus.Warnln("Finding missing images (NotImplemented)") +} + +func uploadImages() { + logrus.Warnln("Uploading missing images (NotImplemented)") +} diff --git a/internal/pkg/localFileStructure/filesystemScanner.go b/internal/pkg/localFileStructure/filesystemScanner.go index 0dff1a5..c221af7 100644 --- a/internal/pkg/localFileStructure/filesystemScanner.go +++ b/internal/pkg/localFileStructure/filesystemScanner.go @@ -1,6 +1,7 @@ package localFileStructure import ( + "github.com/sirupsen/logrus" "os" "path/filepath" "strings" @@ -9,7 +10,10 @@ import ( func ScanLocalFileStructure(path string) (map[string]*FilesystemNode, error) { fileMap := make(map[string]*FilesystemNode) - relativeRoot := filepath.Base(path)+"/" + relativeRoot := filepath.Base(path) + "/" + + numberOfDirectories := 0 + numberOfImages := 0 err := filepath.Walk(path, func(p string, info os.FileInfo, err error) error { if path == p { @@ -18,13 +22,20 @@ func ScanLocalFileStructure(path string) (map[string]*FilesystemNode, error) { //TODO: Only allow jpg and png files here - key := strings.Replace(p,relativeRoot,"",1) + key := strings.Replace(p, relativeRoot, "", 1) fileMap[p] = &FilesystemNode{ Key: key, Name: info.Name(), IsDir: info.IsDir(), } + + if info.IsDir() { + numberOfDirectories += 1 + } else { + numberOfImages += 1 + } + return nil }) @@ -32,5 +43,7 @@ func ScanLocalFileStructure(path string) (map[string]*FilesystemNode, error) { return nil, err } + logrus.Infof("Found %d directories and %d images on the local filesystem", numberOfDirectories, numberOfImages) + return fileMap, nil } diff --git a/internal/pkg/matcher/category.go b/internal/pkg/matcher/category.go deleted file mode 100644 index def6645..0000000 --- a/internal/pkg/matcher/category.go +++ /dev/null @@ -1,27 +0,0 @@ -package matcher - -import ( - "github.com/sirupsen/logrus" - "haefelfinger.net/piwigo/DirectoriesToAlbums/internal/pkg/localFileStructure" - "haefelfinger.net/piwigo/DirectoriesToAlbums/internal/pkg/piwigo/category" -) - -func FindMissingCategories(fileSystem map[string]*localFileStructure.FilesystemNode, categories map[string]*category.PiwigoCategory) []string { - missingCategories := []string{} - for _, file := range fileSystem { - if !file.IsDir { - continue - } - - _, exists := categories[file.Key] - - if !exists { - logrus.Infof("Found missing category %s", file.Key) - missingCategories = append(missingCategories, file.Key) - } else { - logrus.Debugf("Found existing category %s", file.Key) - } - } - - return missingCategories -} diff --git a/internal/pkg/piwigo/category/query.go b/internal/pkg/piwigo/category/query.go index 467547b..72acead 100644 --- a/internal/pkg/piwigo/category/query.go +++ b/internal/pkg/piwigo/category/query.go @@ -11,7 +11,6 @@ import ( ) func GetAllCategories(context *piwigo.PiwigoContext) (map[string]*PiwigoCategory, error) { - logrus.Debugln("Starting GetAllCategories") if context.Cookies == nil { return nil, errors.New("Not logged in and no cookies found! Can not get the category list!") }