forked from ipfs/go-ipfs-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
add.go
150 lines (126 loc) · 3.42 KB
/
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package shell
import (
"context"
"encoding/json"
"errors"
"io"
"os"
"path/filepath"
files "github.com/ipfs/boxo/files"
)
type object struct {
Hash string
}
type AddOpts = func(*RequestBuilder) error
func OnlyHash(enabled bool) AddOpts {
return func(rb *RequestBuilder) error {
rb.Option("only-hash", enabled)
return nil
}
}
func Pin(enabled bool) AddOpts {
return func(rb *RequestBuilder) error {
rb.Option("pin", enabled)
return nil
}
}
func Progress(enabled bool) AddOpts {
return func(rb *RequestBuilder) error {
rb.Option("progress", enabled)
return nil
}
}
func RawLeaves(enabled bool) AddOpts {
return func(rb *RequestBuilder) error {
rb.Option("raw-leaves", enabled)
return nil
}
}
// Hash allows for selecting the multihash type
func Hash(hash string) AddOpts {
return func(rb *RequestBuilder) error {
rb.Option("hash", hash)
return nil
}
}
// CidVersion allows for selecting the CID version that ipfs should use.
func CidVersion(version int) AddOpts {
return func(rb *RequestBuilder) error {
rb.Option("cid-version", version)
return nil
}
}
// Add adds a file to ipfs pinning it with the given options
func (s *Shell) Add(r io.Reader, options ...AddOpts) (string, error) {
fr := files.NewReaderFile(r)
slf := files.NewSliceDirectory([]files.DirEntry{files.FileEntry("", fr)})
fileReader := files.NewMultiFileReader(slf, true)
var out object
rb := s.Request("add")
for _, option := range options {
option(rb)
}
return out.Hash, rb.Body(fileReader).Exec(context.Background(), &out)
}
// AddNoPin adds a file to ipfs without pinning it
// Deprecated: Use Add() with option functions instead
func (s *Shell) AddNoPin(r io.Reader) (string, error) {
return s.Add(r, Pin(false))
}
// AddWithOpts adds a file to ipfs with some additional options
// Deprecated: Use Add() with option functions instead
func (s *Shell) AddWithOpts(r io.Reader, pin bool, rawLeaves bool) (string, error) {
return s.Add(r, Pin(pin), RawLeaves(rawLeaves))
}
func (s *Shell) AddLink(target string) (string, error) {
link := files.NewLinkFile(target, nil)
slf := files.NewSliceDirectory([]files.DirEntry{files.FileEntry("", link)})
reader := files.NewMultiFileReader(slf, true)
var out object
return out.Hash, s.Request("add").Body(reader).Exec(context.Background(), &out)
}
// AddDir adds a directory recursively with all of the files under it
func (s *Shell) AddDir(dir string, options ...AddOpts) (string, error) {
stat, err := os.Lstat(dir)
if err != nil {
return "", err
}
sf, err := files.NewSerialFile(dir, false, stat)
if err != nil {
return "", err
}
slf := files.NewSliceDirectory([]files.DirEntry{files.FileEntry(filepath.Base(dir), sf)})
reader := files.NewMultiFileReader(slf, true)
rb := s.Request("add").Option("recursive", true)
for _, option := range options {
option(rb)
}
// Here we cannot use .Exec because "add" streams responses back for each file
// within the directory, and we only care about the last one, which is the directory
// itself.
resp, err := rb.Body(reader).Send(context.Background())
if err != nil {
return "", err
}
defer resp.Close()
if resp.Error != nil {
return "", resp.Error
}
dec := json.NewDecoder(resp.Output)
var final string
for {
var out object
err = dec.Decode(&out)
if err != nil {
if err == io.EOF {
break
}
return "", err
}
final = out.Hash
}
if final == "" {
return "", errors.New("no results received")
}
return final, nil
}