Commit 036ca3a7 authored by Lars Gierth's avatar Lars Gierth

coreapi: add Add()

License: MIT
Signed-off-by: default avatarLars Gierth <larsg@systemli.org>
parent 029f971d
......@@ -25,6 +25,7 @@ type Reader interface {
}
type UnixfsAPI interface {
Add(context.Context, io.Reader) (*cid.Cid, error)
Cat(context.Context, string) (Reader, error)
Ls(context.Context, string) ([]*Link, error)
}
......
......@@ -2,10 +2,14 @@ package coreapi
import (
"context"
"io"
core "github.com/ipfs/go-ipfs/core"
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/QmXfiyr2RWEXpVDdaYnD2HNiBk6UBddsvEP4RPfXb6nGqY/go-cid"
)
type UnixfsAPI struct {
......@@ -17,6 +21,14 @@ func NewUnixfsAPI(n *core.IpfsNode) coreiface.UnixfsAPI {
return api
}
func (api *UnixfsAPI) Add(ctx context.Context, r io.Reader) (*cid.Cid, error) {
k, err := coreunix.AddWithContext(ctx, api.node, r)
if err != nil {
return nil, err
}
return cid.Decode(k)
}
func (api *UnixfsAPI) Cat(ctx context.Context, p string) (coreiface.Reader, error) {
dagnode, err := resolve(ctx, api.node, p)
if err != nil {
......
......@@ -18,6 +18,10 @@ import (
unixfs "github.com/ipfs/go-ipfs/unixfs"
)
// `echo -n 'hello, world!' | ipfs add`
var hello = "QmQy2Dw4Wk7rdJKjThjYXzfFJNaRKRHhHP5gHHXroJMYxk"
var helloStr = "hello, world!"
// `ipfs object new unixfs-dir`
var emptyUnixfsDir = "QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn"
......@@ -41,6 +45,56 @@ func makeAPI(ctx context.Context) (*core.IpfsNode, coreiface.UnixfsAPI, error) {
return node, api, nil
}
func TestAdd(t *testing.T) {
ctx := context.Background()
_, api, err := makeAPI(ctx)
if err != nil {
t.Error(err)
}
str := strings.NewReader(helloStr)
c, err := api.Add(ctx, str)
if err != nil {
t.Error(err)
}
if c.String() != hello {
t.Fatalf("expected CID %s, got: %s", hello, c)
}
r, err := api.Cat(ctx, hello)
if err != nil {
t.Fatal(err)
}
buf := make([]byte, len(helloStr))
_, err = io.ReadFull(r, buf)
if err != nil {
t.Error(err)
}
if string(buf) != helloStr {
t.Fatalf("expected [%s], got [%s] [err=%s]", helloStr, string(buf), err)
}
}
func TestAddEmptyFile(t *testing.T) {
ctx := context.Background()
_, api, err := makeAPI(ctx)
if err != nil {
t.Error(err)
}
str := strings.NewReader("")
c, err := api.Add(ctx, str)
if err != nil {
t.Error(err)
}
if c.String() != emptyUnixfsFile {
t.Fatalf("expected CID %s, got: %s", hello, c)
}
}
func TestCatBasic(t *testing.T) {
ctx := context.Background()
node, api, err := makeAPI(ctx)
......@@ -48,25 +102,28 @@ func TestCatBasic(t *testing.T) {
t.Fatal(err)
}
hello := "hello, world!"
hr := strings.NewReader(hello)
hr := strings.NewReader(helloStr)
k, err := coreunix.Add(node, hr)
if err != nil {
t.Fatal(err)
}
if k != hello {
t.Fatalf("expected CID %s, got: %s", hello, k)
}
r, err := api.Cat(ctx, k)
if err != nil {
t.Fatal(err)
}
buf := make([]byte, len(hello))
n, err := io.ReadFull(r, buf)
if err != nil && err != io.EOF {
buf := make([]byte, len(helloStr))
_, err = io.ReadFull(r, buf)
if err != nil {
t.Error(err)
}
if string(buf) != hello {
t.Fatalf("expected [hello, world!], got [%s] [err=%s]", string(buf), n, err)
if string(buf) != helloStr {
t.Fatalf("expected [%s], got [%s] [err=%s]", helloStr, string(buf), err)
}
}
......
......@@ -254,6 +254,10 @@ func (adder *Adder) outputDirs(path string, fsn mfs.FSNode) error {
// Add builds a merkledag from the a reader, pinning all objects to the local
// datastore. Returns a key representing the root node.
func Add(n *core.IpfsNode, r io.Reader) (string, error) {
return AddWithContext(n.Context(), n, r)
}
func AddWithContext(ctx context.Context, n *core.IpfsNode, r io.Reader) (string, error) {
defer n.Blockstore.PinLock().Unlock()
fileAdder, err := NewAdder(n.Context(), n.Pinning, n.Blockstore, n.DAG)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment