GetSheet returns the requested sheet, cutoff the content if the content is too long and the `raw` flag in request is false.
(ctx context.Context, request *connect.Request[v1pb.GetSheetRequest])
| 108 | |
| 109 | // GetSheet returns the requested sheet, cutoff the content if the content is too long and the `raw` flag in request is false. |
| 110 | func (s *SheetService) GetSheet(ctx context.Context, request *connect.Request[v1pb.GetSheetRequest]) (*connect.Response[v1pb.Sheet], error) { |
| 111 | projectResourceID, sheetSha256, err := common.GetProjectResourceIDSheetSha256(request.Msg.Name) |
| 112 | if err != nil { |
| 113 | return nil, connect.NewError(connect.CodeInvalidArgument, err) |
| 114 | } |
| 115 | if sheetSha256 == "" { |
| 116 | return nil, connect.NewError(connect.CodeInvalidArgument, errors.Errorf("invalid sheet sha256, must be non-empty")) |
| 117 | } |
| 118 | |
| 119 | project, err := s.store.GetProject(ctx, &store.FindProjectMessage{ |
| 120 | Workspace: common.GetWorkspaceIDFromContext(ctx), |
| 121 | ResourceID: &projectResourceID, |
| 122 | }) |
| 123 | if err != nil { |
| 124 | return nil, connect.NewError(connect.CodeNotFound, errors.Errorf("project with resource id %s not found", projectResourceID)) |
| 125 | } |
| 126 | if project == nil { |
| 127 | return nil, connect.NewError(connect.CodeNotFound, errors.Errorf("project with resource id %s not found", projectResourceID)) |
| 128 | } |
| 129 | if project.Deleted { |
| 130 | return nil, connect.NewError(connect.CodeNotFound, errors.Errorf("project with resource id %q had deleted", projectResourceID)) |
| 131 | } |
| 132 | |
| 133 | var sheet *store.SheetMessage |
| 134 | var sheetErr error |
| 135 | if request.Msg.Raw { |
| 136 | sheet, sheetErr = s.store.GetSheetFull(ctx, sheetSha256) |
| 137 | } else { |
| 138 | sheet, sheetErr = s.store.GetSheetTruncated(ctx, sheetSha256) |
| 139 | } |
| 140 | if sheetErr != nil { |
| 141 | return nil, connect.NewError(connect.CodeInternal, errors.Wrapf(sheetErr, "failed to get sheet")) |
| 142 | } |
| 143 | if sheet == nil { |
| 144 | return nil, connect.NewError(connect.CodeNotFound, errors.Errorf("cannot find the sheet")) |
| 145 | } |
| 146 | |
| 147 | v1pbSheet := convertToAPISheetMessage(project.ResourceID, sheet) |
| 148 | return connect.NewResponse(v1pbSheet), nil |
| 149 | } |
| 150 | |
| 151 | func convertToAPISheetMessage(projectID string, sheet *store.SheetMessage) *v1pb.Sheet { |
| 152 | return &v1pb.Sheet{ |
nothing calls this directly
no test coverage detected