mirror of
https://github.com/ipfs/kubo.git
synced 2026-02-26 12:57:44 +08:00
The new coreiface.Path maps a path to the cid.Cid resulting from a full path resolution. The path is internally represented as a go-ipfs/path.Path, but that doesn't matter to the outside. Apart from the path-to-CID mapping, it also aims to hold all resolved segment CIDs of the path. Right now it only exposes Root(), and only for flat paths a la /ipfs/Qmfoo. In other cases, the root is nil. In the future, resolution will internally use go-ipfs/path.Resolver.ResolvePathComponents and thus always return the proper resolved segments, via Root(), or a future Segments() func. - Add coreiface.Path with Cid() and Root(). - Add CoreAPI.ResolvePath() for getting a coreiface.Path. - All functions now expect and return coreiface.Path. - Add ParsePath() and ParseCid() for constructing a coreiface.Path. - Add coreiface.Node and Link which are simply go-ipld-node.Node and Link. - Add CoreAPI.ResolveNode() for getting a Node from a Path. License: MIT Signed-off-by: Lars Gierth <larsg@systemli.org>
60 lines
1.3 KiB
Go
60 lines
1.3 KiB
Go
package coreapi
|
|
|
|
import (
|
|
"context"
|
|
"io"
|
|
|
|
coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"
|
|
coreunix "github.com/ipfs/go-ipfs/core/coreunix"
|
|
uio "github.com/ipfs/go-ipfs/unixfs/io"
|
|
|
|
cid "gx/ipfs/QmV5gPoRsjN1Gid3LMdNZTyfCtP2DsvqEbMAmz82RmmiGk/go-cid"
|
|
)
|
|
|
|
type UnixfsAPI CoreAPI
|
|
|
|
func (api *UnixfsAPI) Add(ctx context.Context, r io.Reader) (coreiface.Path, error) {
|
|
k, err := coreunix.AddWithContext(ctx, api.node, r)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
c, err := cid.Decode(k)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return ParseCid(c), nil
|
|
}
|
|
|
|
func (api *UnixfsAPI) Cat(ctx context.Context, p coreiface.Path) (coreiface.Reader, error) {
|
|
dagnode, err := api.core().ResolveNode(ctx, p)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
r, err := uio.NewDagReader(ctx, dagnode, api.node.DAG)
|
|
if err == uio.ErrIsDir {
|
|
return nil, coreiface.ErrIsDir
|
|
} else if err != nil {
|
|
return nil, err
|
|
}
|
|
return r, nil
|
|
}
|
|
|
|
func (api *UnixfsAPI) Ls(ctx context.Context, p coreiface.Path) ([]*coreiface.Link, error) {
|
|
dagnode, err := api.core().ResolveNode(ctx, p)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
l := dagnode.Links()
|
|
links := make([]*coreiface.Link, len(l))
|
|
for i, l := range l {
|
|
links[i] = &coreiface.Link{l.Name, l.Size, l.Cid}
|
|
}
|
|
return links, nil
|
|
}
|
|
|
|
func (api *UnixfsAPI) core() coreiface.CoreAPI {
|
|
return (*CoreAPI)(api)
|
|
}
|