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 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-24 00:33:18 +01:00
|
|
|
//ScanLocalDirectories(context)
|
2019-02-25 00:02:59 +01:00
|
|
|
|
|
|
|
GetAllCategoriesFromServer(context)
|
2019-02-24 00:33:18 +01:00
|
|
|
|
|
|
|
//FindMissingAlbums()
|
|
|
|
//CreateMissingAlbums()
|
|
|
|
//FindMissingImages()
|
|
|
|
//UploadImages()
|
|
|
|
|
2019-02-24 21:38:28 +01:00
|
|
|
_ = authentication.Logout(context.Piwigo)
|
2019-02-23 21:57:54 +01:00
|
|
|
}
|
|
|
|
|
2019-02-24 00:33:18 +01:00
|
|
|
func ScanLocalDirectories(context *AppContext) {
|
2019-02-24 21:38:28 +01:00
|
|
|
fileNodes, err := localFileStructure.ScanLocalFileStructure(context.LocalRootPath)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2019-02-24 00:33:18 +01:00
|
|
|
for _, node := range fileNodes {
|
|
|
|
logrus.Debugln("found path entry:", node.Key)
|
|
|
|
}
|
2019-02-23 21:57:54 +01:00
|
|
|
}
|
|
|
|
|
2019-02-25 00:02:59 +01:00
|
|
|
func GetAllCategoriesFromServer(context *AppContext) {
|
|
|
|
|
|
|
|
err := category.GetAllCategories(context.Piwigo)
|
|
|
|
if err != nil {
|
|
|
|
os.Exit(3)
|
|
|
|
}
|
|
|
|
|
2019-02-23 21:57:54 +01:00
|
|
|
}
|
|
|
|
|
2019-02-23 22:02:12 +01:00
|
|
|
func FindMissingAlbums() {
|
2019-02-23 21:57:54 +01:00
|
|
|
logrus.Warnln("Looking up missing albums (NotImplemented)")
|
|
|
|
}
|
|
|
|
|
2019-02-23 22:02:12 +01:00
|
|
|
func CreateMissingAlbums() {
|
2019-02-23 21:57:54 +01:00
|
|
|
logrus.Warnln("Creating missing albums (NotImplemented)")
|
|
|
|
}
|
|
|
|
|
2019-02-23 22:02:12 +01:00
|
|
|
func FindMissingImages() {
|
2019-02-23 21:57:54 +01:00
|
|
|
logrus.Warnln("Finding missing images (NotImplemented)")
|
|
|
|
}
|
|
|
|
|
2019-02-23 22:02:12 +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
|
|
|
}
|