extended datastore to get the images that should be uploaded

This commit is contained in:
Philipp Häfelfinger 2019-03-17 23:36:40 +01:00
parent 798b74d071
commit 066f8f5f95
2 changed files with 65 additions and 2 deletions

View File

@ -24,11 +24,12 @@ type ImageMetaData struct {
} }
func (img *ImageMetaData) String() string { 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 { type ImageMetadataProvider interface {
GetImageMetadata(relativePath string) (ImageMetaData, error) GetImageMetadata(relativePath string) (ImageMetaData, error)
GetImageMetadataToUpload() ([]*ImageMetaData, error)
SaveImageMetadata(m ImageMetaData) error SaveImageMetadata(m ImageMetaData) error
} }
@ -76,7 +77,7 @@ func (d *localDataStore) GetImageMetadata(relativePath string) (ImageMetaData, e
defer rows.Close() defer rows.Close()
if rows.Next() { 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 { if err != nil {
return img, err return img, err
} }
@ -88,6 +89,40 @@ func (d *localDataStore) GetImageMetadata(relativePath string) (ImageMetaData, e
return img, err 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 { func (d *localDataStore) SaveImageMetadata(img ImageMetaData) error {
logrus.Tracef("Saving imagemetadata: %s", img.String()) logrus.Tracef("Saving imagemetadata: %s", img.String())
db, err := d.openDatabase() db, err := d.openDatabase()

View File

@ -40,6 +40,33 @@ func TestSaveAndLoadMetadata(t *testing.T) {
cleanupDatabase(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) { func TestLoadMetadataNotFound(t *testing.T) {
if !dbinitOk { if !dbinitOk {
t.Skip("Skipping test as TestDataStoreInitialize failed!") t.Skip("Skipping test as TestDataStoreInitialize failed!")
@ -114,6 +141,7 @@ func getExampleImageMetadata(filePath string) ImageMetaData {
Filename: "bar.jpg", Filename: "bar.jpg",
CategoryPath: "blah/foo", CategoryPath: "blah/foo",
CategoryId: 100, CategoryId: 100,
UploadRequired: true,
} }
} }