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"
|
"fmt"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
"haefelfinger.net/piwigo/DirectoriesToAlbums/internal/pkg/localFileStructure"
|
"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"
|
||||||
"haefelfinger.net/piwigo/DirectoriesToAlbums/internal/pkg/piwigo/authentication"
|
"haefelfinger.net/piwigo/DirectoriesToAlbums/internal/pkg/piwigo/authentication"
|
||||||
"haefelfinger.net/piwigo/DirectoriesToAlbums/internal/pkg/piwigo/category"
|
|
||||||
"os"
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -31,58 +29,27 @@ func Run() {
|
|||||||
os.Exit(2)
|
os.Exit(2)
|
||||||
}
|
}
|
||||||
|
|
||||||
filesystemNodes := scanLocalDirectories(context)
|
filesystemNodes, err := localFileStructure.ScanLocalFileStructure(context.LocalRootPath)
|
||||||
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)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
os.Exit(3)
|
os.Exit(3)
|
||||||
}
|
}
|
||||||
return fileNodes
|
|
||||||
}
|
|
||||||
|
|
||||||
func getAllCategoriesFromServer(context *AppContext) map[string]*category.PiwigoCategory {
|
categories, err := getAllCategoriesFromServer(context)
|
||||||
|
|
||||||
categories, err := category.GetAllCategories(context.Piwigo)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
os.Exit(4)
|
os.Exit(4)
|
||||||
}
|
}
|
||||||
|
|
||||||
return categories
|
err = synchronizeCategories(filesystemNodes, categories)
|
||||||
}
|
if err != nil {
|
||||||
|
os.Exit(5)
|
||||||
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)
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
func findMissingImages() {
|
err = synchronizeImages()
|
||||||
logrus.Warnln("Finding missing images (NotImplemented)")
|
if err != nil {
|
||||||
}
|
os.Exit(6)
|
||||||
|
}
|
||||||
|
|
||||||
func uploadImages() {
|
_ = authentication.Logout(context.Piwigo)
|
||||||
logrus.Warnln("Uploading missing images (NotImplemented)")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func configureContext() (*AppContext, error) {
|
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
|
package localFileStructure
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/sirupsen/logrus"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
@ -9,7 +10,10 @@ import (
|
|||||||
func ScanLocalFileStructure(path string) (map[string]*FilesystemNode, error) {
|
func ScanLocalFileStructure(path string) (map[string]*FilesystemNode, error) {
|
||||||
fileMap := make(map[string]*FilesystemNode)
|
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 {
|
err := filepath.Walk(path, func(p string, info os.FileInfo, err error) error {
|
||||||
if path == p {
|
if path == p {
|
||||||
@ -18,13 +22,20 @@ func ScanLocalFileStructure(path string) (map[string]*FilesystemNode, error) {
|
|||||||
|
|
||||||
//TODO: Only allow jpg and png files here
|
//TODO: Only allow jpg and png files here
|
||||||
|
|
||||||
key := strings.Replace(p,relativeRoot,"",1)
|
key := strings.Replace(p, relativeRoot, "", 1)
|
||||||
|
|
||||||
fileMap[p] = &FilesystemNode{
|
fileMap[p] = &FilesystemNode{
|
||||||
Key: key,
|
Key: key,
|
||||||
Name: info.Name(),
|
Name: info.Name(),
|
||||||
IsDir: info.IsDir(),
|
IsDir: info.IsDir(),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if info.IsDir() {
|
||||||
|
numberOfDirectories += 1
|
||||||
|
} else {
|
||||||
|
numberOfImages += 1
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -32,5 +43,7 @@ func ScanLocalFileStructure(path string) (map[string]*FilesystemNode, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
logrus.Infof("Found %d directories and %d images on the local filesystem", numberOfDirectories, numberOfImages)
|
||||||
|
|
||||||
return fileMap, nil
|
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) {
|
func GetAllCategories(context *piwigo.PiwigoContext) (map[string]*PiwigoCategory, error) {
|
||||||
logrus.Debugln("Starting GetAllCategories")
|
|
||||||
if context.Cookies == nil {
|
if context.Cookies == nil {
|
||||||
return nil, errors.New("Not logged in and no cookies found! Can not get the category list!")
|
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