From a93ce13625d7fa643fe228c5345255a75de9b78d Mon Sep 17 00:00:00 2001 From: Henrique Dias Date: Thu, 8 Jun 2023 10:33:09 +0200 Subject: [PATCH] feat: use last segment cid as root --- gateway/blocks_backend.go | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/gateway/blocks_backend.go b/gateway/blocks_backend.go index bacb90896..ea6be131e 100644 --- a/gateway/blocks_backend.go +++ b/gateway/blocks_backend.go @@ -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)