added interfaces to the piwigo context to make testing easier

This commit is contained in:
Philipp Häfelfinger 2019-03-20 21:41:41 +01:00
parent 390a60418f
commit 8e3c736bf9
4 changed files with 26 additions and 8 deletions

View File

@ -37,7 +37,7 @@ func Run() {
logErrorAndExit(err, 4) logErrorAndExit(err, 4)
} }
err = synchronizeCategories(context, filesystemNodes, categories) err = synchronizeCategories(context.piwigo, filesystemNodes, categories)
if err != nil { if err != nil {
logErrorAndExit(err, 5) logErrorAndExit(err, 5)
} }

View File

@ -16,7 +16,7 @@ func getAllCategoriesFromServer(context *appContext) (map[string]*piwigo.PiwigoC
return categories, err return categories, err
} }
func synchronizeCategories(context *appContext, filesystemNodes map[string]*localFileStructure.FilesystemNode, existingCategories map[string]*piwigo.PiwigoCategory) error { func synchronizeCategories(piwigoApi piwigo.PiwigoCategoryApi, filesystemNodes map[string]*localFileStructure.FilesystemNode, existingCategories map[string]*piwigo.PiwigoCategory) error {
logrus.Infoln("Synchronizing categories...") logrus.Infoln("Synchronizing categories...")
missingCategories := findMissingCategories(filesystemNodes, existingCategories) missingCategories := findMissingCategories(filesystemNodes, existingCategories)
@ -26,7 +26,7 @@ func synchronizeCategories(context *appContext, filesystemNodes map[string]*loca
return nil return nil
} }
return createMissingCategories(context, missingCategories, existingCategories) return createMissingCategories(piwigoApi, missingCategories, existingCategories)
} }
func findMissingCategories(fileSystem map[string]*localFileStructure.FilesystemNode, existingCategories map[string]*piwigo.PiwigoCategory) []string { func findMissingCategories(fileSystem map[string]*localFileStructure.FilesystemNode, existingCategories map[string]*piwigo.PiwigoCategory) []string {
@ -50,7 +50,7 @@ func findMissingCategories(fileSystem map[string]*localFileStructure.FilesystemN
return missingCategories return missingCategories
} }
func createMissingCategories(context *appContext, missingCategories []string, existingCategories map[string]*piwigo.PiwigoCategory) error { func createMissingCategories(piwigoApi piwigo.PiwigoCategoryApi, missingCategories []string, existingCategories map[string]*piwigo.PiwigoCategory) error {
// we sort them to make sure the categories gets created // we sort them to make sure the categories gets created
// in the right order and we have the parent available while creating the children // in the right order and we have the parent available while creating the children
sort.Strings(missingCategories) sort.Strings(missingCategories)
@ -66,7 +66,7 @@ func createMissingCategories(context *appContext, missingCategories []string, ex
} }
// create category on piwigo // create category on piwigo
id, err := context.piwigo.CreateCategory(parentId, name) id, err := piwigoApi.CreateCategory(parentId, name)
if err != nil { if err != nil {
return errors.New(fmt.Sprintf("Could not create category on piwigo: %s", err)) return errors.New(fmt.Sprintf("Could not create category on piwigo: %s", err))
} }

View File

@ -61,7 +61,7 @@ func synchronizeLocalImageMetadata(metadataStorage ImageMetadataProvider, fileSy
} }
// This method agregates the check for files with missing piwigoids and if changed files need to be uploaded again. // This method agregates the check for files with missing piwigoids and if changed files need to be uploaded again.
func synchronizePiwigoMetadata(piwigoCtx *piwigo.PiwigoContext, metadataStorage ImageMetadataProvider) error { func synchronizePiwigoMetadata(piwigoCtx piwigo.PiwigoImageApi, metadataStorage ImageMetadataProvider) error {
// TODO: check if category has to be assigned (image possibly added to two albums -> only uploaded once but assigned multiple times) -> implement later // TODO: check if category has to be assigned (image possibly added to two albums -> only uploaded once but assigned multiple times) -> implement later
logrus.Debugf("Starting synchronizePiwigoMetadata") logrus.Debugf("Starting synchronizePiwigoMetadata")
err := updatePiwigoIdIfAlreadyUploaded(metadataStorage, piwigoCtx) err := updatePiwigoIdIfAlreadyUploaded(metadataStorage, piwigoCtx)
@ -78,7 +78,7 @@ func synchronizePiwigoMetadata(piwigoCtx *piwigo.PiwigoContext, metadataStorage
} }
// Check all images with upload required if they are really changed and need to be uploaded to the server. // Check all images with upload required if they are really changed and need to be uploaded to the server.
func checkPiwigoForChangedImages(provider ImageMetadataProvider, piwigoCtx *piwigo.PiwigoContext) error { func checkPiwigoForChangedImages(provider ImageMetadataProvider, piwigoCtx piwigo.PiwigoImageApi) error {
logrus.Infof("Checking pending files if they really differ from the version in piwigo...") logrus.Infof("Checking pending files if they really differ from the version in piwigo...")
images, err := provider.ImageMetadataToUpload() images, err := provider.ImageMetadataToUpload()
@ -116,7 +116,7 @@ func checkPiwigoForChangedImages(provider ImageMetadataProvider, piwigoCtx *piwi
// This function calls piwigo and checks if the given md5sum is already present. // This function calls piwigo and checks if the given md5sum is already present.
// Only files without a piwigo id are used to query the server. // Only files without a piwigo id are used to query the server.
func updatePiwigoIdIfAlreadyUploaded(provider ImageMetadataProvider, piwigoCtx *piwigo.PiwigoContext) error { func updatePiwigoIdIfAlreadyUploaded(provider ImageMetadataProvider, piwigoCtx piwigo.PiwigoImageApi) error {
logrus.Infof("checking for pending files that are already on piwigo and updating piwigoids...") logrus.Infof("checking for pending files that are already on piwigo and updating piwigoids...")
images, err := provider.ImageMetadataToUpload() images, err := provider.ImageMetadataToUpload()
if err != nil { if err != nil {

View File

@ -13,6 +13,24 @@ import (
"strings" "strings"
) )
type PiwigoApi interface {
Initialize(baseUrl string, username string, password string, chunkSizeInKB int) error
Login() error
Logout() error
GetStatus() (*getStatusResponse, error)
}
type PiwigoCategoryApi interface {
GetAllCategories() (map[string]*PiwigoCategory, error)
CreateCategory(parentId int, name string) (int, error)
}
type PiwigoImageApi interface {
ImageCheckFile(piwigoId int, md5sum string) (int, error)
ImagesExistOnPiwigo(md5sums []string) (map[string]int, error)
UploadImage(filePath string, md5sum string, category int) (int, error)
}
type PiwigoContext struct { type PiwigoContext struct {
url string url string
username string username string