restructured the app code a bit
updated errorhandling to make exit codes only in app.go
This commit is contained in:
parent
fbd92489f2
commit
6df316fe19
@ -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) {
|
||||
|
57
internal/app/category.go
Normal file
57
internal/app/category.go
Normal file
@ -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
|
||||
}
|
17
internal/app/images.go
Normal file
17
internal/app/images.go
Normal file
@ -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)")
|
||||
}
|
@ -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
|
||||
}
|
||||
|
@ -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
|
||||
}
|
@ -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!")
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user