2019-03-12 23:44:05 +01:00
|
|
|
package app
|
|
|
|
|
2019-03-21 22:37:16 +01:00
|
|
|
//go:generate mockgen -destination=../pkg/mocks/mock_app_datastore.go -package=mocks git.haefelfinger.net/piwigo/PiwigoDirectoryUploader/internal/app ImageMetadataProvider
|
|
|
|
|
2019-03-12 23:44:05 +01:00
|
|
|
import (
|
2019-03-15 00:35:49 +01:00
|
|
|
"database/sql"
|
2019-03-12 23:44:05 +01:00
|
|
|
"errors"
|
2019-03-15 22:50:48 +01:00
|
|
|
"fmt"
|
2019-03-15 00:35:49 +01:00
|
|
|
_ "github.com/mattn/go-sqlite3"
|
2019-03-15 22:50:48 +01:00
|
|
|
"github.com/sirupsen/logrus"
|
2019-03-12 23:44:05 +01:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2019-03-15 22:50:48 +01:00
|
|
|
var ErrorRecordNotFound = errors.New("Record not found")
|
|
|
|
|
2019-03-12 23:44:05 +01:00
|
|
|
type ImageMetaData struct {
|
2019-03-20 23:15:41 +01:00
|
|
|
ImageId int
|
|
|
|
PiwigoId int
|
|
|
|
FullImagePath string
|
|
|
|
Filename string
|
|
|
|
Md5Sum string
|
|
|
|
LastChange time.Time
|
|
|
|
CategoryPath string
|
|
|
|
CategoryId int
|
|
|
|
UploadRequired bool
|
2019-03-12 23:44:05 +01:00
|
|
|
}
|
|
|
|
|
2019-03-15 22:50:48 +01:00
|
|
|
func (img *ImageMetaData) String() string {
|
2019-03-20 23:15:41 +01:00
|
|
|
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.FullImagePath, img.Filename, img.Md5Sum, img.LastChange.String(), img.CategoryPath, img.UploadRequired)
|
2019-03-12 23:44:05 +01:00
|
|
|
}
|
|
|
|
|
2019-03-15 22:50:48 +01:00
|
|
|
type ImageMetadataProvider interface {
|
2019-03-20 23:15:41 +01:00
|
|
|
ImageMetadata(fullImagePath string) (ImageMetaData, error)
|
2019-03-20 00:14:10 +01:00
|
|
|
ImageMetadataToUpload() ([]ImageMetaData, error)
|
2019-03-12 23:44:05 +01:00
|
|
|
SaveImageMetadata(m ImageMetaData) error
|
2019-03-19 23:07:55 +01:00
|
|
|
SavePiwigoIdAndUpdateUploadFlag(md5Sum string, piwigoId int) error
|
2019-03-12 23:44:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type localDataStore struct {
|
|
|
|
connectionString string
|
|
|
|
}
|
|
|
|
|
2019-03-15 00:35:49 +01:00
|
|
|
func (d *localDataStore) Initialize(connectionString string) error {
|
2019-03-12 23:44:05 +01:00
|
|
|
if connectionString == "" {
|
|
|
|
return errors.New("connection string could not be empty.")
|
|
|
|
}
|
|
|
|
|
|
|
|
d.connectionString = connectionString
|
|
|
|
|
2019-03-15 00:35:49 +01:00
|
|
|
db, err := d.openDatabase()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer db.Close()
|
|
|
|
|
|
|
|
err = d.createTablesIfNeeded(db)
|
|
|
|
|
|
|
|
return err
|
2019-03-12 23:44:05 +01:00
|
|
|
}
|
|
|
|
|
2019-03-20 23:15:41 +01:00
|
|
|
func (d *localDataStore) ImageMetadata(fullImagePath string) (ImageMetaData, error) {
|
|
|
|
logrus.Tracef("Query image metadata for file %s", fullImagePath)
|
2019-03-15 22:50:48 +01:00
|
|
|
img := ImageMetaData{}
|
|
|
|
|
2019-03-15 00:35:49 +01:00
|
|
|
db, err := d.openDatabase()
|
|
|
|
if err != nil {
|
2019-03-15 22:50:48 +01:00
|
|
|
return img, err
|
2019-03-15 00:35:49 +01:00
|
|
|
}
|
|
|
|
defer db.Close()
|
|
|
|
|
2019-03-20 23:15:41 +01:00
|
|
|
stmt, err := db.Prepare("SELECT imageId, piwigoId, fullImagePath, fileName, md5sum, lastChanged, categoryPath, categoryId, uploadRequired FROM image WHERE fullImagePath = ?")
|
2019-03-15 00:35:49 +01:00
|
|
|
if err != nil {
|
2019-03-15 22:50:48 +01:00
|
|
|
return img, err
|
2019-03-15 00:35:49 +01:00
|
|
|
}
|
|
|
|
|
2019-03-20 23:15:41 +01:00
|
|
|
rows, err := stmt.Query(fullImagePath)
|
2019-03-15 00:35:49 +01:00
|
|
|
if err != nil {
|
2019-03-15 22:50:48 +01:00
|
|
|
return img, err
|
2019-03-15 00:35:49 +01:00
|
|
|
}
|
2019-03-15 22:50:48 +01:00
|
|
|
defer rows.Close()
|
2019-03-15 00:35:49 +01:00
|
|
|
|
2019-03-15 22:50:48 +01:00
|
|
|
if rows.Next() {
|
2019-03-17 23:36:40 +01:00
|
|
|
err = ReadImageMetadataFromRow(rows, &img)
|
2019-03-15 22:50:48 +01:00
|
|
|
if err != nil {
|
|
|
|
return img, err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return img, ErrorRecordNotFound
|
|
|
|
}
|
|
|
|
err = rows.Err()
|
|
|
|
|
|
|
|
return img, err
|
2019-03-12 23:44:05 +01:00
|
|
|
}
|
|
|
|
|
2019-03-20 00:14:10 +01:00
|
|
|
func (d *localDataStore) ImageMetadataToUpload() ([]ImageMetaData, error) {
|
2019-03-17 23:36:40 +01:00
|
|
|
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()
|
|
|
|
|
2019-03-20 23:15:41 +01:00
|
|
|
rows, err := db.Query("SELECT imageId, piwigoId, fullImagePath, fileName, md5sum, lastChanged, categoryPath, categoryId, uploadRequired FROM image WHERE uploadRequired = 1")
|
2019-03-17 23:36:40 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
2019-03-20 00:14:10 +01:00
|
|
|
images := []ImageMetaData{}
|
2019-03-17 23:36:40 +01:00
|
|
|
for rows.Next() {
|
|
|
|
img := &ImageMetaData{}
|
|
|
|
err = ReadImageMetadataFromRow(rows, img)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-03-20 00:14:10 +01:00
|
|
|
images = append(images, *img)
|
2019-03-17 23:36:40 +01:00
|
|
|
}
|
|
|
|
err = rows.Err()
|
|
|
|
|
|
|
|
return images, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func ReadImageMetadataFromRow(rows *sql.Rows, img *ImageMetaData) error {
|
2019-03-20 23:15:41 +01:00
|
|
|
err := rows.Scan(&img.ImageId, &img.PiwigoId, &img.FullImagePath, &img.Filename, &img.Md5Sum, &img.LastChange, &img.CategoryPath, &img.CategoryId, &img.UploadRequired)
|
2019-03-17 23:36:40 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-03-15 22:50:48 +01:00
|
|
|
func (d *localDataStore) SaveImageMetadata(img ImageMetaData) error {
|
2019-03-17 22:59:18 +01:00
|
|
|
logrus.Tracef("Saving imagemetadata: %s", img.String())
|
2019-03-15 00:35:49 +01:00
|
|
|
db, err := d.openDatabase()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer db.Close()
|
|
|
|
|
|
|
|
tx, err := db.Begin()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-03-15 22:50:48 +01:00
|
|
|
if img.ImageId <= 0 {
|
|
|
|
err = d.insertImageMetaData(tx, img)
|
2019-03-15 00:35:49 +01:00
|
|
|
} else {
|
2019-03-15 22:50:48 +01:00
|
|
|
err = d.updateImageMetaData(tx, img)
|
2019-03-15 00:35:49 +01:00
|
|
|
}
|
|
|
|
|
2019-03-15 22:50:48 +01:00
|
|
|
if err != nil {
|
2019-03-20 23:15:41 +01:00
|
|
|
logrus.Errorf("Rolling back transaction for metadata of %s", img.FullImagePath)
|
2019-03-15 22:50:48 +01:00
|
|
|
errTx := tx.Rollback()
|
|
|
|
if errTx != nil {
|
2019-03-20 23:15:41 +01:00
|
|
|
logrus.Errorf("Rollback of transaction for metadata of %s failed!", img.FullImagePath)
|
2019-03-15 22:50:48 +01:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-03-17 22:59:18 +01:00
|
|
|
logrus.Tracef("Commiting metadata for image %s", img.String())
|
2019-03-15 22:50:48 +01:00
|
|
|
return tx.Commit()
|
2019-03-15 00:35:49 +01:00
|
|
|
}
|
|
|
|
|
2019-03-19 23:07:55 +01:00
|
|
|
func (d *localDataStore) SavePiwigoIdAndUpdateUploadFlag(md5Sum string, piwigoId int) error {
|
|
|
|
logrus.Tracef("Saving piwigo id %d for file with md5sum %s", piwigoId, md5Sum)
|
|
|
|
db, err := d.openDatabase()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer db.Close()
|
|
|
|
|
|
|
|
tx, err := db.Begin()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-03-20 00:31:09 +01:00
|
|
|
uploadRequired := 1
|
|
|
|
if piwigoId > 0 {
|
|
|
|
uploadRequired = 0
|
|
|
|
}
|
|
|
|
|
|
|
|
stmt, err := tx.Prepare("UPDATE image SET piwigoId = ?, uploadRequired = ? WHERE md5sum = ?")
|
2019-03-19 23:07:55 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-03-20 00:31:09 +01:00
|
|
|
_, err = stmt.Exec(piwigoId, uploadRequired, md5Sum)
|
2019-03-19 23:07:55 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("Rolling back transaction for piwigo id update of file %s", md5Sum)
|
|
|
|
errTx := tx.Rollback()
|
|
|
|
if errTx != nil {
|
|
|
|
logrus.Errorf("Rollback of transaction for piwigo id update of file %s failed!", md5Sum)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
logrus.Tracef("Commiting piwigo id %d for file with md5sum %s", piwigoId, md5Sum)
|
|
|
|
return tx.Commit()
|
|
|
|
}
|
|
|
|
|
2019-03-15 22:50:48 +01:00
|
|
|
func (d *localDataStore) insertImageMetaData(tx *sql.Tx, data ImageMetaData) error {
|
2019-03-20 23:15:41 +01:00
|
|
|
stmt, err := tx.Prepare("INSERT INTO image (piwigoId, fullImagePath, fileName, md5sum, lastChanged, categoryPath, categoryId, uploadRequired) VALUES (?,?,?,?,?,?,?,?)")
|
2019-03-15 00:35:49 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-03-20 23:15:41 +01:00
|
|
|
_, err = stmt.Exec(data.PiwigoId, data.FullImagePath, data.Filename, data.Md5Sum, data.LastChange, data.CategoryPath, data.CategoryId, data.UploadRequired)
|
2019-03-15 00:35:49 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *localDataStore) openDatabase() (*sql.DB, error) {
|
|
|
|
db, err := sql.Open("sqlite3", d.connectionString)
|
|
|
|
if err != nil {
|
2019-03-15 22:50:48 +01:00
|
|
|
logrus.Warnf("Could not open database %s", d.connectionString)
|
2019-03-15 00:35:49 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
db.SetMaxOpenConns(1)
|
|
|
|
|
|
|
|
return db, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *localDataStore) createTablesIfNeeded(db *sql.DB) error {
|
|
|
|
_, err := db.Exec("CREATE TABLE IF NOT EXISTS image (" +
|
2019-03-15 22:50:48 +01:00
|
|
|
"imageId INTEGER PRIMARY KEY," +
|
2019-03-15 00:35:49 +01:00
|
|
|
"piwigoId INTEGER NULL," +
|
2019-03-20 23:15:41 +01:00
|
|
|
"fullImagePath NVARCHAR(1000) NOT NULL," +
|
2019-03-15 00:35:49 +01:00
|
|
|
"fileName NVARCHAR(255) NOT NULL," +
|
|
|
|
"md5sum NVARCHAR(50) NOT NULL," +
|
|
|
|
"lastChanged DATETIME NOT NULL," +
|
|
|
|
"categoryPath NVARCHAR(1000) NOT NULL," +
|
2019-03-17 22:59:18 +01:00
|
|
|
"categoryId INTEGER NULL," +
|
|
|
|
"uploadRequired BIT NOT NULL" +
|
2019-03-15 00:35:49 +01:00
|
|
|
");")
|
2019-03-15 22:50:48 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-03-20 23:15:41 +01:00
|
|
|
_, err = db.Exec("CREATE UNIQUE INDEX IF NOT EXISTS UX_ImageFullImagePath ON image (fullImagePath);")
|
2019-03-15 22:50:48 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *localDataStore) updateImageMetaData(tx *sql.Tx, data ImageMetaData) error {
|
2019-03-20 23:15:41 +01:00
|
|
|
stmt, err := tx.Prepare("UPDATE image SET piwigoId = ?, fullImagePath = ?, fileName = ?, md5sum = ?, lastChanged = ?, categoryPath = ?, categoryId = ?, uploadRequired = ? WHERE imageId = ?")
|
2019-03-15 22:50:48 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-03-20 23:15:41 +01:00
|
|
|
_, err = stmt.Exec(data.PiwigoId, data.FullImagePath, data.Filename, data.Md5Sum, data.LastChange, data.CategoryPath, data.CategoryId, data.UploadRequired, data.ImageId)
|
2019-03-15 00:35:49 +01:00
|
|
|
return err
|
2019-03-12 23:44:05 +01:00
|
|
|
}
|