Skip to content

Commit

Permalink
add CAR response for when path resolution fails due to ipld errors
Browse files Browse the repository at this point in the history
  • Loading branch information
aschmahmann committed Jul 6, 2023
1 parent 6576e61 commit 5bca4fc
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions gateway/blocks_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,37 @@ func (bb *BlocksBackend) Head(ctx context.Context, path ImmutablePath) (ContentP
func (bb *BlocksBackend) GetCAR(ctx context.Context, p ImmutablePath, params CarParams) (ContentPathMetadata, io.ReadCloser, error) {
pathMetadata, err := bb.ResolvePath(ctx, p)
if err != nil {
rootCid, err := cid.Decode(strings.Split(p.String(), "/")[2])
if err != nil {
return ContentPathMetadata{}, nil, err
}

var buf bytes.Buffer
cw, err := storage.NewWritable(&buf, []cid.Cid{cid.MustParse("bafkqaaa")}, car.WriteAsCarV1(true))
if err != nil {
return ContentPathMetadata{}, nil, err
}

blockGetter := merkledag.NewDAGService(bb.blockService).Session(ctx)

blockGetter = &nodeGetterToCarExporer{
ng: blockGetter,
cw: cw,
}

// Setup the UnixFS resolver.
f := newNodeGetterFetcherSingleUseFactory(ctx, blockGetter)
pathResolver := resolver.NewBasicResolver(f)
ip := ipfspath.FromString(p.String())
_, _, err = pathResolver.ResolveToLastNode(ctx, ip)

if isErrNotFound(err) {
return ContentPathMetadata{
PathSegmentRoots: nil,
LastSegment: ifacepath.NewResolvedPath(ip, rootCid, rootCid, ""),
ContentType: "",
}, io.NopCloser(&buf), nil
}
return ContentPathMetadata{}, nil, err
}

Expand Down

0 comments on commit 5bca4fc

Please sign in to comment.