client rebuilding in progress
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
package filecli
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func (cli *Client) ListFiles(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.Files()
|
||||
|
||||
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")
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user