made piwigo exists call batched

This commit is contained in:
Philipp Häfelfinger 2019-04-08 22:15:42 +02:00
parent 3c50f36a77
commit 3785de4e56

View File

@ -186,7 +186,24 @@ func (context *PiwigoContext) ImageCheckFile(piwigoId int, md5sum string) (int,
} }
func (context *PiwigoContext) ImagesExistOnPiwigo(md5sums []string) (map[string]int, error) { func (context *PiwigoContext) ImagesExistOnPiwigo(md5sums []string) (map[string]int, error) {
//TODO: make sure to split to multiple queries -> to honor max upload queries existResults := make(map[string]int, len(md5sums))
batchSize := 2000
for i := 0; i < len(md5sums); i += batchSize {
j := i + batchSize
if j > len(md5sums) {
j = len(md5sums)
}
err := context.imagesExistOnPiwigoBatch(md5sums[i:j], existResults)
if err != nil {
return nil, err
}
}
return existResults, nil
}
func (context *PiwigoContext) imagesExistOnPiwigoBatch(md5sums []string, existResults map[string]int) error {
md5sumList := strings.Join(md5sums, "|") md5sumList := strings.Join(md5sums, "|")
formData := url.Values{} formData := url.Values{}
@ -198,11 +215,9 @@ func (context *PiwigoContext) ImagesExistOnPiwigo(md5sums []string) (map[string]
var imageExistResponse imageExistResponse var imageExistResponse imageExistResponse
err := context.executePiwigoRequest(formData, &imageExistResponse) err := context.executePiwigoRequest(formData, &imageExistResponse)
if err != nil { if err != nil {
return nil, err return err
} }
existResults := make(map[string]int, len(imageExistResponse.Result))
for key, value := range imageExistResponse.Result { for key, value := range imageExistResponse.Result {
if value == "" { if value == "" {
logrus.Tracef("Missing file with md5sum: %s", key) logrus.Tracef("Missing file with md5sum: %s", key)
@ -218,7 +233,7 @@ func (context *PiwigoContext) ImagesExistOnPiwigo(md5sums []string) (map[string]
} }
} }
return existResults, nil return nil
} }
func (context *PiwigoContext) UploadImage(piwigoId int, filePath string, md5sum string, category int) (int, error) { func (context *PiwigoContext) UploadImage(piwigoId int, filePath string, md5sum string, category int) (int, error) {