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

56 lines
1.3 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 repocli
import (
"context"
"fmt"
"io"
"net/http"
"strconv"
)
func (cli *Client) PatchUpload(ctx context.Context, rawrepo string, src io.Reader, uploc string, size int64) (string, error) {
var err error
var ouloc string
ref, err := NewReferer(rawrepo)
if err != nil {
return ouloc, err
}
uri, err := ref.PatchEP(uploc)
if err != nil {
return ouloc, err
}
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))
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
}