moved images to own package

This commit is contained in:
Philipp Häfelfinger 2019-04-02 21:45:46 +02:00
parent bd5250f0d5
commit 58c1f8b9ff
5 changed files with 40 additions and 29 deletions

View File

@ -7,6 +7,7 @@ package app
import ( import (
"flag" "flag"
"git.haefelfinger.net/piwigo/PiwigoDirectoryUploader/internal/pkg/images"
"git.haefelfinger.net/piwigo/PiwigoDirectoryUploader/internal/pkg/localFileStructure" "git.haefelfinger.net/piwigo/PiwigoDirectoryUploader/internal/pkg/localFileStructure"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"os" "os"
@ -48,18 +49,18 @@ func Run() {
logErrorAndExit(err, 5) logErrorAndExit(err, 5)
} }
err = synchronizeLocalImageMetadata(context.dataStore, filesystemNodes, categories, localFileStructure.CalculateFileCheckSums) err = images.SynchronizeLocalImageMetadata(context.dataStore, filesystemNodes, categories, localFileStructure.CalculateFileCheckSums)
if err != nil { if err != nil {
logErrorAndExit(err, 6) logErrorAndExit(err, 6)
} }
err = synchronizePiwigoMetadata(context.piwigo, context.dataStore) err = images.SynchronizePiwigoMetadata(context.piwigo, context.dataStore)
if err != nil { if err != nil {
logErrorAndExit(err, 7) logErrorAndExit(err, 7)
} }
if *removeImages { if *removeImages {
err = deleteImages(context.piwigo, context.dataStore) err = images.DeleteImages(context.piwigo, context.dataStore)
if err != nil { if err != nil {
logErrorAndExit(err, 8) logErrorAndExit(err, 8)
} }
@ -68,7 +69,7 @@ func Run() {
} }
if !(*noUpload) { if !(*noUpload) {
err = uploadImages(context.piwigo, context.dataStore) err = images.UploadImages(context.piwigo, context.dataStore)
if err != nil { if err != nil {
logErrorAndExit(err, 9) logErrorAndExit(err, 9)
} }

View File

@ -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. // Code generated by MockGen. DO NOT EDIT.
// Source: git.haefelfinger.net/piwigo/PiwigoDirectoryUploader/internal/pkg/datastore (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 images
import ( import (
datastore "git.haefelfinger.net/piwigo/PiwigoDirectoryUploader/internal/pkg/datastore" datastore "git.haefelfinger.net/piwigo/PiwigoDirectoryUploader/internal/pkg/datastore"

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 images
import ( import (
"git.haefelfinger.net/piwigo/PiwigoDirectoryUploader/internal/pkg/datastore" "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 // 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 datastore.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")
logrus.Info("Synchronizing local image metadata database with local available images") 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 { func synchronizeLocalImageMetadataFindFilesToDelete(provider datastore.ImageMetadataProvider) error {
logrus.Debug("Entering synchronizeLocalImageMetadataFindFilesToDelete") logrus.Debug("Entering SynchronizeLocalImageMetadataFindFilesToDelete")
defer logrus.Debug("Leaving synchronizeLocalImageMetadataFindFilesToDelete") defer logrus.Debug("Leaving SynchronizeLocalImageMetadataFindFilesToDelete")
images, err := provider.ImageMetadataAll() images, err := provider.ImageMetadataAll()
if err != nil { 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. // 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 datastore.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")
@ -153,7 +153,7 @@ func uploadImages(piwigoCtx piwigo.PiwigoImageApi, metadataProvider datastore.Im
return nil return nil
} }
func deleteImages(piwigoCtx piwigo.PiwigoImageApi, metadataProvider datastore.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")
@ -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. // 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 { 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")
// 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
err := updatePiwigoIdIfAlreadyUploaded(metadataProvider, piwigoCtx) err := updatePiwigoIdIfAlreadyUploaded(metadataProvider, piwigoCtx)

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 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=./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 //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() db := NewtestStore()
fileSystemNodes := map[string]*localFileStructure.FilesystemNode{} fileSystemNodes := map[string]*localFileStructure.FilesystemNode{}
err := synchronizeLocalImageMetadata(db, fileSystemNodes, categories, testChecksumCalculator) err := SynchronizeLocalImageMetadata(db, fileSystemNodes, categories, testChecksumCalculator)
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
@ -53,7 +53,7 @@ func Test_synchronize_local_image_metadata_should_add_new_metadata(t *testing.T)
fileSystemNodes[testFileSystemNode.Key] = testFileSystemNode fileSystemNodes[testFileSystemNode.Key] = testFileSystemNode
// execute the sync metadata based on the file system results // 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 { if err != nil {
t.Error(err) t.Error(err)
} }
@ -107,7 +107,7 @@ func Test_synchronize_local_image_metadata_should_mark_unchanged_entries_without
fileSystemNodes[testFileSystemNode.Key] = testFileSystemNode fileSystemNodes[testFileSystemNode.Key] = testFileSystemNode
// execute the sync metadata based on the file system results // 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 { if err != nil {
t.Error(err) t.Error(err)
} }
@ -154,7 +154,7 @@ func Test_synchronize_local_image_metadata_should_mark_changed_entries_as_upload
fileSystemNodes[testFileSystemNode.Key] = testFileSystemNode fileSystemNodes[testFileSystemNode.Key] = testFileSystemNode
// execute the sync metadata based on the file system results // 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 { if err != nil {
t.Error(err) t.Error(err)
} }
@ -202,7 +202,7 @@ func Test_synchronize_local_image_metadata_should_not_mark_unchanged_files_to_up
fileSystemNodes[testFileSystemNode.Key] = testFileSystemNode fileSystemNodes[testFileSystemNode.Key] = testFileSystemNode
// execute the sync metadata based on the file system results // 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 { if err != nil {
t.Error(err) t.Error(err)
} }
@ -237,7 +237,7 @@ func Test_synchronize_local_image_metadata_should_not_process_directories(t *tes
fileSystemNodes[testFileSystemNode.Key] = testFileSystemNode fileSystemNodes[testFileSystemNode.Key] = testFileSystemNode
// execute the sync metadata based on the file system results // 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 { if err != nil {
t.Error(err) t.Error(err)
} }
@ -454,7 +454,7 @@ func Test_uploadImages_saves_new_id_to_db(t *testing.T) {
piwigomock := NewMockPiwigoImageApi(mockCtrl) piwigomock := NewMockPiwigoImageApi(mockCtrl)
piwigomock.EXPECT().UploadImage(0, "/nonexisting/file.jpg", "1234", 2).Times(1).Return(5, nil) 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 { if err != nil {
t.Error(err) t.Error(err)
} }
@ -477,7 +477,7 @@ func Test_uploadImages_saves_same_id_to_db(t *testing.T) {
piwigomock := NewMockPiwigoImageApi(mockCtrl) piwigomock := NewMockPiwigoImageApi(mockCtrl)
piwigomock.EXPECT().UploadImage(5, "/nonexisting/file.jpg", "1234", 2).Times(1).Return(5, nil) 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 { if err != nil {
t.Error(err) t.Error(err)
} }
@ -520,7 +520,7 @@ func Test_deleteImages_should_call_piwigo_and_remove_metadata(t *testing.T) {
piwigomock := NewMockPiwigoImageApi(mockCtrl) piwigomock := NewMockPiwigoImageApi(mockCtrl)
piwigomock.EXPECT().DeleteImages([]int{5}).Times(1).Return(nil) piwigomock.EXPECT().DeleteImages([]int{5}).Times(1).Return(nil)
err := deleteImages(piwigomock, dbmock) err := DeleteImages(piwigomock, dbmock)
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
@ -542,7 +542,7 @@ func Test_deleteImages_should_not_call_piwigo_for_not_uploaded_images_and_remove
piwigomock := NewMockPiwigoImageApi(mockCtrl) piwigomock := NewMockPiwigoImageApi(mockCtrl)
piwigomock.EXPECT().DeleteImages(gomock.Any()).Times(0) piwigomock.EXPECT().DeleteImages(gomock.Any()).Times(0)
err := deleteImages(piwigomock, dbmock) err := DeleteImages(piwigomock, dbmock)
if err != nil { if err != nil {
t.Error(err) 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 := NewMockPiwigoImageApi(mockCtrl)
piwigomock.EXPECT().DeleteImages(gomock.Any()).Times(0) piwigomock.EXPECT().DeleteImages(gomock.Any()).Times(0)
err := deleteImages(piwigomock, dbmock) err := DeleteImages(piwigomock, dbmock)
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }

View File

@ -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. // Code generated by MockGen. DO NOT EDIT.
// Source: git.haefelfinger.net/piwigo/PiwigoDirectoryUploader/internal/pkg/piwigo (interfaces: PiwigoApi,PiwigoCategoryApi,PiwigoImageApi) // Source: git.haefelfinger.net/piwigo/PiwigoDirectoryUploader/internal/pkg/piwigo (interfaces: PiwigoApi,PiwigoCategoryApi,PiwigoImageApi)
// Package app is a generated GoMock package. // Package app is a generated GoMock package.
package app package images
import ( import (
piwigo "git.haefelfinger.net/piwigo/PiwigoDirectoryUploader/internal/pkg/piwigo" piwigo "git.haefelfinger.net/piwigo/PiwigoDirectoryUploader/internal/pkg/piwigo"