1MB Uploads a given file to a CAS server
(ctx context.Context, filepath string)
| 37 | |
| 38 | // Uploads a given file to a CAS server |
| 39 | func (c *Client) UploadFile(ctx context.Context, filepath string) (*UpDownStatus, error) { |
| 40 | // open file and calculate digest |
| 41 | f, err := os.Open(filepath) |
| 42 | if err != nil { |
| 43 | return nil, fmt.Errorf("can't open file to upload: %w", err) |
| 44 | } |
| 45 | defer f.Close() |
| 46 | |
| 47 | hash, _, err := cr_v1.SHA256(f) |
| 48 | if err != nil { |
| 49 | return nil, fmt.Errorf("generating digest: %w", err) |
| 50 | } |
| 51 | |
| 52 | // Since we have already iterated on the file to calculate the digest |
| 53 | // we need to rewind the file pointer |
| 54 | _, err = f.Seek(0, io.SeekStart) |
| 55 | if err != nil { |
| 56 | return nil, fmt.Errorf("rewinding file pointer: %w", err) |
| 57 | } |
| 58 | |
| 59 | return c.Upload(ctx, f, path.Base(filepath), hash.String()) |
| 60 | } |
| 61 | |
| 62 | func (c *Client) Upload(ctx context.Context, r io.Reader, filename, digest string) (*UpDownStatus, error) { |
| 63 | // Check digest format, including the algorithm and the hex portion |