1
0
mirror of https://github.com/ipfs/kubo.git synced 2025-08-06 11:31:54 +08:00

coreapi: add Add()

License: MIT
Signed-off-by: Lars Gierth <larsg@systemli.org>
This commit is contained in:
Lars Gierth
2016-09-20 04:30:43 +02:00
parent 029f971d9c
commit 036ca3a764
4 changed files with 81 additions and 7 deletions

View File

@ -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)
}

View File

@ -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 {

View File

@ -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)
}
}