Files
ociclient/pathupload.go

55 lines
1.2 KiB
Go

package client
import (
"context"
"fmt"
"io"
"net/http"
"strconv"
)
func (cli *Client) PatchUpload(ctx context.Context, rawref string, src io.Reader, uploc string, size int64) (string, error) {
var err error
var ouloc string
ref, err := NewReference(rawref)
if err != nil {
return ouloc, err
}
uri, err := ref.Patch(uploc)
if err != nil {
return ouloc, err
}
user, pass := ref.Userinfo()
req, err := http.NewRequestWithContext(ctx, http.MethodPatch, uri, src)
if err != nil {
return ouloc, 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))
if cli.authenticator != nil {
authHeader, authKey, err := cli.authenticator.MakeHeader(user, pass)
if err != nil {
return ouloc, err
}
req.Header.Set(authHeader, authKey)
}
resp, err := cli.httpClient.Do(req)
if err != nil {
return ouloc, err
}
resp.Body.Close()
if resp.StatusCode != http.StatusAccepted {
err = fmt.Errorf("Upload not accepted, code %d", resp.StatusCode)
return ouloc, err
}
ouloc = resp.Header.Get("Location")
if ouloc == "" {
err := fmt.Errorf("Empty blob location declaration")
return ouloc, err
}
return ouloc, err
}