added DeleteImages to the piwigo context

This commit is contained in:
Philipp Häfelfinger 2019-03-24 23:07:59 +01:00
parent df349a85e6
commit 2c65c5827e
3 changed files with 67 additions and 1 deletions

View File

@ -151,6 +151,20 @@ func (m *MockPiwigoImageApi) EXPECT() *MockPiwigoImageApiMockRecorder {
return m.recorder 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 // ImageCheckFile mocks base method
func (m *MockPiwigoImageApi) ImageCheckFile(arg0 int, arg1 string) (int, error) { func (m *MockPiwigoImageApi) ImageCheckFile(arg0 int, arg1 string) (int, error) {
m.ctrl.T.Helper() m.ctrl.T.Helper()

View File

@ -33,6 +33,7 @@ type PiwigoImageApi interface {
ImageCheckFile(piwigoId int, md5sum string) (int, error) ImageCheckFile(piwigoId int, md5sum string) (int, error)
ImagesExistOnPiwigo(md5sums []string) (map[string]int, error) ImagesExistOnPiwigo(md5sums []string) (map[string]int, error)
UploadImage(piwigoId int, filePath string, md5sum string, category int) (int, error) UploadImage(piwigoId int, filePath string, md5sum string, category int) (int, error)
DeleteImages(imageIds []int) error
} }
type PiwigoContext struct { 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) { func (context *PiwigoContext) ImagesExistOnPiwigo(md5sums []string) (map[string]int, error) {
//TODO: make sure to split to multiple queries -> to honor max upload queries //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 := url.Values{}
formData.Set("method", "pwg.images.exist") formData.Set("method", "pwg.images.exist")
@ -246,6 +247,48 @@ func (context *PiwigoContext) UploadImage(piwigoId int, filePath string, md5sum
return imageId, nil 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() { func (context *PiwigoContext) initializeCookieJarIfRequired() {
if context.cookies != nil { if context.cookies != nil {
return return

View File

@ -130,3 +130,12 @@ type checkFilesResponse struct {
func (r checkFilesResponse) responseStatus() string { func (r checkFilesResponse) responseStatus() string {
return r.Status return r.Status
} }
type deleteResponse struct {
Status string `json:"stat"`
Result int `json:"result"`
}
func (r deleteResponse) responseStatus() string {
return r.Status
}