mirror of
https://github.com/ipfs/kubo.git
synced 2025-08-06 19:44:01 +08:00

(might as well do this at the same time) License: MIT Signed-off-by: Steven Allen <steven@stebalien.com>
180 lines
5.0 KiB
Go
180 lines
5.0 KiB
Go
package coreapi
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"github.com/ipfs/go-ipfs/core"
|
|
"github.com/ipfs/go-ipfs/filestore"
|
|
|
|
coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"
|
|
"github.com/ipfs/go-ipfs/core/coreapi/interface/options"
|
|
"github.com/ipfs/go-ipfs/core/coreunix"
|
|
|
|
cidutil "gx/ipfs/QmQJSeE3CX4zos9qeaG8EhecEK9zvrTEfTG84J8C5NVRwt/go-cidutil"
|
|
ipld "gx/ipfs/QmR7TcHkR9nxkUorfi8XMTAMLUK7GiP64TWWBzY3aacc1o/go-ipld-format"
|
|
offline "gx/ipfs/QmT6dHGp3UYd3vUMpy7rzX2CXQv7HLcj42Vtq8qwwjgASb/go-ipfs-exchange-offline"
|
|
blockservice "gx/ipfs/QmWfhv1D18DRSiSm73r4QGcByspzPtxxRTcmHW3axFXZo8/go-blockservice"
|
|
dag "gx/ipfs/QmXyuFW7at4r9dxRAbrPU9JpHW5aqngAFyxvyhvYjzxRKS/go-merkledag"
|
|
dagtest "gx/ipfs/QmXyuFW7at4r9dxRAbrPU9JpHW5aqngAFyxvyhvYjzxRKS/go-merkledag/test"
|
|
files "gx/ipfs/QmZMWMvWMVKCbHetJ4RgndbuEF1io2UpUxwQwtNjtYPzSC/go-ipfs-files"
|
|
mfs "gx/ipfs/QmbvE3cYaSEfHTUU4GN4tvrRtHWTnyLVppiRcDamx1HC8i/go-mfs"
|
|
bstore "gx/ipfs/QmcDDgAXDbpDUpadCJKLr49KYR4HuL7T8Z1dZTHt6ixsoR/go-ipfs-blockstore"
|
|
ft "gx/ipfs/Qmcba8ak38WXFWuaLak5pfJPUDFxcSWbkycBNQfmarpuTv/go-unixfs"
|
|
uio "gx/ipfs/Qmcba8ak38WXFWuaLak5pfJPUDFxcSWbkycBNQfmarpuTv/go-unixfs/io"
|
|
)
|
|
|
|
type UnixfsAPI CoreAPI
|
|
|
|
// Add builds a merkledag node from a reader, adds it to the blockstore,
|
|
// and returns the key representing that node.
|
|
func (api *UnixfsAPI) Add(ctx context.Context, files files.File, opts ...options.UnixfsAddOption) (coreiface.ResolvedPath, error) {
|
|
settings, prefix, err := options.UnixfsAddOptions(opts...)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
n := api.node
|
|
|
|
cfg, err := n.Repo.Config()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// check if repo will exceed storage limit if added
|
|
// TODO: this doesn't handle the case if the hashed file is already in blocks (deduplicated)
|
|
// TODO: conditional GC is disabled due to it is somehow not possible to pass the size to the daemon
|
|
//if err := corerepo.ConditionalGC(req.Context(), n, uint64(size)); err != nil {
|
|
// res.SetError(err, cmdkit.ErrNormal)
|
|
// return
|
|
//}
|
|
|
|
if settings.NoCopy && !cfg.Experimental.FilestoreEnabled {
|
|
return nil, filestore.ErrFilestoreNotEnabled
|
|
}
|
|
|
|
if settings.OnlyHash {
|
|
nilnode, err := core.NewNode(ctx, &core.BuildCfg{
|
|
//TODO: need this to be true or all files
|
|
// hashed will be stored in memory!
|
|
NilRepo: true,
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
n = nilnode
|
|
}
|
|
|
|
addblockstore := n.Blockstore
|
|
if !(settings.FsCache || settings.NoCopy) {
|
|
addblockstore = bstore.NewGCBlockstore(n.BaseBlocks, n.GCLocker)
|
|
}
|
|
|
|
exch := n.Exchange
|
|
if settings.Local {
|
|
exch = offline.Exchange(addblockstore)
|
|
}
|
|
|
|
bserv := blockservice.New(addblockstore, exch) // hash security 001
|
|
dserv := dag.NewDAGService(bserv)
|
|
|
|
fileAdder, err := coreunix.NewAdder(ctx, n.Pinning, n.Blockstore, dserv)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
fileAdder.Chunker = settings.Chunker
|
|
if settings.Events != nil {
|
|
fileAdder.Out = settings.Events
|
|
fileAdder.Progress = settings.Progress
|
|
}
|
|
fileAdder.Hidden = settings.Hidden
|
|
fileAdder.Wrap = settings.Wrap
|
|
fileAdder.Pin = settings.Pin && !settings.OnlyHash
|
|
fileAdder.Silent = settings.Silent
|
|
fileAdder.RawLeaves = settings.RawLeaves
|
|
fileAdder.NoCopy = settings.NoCopy
|
|
fileAdder.Name = settings.StdinName
|
|
fileAdder.CidBuilder = prefix
|
|
|
|
switch settings.Layout {
|
|
case options.BalancedLayout:
|
|
// Default
|
|
case options.TrickleLayout:
|
|
fileAdder.Trickle = true
|
|
default:
|
|
return nil, fmt.Errorf("unknown layout: %d", settings.Layout)
|
|
}
|
|
|
|
if settings.Inline {
|
|
fileAdder.CidBuilder = cidutil.InlineBuilder{
|
|
Builder: fileAdder.CidBuilder,
|
|
Limit: settings.InlineLimit,
|
|
}
|
|
}
|
|
|
|
if settings.OnlyHash {
|
|
md := dagtest.Mock()
|
|
emptyDirNode := ft.EmptyDirNode()
|
|
// Use the same prefix for the "empty" MFS root as for the file adder.
|
|
emptyDirNode.SetCidBuilder(fileAdder.CidBuilder)
|
|
mr, err := mfs.NewRoot(ctx, md, emptyDirNode, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
fileAdder.SetMfsRoot(mr)
|
|
}
|
|
|
|
nd, err := fileAdder.AddAllAndPin(files)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return coreiface.IpfsPath(nd.Cid()), nil
|
|
}
|
|
|
|
func (api *UnixfsAPI) Get(ctx context.Context, p coreiface.Path) (coreiface.UnixfsFile, error) {
|
|
ses := api.core().getSession(ctx)
|
|
|
|
nd, err := ses.ResolveNode(ctx, p)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return newUnixfsFile(ctx, ses.dag, nd, "", nil)
|
|
}
|
|
|
|
// Ls returns the contents of an IPFS or IPNS object(s) at path p, with the format:
|
|
// `<link base58 hash> <link size in bytes> <link name>`
|
|
func (api *UnixfsAPI) Ls(ctx context.Context, p coreiface.Path) ([]*ipld.Link, error) {
|
|
dagnode, err := api.core().ResolveNode(ctx, p)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var ndlinks []*ipld.Link
|
|
dir, err := uio.NewDirectoryFromNode(api.dag, dagnode)
|
|
switch err {
|
|
case nil:
|
|
l, err := dir.Links(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
ndlinks = l
|
|
case uio.ErrNotADir:
|
|
ndlinks = dagnode.Links()
|
|
default:
|
|
return nil, err
|
|
}
|
|
|
|
links := make([]*ipld.Link, len(ndlinks))
|
|
for i, l := range ndlinks {
|
|
links[i] = &ipld.Link{Name: l.Name, Size: l.Size, Cid: l.Cid}
|
|
}
|
|
return links, nil
|
|
}
|
|
|
|
func (api *UnixfsAPI) core() *CoreAPI {
|
|
return (*CoreAPI)(api)
|
|
}
|