PiwigoDirectoryUploader/internal/app/images.go

81 lines
2.3 KiB
Go
Raw Normal View History

package app
2019-02-25 23:51:18 +01:00
import (
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-03-02 00:21:01 +01:00
"github.com/sirupsen/logrus"
"sort"
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-03-02 00:21:01 +01:00
logrus.Infof("Synchronized %d files.", len(missingFiles))
return nil
}
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 {
// We sort the files by path to populate per category and not random by file
sort.Slice(missingFiles, func(i, j int) bool {
return missingFiles[i].Path < missingFiles[j].Path
})
2019-03-01 00:14:52 +01:00
for _, file := range missingFiles {
2019-03-01 21:51:37 +01:00
logrus.Infof("Uploading %s %s", file.CategoryName, file.Path)
2019-03-01 00:14:52 +01:00
categoryId := existingCategories[file.CategoryName].Id
2019-03-02 00:21:01 +01:00
imageId, err := picture.UploadImage(context.Piwigo, file.Path, file.Md5Sum, categoryId)
2019-03-01 00:14:52 +01:00
if err != nil {
return err
}
2019-03-02 00:21:01 +01:00
file.ImageId = imageId
2019-03-01 00:14:52 +01:00
}
return nil
}