moved datastore in own internal package

This commit is contained in:
Philipp Häfelfinger 2019-04-02 21:30:39 +02:00
parent 582b0621c2
commit bd5250f0d5
6 changed files with 85 additions and 78 deletions

View File

@ -7,6 +7,7 @@ package app
import ( import (
"errors" "errors"
"git.haefelfinger.net/piwigo/PiwigoDirectoryUploader/internal/pkg/datastore"
"git.haefelfinger.net/piwigo/PiwigoDirectoryUploader/internal/pkg/piwigo" "git.haefelfinger.net/piwigo/PiwigoDirectoryUploader/internal/pkg/piwigo"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
) )
@ -14,7 +15,7 @@ import (
type appContext struct { type appContext struct {
// think again if this is a good idea to have such a context! // think again if this is a good idea to have such a context!
piwigo *piwigo.PiwigoContext piwigo *piwigo.PiwigoContext
dataStore *localDataStore dataStore *datastore.LocalDataStore
sessionId string sessionId string
localRootPath string localRootPath string
} }
@ -25,7 +26,7 @@ func (c *appContext) UseMetadataStore(connectionString string) error {
} }
logrus.Infof("Using SQL Lite data store with '%s'", connectionString) logrus.Infof("Using SQL Lite data store with '%s'", connectionString)
c.dataStore = &localDataStore{} c.dataStore = datastore.NewLocalDataStore()
err := c.dataStore.Initialize(connectionString) err := c.dataStore.Initialize(connectionString)
return err return err

View File

@ -1,10 +1,11 @@
// Code generated by MockGen. DO NOT EDIT. // Code generated by MockGen. DO NOT EDIT.
// Source: git.haefelfinger.net/piwigo/PiwigoDirectoryUploader/internal/app (interfaces: ImageMetadataProvider) // Source: git.haefelfinger.net/piwigo/PiwigoDirectoryUploader/internal/pkg/datastore (interfaces: ImageMetadataProvider)
// Package app is a generated GoMock package. // Package app is a generated GoMock package.
package app package app
import ( import (
datastore "git.haefelfinger.net/piwigo/PiwigoDirectoryUploader/internal/pkg/datastore"
gomock "github.com/golang/mock/gomock" gomock "github.com/golang/mock/gomock"
reflect "reflect" reflect "reflect"
) )
@ -47,10 +48,10 @@ func (mr *MockImageMetadataProviderMockRecorder) DeleteMarkedImages() *gomock.Ca
} }
// ImageMetadata mocks base method // ImageMetadata mocks base method
func (m *MockImageMetadataProvider) ImageMetadata(arg0 string) (ImageMetaData, error) { func (m *MockImageMetadataProvider) ImageMetadata(arg0 string) (datastore.ImageMetaData, error) {
m.ctrl.T.Helper() m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "ImageMetadata", arg0) ret := m.ctrl.Call(m, "ImageMetadata", arg0)
ret0, _ := ret[0].(ImageMetaData) ret0, _ := ret[0].(datastore.ImageMetaData)
ret1, _ := ret[1].(error) ret1, _ := ret[1].(error)
return ret0, ret1 return ret0, ret1
} }
@ -62,10 +63,10 @@ func (mr *MockImageMetadataProviderMockRecorder) ImageMetadata(arg0 interface{})
} }
// ImageMetadataAll mocks base method // ImageMetadataAll mocks base method
func (m *MockImageMetadataProvider) ImageMetadataAll() ([]ImageMetaData, error) { func (m *MockImageMetadataProvider) ImageMetadataAll() ([]datastore.ImageMetaData, error) {
m.ctrl.T.Helper() m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "ImageMetadataAll") ret := m.ctrl.Call(m, "ImageMetadataAll")
ret0, _ := ret[0].([]ImageMetaData) ret0, _ := ret[0].([]datastore.ImageMetaData)
ret1, _ := ret[1].(error) ret1, _ := ret[1].(error)
return ret0, ret1 return ret0, ret1
} }
@ -77,10 +78,10 @@ func (mr *MockImageMetadataProviderMockRecorder) ImageMetadataAll() *gomock.Call
} }
// ImageMetadataToDelete mocks base method // ImageMetadataToDelete mocks base method
func (m *MockImageMetadataProvider) ImageMetadataToDelete() ([]ImageMetaData, error) { func (m *MockImageMetadataProvider) ImageMetadataToDelete() ([]datastore.ImageMetaData, error) {
m.ctrl.T.Helper() m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "ImageMetadataToDelete") ret := m.ctrl.Call(m, "ImageMetadataToDelete")
ret0, _ := ret[0].([]ImageMetaData) ret0, _ := ret[0].([]datastore.ImageMetaData)
ret1, _ := ret[1].(error) ret1, _ := ret[1].(error)
return ret0, ret1 return ret0, ret1
} }
@ -92,10 +93,10 @@ func (mr *MockImageMetadataProviderMockRecorder) ImageMetadataToDelete() *gomock
} }
// ImageMetadataToUpload mocks base method // ImageMetadataToUpload mocks base method
func (m *MockImageMetadataProvider) ImageMetadataToUpload() ([]ImageMetaData, error) { func (m *MockImageMetadataProvider) ImageMetadataToUpload() ([]datastore.ImageMetaData, error) {
m.ctrl.T.Helper() m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "ImageMetadataToUpload") ret := m.ctrl.Call(m, "ImageMetadataToUpload")
ret0, _ := ret[0].([]ImageMetaData) ret0, _ := ret[0].([]datastore.ImageMetaData)
ret1, _ := ret[1].(error) ret1, _ := ret[1].(error)
return ret0, ret1 return ret0, ret1
} }
@ -107,7 +108,7 @@ func (mr *MockImageMetadataProviderMockRecorder) ImageMetadataToUpload() *gomock
} }
// SaveImageMetadata mocks base method // SaveImageMetadata mocks base method
func (m *MockImageMetadataProvider) SaveImageMetadata(arg0 ImageMetaData) error { func (m *MockImageMetadataProvider) SaveImageMetadata(arg0 datastore.ImageMetaData) error {
m.ctrl.T.Helper() m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "SaveImageMetadata", arg0) ret := m.ctrl.Call(m, "SaveImageMetadata", arg0)
ret0, _ := ret[0].(error) ret0, _ := ret[0].(error)

View File

@ -6,6 +6,7 @@
package app package app
import ( import (
"git.haefelfinger.net/piwigo/PiwigoDirectoryUploader/internal/pkg/datastore"
"git.haefelfinger.net/piwigo/PiwigoDirectoryUploader/internal/pkg/localFileStructure" "git.haefelfinger.net/piwigo/PiwigoDirectoryUploader/internal/pkg/localFileStructure"
"git.haefelfinger.net/piwigo/PiwigoDirectoryUploader/internal/pkg/piwigo" "git.haefelfinger.net/piwigo/PiwigoDirectoryUploader/internal/pkg/piwigo"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
@ -21,7 +22,7 @@ type fileChecksumCalculator func(filePath string) (string, error)
// Update the local image metadata by walking through all found files and check if the modification date has changed // Update the local image metadata by walking through all found files and check if the modification date has changed
// or if they are new to the local database. If the files is new or changed, the md5sum will be rebuilt as well. // or if they are new to the local database. If the files is new or changed, the md5sum will be rebuilt as well.
func synchronizeLocalImageMetadata(metadataStorage ImageMetadataProvider, fileSystemNodes map[string]*localFileStructure.FilesystemNode, categories map[string]*piwigo.PiwigoCategory, checksumCalculator fileChecksumCalculator) error { func synchronizeLocalImageMetadata(metadataStorage datastore.ImageMetadataProvider, fileSystemNodes map[string]*localFileStructure.FilesystemNode, categories map[string]*piwigo.PiwigoCategory, checksumCalculator fileChecksumCalculator) error {
logrus.Debug("Starting synchronizeLocalImageMetadata") logrus.Debug("Starting synchronizeLocalImageMetadata")
defer logrus.Debug("Leaving synchronizeLocalImageMetadata") defer logrus.Debug("Leaving synchronizeLocalImageMetadata")
@ -39,7 +40,7 @@ func synchronizeLocalImageMetadata(metadataStorage ImageMetadataProvider, fileSy
return nil return nil
} }
func synchronizeLocalImageMetadataScanNewFiles(fileSystemNodes map[string]*localFileStructure.FilesystemNode, metadataStorage ImageMetadataProvider, categories map[string]*piwigo.PiwigoCategory, checksumCalculator fileChecksumCalculator) error { func synchronizeLocalImageMetadataScanNewFiles(fileSystemNodes map[string]*localFileStructure.FilesystemNode, metadataStorage datastore.ImageMetadataProvider, categories map[string]*piwigo.PiwigoCategory, checksumCalculator fileChecksumCalculator) error {
logrus.Debug("Entering synchronizeLocalImageMetadataScanNewFiles") logrus.Debug("Entering synchronizeLocalImageMetadataScanNewFiles")
defer logrus.Debug("Leaving synchronizeLocalImageMetadataScanNewFiles") defer logrus.Debug("Leaving synchronizeLocalImageMetadataScanNewFiles")
@ -50,9 +51,9 @@ func synchronizeLocalImageMetadataScanNewFiles(fileSystemNodes map[string]*local
} }
metadata, err := metadataStorage.ImageMetadata(file.Path) metadata, err := metadataStorage.ImageMetadata(file.Path)
if err == ErrorRecordNotFound { if err == datastore.ErrorRecordNotFound {
logrus.Debugf("Creating new metadata entry for %s.", file.Path) logrus.Debugf("Creating new metadata entry for %s.", file.Path)
metadata = ImageMetaData{} metadata = datastore.ImageMetaData{}
metadata.Filename = file.Name metadata.Filename = file.Name
metadata.FullImagePath = file.Path metadata.FullImagePath = file.Path
metadata.CategoryPath = filepath.Dir(file.Key) metadata.CategoryPath = filepath.Dir(file.Key)
@ -91,7 +92,7 @@ func synchronizeLocalImageMetadataScanNewFiles(fileSystemNodes map[string]*local
return nil return nil
} }
func synchronizeLocalImageMetadataFindFilesToDelete(provider ImageMetadataProvider) error { func synchronizeLocalImageMetadataFindFilesToDelete(provider datastore.ImageMetadataProvider) error {
logrus.Debug("Entering synchronizeLocalImageMetadataFindFilesToDelete") logrus.Debug("Entering synchronizeLocalImageMetadataFindFilesToDelete")
defer logrus.Debug("Leaving synchronizeLocalImageMetadataFindFilesToDelete") defer logrus.Debug("Leaving synchronizeLocalImageMetadataFindFilesToDelete")
@ -115,7 +116,7 @@ func synchronizeLocalImageMetadataFindFilesToDelete(provider ImageMetadataProvid
// Uploads the pending images to the piwigo gallery and assign the category of to the image. // Uploads the pending images to the piwigo gallery and assign the category of to the image.
// Update local metadata and set upload flag to false. Also updates the piwigo image id if there was a difference. // Update local metadata and set upload flag to false. Also updates the piwigo image id if there was a difference.
func uploadImages(piwigoCtx piwigo.PiwigoImageApi, metadataProvider ImageMetadataProvider) error { func uploadImages(piwigoCtx piwigo.PiwigoImageApi, metadataProvider datastore.ImageMetadataProvider) error {
logrus.Debug("Starting uploadImages") logrus.Debug("Starting uploadImages")
defer logrus.Debug("Finished uploadImages successfully") defer logrus.Debug("Finished uploadImages successfully")
@ -152,7 +153,7 @@ func uploadImages(piwigoCtx piwigo.PiwigoImageApi, metadataProvider ImageMetadat
return nil return nil
} }
func deleteImages(piwigoCtx piwigo.PiwigoImageApi, metadataProvider ImageMetadataProvider) error { func deleteImages(piwigoCtx piwigo.PiwigoImageApi, metadataProvider datastore.ImageMetadataProvider) error {
logrus.Debug("Starting deleteImages") logrus.Debug("Starting deleteImages")
defer logrus.Debug("Finished deleteImages successfully") defer logrus.Debug("Finished deleteImages successfully")
@ -189,7 +190,7 @@ func deleteImages(piwigoCtx piwigo.PiwigoImageApi, metadataProvider ImageMetadat
} }
// This method aggregates the check for files with missing piwigoids and if changed files need to be uploaded again. // This method aggregates the check for files with missing piwigoids and if changed files need to be uploaded again.
func synchronizePiwigoMetadata(piwigoCtx piwigo.PiwigoImageApi, metadataProvider ImageMetadataProvider) error { func synchronizePiwigoMetadata(piwigoCtx piwigo.PiwigoImageApi, metadataProvider datastore.ImageMetadataProvider) error {
logrus.Debug("Entering synchronizePiwigoMetadata") logrus.Debug("Entering synchronizePiwigoMetadata")
defer logrus.Debug("Leaving synchronizePiwigoMetadata") defer logrus.Debug("Leaving synchronizePiwigoMetadata")
@ -208,7 +209,7 @@ func synchronizePiwigoMetadata(piwigoCtx piwigo.PiwigoImageApi, metadataProvider
} }
// 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.PiwigoImageApi) error { func checkPiwigoForChangedImages(provider datastore.ImageMetadataProvider, piwigoCtx piwigo.PiwigoImageApi) error {
logrus.Info("Checking pending files if they really differ from the version in piwigo...") logrus.Info("Checking pending files if they really differ from the version in piwigo...")
defer logrus.Info("Finished checking pending files if they really differ from the version in piwigo...") defer logrus.Info("Finished checking pending files if they really differ from the version in piwigo...")
@ -247,7 +248,7 @@ func checkPiwigoForChangedImages(provider ImageMetadataProvider, piwigoCtx piwig
// 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.PiwigoImageApi) error { func updatePiwigoIdIfAlreadyUploaded(provider datastore.ImageMetadataProvider, piwigoCtx piwigo.PiwigoImageApi) error {
logrus.Info("checking for pending files that are already on piwigo and updating piwigoids...") logrus.Info("checking for pending files that are already on piwigo and updating piwigoids...")
defer logrus.Info("finshed checking for pending files that are already on piwigo and updating piwigoids...") defer logrus.Info("finshed checking for pending files that are already on piwigo and updating piwigoids...")
@ -294,6 +295,6 @@ func updatePiwigoIdIfAlreadyUploaded(provider ImageMetadataProvider, piwigoCtx p
return nil return nil
} }
func fileDidNotChange(metadata *ImageMetaData, file *localFileStructure.FilesystemNode) bool { func fileDidNotChange(metadata *datastore.ImageMetaData, file *localFileStructure.FilesystemNode) bool {
return metadata.LastChange.Equal(file.ModTime) && !metadata.DeleteRequired return metadata.LastChange.Equal(file.ModTime) && !metadata.DeleteRequired
} }

View File

@ -6,9 +6,11 @@
package app package app
//go:generate mockgen -destination=./piwigo_mock_test.go -package=app git.haefelfinger.net/piwigo/PiwigoDirectoryUploader/internal/pkg/piwigo PiwigoApi,PiwigoCategoryApi,PiwigoImageApi //go:generate mockgen -destination=./piwigo_mock_test.go -package=app git.haefelfinger.net/piwigo/PiwigoDirectoryUploader/internal/pkg/piwigo PiwigoApi,PiwigoCategoryApi,PiwigoImageApi
//go:generate mockgen -destination=./datastore_mock_test.go -package=app git.haefelfinger.net/piwigo/PiwigoDirectoryUploader/internal/pkg/datastore ImageMetadataProvider
import ( import (
"errors" "errors"
"git.haefelfinger.net/piwigo/PiwigoDirectoryUploader/internal/pkg/datastore"
"git.haefelfinger.net/piwigo/PiwigoDirectoryUploader/internal/pkg/localFileStructure" "git.haefelfinger.net/piwigo/PiwigoDirectoryUploader/internal/pkg/localFileStructure"
"git.haefelfinger.net/piwigo/PiwigoDirectoryUploader/internal/pkg/piwigo" "git.haefelfinger.net/piwigo/PiwigoDirectoryUploader/internal/pkg/piwigo"
"github.com/golang/mock/gomock" "github.com/golang/mock/gomock"
@ -84,7 +86,7 @@ func Test_synchronize_local_image_metadata_should_mark_unchanged_entries_without
categories["2019/shooting1"] = &piwigo.PiwigoCategory{Id: 1} categories["2019/shooting1"] = &piwigo.PiwigoCategory{Id: 1}
db := NewtestStore() db := NewtestStore()
db.savedMetadata["2019/shooting1/abc.jpg"] = ImageMetaData{ db.savedMetadata["2019/shooting1/abc.jpg"] = datastore.ImageMetaData{
Md5Sum: "2019/shooting1/abc.jpg", Md5Sum: "2019/shooting1/abc.jpg",
FullImagePath: "2019/shooting1/abc.jpg", FullImagePath: "2019/shooting1/abc.jpg",
PiwigoId: 0, PiwigoId: 0,
@ -132,7 +134,7 @@ func Test_synchronize_local_image_metadata_should_mark_changed_entries_as_upload
categories["2019/shooting1"] = &piwigo.PiwigoCategory{Id: 1} categories["2019/shooting1"] = &piwigo.PiwigoCategory{Id: 1}
db := NewtestStore() db := NewtestStore()
db.savedMetadata["2019/shooting1/abc.jpg"] = ImageMetaData{ db.savedMetadata["2019/shooting1/abc.jpg"] = datastore.ImageMetaData{
Md5Sum: "2019/shooting1/abc.jpg", Md5Sum: "2019/shooting1/abc.jpg",
FullImagePath: "2019/shooting1/abc.jpg", FullImagePath: "2019/shooting1/abc.jpg",
UploadRequired: false, UploadRequired: false,
@ -179,7 +181,7 @@ func Test_synchronize_local_image_metadata_should_not_mark_unchanged_files_to_up
categories := make(map[string]*piwigo.PiwigoCategory) categories := make(map[string]*piwigo.PiwigoCategory)
categories["2019/shooting1"] = &piwigo.PiwigoCategory{Id: 1} categories["2019/shooting1"] = &piwigo.PiwigoCategory{Id: 1}
db.savedMetadata["2019/shooting1/abc.jpg"] = ImageMetaData{ db.savedMetadata["2019/shooting1/abc.jpg"] = datastore.ImageMetaData{
Md5Sum: "2019/shooting1/abc.jpg", Md5Sum: "2019/shooting1/abc.jpg",
FullImagePath: "2019/shooting1/abc.jpg", FullImagePath: "2019/shooting1/abc.jpg",
PiwigoId: 5, PiwigoId: 5,
@ -249,8 +251,8 @@ func Test_checkPiwigoForChangedImages_none_with_piwigoId(t *testing.T) {
mockCtrl := gomock.NewController(t) mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish() defer mockCtrl.Finish()
img := ImageMetaData{ImageId: 1, UploadRequired: true} img := datastore.ImageMetaData{ImageId: 1, UploadRequired: true}
images := []ImageMetaData{img} images := []datastore.ImageMetaData{img}
dbmock := NewMockImageMetadataProvider(mockCtrl) dbmock := NewMockImageMetadataProvider(mockCtrl)
dbmock.EXPECT().ImageMetadataToUpload().Return(images, nil) dbmock.EXPECT().ImageMetadataToUpload().Return(images, nil)
@ -269,7 +271,7 @@ func Test_checkPiwigoForChangedImages_with_empty_list(t *testing.T) {
mockCtrl := gomock.NewController(t) mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish() defer mockCtrl.Finish()
images := []ImageMetaData{} images := []datastore.ImageMetaData{}
dbmock := NewMockImageMetadataProvider(mockCtrl) dbmock := NewMockImageMetadataProvider(mockCtrl)
dbmock.EXPECT().ImageMetadataToUpload().Return(images, nil) dbmock.EXPECT().ImageMetadataToUpload().Return(images, nil)
@ -288,13 +290,13 @@ func Test_checkPiwigoForChangedImages_should_call_piwigo_set_uploadRequired_to_f
mockCtrl := gomock.NewController(t) mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish() defer mockCtrl.Finish()
img := ImageMetaData{ img := datastore.ImageMetaData{
ImageId: 1, ImageId: 1,
PiwigoId: 1, PiwigoId: 1,
UploadRequired: true, UploadRequired: true,
Md5Sum: "1234", Md5Sum: "1234",
} }
images := []ImageMetaData{img} images := []datastore.ImageMetaData{img}
dbmock := NewMockImageMetadataProvider(mockCtrl) dbmock := NewMockImageMetadataProvider(mockCtrl)
dbmock.EXPECT().ImageMetadataToUpload().Return(images, nil) dbmock.EXPECT().ImageMetadataToUpload().Return(images, nil)
@ -315,13 +317,13 @@ func Test_checkPiwigoForChangedImages_return_image_differs(t *testing.T) {
mockCtrl := gomock.NewController(t) mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish() defer mockCtrl.Finish()
img := ImageMetaData{ img := datastore.ImageMetaData{
ImageId: 1, ImageId: 1,
PiwigoId: 1, PiwigoId: 1,
UploadRequired: true, UploadRequired: true,
Md5Sum: "1234", Md5Sum: "1234",
} }
images := []ImageMetaData{img} images := []datastore.ImageMetaData{img}
dbmock := NewMockImageMetadataProvider(mockCtrl) dbmock := NewMockImageMetadataProvider(mockCtrl)
dbmock.EXPECT().ImageMetadataToUpload().Return(images, nil) dbmock.EXPECT().ImageMetadataToUpload().Return(images, nil)
dbmock.EXPECT().SaveImageMetadata(gomock.Any()).Times(0) dbmock.EXPECT().SaveImageMetadata(gomock.Any()).Times(0)
@ -339,7 +341,7 @@ func Test_updatePiwigoIdIfAlreadyUploaded_without_images_to_upload(t *testing.T)
mockCtrl := gomock.NewController(t) mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish() defer mockCtrl.Finish()
images := []ImageMetaData{} images := []datastore.ImageMetaData{}
dbmock := NewMockImageMetadataProvider(mockCtrl) dbmock := NewMockImageMetadataProvider(mockCtrl)
dbmock.EXPECT().ImageMetadataToUpload().Return(images, nil) dbmock.EXPECT().ImageMetadataToUpload().Return(images, nil)
@ -358,13 +360,13 @@ func Test_updatePiwigoIdIfAlreadyUploaded_without_image_to_check(t *testing.T) {
mockCtrl := gomock.NewController(t) mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish() defer mockCtrl.Finish()
img := ImageMetaData{ img := datastore.ImageMetaData{
ImageId: 1, ImageId: 1,
PiwigoId: 1, PiwigoId: 1,
UploadRequired: true, UploadRequired: true,
Md5Sum: "1234", Md5Sum: "1234",
} }
images := []ImageMetaData{img} images := []datastore.ImageMetaData{img}
dbmock := NewMockImageMetadataProvider(mockCtrl) dbmock := NewMockImageMetadataProvider(mockCtrl)
dbmock.EXPECT().ImageMetadataToUpload().Return(images, nil) dbmock.EXPECT().ImageMetadataToUpload().Return(images, nil)
@ -383,13 +385,13 @@ func Test_updatePiwigoIdIfAlreadyUploaded_with_image_to_check(t *testing.T) {
mockCtrl := gomock.NewController(t) mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish() defer mockCtrl.Finish()
img := ImageMetaData{ img := datastore.ImageMetaData{
ImageId: 1, ImageId: 1,
PiwigoId: 0, PiwigoId: 0,
UploadRequired: true, UploadRequired: true,
Md5Sum: "1234", Md5Sum: "1234",
} }
images := []ImageMetaData{img} images := []datastore.ImageMetaData{img}
dbmock := NewMockImageMetadataProvider(mockCtrl) dbmock := NewMockImageMetadataProvider(mockCtrl)
dbmock.EXPECT().ImageMetadataToUpload().Return(images, nil) dbmock.EXPECT().ImageMetadataToUpload().Return(images, nil)
@ -411,13 +413,13 @@ func Test_updatePiwigoIdIfAlreadyUploaded_with_image_to_check_missing_on_server(
mockCtrl := gomock.NewController(t) mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish() defer mockCtrl.Finish()
img := ImageMetaData{ img := datastore.ImageMetaData{
ImageId: 1, ImageId: 1,
PiwigoId: 0, PiwigoId: 0,
UploadRequired: true, UploadRequired: true,
Md5Sum: "1234", Md5Sum: "1234",
} }
images := []ImageMetaData{img} images := []datastore.ImageMetaData{img}
dbmock := NewMockImageMetadataProvider(mockCtrl) dbmock := NewMockImageMetadataProvider(mockCtrl)
dbmock.EXPECT().ImageMetadataToUpload().Return(images, nil) dbmock.EXPECT().ImageMetadataToUpload().Return(images, nil)
@ -439,7 +441,7 @@ func Test_uploadImages_saves_new_id_to_db(t *testing.T) {
defer mockCtrl.Finish() defer mockCtrl.Finish()
img := createTestImageMetaData(0) img := createTestImageMetaData(0)
images := []ImageMetaData{img} images := []datastore.ImageMetaData{img}
imgToSave := img imgToSave := img
imgToSave.PiwigoId = 5 imgToSave.PiwigoId = 5
@ -463,7 +465,7 @@ func Test_uploadImages_saves_same_id_to_db(t *testing.T) {
defer mockCtrl.Finish() defer mockCtrl.Finish()
img := createTestImageMetaData(5) img := createTestImageMetaData(5)
images := []ImageMetaData{img} images := []datastore.ImageMetaData{img}
imgToSave := img imgToSave := img
imgToSave.UploadRequired = false imgToSave.UploadRequired = false
@ -486,7 +488,7 @@ func Test_synchronizeLocalImageMetadataFindFilesToDelete(t *testing.T) {
defer mockCtrl.Finish() defer mockCtrl.Finish()
img := createTestImageMetaData(5) img := createTestImageMetaData(5)
images := []ImageMetaData{img} images := []datastore.ImageMetaData{img}
imgToSave := img imgToSave := img
imgToSave.UploadRequired = false imgToSave.UploadRequired = false
@ -509,7 +511,7 @@ func Test_deleteImages_should_call_piwigo_and_remove_metadata(t *testing.T) {
img := createTestImageMetaData(5) img := createTestImageMetaData(5)
img.UploadRequired = false img.UploadRequired = false
img.DeleteRequired = true img.DeleteRequired = true
images := []ImageMetaData{img} images := []datastore.ImageMetaData{img}
dbmock := NewMockImageMetadataProvider(mockCtrl) dbmock := NewMockImageMetadataProvider(mockCtrl)
dbmock.EXPECT().ImageMetadataToDelete().Times(1).Return(images, nil) dbmock.EXPECT().ImageMetadataToDelete().Times(1).Return(images, nil)
@ -531,7 +533,7 @@ func Test_deleteImages_should_not_call_piwigo_for_not_uploaded_images_and_remove
img := createTestImageMetaData(0) img := createTestImageMetaData(0)
img.UploadRequired = false img.UploadRequired = false
img.DeleteRequired = true img.DeleteRequired = true
images := []ImageMetaData{img} images := []datastore.ImageMetaData{img}
dbmock := NewMockImageMetadataProvider(mockCtrl) dbmock := NewMockImageMetadataProvider(mockCtrl)
dbmock.EXPECT().ImageMetadataToDelete().Times(1).Return(images, nil) dbmock.EXPECT().ImageMetadataToDelete().Times(1).Return(images, nil)
@ -550,7 +552,7 @@ func Test_deleteImages_should_not_call_anything_if_no_images_are_marked_for_dele
mockCtrl := gomock.NewController(t) mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish() defer mockCtrl.Finish()
images := []ImageMetaData{} images := []datastore.ImageMetaData{}
dbmock := NewMockImageMetadataProvider(mockCtrl) dbmock := NewMockImageMetadataProvider(mockCtrl)
dbmock.EXPECT().ImageMetadataToDelete().Times(1).Return(images, nil) dbmock.EXPECT().ImageMetadataToDelete().Times(1).Return(images, nil)
@ -568,35 +570,35 @@ func Test_deleteImages_should_not_call_anything_if_no_images_are_marked_for_dele
// test metadata store to store save the metadat and simulate the database // test metadata store to store save the metadat and simulate the database
//TODO: refactor to use generated test implementation //TODO: refactor to use generated test implementation
type testStore struct { type testStore struct {
savedMetadata map[string]ImageMetaData savedMetadata map[string]datastore.ImageMetaData
} }
func NewtestStore() *testStore { func NewtestStore() *testStore {
return &testStore{savedMetadata: make(map[string]ImageMetaData)} return &testStore{savedMetadata: make(map[string]datastore.ImageMetaData)}
} }
func (s *testStore) ImageMetadata(fullImagePath string) (ImageMetaData, error) { func (s *testStore) ImageMetadata(fullImagePath string) (datastore.ImageMetaData, error) {
metadata, exist := s.savedMetadata[fullImagePath] metadata, exist := s.savedMetadata[fullImagePath]
if !exist { if !exist {
return ImageMetaData{}, ErrorRecordNotFound return datastore.ImageMetaData{}, datastore.ErrorRecordNotFound
} }
return metadata, nil return metadata, nil
} }
func (d *testStore) ImageMetadataAll() ([]ImageMetaData, error) { func (d *testStore) ImageMetadataAll() ([]datastore.ImageMetaData, error) {
return []ImageMetaData{}, nil return []datastore.ImageMetaData{}, nil
} }
func (s *testStore) SaveImageMetadata(m ImageMetaData) error { func (s *testStore) SaveImageMetadata(m datastore.ImageMetaData) error {
s.savedMetadata[m.FullImagePath] = m s.savedMetadata[m.FullImagePath] = m
return nil return nil
} }
func (d *testStore) ImageMetadataToUpload() ([]ImageMetaData, error) { func (d *testStore) ImageMetadataToUpload() ([]datastore.ImageMetaData, error) {
return nil, errors.New("N/A") return nil, errors.New("N/A")
} }
func (d *testStore) ImageMetadataToDelete() ([]ImageMetaData, error) { func (d *testStore) ImageMetadataToDelete() ([]datastore.ImageMetaData, error) {
return nil, errors.New("N/A") return nil, errors.New("N/A")
} }
@ -613,8 +615,8 @@ func testChecksumCalculator(file string) (string, error) {
return file, nil return file, nil
} }
func createTestImageMetaData(piwigoId int) ImageMetaData { func createTestImageMetaData(piwigoId int) datastore.ImageMetaData {
img := ImageMetaData{ img := datastore.ImageMetaData{
ImageId: 1, ImageId: 1,
PiwigoId: piwigoId, PiwigoId: piwigoId,
FullImagePath: "/nonexisting/file.jpg", FullImagePath: "/nonexisting/file.jpg",

View File

@ -3,9 +3,7 @@
* This application is licensed under GPLv2. See the LICENSE file in the root directory of the project. * This application is licensed under GPLv2. See the LICENSE file in the root directory of the project.
*/ */
package app package datastore
//go:generate mockgen -destination=./datastore_mock_test.go -package=app git.haefelfinger.net/piwigo/PiwigoDirectoryUploader/internal/app ImageMetadataProvider
import ( import (
"database/sql" "database/sql"
@ -45,11 +43,15 @@ type ImageMetadataProvider interface {
DeleteMarkedImages() error DeleteMarkedImages() error
} }
type localDataStore struct { type LocalDataStore struct {
connectionString string connectionString string
} }
func (d *localDataStore) Initialize(connectionString string) error { func NewLocalDataStore() *LocalDataStore {
return &LocalDataStore{}
}
func (d *LocalDataStore) Initialize(connectionString string) error {
if connectionString == "" { if connectionString == "" {
return errors.New("connection string could not be empty.") return errors.New("connection string could not be empty.")
} }
@ -67,7 +69,7 @@ func (d *localDataStore) Initialize(connectionString string) error {
return err return err
} }
func (d *localDataStore) ImageMetadata(fullImagePath string) (ImageMetaData, error) { func (d *LocalDataStore) ImageMetadata(fullImagePath string) (ImageMetaData, error) {
logrus.Tracef("Query image metadata for file %s", fullImagePath) logrus.Tracef("Query image metadata for file %s", fullImagePath)
img := ImageMetaData{} img := ImageMetaData{}
@ -101,7 +103,7 @@ func (d *localDataStore) ImageMetadata(fullImagePath string) (ImageMetaData, err
return img, err return img, err
} }
func (d *localDataStore) ImageMetadataAll() ([]ImageMetaData, error) { func (d *LocalDataStore) ImageMetadataAll() ([]ImageMetaData, error) {
logrus.Tracef("Query all image metadata that represent files on the disk") logrus.Tracef("Query all image metadata that represent files on the disk")
db, err := d.openDatabase() db, err := d.openDatabase()
@ -130,7 +132,7 @@ func (d *localDataStore) ImageMetadataAll() ([]ImageMetaData, error) {
return images, err return images, err
} }
func (d *localDataStore) ImageMetadataToDelete() ([]ImageMetaData, error) { func (d *LocalDataStore) ImageMetadataToDelete() ([]ImageMetaData, error) {
logrus.Tracef("Query all image metadata that represent files queued to delete") logrus.Tracef("Query all image metadata that represent files queued to delete")
db, err := d.openDatabase() db, err := d.openDatabase()
@ -159,7 +161,7 @@ func (d *localDataStore) ImageMetadataToDelete() ([]ImageMetaData, error) {
return images, err return images, err
} }
func (d *localDataStore) ImageMetadataToUpload() ([]ImageMetaData, error) { func (d *LocalDataStore) ImageMetadataToUpload() ([]ImageMetaData, error) {
logrus.Tracef("Query all image metadata that represent files queued to upload") logrus.Tracef("Query all image metadata that represent files queued to upload")
db, err := d.openDatabase() db, err := d.openDatabase()
@ -193,7 +195,7 @@ func ReadImageMetadataFromRow(rows *sql.Rows, img *ImageMetaData) error {
return err return err
} }
func (d *localDataStore) SaveImageMetadata(img ImageMetaData) error { func (d *LocalDataStore) SaveImageMetadata(img ImageMetaData) error {
logrus.Tracef("Saving imagemetadata: %s", img.String()) logrus.Tracef("Saving imagemetadata: %s", img.String())
db, err := d.openDatabase() db, err := d.openDatabase()
if err != nil { if err != nil {
@ -225,7 +227,7 @@ func (d *localDataStore) SaveImageMetadata(img ImageMetaData) error {
return tx.Commit() return tx.Commit()
} }
func (d *localDataStore) SavePiwigoIdAndUpdateUploadFlag(md5Sum string, piwigoId int) error { func (d *LocalDataStore) SavePiwigoIdAndUpdateUploadFlag(md5Sum string, piwigoId int) error {
logrus.Tracef("Saving piwigo id %d for file with md5sum %s", piwigoId, md5Sum) logrus.Tracef("Saving piwigo id %d for file with md5sum %s", piwigoId, md5Sum)
db, err := d.openDatabase() db, err := d.openDatabase()
if err != nil { if err != nil {
@ -266,7 +268,7 @@ func (d *localDataStore) SavePiwigoIdAndUpdateUploadFlag(md5Sum string, piwigoId
return tx.Commit() return tx.Commit()
} }
func (d *localDataStore) DeleteMarkedImages() error { func (d *LocalDataStore) DeleteMarkedImages() error {
logrus.Trace("Deleting marked records from database...") logrus.Trace("Deleting marked records from database...")
db, err := d.openDatabase() db, err := d.openDatabase()
if err != nil { if err != nil {
@ -293,7 +295,7 @@ func (d *localDataStore) DeleteMarkedImages() error {
return tx.Commit() return tx.Commit()
} }
func (d *localDataStore) insertImageMetaData(tx *sql.Tx, data ImageMetaData) error { func (d *LocalDataStore) insertImageMetaData(tx *sql.Tx, data ImageMetaData) error {
stmt, err := tx.Prepare("INSERT INTO image (piwigoId, fullImagePath, fileName, md5sum, lastChanged, categoryPath, categoryId, uploadRequired, deleteRequired) VALUES (?,?,?,?,?,?,?,?,?)") stmt, err := tx.Prepare("INSERT INTO image (piwigoId, fullImagePath, fileName, md5sum, lastChanged, categoryPath, categoryId, uploadRequired, deleteRequired) VALUES (?,?,?,?,?,?,?,?,?)")
if err != nil { if err != nil {
return err return err
@ -302,7 +304,7 @@ func (d *localDataStore) insertImageMetaData(tx *sql.Tx, data ImageMetaData) err
return err return err
} }
func (d *localDataStore) openDatabase() (*sql.DB, error) { func (d *LocalDataStore) openDatabase() (*sql.DB, error) {
db, err := sql.Open("sqlite3", d.connectionString) db, err := sql.Open("sqlite3", d.connectionString)
if err != nil { if err != nil {
logrus.Warnf("Could not open database %s", d.connectionString) logrus.Warnf("Could not open database %s", d.connectionString)
@ -313,7 +315,7 @@ func (d *localDataStore) openDatabase() (*sql.DB, error) {
return db, err return db, err
} }
func (d *localDataStore) createTablesIfNeeded(db *sql.DB) error { func (d *LocalDataStore) createTablesIfNeeded(db *sql.DB) error {
_, err := db.Exec("CREATE TABLE IF NOT EXISTS image (" + _, err := db.Exec("CREATE TABLE IF NOT EXISTS image (" +
"imageId INTEGER PRIMARY KEY," + "imageId INTEGER PRIMARY KEY," +
"piwigoId INTEGER NULL," + "piwigoId INTEGER NULL," +
@ -334,7 +336,7 @@ func (d *localDataStore) createTablesIfNeeded(db *sql.DB) error {
return err return err
} }
func (d *localDataStore) updateImageMetaData(tx *sql.Tx, data ImageMetaData) error { func (d *LocalDataStore) updateImageMetaData(tx *sql.Tx, data ImageMetaData) error {
stmt, err := tx.Prepare("UPDATE image SET piwigoId = ?, fullImagePath = ?, fileName = ?, md5sum = ?, lastChanged = ?, categoryPath = ?, categoryId = ?, uploadRequired = ?, deleteRequired = ? WHERE imageId = ?") stmt, err := tx.Prepare("UPDATE image SET piwigoId = ?, fullImagePath = ?, fileName = ?, md5sum = ?, lastChanged = ?, categoryPath = ?, categoryId = ?, uploadRequired = ?, deleteRequired = ? WHERE imageId = ?")
if err != nil { if err != nil {
return err return err

View File

@ -3,7 +3,7 @@
* This application is licensed under GPLv2. See the LICENSE file in the root directory of the project. * This application is licensed under GPLv2. See the LICENSE file in the root directory of the project.
*/ */
package app package datastore
import ( import (
"os" "os"
@ -288,14 +288,14 @@ func Test_deleteMarkedImages_should_remove_records(t *testing.T) {
} }
} }
func saveImageShouldNotFail(action string, dataStore *localDataStore, img ImageMetaData, t *testing.T) { func saveImageShouldNotFail(action string, dataStore *LocalDataStore, img ImageMetaData, t *testing.T) {
err := dataStore.SaveImageMetadata(img) err := dataStore.SaveImageMetadata(img)
if err != nil { if err != nil {
t.Errorf("%s: Could not save Metadata: %s", action, err) t.Errorf("%s: Could not save Metadata: %s", action, err)
} }
} }
func loadMetadataShouldNotFail(action string, dataStore *localDataStore, filePath string, t *testing.T) ImageMetaData { func loadMetadataShouldNotFail(action string, dataStore *LocalDataStore, filePath string, t *testing.T) ImageMetaData {
imgLoad, err := dataStore.ImageMetadata(filePath) imgLoad, err := dataStore.ImageMetadata(filePath)
if err != nil { if err != nil {
t.Errorf("%s: Could not load saved Metadata: %s - %s", action, filePath, err) t.Errorf("%s: Could not load saved Metadata: %s - %s", action, filePath, err)
@ -330,8 +330,8 @@ func cleanupDatabase(t *testing.T) {
} }
} }
func setupDatabase(t *testing.T) *localDataStore { func setupDatabase(t *testing.T) *LocalDataStore {
dataStore := &localDataStore{} dataStore := &LocalDataStore{}
err := dataStore.Initialize(databaseFile) err := dataStore.Initialize(databaseFile)
if err != nil { if err != nil {
t.Errorf("Failed to init datastore: %s", err) t.Errorf("Failed to init datastore: %s", err)