Files
mstore/pkg/filecli/listcoll.go
T

61 lines
1.3 KiB
Go

/*
* Copyright 2026 Oleg Borodin <onborodin@gmail.com>
*/
package filecli
import (
"bytes"
"context"
"fmt"
"net/http"
"strconv"
)
func (cli *Client) ListCollections(ctx context.Context, rawpath string) ([]byte, error) {
var err error
var list []byte
ref, err := ParsePath(rawpath)
if err != nil {
return list, err
}
uri := ref.CollectionsEP()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return list, err
}
req.Header.Set("User-Agent", cli.userAgent)
req.Header.Set("Accept", "*/*")
resp, err := cli.httpClient.Do(req)
if err != nil {
return list, err
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusNotFound {
return list, err
}
if resp.StatusCode != http.StatusOK {
err := fmt.Errorf("Unexpected response code %s", resp.Status)
return list, err
}
contentLength := resp.Header.Get("Content-Length")
if contentLength == "" {
err := fmt.Errorf("Content-Length header is missing")
return list, err
}
blobSize, err := strconv.ParseInt(contentLength, 10, 64)
if err != nil {
return list, err
}
buffer := bytes.NewBuffer(nil)
recSize, err := Copy(ctx, buffer, resp.Body)
if blobSize != recSize {
err := fmt.Errorf("Mismatch declared and actual body size: %d and %d", blobSize, recSize)
return list, err
}
list = buffer.Bytes()
return list, err
}