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

View File

@ -1,10 +1,11 @@
// 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
import (
datastore "git.haefelfinger.net/piwigo/PiwigoDirectoryUploader/internal/pkg/datastore"
gomock "github.com/golang/mock/gomock"
reflect "reflect"
)
@ -47,10 +48,10 @@ func (mr *MockImageMetadataProviderMockRecorder) DeleteMarkedImages() *gomock.Ca
}
// 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()
ret := m.ctrl.Call(m, "ImageMetadata", arg0)
ret0, _ := ret[0].(ImageMetaData)
ret0, _ := ret[0].(datastore.ImageMetaData)
ret1, _ := ret[1].(error)
return ret0, ret1
}
@ -62,10 +63,10 @@ func (mr *MockImageMetadataProviderMockRecorder) ImageMetadata(arg0 interface{})
}
// ImageMetadataAll mocks base method
func (m *MockImageMetadataProvider) ImageMetadataAll() ([]ImageMetaData, error) {
func (m *MockImageMetadataProvider) ImageMetadataAll() ([]datastore.ImageMetaData, error) {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "ImageMetadataAll")
ret0, _ := ret[0].([]ImageMetaData)
ret0, _ := ret[0].([]datastore.ImageMetaData)
ret1, _ := ret[1].(error)
return ret0, ret1
}
@ -77,10 +78,10 @@ func (mr *MockImageMetadataProviderMockRecorder) ImageMetadataAll() *gomock.Call
}
// ImageMetadataToDelete mocks base method
func (m *MockImageMetadataProvider) ImageMetadataToDelete() ([]ImageMetaData, error) {
func (m *MockImageMetadataProvider) ImageMetadataToDelete() ([]datastore.ImageMetaData, error) {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "ImageMetadataToDelete")
ret0, _ := ret[0].([]ImageMetaData)
ret0, _ := ret[0].([]datastore.ImageMetaData)
ret1, _ := ret[1].(error)
return ret0, ret1
}
@ -92,10 +93,10 @@ func (mr *MockImageMetadataProviderMockRecorder) ImageMetadataToDelete() *gomock
}
// ImageMetadataToUpload mocks base method
func (m *MockImageMetadataProvider) ImageMetadataToUpload() ([]ImageMetaData, error) {
func (m *MockImageMetadataProvider) ImageMetadataToUpload() ([]datastore.ImageMetaData, error) {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "ImageMetadataToUpload")
ret0, _ := ret[0].([]ImageMetaData)
ret0, _ := ret[0].([]datastore.ImageMetaData)
ret1, _ := ret[1].(error)
return ret0, ret1
}
@ -107,7 +108,7 @@ func (mr *MockImageMetadataProviderMockRecorder) ImageMetadataToUpload() *gomock
}
// SaveImageMetadata mocks base method
func (m *MockImageMetadataProvider) SaveImageMetadata(arg0 ImageMetaData) error {
func (m *MockImageMetadataProvider) SaveImageMetadata(arg0 datastore.ImageMetaData) error {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "SaveImageMetadata", arg0)
ret0, _ := ret[0].(error)

View File

@ -6,6 +6,7 @@
package app
import (
"git.haefelfinger.net/piwigo/PiwigoDirectoryUploader/internal/pkg/datastore"
"git.haefelfinger.net/piwigo/PiwigoDirectoryUploader/internal/pkg/localFileStructure"
"git.haefelfinger.net/piwigo/PiwigoDirectoryUploader/internal/pkg/piwigo"
"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
// 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")
defer logrus.Debug("Leaving synchronizeLocalImageMetadata")
@ -39,7 +40,7 @@ func synchronizeLocalImageMetadata(metadataStorage ImageMetadataProvider, fileSy
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")
defer logrus.Debug("Leaving synchronizeLocalImageMetadataScanNewFiles")
@ -50,9 +51,9 @@ func synchronizeLocalImageMetadataScanNewFiles(fileSystemNodes map[string]*local
}
metadata, err := metadataStorage.ImageMetadata(file.Path)
if err == ErrorRecordNotFound {
if err == datastore.ErrorRecordNotFound {
logrus.Debugf("Creating new metadata entry for %s.", file.Path)
metadata = ImageMetaData{}
metadata = datastore.ImageMetaData{}
metadata.Filename = file.Name
metadata.FullImagePath = file.Path
metadata.CategoryPath = filepath.Dir(file.Key)
@ -91,7 +92,7 @@ func synchronizeLocalImageMetadataScanNewFiles(fileSystemNodes map[string]*local
return nil
}
func synchronizeLocalImageMetadataFindFilesToDelete(provider ImageMetadataProvider) error {
func synchronizeLocalImageMetadataFindFilesToDelete(provider datastore.ImageMetadataProvider) error {
logrus.Debug("Entering 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.
// 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")
defer logrus.Debug("Finished uploadImages successfully")
@ -152,7 +153,7 @@ func uploadImages(piwigoCtx piwigo.PiwigoImageApi, metadataProvider ImageMetadat
return nil
}
func deleteImages(piwigoCtx piwigo.PiwigoImageApi, metadataProvider ImageMetadataProvider) error {
func deleteImages(piwigoCtx piwigo.PiwigoImageApi, metadataProvider datastore.ImageMetadataProvider) error {
logrus.Debug("Starting deleteImages")
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.
func synchronizePiwigoMetadata(piwigoCtx piwigo.PiwigoImageApi, metadataProvider ImageMetadataProvider) error {
func synchronizePiwigoMetadata(piwigoCtx piwigo.PiwigoImageApi, metadataProvider datastore.ImageMetadataProvider) error {
logrus.Debug("Entering 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.
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...")
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.
// 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...")
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
}
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
}

View File

@ -6,9 +6,11 @@
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=./datastore_mock_test.go -package=app git.haefelfinger.net/piwigo/PiwigoDirectoryUploader/internal/pkg/datastore ImageMetadataProvider
import (
"errors"
"git.haefelfinger.net/piwigo/PiwigoDirectoryUploader/internal/pkg/datastore"
"git.haefelfinger.net/piwigo/PiwigoDirectoryUploader/internal/pkg/localFileStructure"
"git.haefelfinger.net/piwigo/PiwigoDirectoryUploader/internal/pkg/piwigo"
"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}
db := NewtestStore()
db.savedMetadata["2019/shooting1/abc.jpg"] = ImageMetaData{
db.savedMetadata["2019/shooting1/abc.jpg"] = datastore.ImageMetaData{
Md5Sum: "2019/shooting1/abc.jpg",
FullImagePath: "2019/shooting1/abc.jpg",
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}
db := NewtestStore()
db.savedMetadata["2019/shooting1/abc.jpg"] = ImageMetaData{
db.savedMetadata["2019/shooting1/abc.jpg"] = datastore.ImageMetaData{
Md5Sum: "2019/shooting1/abc.jpg",
FullImagePath: "2019/shooting1/abc.jpg",
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["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",
FullImagePath: "2019/shooting1/abc.jpg",
PiwigoId: 5,
@ -249,8 +251,8 @@ func Test_checkPiwigoForChangedImages_none_with_piwigoId(t *testing.T) {
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()
img := ImageMetaData{ImageId: 1, UploadRequired: true}
images := []ImageMetaData{img}
img := datastore.ImageMetaData{ImageId: 1, UploadRequired: true}
images := []datastore.ImageMetaData{img}
dbmock := NewMockImageMetadataProvider(mockCtrl)
dbmock.EXPECT().ImageMetadataToUpload().Return(images, nil)
@ -269,7 +271,7 @@ func Test_checkPiwigoForChangedImages_with_empty_list(t *testing.T) {
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()
images := []ImageMetaData{}
images := []datastore.ImageMetaData{}
dbmock := NewMockImageMetadataProvider(mockCtrl)
dbmock.EXPECT().ImageMetadataToUpload().Return(images, nil)
@ -288,13 +290,13 @@ func Test_checkPiwigoForChangedImages_should_call_piwigo_set_uploadRequired_to_f
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()
img := ImageMetaData{
img := datastore.ImageMetaData{
ImageId: 1,
PiwigoId: 1,
UploadRequired: true,
Md5Sum: "1234",
}
images := []ImageMetaData{img}
images := []datastore.ImageMetaData{img}
dbmock := NewMockImageMetadataProvider(mockCtrl)
dbmock.EXPECT().ImageMetadataToUpload().Return(images, nil)
@ -315,13 +317,13 @@ func Test_checkPiwigoForChangedImages_return_image_differs(t *testing.T) {
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()
img := ImageMetaData{
img := datastore.ImageMetaData{
ImageId: 1,
PiwigoId: 1,
UploadRequired: true,
Md5Sum: "1234",
}
images := []ImageMetaData{img}
images := []datastore.ImageMetaData{img}
dbmock := NewMockImageMetadataProvider(mockCtrl)
dbmock.EXPECT().ImageMetadataToUpload().Return(images, nil)
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)
defer mockCtrl.Finish()
images := []ImageMetaData{}
images := []datastore.ImageMetaData{}
dbmock := NewMockImageMetadataProvider(mockCtrl)
dbmock.EXPECT().ImageMetadataToUpload().Return(images, nil)
@ -358,13 +360,13 @@ func Test_updatePiwigoIdIfAlreadyUploaded_without_image_to_check(t *testing.T) {
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()
img := ImageMetaData{
img := datastore.ImageMetaData{
ImageId: 1,
PiwigoId: 1,
UploadRequired: true,
Md5Sum: "1234",
}
images := []ImageMetaData{img}
images := []datastore.ImageMetaData{img}
dbmock := NewMockImageMetadataProvider(mockCtrl)
dbmock.EXPECT().ImageMetadataToUpload().Return(images, nil)
@ -383,13 +385,13 @@ func Test_updatePiwigoIdIfAlreadyUploaded_with_image_to_check(t *testing.T) {
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()
img := ImageMetaData{
img := datastore.ImageMetaData{
ImageId: 1,
PiwigoId: 0,
UploadRequired: true,
Md5Sum: "1234",
}
images := []ImageMetaData{img}
images := []datastore.ImageMetaData{img}
dbmock := NewMockImageMetadataProvider(mockCtrl)
dbmock.EXPECT().ImageMetadataToUpload().Return(images, nil)
@ -411,13 +413,13 @@ func Test_updatePiwigoIdIfAlreadyUploaded_with_image_to_check_missing_on_server(
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()
img := ImageMetaData{
img := datastore.ImageMetaData{
ImageId: 1,
PiwigoId: 0,
UploadRequired: true,
Md5Sum: "1234",
}
images := []ImageMetaData{img}
images := []datastore.ImageMetaData{img}
dbmock := NewMockImageMetadataProvider(mockCtrl)
dbmock.EXPECT().ImageMetadataToUpload().Return(images, nil)
@ -439,7 +441,7 @@ func Test_uploadImages_saves_new_id_to_db(t *testing.T) {
defer mockCtrl.Finish()
img := createTestImageMetaData(0)
images := []ImageMetaData{img}
images := []datastore.ImageMetaData{img}
imgToSave := img
imgToSave.PiwigoId = 5
@ -463,7 +465,7 @@ func Test_uploadImages_saves_same_id_to_db(t *testing.T) {
defer mockCtrl.Finish()
img := createTestImageMetaData(5)
images := []ImageMetaData{img}
images := []datastore.ImageMetaData{img}
imgToSave := img
imgToSave.UploadRequired = false
@ -486,7 +488,7 @@ func Test_synchronizeLocalImageMetadataFindFilesToDelete(t *testing.T) {
defer mockCtrl.Finish()
img := createTestImageMetaData(5)
images := []ImageMetaData{img}
images := []datastore.ImageMetaData{img}
imgToSave := img
imgToSave.UploadRequired = false
@ -509,7 +511,7 @@ func Test_deleteImages_should_call_piwigo_and_remove_metadata(t *testing.T) {
img := createTestImageMetaData(5)
img.UploadRequired = false
img.DeleteRequired = true
images := []ImageMetaData{img}
images := []datastore.ImageMetaData{img}
dbmock := NewMockImageMetadataProvider(mockCtrl)
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.UploadRequired = false
img.DeleteRequired = true
images := []ImageMetaData{img}
images := []datastore.ImageMetaData{img}
dbmock := NewMockImageMetadataProvider(mockCtrl)
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)
defer mockCtrl.Finish()
images := []ImageMetaData{}
images := []datastore.ImageMetaData{}
dbmock := NewMockImageMetadataProvider(mockCtrl)
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
//TODO: refactor to use generated test implementation
type testStore struct {
savedMetadata map[string]ImageMetaData
savedMetadata map[string]datastore.ImageMetaData
}
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]
if !exist {
return ImageMetaData{}, ErrorRecordNotFound
return datastore.ImageMetaData{}, datastore.ErrorRecordNotFound
}
return metadata, nil
}
func (d *testStore) ImageMetadataAll() ([]ImageMetaData, error) {
return []ImageMetaData{}, nil
func (d *testStore) ImageMetadataAll() ([]datastore.ImageMetaData, error) {
return []datastore.ImageMetaData{}, nil
}
func (s *testStore) SaveImageMetadata(m ImageMetaData) error {
func (s *testStore) SaveImageMetadata(m datastore.ImageMetaData) error {
s.savedMetadata[m.FullImagePath] = m
return nil
}
func (d *testStore) ImageMetadataToUpload() ([]ImageMetaData, error) {
func (d *testStore) ImageMetadataToUpload() ([]datastore.ImageMetaData, error) {
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")
}
@ -613,8 +615,8 @@ func testChecksumCalculator(file string) (string, error) {
return file, nil
}
func createTestImageMetaData(piwigoId int) ImageMetaData {
img := ImageMetaData{
func createTestImageMetaData(piwigoId int) datastore.ImageMetaData {
img := datastore.ImageMetaData{
ImageId: 1,
PiwigoId: piwigoId,
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.
*/
package app
//go:generate mockgen -destination=./datastore_mock_test.go -package=app git.haefelfinger.net/piwigo/PiwigoDirectoryUploader/internal/app ImageMetadataProvider
package datastore
import (
"database/sql"
@ -45,11 +43,15 @@ type ImageMetadataProvider interface {
DeleteMarkedImages() error
}
type localDataStore struct {
type LocalDataStore struct {
connectionString string
}
func (d *localDataStore) Initialize(connectionString string) error {
func NewLocalDataStore() *LocalDataStore {
return &LocalDataStore{}
}
func (d *LocalDataStore) Initialize(connectionString string) error {
if connectionString == "" {
return errors.New("connection string could not be empty.")
}
@ -67,7 +69,7 @@ func (d *localDataStore) Initialize(connectionString string) error {
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)
img := ImageMetaData{}
@ -101,7 +103,7 @@ func (d *localDataStore) ImageMetadata(fullImagePath string) (ImageMetaData, 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")
db, err := d.openDatabase()
@ -130,7 +132,7 @@ func (d *localDataStore) ImageMetadataAll() ([]ImageMetaData, error) {
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")
db, err := d.openDatabase()
@ -159,7 +161,7 @@ func (d *localDataStore) ImageMetadataToDelete() ([]ImageMetaData, error) {
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")
db, err := d.openDatabase()
@ -193,7 +195,7 @@ func ReadImageMetadataFromRow(rows *sql.Rows, img *ImageMetaData) error {
return err
}
func (d *localDataStore) SaveImageMetadata(img ImageMetaData) error {
func (d *LocalDataStore) SaveImageMetadata(img ImageMetaData) error {
logrus.Tracef("Saving imagemetadata: %s", img.String())
db, err := d.openDatabase()
if err != nil {
@ -225,7 +227,7 @@ func (d *localDataStore) SaveImageMetadata(img ImageMetaData) error {
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)
db, err := d.openDatabase()
if err != nil {
@ -266,7 +268,7 @@ func (d *localDataStore) SavePiwigoIdAndUpdateUploadFlag(md5Sum string, piwigoId
return tx.Commit()
}
func (d *localDataStore) DeleteMarkedImages() error {
func (d *LocalDataStore) DeleteMarkedImages() error {
logrus.Trace("Deleting marked records from database...")
db, err := d.openDatabase()
if err != nil {
@ -293,7 +295,7 @@ func (d *localDataStore) DeleteMarkedImages() error {
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 (?,?,?,?,?,?,?,?,?)")
if err != nil {
return err
@ -302,7 +304,7 @@ func (d *localDataStore) insertImageMetaData(tx *sql.Tx, data ImageMetaData) err
return err
}
func (d *localDataStore) openDatabase() (*sql.DB, error) {
func (d *LocalDataStore) openDatabase() (*sql.DB, error) {
db, err := sql.Open("sqlite3", d.connectionString)
if err != nil {
logrus.Warnf("Could not open database %s", d.connectionString)
@ -313,7 +315,7 @@ func (d *localDataStore) openDatabase() (*sql.DB, error) {
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 (" +
"imageId INTEGER PRIMARY KEY," +
"piwigoId INTEGER NULL," +
@ -334,7 +336,7 @@ func (d *localDataStore) createTablesIfNeeded(db *sql.DB) error {
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 = ?")
if err != nil {
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.
*/
package app
package datastore
import (
"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)
if err != nil {
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)
if err != nil {
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 {
dataStore := &localDataStore{}
func setupDatabase(t *testing.T) *LocalDataStore {
dataStore := &LocalDataStore{}
err := dataStore.Initialize(databaseFile)
if err != nil {
t.Errorf("Failed to init datastore: %s", err)