2019-04-07 22:50:26 +02:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package images
|
|
|
|
|
|
|
|
import (
|
|
|
|
"git.haefelfinger.net/piwigo/PiwigoDirectoryUploader/internal/pkg/datastore"
|
|
|
|
"git.haefelfinger.net/piwigo/PiwigoDirectoryUploader/internal/pkg/piwigo"
|
|
|
|
"github.com/sirupsen/logrus"
|
2019-04-08 23:08:27 +02:00
|
|
|
"sync"
|
2019-04-07 22:50:26 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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.
|
2019-04-09 00:05:02 +02:00
|
|
|
func UploadImages(piwigoCtx piwigo.ImageApi, metadataProvider datastore.ImageMetadataProvider, numberOfWorkers int) error {
|
2019-04-07 22:50:26 +02:00
|
|
|
logrus.Debug("Starting uploadImages")
|
|
|
|
defer logrus.Debug("Finished uploadImages successfully")
|
|
|
|
|
|
|
|
images, err := metadataProvider.ImageMetadataToUpload()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-04-08 23:08:27 +02:00
|
|
|
if len(images) == 0 {
|
|
|
|
logrus.Info("No images to upload.")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if numberOfWorkers <= 0 {
|
|
|
|
logrus.Warnf("Invalid numbers of worker set: %d falling back to default of 4", numberOfWorkers)
|
|
|
|
numberOfWorkers = 4
|
|
|
|
}
|
|
|
|
|
|
|
|
logrus.Infof("Uploading %d images to piwigo using %d workers", len(images), numberOfWorkers)
|
|
|
|
workQueue := make(chan datastore.ImageMetaData, numberOfWorkers)
|
2019-04-07 22:50:26 +02:00
|
|
|
|
2019-04-08 23:08:27 +02:00
|
|
|
wg := sync.WaitGroup{}
|
|
|
|
|
|
|
|
wg.Add(1)
|
|
|
|
go uploadQueueProducer(images, workQueue, &wg)
|
|
|
|
|
|
|
|
for i := 0; i < numberOfWorkers; i++ {
|
|
|
|
logrus.Debugf("Starting image upload worker %d", i)
|
|
|
|
wg.Add(1)
|
|
|
|
go uploadQueueWorker(workQueue, piwigoCtx, metadataProvider, &wg)
|
|
|
|
}
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-04-09 00:05:02 +02:00
|
|
|
func uploadQueueWorker(workQueue <-chan datastore.ImageMetaData, piwigoCtx piwigo.ImageApi, metadataProvider datastore.ImageMetadataProvider, waitGroup *sync.WaitGroup) {
|
2019-04-08 23:08:27 +02:00
|
|
|
for img := range workQueue {
|
|
|
|
logrus.Debugf("%s: uploading image to piwigo", img.FullImagePath)
|
2019-04-07 22:50:26 +02:00
|
|
|
|
|
|
|
imgId, err := piwigoCtx.UploadImage(img.PiwigoId, img.FullImagePath, img.Md5Sum, img.CategoryPiwigoId)
|
|
|
|
if err != nil {
|
2019-04-08 23:08:27 +02:00
|
|
|
logrus.Warnf("%s: could not upload image. Continuing with the next image.", img.FullImagePath)
|
2019-04-07 22:50:26 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if imgId > 0 && imgId != img.PiwigoId {
|
|
|
|
img.PiwigoId = imgId
|
2019-04-08 23:08:27 +02:00
|
|
|
logrus.Debugf("%s: Updating image %d with piwigo id %d", img.FullImagePath, img.ImageId, img.PiwigoId)
|
2019-04-07 22:50:26 +02:00
|
|
|
}
|
2019-04-08 23:08:27 +02:00
|
|
|
logrus.Infof("%s: Successfully uploaded", img.FullImagePath)
|
2019-04-07 22:50:26 +02:00
|
|
|
|
|
|
|
img.UploadRequired = false
|
|
|
|
err = metadataProvider.SaveImageMetadata(img)
|
|
|
|
if err != nil {
|
2019-04-08 23:08:27 +02:00
|
|
|
logrus.Warnf("%s: could not save uploaded image. Continuing with the next image.", img.FullImagePath)
|
2019-04-07 22:50:26 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
2019-04-08 23:08:27 +02:00
|
|
|
waitGroup.Done()
|
|
|
|
}
|
2019-04-07 22:50:26 +02:00
|
|
|
|
2019-04-08 23:08:27 +02:00
|
|
|
func uploadQueueProducer(imagesToUpload []datastore.ImageMetaData, workQueue chan<- datastore.ImageMetaData, waitGroup *sync.WaitGroup) {
|
|
|
|
for _, img := range imagesToUpload {
|
|
|
|
logrus.Debugf("%s: Adding image to queue", img.FullImagePath)
|
|
|
|
workQueue <- img
|
|
|
|
}
|
|
|
|
waitGroup.Done()
|
|
|
|
close(workQueue)
|
2019-04-07 22:50:26 +02:00
|
|
|
}
|