From 58c1f8b9ff79d4dc22ce4a814447728c0757ba9b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20H=C3=A4felfinger?= Date: Tue, 2 Apr 2019 21:45:46 +0200 Subject: [PATCH] moved images to own package --- internal/app/app.go | 9 +++---- .../images}/datastore_mock_test.go | 7 +++++- internal/{app => pkg/images}/images.go | 22 ++++++++--------- internal/{app => pkg/images}/images_test.go | 24 +++++++++---------- .../{app => pkg/images}/piwigo_mock_test.go | 7 +++++- 5 files changed, 40 insertions(+), 29 deletions(-) rename internal/{app => pkg/images}/datastore_mock_test.go (96%) rename internal/{app => pkg/images}/images.go (93%) rename internal/{app => pkg/images}/images_test.go (96%) rename internal/{app => pkg/images}/piwigo_mock_test.go (97%) diff --git a/internal/app/app.go b/internal/app/app.go index 9958e07..93f72fd 100644 --- a/internal/app/app.go +++ b/internal/app/app.go @@ -7,6 +7,7 @@ package app import ( "flag" + "git.haefelfinger.net/piwigo/PiwigoDirectoryUploader/internal/pkg/images" "git.haefelfinger.net/piwigo/PiwigoDirectoryUploader/internal/pkg/localFileStructure" "github.com/sirupsen/logrus" "os" @@ -48,18 +49,18 @@ func Run() { logErrorAndExit(err, 5) } - err = synchronizeLocalImageMetadata(context.dataStore, filesystemNodes, categories, localFileStructure.CalculateFileCheckSums) + err = images.SynchronizeLocalImageMetadata(context.dataStore, filesystemNodes, categories, localFileStructure.CalculateFileCheckSums) if err != nil { logErrorAndExit(err, 6) } - err = synchronizePiwigoMetadata(context.piwigo, context.dataStore) + err = images.SynchronizePiwigoMetadata(context.piwigo, context.dataStore) if err != nil { logErrorAndExit(err, 7) } if *removeImages { - err = deleteImages(context.piwigo, context.dataStore) + err = images.DeleteImages(context.piwigo, context.dataStore) if err != nil { logErrorAndExit(err, 8) } @@ -68,7 +69,7 @@ func Run() { } if !(*noUpload) { - err = uploadImages(context.piwigo, context.dataStore) + err = images.UploadImages(context.piwigo, context.dataStore) if err != nil { logErrorAndExit(err, 9) } diff --git a/internal/app/datastore_mock_test.go b/internal/pkg/images/datastore_mock_test.go similarity index 96% rename from internal/app/datastore_mock_test.go rename to internal/pkg/images/datastore_mock_test.go index 8de1225..46e1133 100644 --- a/internal/app/datastore_mock_test.go +++ b/internal/pkg/images/datastore_mock_test.go @@ -1,8 +1,13 @@ +/* + * 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. + */ + // Code generated by MockGen. DO NOT EDIT. // Source: git.haefelfinger.net/piwigo/PiwigoDirectoryUploader/internal/pkg/datastore (interfaces: ImageMetadataProvider) // Package app is a generated GoMock package. -package app +package images import ( datastore "git.haefelfinger.net/piwigo/PiwigoDirectoryUploader/internal/pkg/datastore" diff --git a/internal/app/images.go b/internal/pkg/images/images.go similarity index 93% rename from internal/app/images.go rename to internal/pkg/images/images.go index f8148f2..b886008 100644 --- a/internal/app/images.go +++ b/internal/pkg/images/images.go @@ -3,7 +3,7 @@ * This application is licensed under GPLv2. See the LICENSE file in the root directory of the project. */ -package app +package images import ( "git.haefelfinger.net/piwigo/PiwigoDirectoryUploader/internal/pkg/datastore" @@ -22,9 +22,9 @@ 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 datastore.ImageMetadataProvider, fileSystemNodes map[string]*localFileStructure.FilesystemNode, categories map[string]*piwigo.PiwigoCategory, checksumCalculator fileChecksumCalculator) error { - logrus.Debug("Starting synchronizeLocalImageMetadata") - defer logrus.Debug("Leaving synchronizeLocalImageMetadata") +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") logrus.Info("Synchronizing local image metadata database with local available images") @@ -93,8 +93,8 @@ func synchronizeLocalImageMetadataScanNewFiles(fileSystemNodes map[string]*local } func synchronizeLocalImageMetadataFindFilesToDelete(provider datastore.ImageMetadataProvider) error { - logrus.Debug("Entering synchronizeLocalImageMetadataFindFilesToDelete") - defer logrus.Debug("Leaving synchronizeLocalImageMetadataFindFilesToDelete") + logrus.Debug("Entering SynchronizeLocalImageMetadataFindFilesToDelete") + defer logrus.Debug("Leaving SynchronizeLocalImageMetadataFindFilesToDelete") images, err := provider.ImageMetadataAll() if err != nil { @@ -116,7 +116,7 @@ func synchronizeLocalImageMetadataFindFilesToDelete(provider datastore.ImageMeta // 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 datastore.ImageMetadataProvider) error { +func UploadImages(piwigoCtx piwigo.PiwigoImageApi, metadataProvider datastore.ImageMetadataProvider) error { logrus.Debug("Starting uploadImages") defer logrus.Debug("Finished uploadImages successfully") @@ -153,7 +153,7 @@ func uploadImages(piwigoCtx piwigo.PiwigoImageApi, metadataProvider datastore.Im return nil } -func deleteImages(piwigoCtx piwigo.PiwigoImageApi, metadataProvider datastore.ImageMetadataProvider) error { +func DeleteImages(piwigoCtx piwigo.PiwigoImageApi, metadataProvider datastore.ImageMetadataProvider) error { logrus.Debug("Starting deleteImages") defer logrus.Debug("Finished deleteImages successfully") @@ -190,9 +190,9 @@ func deleteImages(piwigoCtx piwigo.PiwigoImageApi, metadataProvider datastore.Im } // 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 datastore.ImageMetadataProvider) error { - logrus.Debug("Entering synchronizePiwigoMetadata") - defer logrus.Debug("Leaving synchronizePiwigoMetadata") +func SynchronizePiwigoMetadata(piwigoCtx piwigo.PiwigoImageApi, metadataProvider datastore.ImageMetadataProvider) error { + logrus.Debug("Entering SynchronizePiwigoMetadata") + defer logrus.Debug("Leaving SynchronizePiwigoMetadata") // TODO: check if category has to be assigned (image possibly added to two albums -> only uploaded once but assigned multiple times) -> implement later err := updatePiwigoIdIfAlreadyUploaded(metadataProvider, piwigoCtx) diff --git a/internal/app/images_test.go b/internal/pkg/images/images_test.go similarity index 96% rename from internal/app/images_test.go rename to internal/pkg/images/images_test.go index 889307e..30542dc 100644 --- a/internal/app/images_test.go +++ b/internal/pkg/images/images_test.go @@ -3,7 +3,7 @@ * This application is licensed under GPLv2. See the LICENSE file in the root directory of the project. */ -package app +package images //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 @@ -25,7 +25,7 @@ func Test_synchronize_local_image_metadata_should_find_nothing_if_empty(t *testi db := NewtestStore() fileSystemNodes := map[string]*localFileStructure.FilesystemNode{} - err := synchronizeLocalImageMetadata(db, fileSystemNodes, categories, testChecksumCalculator) + err := SynchronizeLocalImageMetadata(db, fileSystemNodes, categories, testChecksumCalculator) if err != nil { t.Error(err) } @@ -53,7 +53,7 @@ func Test_synchronize_local_image_metadata_should_add_new_metadata(t *testing.T) fileSystemNodes[testFileSystemNode.Key] = testFileSystemNode // execute the sync metadata based on the file system results - err := synchronizeLocalImageMetadata(db, fileSystemNodes, categories, testChecksumCalculator) + err := SynchronizeLocalImageMetadata(db, fileSystemNodes, categories, testChecksumCalculator) if err != nil { t.Error(err) } @@ -107,7 +107,7 @@ func Test_synchronize_local_image_metadata_should_mark_unchanged_entries_without fileSystemNodes[testFileSystemNode.Key] = testFileSystemNode // execute the sync metadata based on the file system results - err := synchronizeLocalImageMetadata(db, fileSystemNodes, categories, testChecksumCalculator) + err := SynchronizeLocalImageMetadata(db, fileSystemNodes, categories, testChecksumCalculator) if err != nil { t.Error(err) } @@ -154,7 +154,7 @@ func Test_synchronize_local_image_metadata_should_mark_changed_entries_as_upload fileSystemNodes[testFileSystemNode.Key] = testFileSystemNode // execute the sync metadata based on the file system results - err := synchronizeLocalImageMetadata(db, fileSystemNodes, categories, testChecksumCalculator) + err := SynchronizeLocalImageMetadata(db, fileSystemNodes, categories, testChecksumCalculator) if err != nil { t.Error(err) } @@ -202,7 +202,7 @@ func Test_synchronize_local_image_metadata_should_not_mark_unchanged_files_to_up fileSystemNodes[testFileSystemNode.Key] = testFileSystemNode // execute the sync metadata based on the file system results - err := synchronizeLocalImageMetadata(db, fileSystemNodes, categories, testChecksumCalculator) + err := SynchronizeLocalImageMetadata(db, fileSystemNodes, categories, testChecksumCalculator) if err != nil { t.Error(err) } @@ -237,7 +237,7 @@ func Test_synchronize_local_image_metadata_should_not_process_directories(t *tes fileSystemNodes[testFileSystemNode.Key] = testFileSystemNode // execute the sync metadata based on the file system results - err := synchronizeLocalImageMetadata(db, fileSystemNodes, categories, testChecksumCalculator) + err := SynchronizeLocalImageMetadata(db, fileSystemNodes, categories, testChecksumCalculator) if err != nil { t.Error(err) } @@ -454,7 +454,7 @@ func Test_uploadImages_saves_new_id_to_db(t *testing.T) { piwigomock := NewMockPiwigoImageApi(mockCtrl) piwigomock.EXPECT().UploadImage(0, "/nonexisting/file.jpg", "1234", 2).Times(1).Return(5, nil) - err := uploadImages(piwigomock, dbmock) + err := UploadImages(piwigomock, dbmock) if err != nil { t.Error(err) } @@ -477,7 +477,7 @@ func Test_uploadImages_saves_same_id_to_db(t *testing.T) { piwigomock := NewMockPiwigoImageApi(mockCtrl) piwigomock.EXPECT().UploadImage(5, "/nonexisting/file.jpg", "1234", 2).Times(1).Return(5, nil) - err := uploadImages(piwigomock, dbmock) + err := UploadImages(piwigomock, dbmock) if err != nil { t.Error(err) } @@ -520,7 +520,7 @@ func Test_deleteImages_should_call_piwigo_and_remove_metadata(t *testing.T) { piwigomock := NewMockPiwigoImageApi(mockCtrl) piwigomock.EXPECT().DeleteImages([]int{5}).Times(1).Return(nil) - err := deleteImages(piwigomock, dbmock) + err := DeleteImages(piwigomock, dbmock) if err != nil { t.Error(err) } @@ -542,7 +542,7 @@ func Test_deleteImages_should_not_call_piwigo_for_not_uploaded_images_and_remove piwigomock := NewMockPiwigoImageApi(mockCtrl) piwigomock.EXPECT().DeleteImages(gomock.Any()).Times(0) - err := deleteImages(piwigomock, dbmock) + err := DeleteImages(piwigomock, dbmock) if err != nil { t.Error(err) } @@ -561,7 +561,7 @@ func Test_deleteImages_should_not_call_anything_if_no_images_are_marked_for_dele piwigomock := NewMockPiwigoImageApi(mockCtrl) piwigomock.EXPECT().DeleteImages(gomock.Any()).Times(0) - err := deleteImages(piwigomock, dbmock) + err := DeleteImages(piwigomock, dbmock) if err != nil { t.Error(err) } diff --git a/internal/app/piwigo_mock_test.go b/internal/pkg/images/piwigo_mock_test.go similarity index 97% rename from internal/app/piwigo_mock_test.go rename to internal/pkg/images/piwigo_mock_test.go index 4820a2e..6a18b62 100644 --- a/internal/app/piwigo_mock_test.go +++ b/internal/pkg/images/piwigo_mock_test.go @@ -1,8 +1,13 @@ +/* + * 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. + */ + // Code generated by MockGen. DO NOT EDIT. // Source: git.haefelfinger.net/piwigo/PiwigoDirectoryUploader/internal/pkg/piwigo (interfaces: PiwigoApi,PiwigoCategoryApi,PiwigoImageApi) // Package app is a generated GoMock package. -package app +package images import ( piwigo "git.haefelfinger.net/piwigo/PiwigoDirectoryUploader/internal/pkg/piwigo"