forked from whyrusleeping/ipfs-embedded-shell
/
add.go
34 lines (29 loc) · 739 Bytes
/
add.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package embeddedShell
import (
"io"
"gopkg.in/errgo.v1"
"github.com/ipfs/go-ipfs/importer"
"github.com/ipfs/go-ipfs/importer/chunk"
dag "github.com/ipfs/go-ipfs/merkledag"
ft "github.com/ipfs/go-ipfs/unixfs"
)
func (s *Shell) Add(r io.Reader) (string, error) {
dag, err := importer.BuildDagFromReader(
s.node.DAG,
chunk.DefaultSplitter(r),
)
if err != nil {
return "", errgo.Notef(err, "add: importing DAG failed.")
}
return dag.Cid().String(), nil
}
// AddLink creates a unixfs symlink and returns its hash
func (s *Shell) AddLink(target string) (string, error) {
d, _ := ft.SymlinkData(target)
nd := dag.NodeWithData(d)
c, err := s.node.DAG.Add(nd)
if err != nil {
return "", err
}
return c.String(), nil
}