Skip to content

Commit

Permalink
feat: use last segment cid as root
Browse files Browse the repository at this point in the history
  • Loading branch information
hacdias committed Jun 8, 2023
1 parent f7d821e commit df19eb1
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions gateway/blocks_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,19 +233,24 @@ func (bb *BlocksBackend) Head(ctx context.Context, path ImmutablePath) (ContentP
}

func (bb *BlocksBackend) GetCAR(ctx context.Context, p ImmutablePath, params CarParams) (io.ReadCloser, error) {
// Try resolving the path and set the last segment as the root CID. If the path
// is not resolvable, there's likely an error in the path. Then, we send an empty
// root. This allows the response to succeed and the client can verify by themselves
// that the request path is non existent.
var roots []cid.Cid
pathMetadata, err := bb.ResolvePath(ctx, p)
if err == nil {
roots = append(roots, pathMetadata.LastSegment.Cid())
}

contentPathStr := p.String()
if !strings.HasPrefix(contentPathStr, "/ipfs/") {
return nil, fmt.Errorf("path does not have /ipfs/ prefix")
}
firstSegment, _, _ := strings.Cut(contentPathStr[6:], "/")
rootCid, err := cid.Decode(firstSegment)
if err != nil {
return nil, err
}

r, w := io.Pipe()
go func() {
cw, err := storage.NewWritable(w, []cid.Cid{rootCid}, car.WriteAsCarV1(true))
cw, err := storage.NewWritable(w, roots, car.WriteAsCarV1(true))
if err != nil {
// io.PipeWriter.CloseWithError always returns nil.
_ = w.CloseWithError(err)
Expand Down

0 comments on commit df19eb1

Please sign in to comment.