added pkg/filecli/
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
package filecli
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func (cli *Client) PutFile(ctx context.Context, rawpath string, src io.Reader, size int64) error {
|
||||
var err error
|
||||
ref, err := ParsePath(rawpath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
uri := ref.File()
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodPut, uri, src)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
req.Header.Set("User-Agent", cli.userAgent)
|
||||
req.Header.Set("Content-Type", "application/octet-stream")
|
||||
req.Header.Set("Content-Length", strconv.FormatInt(size, 10))
|
||||
resp, err := cli.httpClient.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
resp.Body.Close()
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
err = fmt.Errorf("File not accepted, code %d", resp.StatusCode)
|
||||
return err
|
||||
}
|
||||
return err
|
||||
}
|
||||
Reference in New Issue
Block a user