Upload uploads a blob, as described by the provided UploadHandle parameters.
(ctx context.Context, h *UploadHandle)
| 249 | |
| 250 | // Upload uploads a blob, as described by the provided UploadHandle parameters. |
| 251 | func (c *Client) Upload(ctx context.Context, h *UploadHandle) (*PutResult, error) { |
| 252 | errorf := func(msg string, arg ...interface{}) (*PutResult, error) { |
| 253 | err := fmt.Errorf(msg, arg...) |
| 254 | c.printf("%v", err) |
| 255 | return nil, err |
| 256 | } |
| 257 | |
| 258 | bodyReader, bodySize, err := h.readerAndSize() |
| 259 | if err != nil { |
| 260 | return nil, fmt.Errorf("client: error slurping upload handle to find its length: %v", err) |
| 261 | } |
| 262 | if bodySize > constants.MaxBlobSize { |
| 263 | return nil, errors.New("client: body is bigger then max blob size") |
| 264 | } |
| 265 | |
| 266 | c.statsMutex.Lock() |
| 267 | c.stats.UploadRequests.Blobs++ |
| 268 | c.stats.UploadRequests.Bytes += bodySize |
| 269 | c.statsMutex.Unlock() |
| 270 | |
| 271 | pr := &PutResult{BlobRef: h.BlobRef, Size: uint32(bodySize)} |
| 272 | |
| 273 | if c.sto != nil { |
| 274 | // TODO: stat first so we can show skipped? |
| 275 | _, err := blobserver.Receive(ctx, c.sto, h.BlobRef, bodyReader) |
| 276 | if err != nil { |
| 277 | return nil, err |
| 278 | } |
| 279 | return pr, nil |
| 280 | } |
| 281 | |
| 282 | if !h.Vivify { |
| 283 | if _, ok := c.haveCache.StatBlobCache(h.BlobRef); ok { |
| 284 | pr.Skipped = true |
| 285 | return pr, nil |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | blobrefStr := h.BlobRef.String() |
| 290 | |
| 291 | // Pre-upload. Check whether the blob already exists on the |
| 292 | // server and if not, the URL to upload it to. |
| 293 | pfx, err := c.prefix() |
| 294 | if err != nil { |
| 295 | return nil, err |
| 296 | } |
| 297 | |
| 298 | if !h.SkipStat { |
| 299 | url_ := fmt.Sprintf("%s/camli/stat", pfx) |
| 300 | req := c.newRequest(ctx, "POST", url_, strings.NewReader("camliversion=1&blob1="+blobrefStr)) |
| 301 | req.Header.Add("Content-Type", "application/x-www-form-urlencoded") |
| 302 | |
| 303 | resp, err := c.doReqGated(req) |
| 304 | if err != nil { |
| 305 | return errorf("stat http error: %w", err) |
| 306 | } |
| 307 | defer resp.Body.Close() |
| 308 |
no test coverage detected