2019-03-23 22:40:56 +01:00
|
|
|
/*
|
2020-04-14 01:08:07 +02:00
|
|
|
* Copyright (C) 2020 Philipp Haefelfinger (http://www.haefelfinger.ch/). All Rights Reserved.
|
2019-03-23 22:40:56 +01:00
|
|
|
* This application is licensed under GPLv2. See the LICENSE file in the root directory of the project.
|
|
|
|
*/
|
|
|
|
|
2019-02-23 21:57:54 +01:00
|
|
|
package app
|
|
|
|
|
|
|
|
import (
|
2019-04-06 22:48:11 +02:00
|
|
|
"git.haefelfinger.net/piwigo/PiwigoDirectoryUploader/internal/pkg/category"
|
|
|
|
"git.haefelfinger.net/piwigo/PiwigoDirectoryUploader/internal/pkg/images"
|
|
|
|
"git.haefelfinger.net/piwigo/PiwigoDirectoryUploader/internal/pkg/localFileStructure"
|
2019-03-02 00:21:01 +01:00
|
|
|
"github.com/sirupsen/logrus"
|
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
|
|
|
func Run() {
|
2020-04-14 23:14:11 +02:00
|
|
|
initializeFlags()
|
|
|
|
initializeLog()
|
|
|
|
|
2019-03-20 23:28:28 +01:00
|
|
|
context, err := newAppContext()
|
2019-02-24 23:27:31 +01:00
|
|
|
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-03-20 00:14:10 +01:00
|
|
|
err = context.piwigo.Login()
|
2019-02-24 23:32:05 +01:00
|
|
|
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
|
|
|
|
2020-04-14 23:14:11 +02:00
|
|
|
filesystemNodes, err := localFileStructure.ScanLocalFileStructure(context.localRootPath, extensions, ignoreDirs, *dirSuffixToSkip)
|
2019-04-06 22:48:11 +02:00
|
|
|
if err != nil {
|
|
|
|
logErrorAndExit(err, 3)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = category.SynchronizeCategories(filesystemNodes, context.piwigo, context.dataStore)
|
|
|
|
if err != nil {
|
|
|
|
logErrorAndExit(err, 4)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = images.SynchronizeLocalImageMetadata(context.dataStore, context.dataStore, filesystemNodes, localFileStructure.CalculateFileCheckSums)
|
|
|
|
if err != nil {
|
|
|
|
logErrorAndExit(err, 5)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = images.SynchronizePiwigoMetadata(context.piwigo, context.dataStore)
|
|
|
|
if err != nil {
|
|
|
|
logErrorAndExit(err, 6)
|
|
|
|
}
|
2019-02-25 00:02:59 +01:00
|
|
|
|
2019-04-06 22:48:11 +02:00
|
|
|
if *removeImages {
|
|
|
|
err = images.DeleteImages(context.piwigo, context.dataStore)
|
|
|
|
if err != nil {
|
|
|
|
logErrorAndExit(err, 7)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
logrus.Info("The flag removeImages is disabled. Skipping...")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !(*noUpload) {
|
2019-04-08 23:08:27 +02:00
|
|
|
err = images.UploadImages(context.piwigo, context.dataStore, *parallelUploads)
|
2019-04-06 22:48:11 +02:00
|
|
|
if err != nil {
|
|
|
|
logErrorAndExit(err, 8)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
logrus.Warnln("Skipping upload of images as flag noUpload is set to true!")
|
|
|
|
}
|
2019-03-17 23:57:28 +01:00
|
|
|
|
2019-03-20 00:14:10 +01:00
|
|
|
_ = context.piwigo.Logout()
|
2019-02-24 00:33:18 +01:00
|
|
|
}
|
2019-02-25 23:51:18 +01:00
|
|
|
|
2020-04-14 23:14:11 +02:00
|
|
|
func initializeLog() {
|
|
|
|
level, err := logrus.ParseLevel(*logLevel)
|
|
|
|
if err != nil {
|
|
|
|
level = logrus.DebugLevel
|
|
|
|
}
|
|
|
|
logrus.SetLevel(level)
|
|
|
|
|
|
|
|
logrus.SetOutput(os.Stdout)
|
|
|
|
|
|
|
|
logrus.Infoln("Starting Piwigo directories to albums...")
|
|
|
|
}
|
|
|
|
|
2019-02-25 23:51:18 +01:00
|
|
|
func logErrorAndExit(err error, exitCode int) {
|
|
|
|
logrus.Errorln(err)
|
|
|
|
os.Exit(exitCode)
|
|
|
|
}
|