2019-03-23 22:40:56 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2019 Philipp Haefelfinger (http://www.haefelfinger.ch/). All Rights Reserved.
|
|
|
|
* This application is licensed under GPLv2. See the LICENSE file in the root directory of the project.
|
|
|
|
*/
|
|
|
|
|
2019-03-12 23:44:05 +01:00
|
|
|
package app
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2019-04-02 21:30:39 +02:00
|
|
|
"git.haefelfinger.net/piwigo/PiwigoDirectoryUploader/internal/pkg/datastore"
|
2019-03-12 23:44:05 +01:00
|
|
|
"git.haefelfinger.net/piwigo/PiwigoDirectoryUploader/internal/pkg/piwigo"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
type appContext struct {
|
2019-03-15 23:54:22 +01:00
|
|
|
// think again if this is a good idea to have such a context!
|
2019-04-09 00:01:51 +02:00
|
|
|
piwigo *piwigo.ServerContext
|
2019-04-02 21:30:39 +02:00
|
|
|
dataStore *datastore.LocalDataStore
|
2019-03-12 23:44:05 +01:00
|
|
|
sessionId string
|
|
|
|
localRootPath string
|
|
|
|
}
|
|
|
|
|
2019-04-08 23:56:01 +02:00
|
|
|
func (c *appContext) useMetadataStore(connectionString string) error {
|
2019-03-12 23:44:05 +01:00
|
|
|
if connectionString == "" {
|
2019-04-08 23:56:01 +02:00
|
|
|
return errors.New("missing connectionString to use metadata store")
|
2019-03-12 23:44:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
logrus.Infof("Using SQL Lite data store with '%s'", connectionString)
|
2019-04-02 21:30:39 +02:00
|
|
|
c.dataStore = datastore.NewLocalDataStore()
|
2019-03-15 00:35:49 +01:00
|
|
|
err := c.dataStore.Initialize(connectionString)
|
2019-03-12 23:44:05 +01:00
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-04-08 23:56:01 +02:00
|
|
|
func (c *appContext) usePiwigo(url string, user string, password string) error {
|
2019-03-12 23:44:05 +01:00
|
|
|
if url == "" {
|
2019-04-08 23:56:01 +02:00
|
|
|
return errors.New("missing piwigo url")
|
2019-03-12 23:44:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if user == "" {
|
2019-04-08 23:56:01 +02:00
|
|
|
return errors.New("missing piwigo user")
|
2019-03-12 23:44:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if password == "" {
|
2019-04-08 23:56:01 +02:00
|
|
|
return errors.New("missing piwigo password")
|
2019-03-12 23:44:05 +01:00
|
|
|
}
|
|
|
|
|
2019-04-09 00:01:51 +02:00
|
|
|
c.piwigo = new(piwigo.ServerContext)
|
2019-04-08 23:07:45 +02:00
|
|
|
return c.piwigo.Initialize(url, user, password)
|
2019-03-12 23:44:05 +01:00
|
|
|
}
|
|
|
|
|
2019-03-20 23:28:28 +01:00
|
|
|
func newAppContext() (*appContext, error) {
|
2019-03-12 23:44:05 +01:00
|
|
|
logrus.Infoln("Preparing application context and configuration")
|
|
|
|
|
|
|
|
context := new(appContext)
|
|
|
|
context.localRootPath = *imagesRootPath
|
|
|
|
|
|
|
|
if *sqliteDb != "" {
|
2019-04-08 23:56:01 +02:00
|
|
|
err := context.useMetadataStore(*sqliteDb)
|
2019-03-12 23:44:05 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
logrus.Warnln("No persistence configured. Skipping metadata storage. This might affect performance on large collections!")
|
|
|
|
}
|
|
|
|
|
2019-04-08 23:56:01 +02:00
|
|
|
err := context.usePiwigo(*piwigoUrl, *piwigoUser, *piwigoPassword)
|
2019-03-12 23:44:05 +01:00
|
|
|
|
|
|
|
return context, err
|
|
|
|
}
|