UploadModuleFiles will insert a file into the database of coderd.
(ctx context.Context, moduleFiles []byte)
| 521 | |
| 522 | // UploadModuleFiles will insert a file into the database of coderd. |
| 523 | func (p *Server) UploadModuleFiles(ctx context.Context, moduleFiles []byte) error { |
| 524 | // Send the files separately if the message size is too large. |
| 525 | _, err := clientDoWithRetries(ctx, p.client, func(ctx context.Context, client proto.DRPCProvisionerDaemonClient) (*proto.Empty, error) { |
| 526 | // Add some timeout to prevent the stream from hanging indefinitely. |
| 527 | ctx, cancel := context.WithTimeout(ctx, 5*time.Minute) |
| 528 | defer cancel() |
| 529 | |
| 530 | stream, err := client.UploadFile(ctx) |
| 531 | if err != nil { |
| 532 | return nil, xerrors.Errorf("failed to start UploadModuleFiles stream: %w", err) |
| 533 | } |
| 534 | defer stream.Close() |
| 535 | |
| 536 | dataUp, chunks, err := sdkproto.BytesToDataUpload(sdkproto.DataUploadType_UPLOAD_TYPE_MODULE_FILES, moduleFiles) |
| 537 | if err != nil { |
| 538 | return nil, xerrors.Errorf("prepare module files upload: %w", err) |
| 539 | } |
| 540 | |
| 541 | err = stream.Send(&sdkproto.FileUpload{Type: &sdkproto.FileUpload_DataUpload{DataUpload: dataUp}}) |
| 542 | if err != nil { |
| 543 | if retryable(err) { // Do not retry |
| 544 | return nil, xerrors.Errorf("send data upload: %s", err.Error()) |
| 545 | } |
| 546 | return nil, xerrors.Errorf("send data upload: %w", err) |
| 547 | } |
| 548 | |
| 549 | for i, chunk := range chunks { |
| 550 | err = stream.Send(&sdkproto.FileUpload{Type: &sdkproto.FileUpload_ChunkPiece{ChunkPiece: chunk}}) |
| 551 | if err != nil { |
| 552 | if retryable(err) { // Do not retry |
| 553 | return nil, xerrors.Errorf("send chunk piece: %s", err.Error()) |
| 554 | } |
| 555 | return nil, xerrors.Errorf("send chunk piece %d: %w", i, err) |
| 556 | } |
| 557 | } |
| 558 | |
| 559 | resp, err := stream.CloseAndRecv() |
| 560 | if err != nil { |
| 561 | if retryable(err) { // Do not retry |
| 562 | return nil, xerrors.Errorf("close stream: %s", err.Error()) |
| 563 | } |
| 564 | return nil, xerrors.Errorf("close stream: %w", err) |
| 565 | } |
| 566 | return resp, nil |
| 567 | }) |
| 568 | if err != nil { |
| 569 | return xerrors.Errorf("upload module files: %w", err) |
| 570 | } |
| 571 | |
| 572 | return nil |
| 573 | } |
| 574 | |
| 575 | // DownloadFile will download a module file from coderd. |
| 576 | func (p *Server) DownloadFile(ctx context.Context, request *proto.FileRequest) ([]byte, error) { |
no test coverage detected