PiwigoDirectoryUploader/internal/app/images.go

75 lines
2.1 KiB
Go
Raw Normal View History

package app
2019-02-25 23:51:18 +01:00
import (
"errors"
"github.com/sirupsen/logrus"
2019-03-01 21:36:16 +01:00
"git.haefelfinger.net/piwigo/DirectoriesToAlbums/internal/pkg/localFileStructure"
"git.haefelfinger.net/piwigo/DirectoriesToAlbums/internal/pkg/piwigo/category"
"git.haefelfinger.net/piwigo/DirectoriesToAlbums/internal/pkg/piwigo/picture"
2019-02-25 23:51:18 +01:00
)
func synchronizeImages(context *appContext, fileSystem map[string]*localFileStructure.FilesystemNode, existingCategories map[string]*category.PiwigoCategory) error {
2019-02-27 00:09:41 +01:00
imageFiles, err := localFileStructure.GetImageList(fileSystem)
if err != nil {
return err
}
2019-02-27 00:09:41 +01:00
2019-02-27 23:26:18 +01:00
missingFiles, err := findMissingImages(context, imageFiles)
if err != nil {
return err
}
2019-03-01 00:14:52 +01:00
err = uploadImages(context, missingFiles, existingCategories)
if err != nil {
return err
}
2019-02-27 00:09:41 +01:00
2019-02-26 00:02:22 +01:00
return errors.New("synchronizeImages: NOT IMPLEMENTED")
}
2019-02-27 23:26:18 +01:00
func findMissingImages(context *appContext, imageFiles []*localFileStructure.ImageNode) ([]*localFileStructure.ImageNode, error) {
logrus.Debugln("Preparing lookuplist for missing files...")
files := make([]string, 0, len(imageFiles))
md5map := make(map[string]*localFileStructure.ImageNode, len(imageFiles))
2019-03-01 00:26:15 +01:00
for _, file := range imageFiles {
2019-02-27 23:26:18 +01:00
md5map[file.Md5Sum] = file
files = append(files, file.Md5Sum)
}
misingSums, err := picture.ImageUploadRequired(context.Piwigo, files)
if err != nil {
return nil, err
}
missingFiles := make([]*localFileStructure.ImageNode, 0, len(misingSums))
2019-03-01 00:26:15 +01:00
for _, sum := range misingSums {
2019-02-27 23:26:18 +01:00
file := md5map[sum]
logrus.Infof("Found missing file %s", file.Path)
missingFiles = append(missingFiles, file)
}
2019-02-27 00:09:41 +01:00
2019-02-27 23:26:18 +01:00
logrus.Infof("Found %d missing files", len(missingFiles))
2019-02-27 00:09:41 +01:00
2019-02-27 23:26:18 +01:00
return missingFiles, nil
}
2019-03-01 00:14:52 +01:00
func uploadImages(context *appContext, missingFiles []*localFileStructure.ImageNode, existingCategories map[string]*category.PiwigoCategory) error {
logrus.Warnln("Uploading missing images (NotImplemented)")
2019-03-01 00:14:52 +01:00
for _, file := range missingFiles {
logrus.Infof("Uploading %s", file.Path)
categoryId := existingCategories[file.CategoryName].Id
//TODO handle added id
_, err := picture.UploadImage(context.Piwigo, file.Path, file.Md5Sum, categoryId)
if err != nil {
return err
}
}
return nil
}