|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + gopath "path" |
| 8 | + |
| 9 | + "github.com/ipfs/go-blockservice" |
| 10 | + "github.com/ipfs/go-cid" |
| 11 | + bsfetcher "github.com/ipfs/go-fetcher/impl/blockservice" |
| 12 | + blockstore "github.com/ipfs/go-ipfs-blockstore" |
| 13 | + format "github.com/ipfs/go-ipld-format" |
| 14 | + "github.com/ipfs/go-libipfs/blocks" |
| 15 | + "github.com/ipfs/go-libipfs/files" |
| 16 | + "github.com/ipfs/go-merkledag" |
| 17 | + ipfspath "github.com/ipfs/go-path" |
| 18 | + "github.com/ipfs/go-path/resolver" |
| 19 | + "github.com/ipfs/go-unixfs" |
| 20 | + ufile "github.com/ipfs/go-unixfs/file" |
| 21 | + uio "github.com/ipfs/go-unixfs/io" |
| 22 | + "github.com/ipfs/go-unixfsnode" |
| 23 | + iface "github.com/ipfs/interface-go-ipfs-core" |
| 24 | + ifacepath "github.com/ipfs/interface-go-ipfs-core/path" |
| 25 | + dagpb "github.com/ipld/go-codec-dagpb" |
| 26 | + "github.com/ipld/go-ipld-prime" |
| 27 | + "github.com/ipld/go-ipld-prime/node/basicnode" |
| 28 | + "github.com/ipld/go-ipld-prime/schema" |
| 29 | +) |
| 30 | + |
| 31 | +type blocksGateway struct { |
| 32 | + blockStore blockstore.Blockstore |
| 33 | + blockService blockservice.BlockService |
| 34 | + dagService format.DAGService |
| 35 | + resolver resolver.Resolver |
| 36 | +} |
| 37 | + |
| 38 | +func newBlocksGateway(blockService blockservice.BlockService) (*blocksGateway, error) { |
| 39 | + // Setup the DAG services, which use the CAR block store. |
| 40 | + dagService := merkledag.NewDAGService(blockService) |
| 41 | + |
| 42 | + // Setup the UnixFS resolver. |
| 43 | + fetcherConfig := bsfetcher.NewFetcherConfig(blockService) |
| 44 | + fetcherConfig.PrototypeChooser = dagpb.AddSupportToChooser(func(lnk ipld.Link, lnkCtx ipld.LinkContext) (ipld.NodePrototype, error) { |
| 45 | + if tlnkNd, ok := lnkCtx.LinkNode.(schema.TypedLinkNode); ok { |
| 46 | + return tlnkNd.LinkTargetNodePrototype(), nil |
| 47 | + } |
| 48 | + return basicnode.Prototype.Any, nil |
| 49 | + }) |
| 50 | + fetcher := fetcherConfig.WithReifier(unixfsnode.Reify) |
| 51 | + resolver := resolver.NewBasicResolver(fetcher) |
| 52 | + |
| 53 | + return &blocksGateway{ |
| 54 | + blockStore: blockService.Blockstore(), |
| 55 | + blockService: blockService, |
| 56 | + dagService: dagService, |
| 57 | + resolver: resolver, |
| 58 | + }, nil |
| 59 | +} |
| 60 | + |
| 61 | +func (api *blocksGateway) GetUnixFsNode(ctx context.Context, p ifacepath.Resolved) (files.Node, error) { |
| 62 | + nd, err := api.resolveNode(ctx, p) |
| 63 | + if err != nil { |
| 64 | + return nil, err |
| 65 | + } |
| 66 | + |
| 67 | + return ufile.NewUnixfsFile(ctx, api.dagService, nd) |
| 68 | +} |
| 69 | + |
| 70 | +func (api *blocksGateway) LsUnixFsDir(ctx context.Context, p ifacepath.Resolved) (<-chan iface.DirEntry, error) { |
| 71 | + node, err := api.resolveNode(ctx, p) |
| 72 | + if err != nil { |
| 73 | + return nil, err |
| 74 | + } |
| 75 | + |
| 76 | + dir, err := uio.NewDirectoryFromNode(api.dagService, node) |
| 77 | + if err != nil { |
| 78 | + return nil, err |
| 79 | + } |
| 80 | + |
| 81 | + out := make(chan iface.DirEntry, uio.DefaultShardWidth) |
| 82 | + |
| 83 | + go func() { |
| 84 | + defer close(out) |
| 85 | + for l := range dir.EnumLinksAsync(ctx) { |
| 86 | + select { |
| 87 | + case out <- api.processLink(ctx, l): |
| 88 | + case <-ctx.Done(): |
| 89 | + return |
| 90 | + } |
| 91 | + } |
| 92 | + }() |
| 93 | + |
| 94 | + return out, nil |
| 95 | +} |
| 96 | + |
| 97 | +func (api *blocksGateway) GetBlock(ctx context.Context, c cid.Cid) (blocks.Block, error) { |
| 98 | + return api.blockService.GetBlock(ctx, c) |
| 99 | +} |
| 100 | + |
| 101 | +func (api *blocksGateway) GetIPNSRecord(context.Context, cid.Cid) ([]byte, error) { |
| 102 | + return nil, errors.New("not implemented") |
| 103 | +} |
| 104 | + |
| 105 | +func (api *blocksGateway) IsCached(ctx context.Context, p ifacepath.Path) bool { |
| 106 | + rp, err := api.ResolvePath(ctx, p) |
| 107 | + if err != nil { |
| 108 | + return false |
| 109 | + } |
| 110 | + |
| 111 | + has, _ := api.blockStore.Has(ctx, rp.Cid()) |
| 112 | + return has |
| 113 | +} |
| 114 | + |
| 115 | +func (api *blocksGateway) ResolvePath(ctx context.Context, p ifacepath.Path) (ifacepath.Resolved, error) { |
| 116 | + if _, ok := p.(ifacepath.Resolved); ok { |
| 117 | + return p.(ifacepath.Resolved), nil |
| 118 | + } |
| 119 | + |
| 120 | + if err := p.IsValid(); err != nil { |
| 121 | + return nil, err |
| 122 | + } |
| 123 | + |
| 124 | + if p.Namespace() != "ipfs" { |
| 125 | + return nil, fmt.Errorf("unsupported path namespace: %s", p.Namespace()) |
| 126 | + } |
| 127 | + |
| 128 | + ipath := ipfspath.Path(p.String()) |
| 129 | + node, rest, err := api.resolver.ResolveToLastNode(ctx, ipath) |
| 130 | + if err != nil { |
| 131 | + return nil, err |
| 132 | + } |
| 133 | + |
| 134 | + root, err := cid.Parse(ipath.Segments()[1]) |
| 135 | + if err != nil { |
| 136 | + return nil, err |
| 137 | + } |
| 138 | + |
| 139 | + return ifacepath.NewResolvedPath(ipath, node, root, gopath.Join(rest...)), nil |
| 140 | +} |
| 141 | + |
| 142 | +func (api *blocksGateway) resolveNode(ctx context.Context, p ifacepath.Path) (format.Node, error) { |
| 143 | + rp, err := api.ResolvePath(ctx, p) |
| 144 | + if err != nil { |
| 145 | + return nil, err |
| 146 | + } |
| 147 | + |
| 148 | + node, err := api.dagService.Get(ctx, rp.Cid()) |
| 149 | + if err != nil { |
| 150 | + return nil, fmt.Errorf("get node: %w", err) |
| 151 | + } |
| 152 | + return node, nil |
| 153 | +} |
| 154 | + |
| 155 | +func (api *blocksGateway) processLink(ctx context.Context, result unixfs.LinkResult) iface.DirEntry { |
| 156 | + if result.Err != nil { |
| 157 | + return iface.DirEntry{Err: result.Err} |
| 158 | + } |
| 159 | + |
| 160 | + link := iface.DirEntry{ |
| 161 | + Name: result.Link.Name, |
| 162 | + Cid: result.Link.Cid, |
| 163 | + } |
| 164 | + |
| 165 | + switch link.Cid.Type() { |
| 166 | + case cid.Raw: |
| 167 | + link.Type = iface.TFile |
| 168 | + link.Size = result.Link.Size |
| 169 | + case cid.DagProtobuf: |
| 170 | + link.Size = result.Link.Size |
| 171 | + } |
| 172 | + |
| 173 | + return link |
| 174 | +} |
0 commit comments