11 "github.com/ipfs/go-ipfs-files"
18 type AddOpts = func(*RequestBuilder) error
20 func OnlyHash(enabled bool) AddOpts {
21 return func(rb *RequestBuilder) error {
22 rb.Option("only-hash", enabled)
27 func Pin(enabled bool) AddOpts {
28 return func(rb *RequestBuilder) error {
29 rb.Option("pin", enabled)
34 func Progress(enabled bool) AddOpts {
35 return func(rb *RequestBuilder) error {
36 rb.Option("progress", enabled)
41 func RawLeaves(enabled bool) AddOpts {
42 return func(rb *RequestBuilder) error {
43 rb.Option("raw-leaves", enabled)
48 func (s *Shell) Add(r io.Reader, options ...AddOpts) (string, error) {
49 fr := files.NewReaderFile(r)
50 slf := files.NewSliceDirectory([]files.DirEntry{files.FileEntry("", fr)})
51 fileReader := files.NewMultiFileReader(slf, true)
54 rb := s.Request("add")
55 for _, option := range options {
58 return out.Hash, rb.Body(fileReader).Exec(context.Background(), &out)
61 // AddNoPin adds a file to ipfs without pinning it
62 // Deprecated: Use Add() with option functions instead
63 func (s *Shell) AddNoPin(r io.Reader) (string, error) {
64 return s.Add(r, Pin(false))
67 // AddWithOpts adds a file to ipfs with some additional options
68 // Deprecated: Use Add() with option functions instead
69 func (s *Shell) AddWithOpts(r io.Reader, pin bool, rawLeaves bool) (string, error) {
70 return s.Add(r, Pin(pin), RawLeaves(rawLeaves))
73 func (s *Shell) AddLink(target string) (string, error) {
74 link := files.NewLinkFile(target, nil)
75 slf := files.NewSliceDirectory([]files.DirEntry{files.FileEntry("", link)})
76 reader := files.NewMultiFileReader(slf, true)
79 return out.Hash, s.Request("add").Body(reader).Exec(context.Background(), &out)
82 // AddDir adds a directory recursively with all of the files under it
83 func (s *Shell) AddDir(dir string) (string, error) {
84 stat, err := os.Lstat(dir)
89 sf, err := files.NewSerialFile(dir, false, stat)
93 slf := files.NewSliceDirectory([]files.DirEntry{files.FileEntry(path.Base(dir), sf)})
94 reader := files.NewMultiFileReader(slf, true)
96 resp, err := s.Request("add").
97 Option("recursive", true).
99 Send(context.Background())
106 if resp.Error != nil {
107 return "", resp.Error
110 dec := json.NewDecoder(resp.Output)
114 err = dec.Decode(&out)
125 return "", errors.New("no results received")