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)
| 448 | // Note: this method is still a work in progress, and might change to accommodate |
| 449 | // the needs of pk-put file. |
| 450 | func (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 |