2019-02-23 21:57:54 +01:00
|
|
|
package app
|
|
|
|
|
|
|
|
import (
|
2019-02-24 23:27:31 +01:00
|
|
|
"errors"
|
2019-02-24 23:11:52 +01:00
|
|
|
"flag"
|
2019-02-24 23:39:55 +01:00
|
|
|
"fmt"
|
2019-02-23 21:57:54 +01:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
"haefelfinger.net/piwigo/DirectoriesToAlbums/internal/pkg/localFileStructure"
|
2019-02-25 23:11:09 +01:00
|
|
|
"haefelfinger.net/piwigo/DirectoriesToAlbums/internal/pkg/matcher"
|
2019-02-25 00:02:59 +01:00
|
|
|
"haefelfinger.net/piwigo/DirectoriesToAlbums/internal/pkg/piwigo"
|
2019-02-24 00:33:18 +01:00
|
|
|
"haefelfinger.net/piwigo/DirectoriesToAlbums/internal/pkg/piwigo/authentication"
|
2019-02-25 00:02:59 +01:00
|
|
|
"haefelfinger.net/piwigo/DirectoriesToAlbums/internal/pkg/piwigo/category"
|
2019-02-24 23:27:31 +01:00
|
|
|
"os"
|
2019-02-23 21:57:54 +01:00
|
|
|
)
|
|
|
|
|
2019-02-24 23:11:52 +01:00
|
|
|
var (
|
2019-02-24 23:27:31 +01:00
|
|
|
imagesRootPath = flag.String("imagesRootPath", "", "This is the images root path that should be mirrored to piwigo.")
|
2019-02-24 23:39:55 +01:00
|
|
|
piwigoUrl = flag.String("piwigoUrl", "", "The root url without tailing slash to your piwigo installation.")
|
2019-02-24 23:27:31 +01:00
|
|
|
piwigoUser = flag.String("piwigoUser", "", "The username to use during sync.")
|
|
|
|
piwigoPassword = flag.String("piwigoPassword", "", "This is password to the given username.")
|
2019-02-24 23:11:52 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func Run() {
|
2019-02-24 23:27:31 +01:00
|
|
|
context, err := configureContext()
|
|
|
|
if err != nil {
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2019-02-24 00:33:18 +01:00
|
|
|
|
2019-02-24 23:32:05 +01:00
|
|
|
err = loginToPiwigoAndConfigureContext(context)
|
|
|
|
if err != nil {
|
|
|
|
os.Exit(2)
|
|
|
|
}
|
2019-02-25 00:02:59 +01:00
|
|
|
|
2019-02-25 23:11:09 +01:00
|
|
|
filesystemNodes := scanLocalDirectories(context)
|
|
|
|
categories := getAllCategoriesFromServer(context)
|
2019-02-24 00:33:18 +01:00
|
|
|
|
2019-02-25 23:11:09 +01:00
|
|
|
synchronizeCategories(filesystemNodes, categories)
|
|
|
|
|
|
|
|
|
|
|
|
findMissingImages()
|
|
|
|
uploadImages()
|
2019-02-24 00:33:18 +01:00
|
|
|
|
2019-02-24 21:38:28 +01:00
|
|
|
_ = authentication.Logout(context.Piwigo)
|
2019-02-23 21:57:54 +01:00
|
|
|
}
|
|
|
|
|
2019-02-25 23:11:09 +01:00
|
|
|
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 {
|
2019-02-24 21:38:28 +01:00
|
|
|
fileNodes, err := localFileStructure.ScanLocalFileStructure(context.LocalRootPath)
|
|
|
|
if err != nil {
|
2019-02-25 23:11:09 +01:00
|
|
|
os.Exit(3)
|
2019-02-24 00:33:18 +01:00
|
|
|
}
|
2019-02-25 23:11:09 +01:00
|
|
|
return fileNodes
|
2019-02-23 21:57:54 +01:00
|
|
|
}
|
|
|
|
|
2019-02-25 23:11:09 +01:00
|
|
|
func getAllCategoriesFromServer(context *AppContext) map[string]*category.PiwigoCategory {
|
2019-02-25 00:02:59 +01:00
|
|
|
|
2019-02-25 23:11:09 +01:00
|
|
|
categories, err := category.GetAllCategories(context.Piwigo)
|
2019-02-25 00:02:59 +01:00
|
|
|
if err != nil {
|
2019-02-25 23:11:09 +01:00
|
|
|
os.Exit(4)
|
2019-02-25 00:02:59 +01:00
|
|
|
}
|
|
|
|
|
2019-02-25 23:11:09 +01:00
|
|
|
return categories
|
2019-02-23 21:57:54 +01:00
|
|
|
}
|
|
|
|
|
2019-02-25 23:11:09 +01:00
|
|
|
func findMissingCategories(fileSystem map[string]*localFileStructure.FilesystemNode, categories map[string]*category.PiwigoCategory) []string {
|
|
|
|
return matcher.FindMissingCategories(fileSystem, categories)
|
2019-02-23 21:57:54 +01:00
|
|
|
}
|
|
|
|
|
2019-02-25 23:11:09 +01:00
|
|
|
func createMissingCategories(categories []string) {
|
2019-02-23 21:57:54 +01:00
|
|
|
logrus.Warnln("Creating missing albums (NotImplemented)")
|
2019-02-25 23:11:09 +01:00
|
|
|
for _, c := range categories {
|
|
|
|
logrus.Debug(c)
|
|
|
|
}
|
2019-02-23 21:57:54 +01:00
|
|
|
}
|
|
|
|
|
2019-02-25 23:11:09 +01:00
|
|
|
func findMissingImages() {
|
2019-02-23 21:57:54 +01:00
|
|
|
logrus.Warnln("Finding missing images (NotImplemented)")
|
|
|
|
}
|
|
|
|
|
2019-02-25 23:11:09 +01:00
|
|
|
func uploadImages() {
|
2019-02-23 21:57:54 +01:00
|
|
|
logrus.Warnln("Uploading missing images (NotImplemented)")
|
2019-02-23 22:02:12 +01:00
|
|
|
}
|
2019-02-24 00:33:18 +01:00
|
|
|
|
2019-02-24 23:27:31 +01:00
|
|
|
func configureContext() (*AppContext, error) {
|
2019-02-24 01:17:10 +01:00
|
|
|
logrus.Infoln("Preparing application context and configuration")
|
|
|
|
|
2019-02-24 23:27:31 +01:00
|
|
|
if *piwigoUrl == "" {
|
|
|
|
return nil, errors.New("missing piwigo url!")
|
|
|
|
}
|
|
|
|
|
|
|
|
if *piwigoUser == "" {
|
|
|
|
return nil, errors.New("missing piwigo user!")
|
|
|
|
}
|
|
|
|
|
|
|
|
if *piwigoPassword == "" {
|
|
|
|
return nil, errors.New("missing piwigo password!")
|
|
|
|
}
|
|
|
|
|
2019-02-24 01:17:10 +01:00
|
|
|
context := new(AppContext)
|
2019-02-24 23:11:52 +01:00
|
|
|
context.LocalRootPath = *imagesRootPath
|
2019-02-25 00:02:59 +01:00
|
|
|
context.Piwigo = new(piwigo.PiwigoContext)
|
2019-02-24 23:39:55 +01:00
|
|
|
context.Piwigo.Url = fmt.Sprintf("%s/ws.php?format=json", *piwigoUrl)
|
2019-02-24 23:11:52 +01:00
|
|
|
context.Piwigo.Username = *piwigoUser
|
|
|
|
context.Piwigo.Password = *piwigoPassword
|
2019-02-24 01:17:10 +01:00
|
|
|
|
2019-02-24 23:27:31 +01:00
|
|
|
return context, nil
|
2019-02-24 01:17:10 +01:00
|
|
|
}
|
|
|
|
|
2019-02-24 23:32:05 +01:00
|
|
|
func loginToPiwigoAndConfigureContext(context *AppContext) error {
|
2019-02-24 00:33:18 +01:00
|
|
|
logrus.Infoln("Logging in to piwigo and getting chunk size configuration for uploads")
|
2019-02-24 21:38:28 +01:00
|
|
|
err := authentication.Login(context.Piwigo)
|
|
|
|
if err != nil {
|
2019-02-24 23:32:05 +01:00
|
|
|
return err
|
2019-02-24 21:38:28 +01:00
|
|
|
}
|
2019-02-24 23:32:05 +01:00
|
|
|
return initializeUploadChunkSize(context)
|
2019-02-24 00:33:18 +01:00
|
|
|
}
|
|
|
|
|
2019-02-24 23:32:05 +01:00
|
|
|
func initializeUploadChunkSize(context *AppContext) error {
|
2019-02-24 21:38:28 +01:00
|
|
|
userStatus, err := authentication.GetStatus(context.Piwigo)
|
|
|
|
if err != nil {
|
2019-02-24 23:32:05 +01:00
|
|
|
return err
|
2019-02-24 21:38:28 +01:00
|
|
|
}
|
2019-02-24 00:33:18 +01:00
|
|
|
context.ChunkSizeBytes = userStatus.Result.UploadFormChunkSize * 1024
|
|
|
|
logrus.Debugln(context.ChunkSizeBytes)
|
2019-02-24 23:32:05 +01:00
|
|
|
return nil
|
2019-02-24 00:33:18 +01:00
|
|
|
}
|