perkeep/pkg/client/remove.go

100 lines
2.8 KiB
Go

/*
Copyright 2011 The Perkeep Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package client
import (
"context"
"errors"
"fmt"
"net/http"
"net/url"
"strings"
"perkeep.org/internal/httputil"
"perkeep.org/pkg/blob"
)
type removeResponse struct {
Removed []string `json:"removed"`
}
// RemoveBlobs removes the list of blobs. An error is returned if the
// server failed to remove a blob. Removing a non-existent blob isn't
// an error.
func (c *Client) RemoveBlobs(ctx context.Context, blobs []blob.Ref) error {
if c.sto != nil {
return c.sto.RemoveBlobs(ctx, blobs)
}
pfx, err := c.prefix()
if err != nil {
return err
}
url_ := fmt.Sprintf("%s/camli/remove", pfx)
params := make(url.Values) // "blobN" -> BlobRefStr
needsDelete := make(map[string]bool) // BlobRefStr -> true
for n, b := range blobs {
if !b.Valid() {
return errors.New("Cannot delete invalid blobref")
}
key := fmt.Sprintf("blob%v", n+1)
params.Add(key, b.String())
needsDelete[b.String()] = true
}
req, err := http.NewRequest("POST", url_, strings.NewReader(params.Encode()))
if err != nil {
return fmt.Errorf("Error creating RemoveBlobs POST request: %v", err)
}
req = req.WithContext(ctx)
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
c.authMode.AddAuthHeader(req)
resp, err := c.httpClient.Do(req)
if err != nil {
resp.Body.Close()
return fmt.Errorf("Got status code %d from blobserver for remove %s", resp.StatusCode, params.Encode())
}
if resp.StatusCode != 200 {
resp.Body.Close()
return fmt.Errorf("Invalid http response %d in remove response", resp.StatusCode)
}
var remResp removeResponse
if err := httputil.DecodeJSON(resp, &remResp); err != nil {
return fmt.Errorf("Failed to parse remove response: %v", err)
}
for _, value := range remResp.Removed {
delete(needsDelete, value)
}
if len(needsDelete) > 0 {
return fmt.Errorf("Failed to remove blobs %s", strings.Join(stringKeys(needsDelete), ", "))
}
return nil
}
// RemoveBlob removes the provided blob. An error is returned if the server failed to remove
// the blob. Removing a non-existent blob isn't an error.
func (c *Client) RemoveBlob(ctx context.Context, b blob.Ref) error {
return c.RemoveBlobs(ctx, []blob.Ref{b})
}
func stringKeys(m map[string]bool) (s []string) {
s = make([]string, 0, len(m))
for key := range m {
s = append(s, key)
}
return
}