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"
|
2019-03-01 21:36:16 +01:00
|
|
|
"git.haefelfinger.net/piwigo/DirectoriesToAlbums/internal/pkg/localFileStructure"
|
|
|
|
"git.haefelfinger.net/piwigo/DirectoriesToAlbums/internal/pkg/piwigo"
|
|
|
|
"git.haefelfinger.net/piwigo/DirectoriesToAlbums/internal/pkg/piwigo/authentication"
|
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 {
|
2019-02-25 23:51:18 +01:00
|
|
|
logErrorAndExit(err, 1)
|
2019-02-24 23:27:31 +01:00
|
|
|
}
|
2019-02-24 00:33:18 +01:00
|
|
|
|
2019-02-24 23:32:05 +01:00
|
|
|
err = loginToPiwigoAndConfigureContext(context)
|
|
|
|
if err != nil {
|
2019-02-25 23:51:18 +01:00
|
|
|
logErrorAndExit(err, 2)
|
2019-02-24 23:32:05 +01:00
|
|
|
}
|
2019-02-25 00:02:59 +01:00
|
|
|
|
2019-02-25 23:36:18 +01:00
|
|
|
filesystemNodes, err := localFileStructure.ScanLocalFileStructure(context.LocalRootPath)
|
2019-02-24 21:38:28 +01:00
|
|
|
if err != nil {
|
2019-02-25 23:51:18 +01:00
|
|
|
logErrorAndExit(err, 3)
|
2019-02-24 00:33:18 +01:00
|
|
|
}
|
2019-02-25 00:02:59 +01:00
|
|
|
|
2019-02-25 23:36:18 +01:00
|
|
|
categories, err := getAllCategoriesFromServer(context)
|
2019-02-25 00:02:59 +01:00
|
|
|
if err != nil {
|
2019-02-25 23:51:18 +01:00
|
|
|
logErrorAndExit(err, 4)
|
2019-02-25 00:02:59 +01:00
|
|
|
}
|
|
|
|
|
2019-02-26 22:50:33 +01:00
|
|
|
err = synchronizeCategories(context, filesystemNodes, categories)
|
2019-02-25 23:36:18 +01:00
|
|
|
if err != nil {
|
2019-02-25 23:51:18 +01:00
|
|
|
logErrorAndExit(err, 5)
|
2019-02-25 23:11:09 +01:00
|
|
|
}
|
2019-02-23 21:57:54 +01:00
|
|
|
|
2019-02-27 22:11:47 +01:00
|
|
|
err = synchronizeImages(context, filesystemNodes, categories)
|
2019-02-25 23:36:18 +01:00
|
|
|
if err != nil {
|
2019-02-25 23:51:18 +01:00
|
|
|
logErrorAndExit(err, 6)
|
2019-02-25 23:36:18 +01:00
|
|
|
}
|
2019-02-23 21:57:54 +01:00
|
|
|
|
2019-02-25 23:36:18 +01:00
|
|
|
_ = authentication.Logout(context.Piwigo)
|
2019-02-23 22:02:12 +01:00
|
|
|
}
|
2019-02-24 00:33:18 +01:00
|
|
|
|
2019-02-27 22:13:23 +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-27 22:13:23 +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-27 22:13:23 +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-27 22:13:23 +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
|
|
|
}
|
2019-02-25 23:51:18 +01:00
|
|
|
|
|
|
|
func logErrorAndExit(err error, exitCode int) {
|
|
|
|
logrus.Errorln(err)
|
|
|
|
os.Exit(exitCode)
|
|
|
|
}
|