Download downloads a file from the CAS and writes it to the provided writer
(ctx context.Context, w io.Writer, digest string)
| 28 | |
| 29 | // Download downloads a file from the CAS and writes it to the provided writer |
| 30 | func (c *Client) Download(ctx context.Context, w io.Writer, digest string) error { |
| 31 | // Check digest format, including the algorithm and the hex portion |
| 32 | h, err := cr_v1.NewHash(digest) |
| 33 | if err != nil { |
| 34 | return fmt.Errorf("decoding digest: %w", err) |
| 35 | } |
| 36 | |
| 37 | ctx, cancel := context.WithCancel(ctx) |
| 38 | defer cancel() |
| 39 | |
| 40 | // Open the stream to start reading chunks |
| 41 | // TODO: send the full hash, not just the hex portion |
| 42 | reader, err := bytestream.NewByteStreamClient(c.conn).Read(ctx, &bytestream.ReadRequest{ResourceName: h.Hex}) |
| 43 | if err != nil { |
| 44 | return fmt.Errorf("creating the gRPC client: %w", err) |
| 45 | } |
| 46 | |
| 47 | var totalDownloaded int64 |
| 48 | var latestStatus *UpDownStatus |
| 49 | |
| 50 | for { |
| 51 | // Get a chunk |
| 52 | res, err := reader.Recv() |
| 53 | if errors.Is(err, io.EOF) { |
| 54 | break |
| 55 | } else if err != nil { |
| 56 | return err |
| 57 | } |
| 58 | |
| 59 | // Write the chunk to the writer and send its status |
| 60 | n, err := w.Write(res.GetData()) |
| 61 | if err != nil { |
| 62 | return err |
| 63 | } |
| 64 | |
| 65 | totalDownloaded += int64(n) |
| 66 | |
| 67 | latestStatus = &UpDownStatus{ProcessedBytes: totalDownloaded} |
| 68 | |
| 69 | select { |
| 70 | case c.ProgressStatus <- latestStatus: |
| 71 | // message sent |
| 72 | default: |
| 73 | c.logger.Debug().Msg("nobody listening to progress updates, dropping message") |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | return nil |
| 78 | } |
| 79 | |
| 80 | // Describe returns the metadata of a resource by its digest |
| 81 | // We use this to get the filename and the total size of the artifact |