From a20f0771f17a34c5fad6ca1a176ee97d24846127 Mon Sep 17 00:00:00 2001 From: Sergey Cherepanov Date: Wed, 11 Jan 2023 15:39:32 +0300 Subject: [PATCH] fileservice.HasCid --- commonfile/fileservice/fileservice.go | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/commonfile/fileservice/fileservice.go b/commonfile/fileservice/fileservice.go index 0c23a5fc..fabc70c3 100644 --- a/commonfile/fileservice/fileservice.go +++ b/commonfile/fileservice/fileservice.go @@ -33,10 +33,12 @@ type FileService interface { AddFile(ctx context.Context, r io.Reader) (ipld.Node, error) // DAGService returns ipld.DAGService object DAGService() ipld.DAGService + // HasCid checks is CID exists + HasCid(ctx context.Context, c cid.Cid) (exists bool, err error) app.Component } - type fileService struct { + bs fileblockstore.BlockStoreLocal merkledag ipld.DAGService prefix cid.Prefix } @@ -52,8 +54,8 @@ func (fs *fileService) Init(a *app.App) (err error) { } prefix.MhType = hashFunCode prefix.MhLength = -1 - bs := newBlockService(a.MustComponent(fileblockstore.CName).(fileblockstore.BlockStore)) - fs.merkledag = merkledag.NewDAGService(bs) + fs.bs = a.MustComponent(fileblockstore.CName).(fileblockstore.BlockStoreLocal) + fs.merkledag = merkledag.NewDAGService(newBlockService(fs.bs)) fs.prefix = prefix return } @@ -92,3 +94,11 @@ func (fs *fileService) GetFile(ctx context.Context, c cid.Cid) (ufsio.ReadSeekCl } return ufsio.NewDagReader(ctx, n, fs.merkledag) } + +func (fs *fileService) HasCid(ctx context.Context, c cid.Cid) (exists bool, err error) { + res, err := fs.bs.ExistsCids(ctx, []cid.Cid{c}) + if err != nil { + return + } + return len(res) > 0, nil +}