PiwigoDirectoryUploader/internal/app/app.go

100 lines
2.7 KiB
Go
Raw Normal View History

2019-02-23 21:57:54 +01:00
package app
import (
"errors"
2019-02-24 23:11:52 +01:00
"flag"
"git.haefelfinger.net/piwigo/PiwigoDirectoryUploader/internal/pkg/localFileStructure"
"git.haefelfinger.net/piwigo/PiwigoDirectoryUploader/internal/pkg/piwigo"
2019-03-02 00:21:01 +01:00
"github.com/sirupsen/logrus"
"os"
2019-02-23 21:57:54 +01:00
)
2019-02-24 23:11:52 +01:00
var (
2019-03-02 00:21:01 +01:00
imagesRootPath = flag.String("imagesRootPath", "", "This is the images root path that should be mirrored to piwigo.")
piwigoUrl = flag.String("piwigoUrl", "", "The root url without tailing slash to your piwigo installation.")
piwigoUser = flag.String("piwigoUser", "", "The username to use during sync.")
piwigoPassword = flag.String("piwigoPassword", "", "This is password to the given username.")
piwigoUploadChunkSizeInKB = flag.Int("piwigoUploadChunkSizeInKB", 512, "The chunksize used to upload an image to piwigo.")
2019-02-24 23:11:52 +01:00
)
func Run() {
context, err := configureContext()
if err != nil {
2019-02-25 23:51:18 +01:00
logErrorAndExit(err, 1)
}
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
}
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)
}
categories, err := getAllCategoriesFromServer(context)
if err != nil {
2019-02-25 23:51:18 +01:00
logErrorAndExit(err, 4)
}
2019-02-26 22:50:33 +01:00
err = synchronizeCategories(context, filesystemNodes, categories)
if err != nil {
2019-02-25 23:51:18 +01:00
logErrorAndExit(err, 5)
}
2019-02-23 21:57:54 +01:00
err = synchronizeImages(context, filesystemNodes, categories)
if err != nil {
2019-02-25 23:51:18 +01:00
logErrorAndExit(err, 6)
}
2019-02-23 21:57:54 +01:00
2019-03-11 21:47:16 +01:00
_ = piwigo.Logout(context.Piwigo)
2019-02-23 22:02:12 +01:00
}
func configureContext() (*appContext, error) {
2019-02-24 01:17:10 +01:00
logrus.Infoln("Preparing application context and configuration")
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!")
}
context := new(appContext)
2019-02-24 23:11:52 +01:00
context.LocalRootPath = *imagesRootPath
context.Piwigo = new(piwigo.PiwigoContext)
err := context.Piwigo.Initialize(*piwigoUrl, *piwigoUser, *piwigoPassword, *piwigoUploadChunkSizeInKB)
2019-02-24 01:17:10 +01:00
return context, err
2019-02-24 01:17:10 +01:00
}
func loginToPiwigoAndConfigureContext(context *appContext) error {
logrus.Infoln("Logging in to piwigo and getting chunk size configuration for uploads")
2019-03-11 21:47:16 +01:00
err := piwigo.Login(context.Piwigo)
2019-02-24 21:38:28 +01:00
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)
}
func initializeUploadChunkSize(context *appContext) error {
2019-03-11 21:47:16 +01:00
userStatus, err := piwigo.GetStatus(context.Piwigo)
2019-02-24 21:38:28 +01:00
if err != nil {
2019-02-24 23:32:05 +01:00
return err
2019-02-24 21:38:28 +01:00
}
context.ChunkSizeBytes = userStatus.Result.UploadFormChunkSize * 1024
logrus.Debugln(context.ChunkSizeBytes)
2019-02-24 23:32:05 +01:00
return nil
}
2019-02-25 23:51:18 +01:00
func logErrorAndExit(err error, exitCode int) {
logrus.Errorln(err)
os.Exit(exitCode)
}