From 5166b0c6b88995b10cc31593837b5f952fa04c51 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20H=C3=A4felfinger?= Date: Sun, 17 Mar 2019 23:57:28 +0100 Subject: [PATCH] started synchronization of piwigo ids and upload required --- internal/app/app.go | 7 ++++++- internal/app/images.go | 28 +++++++++++++++++++++++++--- internal/app/images_test.go | 24 ++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 4 deletions(-) diff --git a/internal/app/app.go b/internal/app/app.go index cd7d373..8c1ad83 100644 --- a/internal/app/app.go +++ b/internal/app/app.go @@ -48,11 +48,16 @@ func Run() { logErrorAndExit(err, 6) } - err = synchronizeImages(context.piwigo, context.dataStore, categories) + err = synchronizePiwigoMetadata(context.piwigo, context.dataStore) if err != nil { logErrorAndExit(err, 7) } + err = synchronizeImages(context.piwigo, context.dataStore, categories) + if err != nil { + logErrorAndExit(err, 8) + } + _ = piwigo.Logout(context.piwigo) } diff --git a/internal/app/images.go b/internal/app/images.go index 8de9041..f91a6a6 100644 --- a/internal/app/images.go +++ b/internal/app/images.go @@ -1,6 +1,7 @@ package app import ( + "errors" "git.haefelfinger.net/piwigo/PiwigoDirectoryUploader/internal/pkg/localFileStructure" "git.haefelfinger.net/piwigo/PiwigoDirectoryUploader/internal/pkg/piwigo" "github.com/sirupsen/logrus" @@ -18,6 +19,7 @@ func synchronizeLocalImageMetadata(metadataStorage ImageMetadataProvider, fileSy // - get file metadata from filesystem (date, filename, dir, modtime etc.) // - recalculate md5 sum if file changed referring to the stored record (reduces load after first calculation a lot) // - mark metadata as upload required if changed or new + logrus.Debugf("Starting synchronizeLocalImageMetadata") logrus.Info("Synchronizing local image metadata database with local available images") @@ -58,12 +60,32 @@ func synchronizeLocalImageMetadata(metadataStorage ImageMetadataProvider, fileSy } } + logrus.Debugf("Finished synchronizeLocalImageMetadata") return nil } -// STEP 2 - get file states from piwigo (pwg.images.checkFiles) -// - get upload status of md5 sum from piwigo for all marked to upload -// - check if category has to be assigned (image possibly added to two albums -> only uploaded once but assigned multiple times) +func synchronizePiwigoMetadata(piwigo *piwigo.PiwigoContext, metadataStorage ImageMetadataProvider) error { + // STEP 2 - get file states from piwigo (pwg.images.checkFiles) + // - make bulk query possible + // - get upload status of md5 sum from piwigo for all marked to upload + // - build method to update database with new piwigoId by md5 sum + // - set UploadRequired to false and the returned piwigoid + // - 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") + images, err := metadataStorage.GetImageMetadataToUpload() + if err != nil { + return err + } + + logrus.Infof("Synchronizing metadata with existing Piwigo data...") + for img := range images { + logrus.Debug(img) + } + + logrus.Debugf("Finished synchronizePiwigoMetadata") + return errors.New("N/A") +} // STEP 3: Upload missing images // - upload file in chunks diff --git a/internal/app/images_test.go b/internal/app/images_test.go index d76e263..2323280 100644 --- a/internal/app/images_test.go +++ b/internal/app/images_test.go @@ -1,6 +1,7 @@ package app import ( + "errors" "git.haefelfinger.net/piwigo/PiwigoDirectoryUploader/internal/pkg/localFileStructure" "testing" "time" @@ -160,6 +161,25 @@ func TestSynchronizeLocalImageMetadataShouldNotProcessDirectories(t *testing.T) } } +func TestSynchronizePiwigoMetadata(t *testing.T) { + db := NewtestStore() + db.savedMetadata["2019/shooting1/abc.jpg"] = ImageMetaData{ + Md5Sum: "2019/shooting1/abc.jpg", + RelativeImagePath: "2019/shooting1/abc.jpg", + UploadRequired: false, + LastChange: time.Date(2019, 01, 01, 00, 0, 0, 0, time.UTC), + Filename: "abc.jpg", + } + + + // execute the sync metadata based on the file system results + //err := synchronizeLocalImageMetadata( db) + //if err != nil { + // t.Error(err) + //} + t.FailNow() +} + // test metadata store to store save the metadat and simulate the database type testStore struct { savedMetadata map[string]ImageMetaData @@ -182,6 +202,10 @@ func (s *testStore) SaveImageMetadata(m ImageMetaData) error { return nil } +func (d *testStore) GetImageMetadataToUpload() ([]*ImageMetaData, error) { + return nil, errors.New("N/A") +} + // to make the sync testable, we pass in a simple mock that returns the filepath as checksum func testChecksumCalculator(file string) (string, error) { return file, nil