MCPcopy
hub / github.com/perkeep/perkeep / UploadFile

Method UploadFile

pkg/client/upload.go:450–483  ·  view source on GitHub ↗

UploadFile uploads the contents of the file, as well as a file blob with filename for these contents. If the contents or the file blob are found on the server, they're not uploaded. Note: this method is still a work in progress, and might change to accommodate the needs of pk-put file.

(ctx context.Context, filename string, contents io.Reader, opts *FileUploadOptions)

Source from the content-addressed store, hash-verified

448// Note: this method is still a work in progress, and might change to accommodate
449// the needs of pk-put file.
450func (c *Client) UploadFile(ctx context.Context, filename string, contents io.Reader, opts *FileUploadOptions) (blob.Ref, error) {
451 fileMap := schema.NewFileMap(filename)
452 if opts != nil && opts.FileInfo != nil {
453 fileMap = schema.NewCommonFileMap(filename, opts.FileInfo)
454 modTime := opts.FileInfo.ModTime()
455 if !modTime.IsZero() {
456 fileMap.SetModTime(modTime)
457 }
458 }
459 fileMap.SetType(schema.TypeFile)
460
461 var wholeRef []blob.Ref
462 if opts != nil && opts.WholeRef.Valid() {
463 wholeRef = append(wholeRef, opts.WholeRef)
464 } else {
465 var buf bytes.Buffer
466 var err error
467 wholeRef, err = c.wholeRef(io.TeeReader(contents, &buf))
468 if err != nil {
469 return blob.Ref{}, err
470 }
471 contents = io.MultiReader(&buf, contents)
472 }
473
474 fileRef, err := c.fileMapFromDuplicate(ctx, fileMap, wholeRef)
475 if err != nil {
476 return blob.Ref{}, err
477 }
478 if fileRef.Valid() {
479 return fileRef, nil
480 }
481
482 return schema.WriteFileMap(ctx, c, fileMap, contents)
483}
484
485// TODO(mpl): replace up.wholeFileDigest in pk-put with c.wholeRef maybe.
486

Callers 15

TestQueryFunction · 0.45
TestQueryCamliTypeFunction · 0.45
TestQueryAnyCamliTypeFunction · 0.45
TestQueryBlobSizeFunction · 0.45
TestQueryBlobRefPrefixFunction · 0.45
TestQueryLogicalOrFunction · 0.45
TestQueryLogicalAndFunction · 0.45
TestQueryLogicalXorFunction · 0.45
TestQueryLogicalNotFunction · 0.45
TestQueryFileLocationFunction · 0.45
TestQueryFileConstraintFunction · 0.45

Calls 9

wholeRefMethod · 0.95
fileMapFromDuplicateMethod · 0.95
NewFileMapFunction · 0.92
NewCommonFileMapFunction · 0.92
WriteFileMapFunction · 0.92
SetModTimeMethod · 0.80
SetTypeMethod · 0.80
ModTimeMethod · 0.45
ValidMethod · 0.45

Tested by 15

TestQueryFunction · 0.36
TestQueryCamliTypeFunction · 0.36
TestQueryAnyCamliTypeFunction · 0.36
TestQueryBlobSizeFunction · 0.36
TestQueryBlobRefPrefixFunction · 0.36
TestQueryLogicalOrFunction · 0.36
TestQueryLogicalAndFunction · 0.36
TestQueryLogicalXorFunction · 0.36
TestQueryLogicalNotFunction · 0.36
TestQueryFileLocationFunction · 0.36
TestQueryFileConstraintFunction · 0.36