MCPcopy
hub / github.com/perkeep/perkeep / Upload

Method Upload

pkg/client/upload.go:251–432  ·  view source on GitHub ↗

Upload uploads a blob, as described by the provided UploadHandle parameters.

(ctx context.Context, h *UploadHandle)

Source from the content-addressed store, hash-verified

249
250// Upload uploads a blob, as described by the provided UploadHandle parameters.
251func (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

Callers 10

ReceiveBlobMethod · 0.95
uploadStringMethod · 0.95
ReceiveBlobMethod · 0.45
RunCommandMethod · 0.45
fileMapFromDuplicateMethod · 0.45
uploadNodeRegularFileMethod · 0.45
uploadStringMethod · 0.45
RunCommandMethod · 0.45
signAndSendMethod · 0.45
newPermanodeMethod · 0.45

Calls 15

printfMethod · 0.95
prefixMethod · 0.95
newRequestMethod · 0.95
doReqGatedMethod · 0.95
ReceiveFunction · 0.92
DebugUploadsFunction · 0.92
DecodeJSONFunction · 0.92
parseStatResponseFunction · 0.85
getMultipartOverheadFunction · 0.85
readerAndSizeMethod · 0.80
LockMethod · 0.80
UnlockMethod · 0.80

Tested by

no test coverage detected