From 066f8f5f95dacb75b5cd94c1ed440488e4f9cf95 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20H=C3=A4felfinger?= Date: Sun, 17 Mar 2019 23:36:40 +0100 Subject: [PATCH] extended datastore to get the images that should be uploaded --- internal/app/datastore.go | 39 ++++++++++++++++++++++++++++++++-- internal/app/datastore_test.go | 28 ++++++++++++++++++++++++ 2 files changed, 65 insertions(+), 2 deletions(-) diff --git a/internal/app/datastore.go b/internal/app/datastore.go index 77ece7d..a98e0c2 100644 --- a/internal/app/datastore.go +++ b/internal/app/datastore.go @@ -24,11 +24,12 @@ type ImageMetaData struct { } func (img *ImageMetaData) String() string { - return fmt.Sprintf("ImageMetaData{ImageId:%d, PiwigoId:%d, CategoryId:%d, RelPath:%s, File:%s, Md5:%s, Change:%sS, catpath:%s}", img.ImageId, img.PiwigoId, img.CategoryId, img.RelativeImagePath, img.Filename, img.Md5Sum, img.LastChange.String(), img.CategoryPath) + return fmt.Sprintf("ImageMetaData{ImageId:%d, PiwigoId:%d, CategoryId:%d, RelPath:%s, File:%s, Md5:%s, Change:%sS, catpath:%s, UploadRequired: %t}", img.ImageId, img.PiwigoId, img.CategoryId, img.RelativeImagePath, img.Filename, img.Md5Sum, img.LastChange.String(), img.CategoryPath, img.UploadRequired) } type ImageMetadataProvider interface { GetImageMetadata(relativePath string) (ImageMetaData, error) + GetImageMetadataToUpload() ([]*ImageMetaData, error) SaveImageMetadata(m ImageMetaData) error } @@ -76,7 +77,7 @@ func (d *localDataStore) GetImageMetadata(relativePath string) (ImageMetaData, e defer rows.Close() if rows.Next() { - err = rows.Scan(&img.ImageId, &img.PiwigoId, &img.RelativeImagePath, &img.Filename, &img.Md5Sum, &img.LastChange, &img.CategoryPath, &img.CategoryId, &img.UploadRequired) + err = ReadImageMetadataFromRow(rows, &img) if err != nil { return img, err } @@ -88,6 +89,40 @@ func (d *localDataStore) GetImageMetadata(relativePath string) (ImageMetaData, e return img, err } +func (d *localDataStore) GetImageMetadataToUpload() ([]*ImageMetaData, error) { + logrus.Tracef("Query all image metadata that represent files queued to upload") + + db, err := d.openDatabase() + if err != nil { + return nil, err + } + defer db.Close() + + rows, err := db.Query("SELECT imageId, piwigoId, relativePath, fileName, md5sum, lastChanged, categoryPath, categoryId, uploadRequired FROM image WHERE uploadRequired = 1") + if err != nil { + return nil, err + } + defer rows.Close() + + images := []*ImageMetaData{} + for rows.Next() { + img := &ImageMetaData{} + err = ReadImageMetadataFromRow(rows, img) + if err != nil { + return nil, err + } + images = append(images, img) + } + err = rows.Err() + + return images, err +} + +func ReadImageMetadataFromRow(rows *sql.Rows, img *ImageMetaData) error { + err := rows.Scan(&img.ImageId, &img.PiwigoId, &img.RelativeImagePath, &img.Filename, &img.Md5Sum, &img.LastChange, &img.CategoryPath, &img.CategoryId, &img.UploadRequired) + return err +} + func (d *localDataStore) SaveImageMetadata(img ImageMetaData) error { logrus.Tracef("Saving imagemetadata: %s", img.String()) db, err := d.openDatabase() diff --git a/internal/app/datastore_test.go b/internal/app/datastore_test.go index 74785bb..c668ccc 100644 --- a/internal/app/datastore_test.go +++ b/internal/app/datastore_test.go @@ -40,6 +40,33 @@ func TestSaveAndLoadMetadata(t *testing.T) { cleanupDatabase(t) } +func TestSaveAndQueryForUploadRecords(t *testing.T) { + if !dbinitOk { + t.Skip("Skipping test as TestDataStoreInitialize failed!") + } + dataStore := setupDatabase(t) + + filePath := "blah/foo/bar.jpg" + img := getExampleImageMetadata(filePath) + + saveImageShouldNotFail("toupload", dataStore, img, t) + img.ImageId = 1 + + images, err := dataStore.GetImageMetadataToUpload() + if err != nil { + t.Fatalf("Could not query images to upload! %s", err) + } + + if len(images)<1 { + t.Fatal("Did not get any saved images to upload!") + } + + imgLoad := images[0] + EnsureMetadataAreEqual("toupload", img, *imgLoad, t) + + cleanupDatabase(t) +} + func TestLoadMetadataNotFound(t *testing.T) { if !dbinitOk { t.Skip("Skipping test as TestDataStoreInitialize failed!") @@ -114,6 +141,7 @@ func getExampleImageMetadata(filePath string) ImageMetaData { Filename: "bar.jpg", CategoryPath: "blah/foo", CategoryId: 100, + UploadRequired: true, } }