PiwigoDirectoryUploader/internal/app/app.go

105 lines
2.9 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"
"fmt"
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-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
_ = authentication.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)
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-03-02 00:21:01 +01:00
context.Piwigo.ChunkSizeInKB = *piwigoUploadChunkSizeInKB
2019-02-24 01:17:10 +01:00
return context, nil
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-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)
}
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
}
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)
}