diff --git a/internal/app/piwigo_mock_test.go b/internal/app/piwigo_mock_test.go index b85cd43..4820a2e 100644 --- a/internal/app/piwigo_mock_test.go +++ b/internal/app/piwigo_mock_test.go @@ -151,6 +151,20 @@ func (m *MockPiwigoImageApi) EXPECT() *MockPiwigoImageApiMockRecorder { return m.recorder } +// DeleteImages mocks base method +func (m *MockPiwigoImageApi) DeleteImages(arg0 []int) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "DeleteImages", arg0) + ret0, _ := ret[0].(error) + return ret0 +} + +// DeleteImages indicates an expected call of DeleteImages +func (mr *MockPiwigoImageApiMockRecorder) DeleteImages(arg0 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DeleteImages", reflect.TypeOf((*MockPiwigoImageApi)(nil).DeleteImages), arg0) +} + // ImageCheckFile mocks base method func (m *MockPiwigoImageApi) ImageCheckFile(arg0 int, arg1 string) (int, error) { m.ctrl.T.Helper() diff --git a/internal/pkg/piwigo/piwigoContext.go b/internal/pkg/piwigo/piwigoContext.go index 2c5cc3c..cd9b71a 100644 --- a/internal/pkg/piwigo/piwigoContext.go +++ b/internal/pkg/piwigo/piwigoContext.go @@ -33,6 +33,7 @@ type PiwigoImageApi interface { ImageCheckFile(piwigoId int, md5sum string) (int, error) ImagesExistOnPiwigo(md5sums []string) (map[string]int, error) UploadImage(piwigoId int, filePath string, md5sum string, category int) (int, error) + DeleteImages(imageIds []int) error } type PiwigoContext struct { @@ -186,7 +187,7 @@ func (context *PiwigoContext) ImageCheckFile(piwigoId int, md5sum string) (int, func (context *PiwigoContext) ImagesExistOnPiwigo(md5sums []string) (map[string]int, error) { //TODO: make sure to split to multiple queries -> to honor max upload queries - md5sumList := strings.Join(md5sums, ",") + md5sumList := strings.Join(md5sums, "|") formData := url.Values{} formData.Set("method", "pwg.images.exist") @@ -246,6 +247,48 @@ func (context *PiwigoContext) UploadImage(piwigoId int, filePath string, md5sum return imageId, nil } +func (context *PiwigoContext) DeleteImages(imageIds []int) error { + logrus.Debug("Entering DeleteImages") + defer logrus.Debug("Leaving DeleteImages") + + pwgToken, err := context.getPiwigoToken() + if err != nil { + return err + } + + parts := make([]string, len(imageIds)) + for id := range imageIds { + parts = append(parts, strconv.Itoa(id)) + } + joinedIds := strings.Join(parts, "|") + + logrus.Infof("Deleting images: %s", joinedIds) + + formData := url.Values{} + formData.Set("method", "pwg.images.delete") + formData.Set("image_id", joinedIds) + formData.Set("pwg_token", pwgToken) + + var deleteResponse deleteResponse + return context.executePiwigoRequest(formData, &deleteResponse) +} + +func (context *PiwigoContext) getPiwigoToken() (string, error) { + logrus.Debug("Entering getPiwigoToken") + defer logrus.Debug("Leaving getPiwigoToken") + + status, err := context.getStatus() + if err != nil { + logrus.Error("Could not get piwigo status.") + return "", err + } + pwgToken := status.Result.PwgToken + if pwgToken == "" { + return "", errors.New("Did not get a valid piwigo token. Could not delete the images.") + } + return pwgToken, nil +} + func (context *PiwigoContext) initializeCookieJarIfRequired() { if context.cookies != nil { return diff --git a/internal/pkg/piwigo/responses.go b/internal/pkg/piwigo/responses.go index 9805111..8db940d 100644 --- a/internal/pkg/piwigo/responses.go +++ b/internal/pkg/piwigo/responses.go @@ -130,3 +130,12 @@ type checkFilesResponse struct { func (r checkFilesResponse) responseStatus() string { return r.Status } + +type deleteResponse struct { + Status string `json:"stat"` + Result int `json:"result"` +} + +func (r deleteResponse) responseStatus() string { + return r.Status +}