From 5bca4fc1cda38893b185163ea1b5960e398128d9 Mon Sep 17 00:00:00 2001 From: Adin Schmahmann Date: Thu, 6 Jul 2023 11:49:23 -0400 Subject: [PATCH] add CAR response for when path resolution fails due to ipld errors --- gateway/blocks_backend.go | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/gateway/blocks_backend.go b/gateway/blocks_backend.go index 608fc928c2..45f6bc53d4 100644 --- a/gateway/blocks_backend.go +++ b/gateway/blocks_backend.go @@ -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 }