Files
mstore/pkg/filecli/putfile.go
T
2026-03-11 19:47:40 +02:00

45 lines
1.0 KiB
Go

/*
* Copyright 2026 Oleg Borodin <onborodin@gmail.com>
*
* This work is published and licensed under a Creative Commons
* Attribution-NonCommercial-NoDerivatives 4.0 International License.
*
* Distribution of this work is permitted, but commercial use and
* modifications are strictly prohibited.
*/
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.FileEP()
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-Size", 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 %s", resp.Status)
return err
}
return err
}