moved app types to appContext.go and added some methods
moved piwigo login from app.go to piwigoContext.go Updated visibility of some types Added first elements of the local data store including configuration on startup and flag yea yea... I know, the commit contains to many changes ;-)
This commit is contained in:
parent
eae6db1683
commit
5a4c133f27
@ -1,7 +1,6 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"flag"
|
||||
"git.haefelfinger.net/piwigo/PiwigoDirectoryUploader/internal/pkg/localFileStructure"
|
||||
"git.haefelfinger.net/piwigo/PiwigoDirectoryUploader/internal/pkg/piwigo"
|
||||
@ -11,6 +10,7 @@ import (
|
||||
|
||||
var (
|
||||
imagesRootPath = flag.String("imagesRootPath", "", "This is the images root path that should be mirrored to piwigo.")
|
||||
sqliteDb = flag.String("sqliteDb", "", "The connection string to the sql lite database file.")
|
||||
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.")
|
||||
@ -18,17 +18,17 @@ var (
|
||||
)
|
||||
|
||||
func Run() {
|
||||
context, err := configureContext()
|
||||
context, err := createAppContext()
|
||||
if err != nil {
|
||||
logErrorAndExit(err, 1)
|
||||
}
|
||||
|
||||
err = loginToPiwigoAndConfigureContext(context)
|
||||
err = context.piwigo.LoginToPiwigoAndConfigureContext()
|
||||
if err != nil {
|
||||
logErrorAndExit(err, 2)
|
||||
}
|
||||
|
||||
filesystemNodes, err := localFileStructure.ScanLocalFileStructure(context.LocalRootPath)
|
||||
filesystemNodes, err := localFileStructure.ScanLocalFileStructure(context.localRootPath)
|
||||
if err != nil {
|
||||
logErrorAndExit(err, 3)
|
||||
}
|
||||
@ -48,49 +48,7 @@ func Run() {
|
||||
logErrorAndExit(err, 6)
|
||||
}
|
||||
|
||||
_ = piwigo.Logout(context.Piwigo)
|
||||
}
|
||||
|
||||
func configureContext() (*appContext, error) {
|
||||
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)
|
||||
context.LocalRootPath = *imagesRootPath
|
||||
context.Piwigo = new(piwigo.PiwigoContext)
|
||||
err := context.Piwigo.Initialize(*piwigoUrl, *piwigoUser, *piwigoPassword, *piwigoUploadChunkSizeInKB)
|
||||
|
||||
return context, err
|
||||
}
|
||||
|
||||
func loginToPiwigoAndConfigureContext(context *appContext) error {
|
||||
logrus.Infoln("Logging in to piwigo and getting chunk size configuration for uploads")
|
||||
err := piwigo.Login(context.Piwigo)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return initializeUploadChunkSize(context)
|
||||
}
|
||||
|
||||
func initializeUploadChunkSize(context *appContext) error {
|
||||
userStatus, err := piwigo.GetStatus(context.Piwigo)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
context.ChunkSizeBytes = userStatus.Result.UploadFormChunkSize * 1024
|
||||
logrus.Debugln(context.ChunkSizeBytes)
|
||||
return nil
|
||||
_ = piwigo.Logout(context.piwigo)
|
||||
}
|
||||
|
||||
func logErrorAndExit(err error, exitCode int) {
|
||||
|
63
internal/app/appContext.go
Normal file
63
internal/app/appContext.go
Normal file
@ -0,0 +1,63 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"git.haefelfinger.net/piwigo/PiwigoDirectoryUploader/internal/pkg/piwigo"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
type appContext struct {
|
||||
piwigo *piwigo.PiwigoContext
|
||||
sessionId string
|
||||
localRootPath string
|
||||
dataStore localDataStore
|
||||
}
|
||||
|
||||
func (c *appContext) UseMetadataStore(connectionString string) error {
|
||||
if connectionString == "" {
|
||||
return errors.New("missing connectionString to use metadata store!")
|
||||
}
|
||||
|
||||
logrus.Infof("Using SQL Lite data store with '%s'", connectionString)
|
||||
c.dataStore = localDataStore{}
|
||||
err := c.dataStore.Open(connectionString)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func (c *appContext) UsePiwigo(url string, user string, password string) error {
|
||||
if url == "" {
|
||||
return errors.New("missing piwigo url!")
|
||||
}
|
||||
|
||||
if user == "" {
|
||||
return errors.New("missing piwigo user!")
|
||||
}
|
||||
|
||||
if password == "" {
|
||||
return errors.New("missing piwigo password!")
|
||||
}
|
||||
|
||||
c.piwigo = new(piwigo.PiwigoContext)
|
||||
return c.piwigo.Initialize(*piwigoUrl, *piwigoUser, *piwigoPassword, *piwigoUploadChunkSizeInKB)
|
||||
}
|
||||
|
||||
func createAppContext() (*appContext, error) {
|
||||
logrus.Infoln("Preparing application context and configuration")
|
||||
|
||||
context := new(appContext)
|
||||
context.localRootPath = *imagesRootPath
|
||||
|
||||
if *sqliteDb != "" {
|
||||
err := context.UseMetadataStore(*sqliteDb)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
} else {
|
||||
logrus.Warnln("No persistence configured. Skipping metadata storage. This might affect performance on large collections!")
|
||||
}
|
||||
|
||||
err := context.UsePiwigo(*piwigoUrl, *piwigoUser, *piwigoPassword)
|
||||
|
||||
return context, err
|
||||
}
|
@ -12,7 +12,7 @@ import (
|
||||
|
||||
func getAllCategoriesFromServer(context *appContext) (map[string]*piwigo.PiwigoCategory, error) {
|
||||
logrus.Debugln("Starting GetAllCategories")
|
||||
categories, err := piwigo.GetAllCategories(context.Piwigo)
|
||||
categories, err := piwigo.GetAllCategories(context.piwigo)
|
||||
return categories, err
|
||||
}
|
||||
|
||||
@ -66,7 +66,7 @@ func createMissingCategories(context *appContext, missingCategories []string, ex
|
||||
}
|
||||
|
||||
// create category on piwigo
|
||||
id, err := piwigo.CreateCategory(context.Piwigo, parentId, name)
|
||||
id, err := piwigo.CreateCategory(context.piwigo, parentId, name)
|
||||
if err != nil {
|
||||
return errors.New(fmt.Sprintf("Could not create category on piwigo: %s", err))
|
||||
}
|
||||
|
47
internal/app/datastore.go
Normal file
47
internal/app/datastore.go
Normal file
@ -0,0 +1,47 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"time"
|
||||
)
|
||||
|
||||
type ImageMetaData struct {
|
||||
ImageId int
|
||||
RelativeImagePath string
|
||||
Filename string
|
||||
Md5Sum string
|
||||
LastChange time.Time
|
||||
CategoryPath string
|
||||
CategoryId int
|
||||
}
|
||||
|
||||
type ImageMetadataLoader interface {
|
||||
GetImageMetadata(relativePath string) (ImageMetaData, error)
|
||||
}
|
||||
|
||||
type ImageMetadataSaver interface {
|
||||
SaveImageMetadata(m ImageMetaData) error
|
||||
}
|
||||
|
||||
type localDataStore struct {
|
||||
connectionString string
|
||||
}
|
||||
|
||||
func (d *localDataStore) Open(connectionString string) error {
|
||||
if connectionString == "" {
|
||||
return errors.New("connection string could not be empty.")
|
||||
}
|
||||
|
||||
d.connectionString = connectionString
|
||||
|
||||
//TODO: open and test connection
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *localDataStore) GetImageMetadata(relativePath string) (ImageMetaData, error) {
|
||||
return ImageMetaData{}, nil
|
||||
}
|
||||
|
||||
func (d *localDataStore) SaveImageMetadata(m ImageMetaData) error {
|
||||
return nil
|
||||
}
|
@ -40,7 +40,7 @@ func findMissingImages(context *appContext, imageFiles []*localFileStructure.Ima
|
||||
files = append(files, file.Md5Sum)
|
||||
}
|
||||
|
||||
misingSums, err := piwigo.ImageUploadRequired(context.Piwigo, files)
|
||||
misingSums, err := piwigo.ImageUploadRequired(context.piwigo, files)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -67,7 +67,7 @@ func uploadImages(context *appContext, missingFiles []*localFileStructure.ImageN
|
||||
for _, file := range missingFiles {
|
||||
categoryId := existingCategories[file.CategoryName].Id
|
||||
|
||||
imageId, err := piwigo.UploadImage(context.Piwigo, file.Path, file.Md5Sum, categoryId)
|
||||
imageId, err := piwigo.UploadImage(context.piwigo, file.Path, file.Md5Sum, categoryId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -1,12 +0,0 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"git.haefelfinger.net/piwigo/PiwigoDirectoryUploader/internal/pkg/piwigo"
|
||||
)
|
||||
|
||||
type appContext struct {
|
||||
Piwigo *piwigo.PiwigoContext
|
||||
SessionId string
|
||||
LocalRootPath string
|
||||
ChunkSizeBytes int
|
||||
}
|
@ -47,6 +47,15 @@ func (context *PiwigoContext) Initialize(baseUrl string, username string, passwo
|
||||
return nil
|
||||
}
|
||||
|
||||
func (context *PiwigoContext) LoginToPiwigoAndConfigureContext() error {
|
||||
logrus.Infoln("Logging in to piwigo and getting chunk size configuration for uploads")
|
||||
err := Login(context)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return initializeUploadChunkSize(context)
|
||||
}
|
||||
|
||||
func (context *PiwigoContext) getChunkSizeInKB() int {
|
||||
return context.chunkSizeInKB
|
||||
}
|
||||
@ -72,3 +81,13 @@ func (context *PiwigoContext) initializeCookieJarIfRequired() {
|
||||
jar, _ := cookiejar.New(&options)
|
||||
context.Cookies = jar
|
||||
}
|
||||
|
||||
func initializeUploadChunkSize(context *PiwigoContext) error {
|
||||
userStatus, err := GetStatus(context)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
context.chunkSizeInKB = userStatus.Result.UploadFormChunkSize * 1024
|
||||
logrus.Debugf("Got chunksize of %d KB from server.", context.chunkSizeInKB)
|
||||
return nil
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user