-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdag.go
More file actions
160 lines (133 loc) · 3.63 KB
/
Copy pathdag.go
File metadata and controls
160 lines (133 loc) · 3.63 KB
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
151
152
153
154
155
156
157
158
159
160
package gw3
import (
"bytes"
"context"
"fmt"
gohttp "net/http"
"path"
ipfsfiles "github.com/ipfs/boxo/files"
"github.com/photon-storage/go-gw3/common/http"
car "github.com/photon-storage/go-ipfs-car"
)
const (
EmptyDAGRoot = "QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn"
)
// DAGAdd adds a new CID and path to the existing dag, generating a new dag root.
func (c *Client) DAGAdd(root, path string, data []byte) (string, error) {
url, err := c.AuthDAGAdd(root, path, len(data))
if err != nil {
return "", err
}
req, err := gohttp.NewRequest(gohttp.MethodPut, url, bytes.NewReader(data))
if err != nil {
return "", err
}
resp, err := c.hc.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
return resp.Header.Get("IPFS-Hash"), nil
}
// AuthDAGAdd requests Gateway3 for an authorized redirect URL for subsequently adding a new CID and path to the existing dag.
func (c *Client) AuthDAGAdd(root, filePath string, size int) (string, error) {
p := path.Join(root, filePath)
req, err := gohttp.NewRequest(
gohttp.MethodPut,
fmt.Sprintf(
"%s/ipfs/%s?size=%d",
c.endPoint,
p,
size,
),
nil,
)
if err != nil {
return "", err
}
var r redirect
return r.URL, c.callGateway(req, &r)
}
// AuthDAGRemove requests Gateway3 for an authorized redirect URL for subsequently removing a path from the existing dag, generating a new dag root.
func (c *Client) AuthDAGRemove(root, filePath string) (string, error) {
p := path.Join(root, filePath)
req, err := gohttp.NewRequest(
gohttp.MethodDelete,
fmt.Sprintf("%s/ipfs/%s", c.endPoint, p),
nil,
)
if err != nil {
return "", err
}
var r redirect
return r.URL, c.callGateway(req, &r)
}
// DAGRemove removes a path from the existing dag, generating a new dag root.
func (c *Client) DAGRemove(root, path string) (string, error) {
url, err := c.AuthDAGRemove(root, path)
if err != nil {
return "", err
}
req, err := gohttp.NewRequest(gohttp.MethodDelete, url, nil)
if err != nil {
return "", err
}
resp, err := c.hc.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
return resp.Header.Get("IPFS-Hash"), nil
}
// AuthDAGImport requests Gateway3 for an authorized redirect URL for uploading a CAR file.
func (c *Client) AuthDAGImport(size int) (string, error) {
req, err := gohttp.NewRequest(
gohttp.MethodPost,
fmt.Sprintf("%s/api/v0/dag/import", c.endPoint),
nil,
)
if err != nil {
return "", err
}
query := req.URL.Query()
query.Set(http.ParamP3Size, fmt.Sprintf("%v", size))
req.URL.RawQuery = query.Encode()
var r redirect
return r.URL, c.callGateway(req, &r)
}
// DAGImport imports the given src input as a CAR format and returns its root CID. The `src` can be a path to a directory, a byte array or a io.Reader.
func (c *Client) DAGImport(src any) (string, error) {
b := car.NewBuilder()
v1car, err := b.Buildv1(context.TODO(), src, car.ImportOpts.CIDv1())
if err != nil {
return "", err
}
buf := bytes.Buffer{}
if err := v1car.Write(&buf); err != nil {
return "", err
}
r := ipfsfiles.NewMultiFileReader(
ipfsfiles.NewMapDirectory(map[string]ipfsfiles.Node{
"path": ipfsfiles.NewBytesFile(buf.Bytes()),
}),
true,
)
url, err := c.AuthDAGImport(buf.Len())
if err != nil {
return "", err
}
req, err := gohttp.NewRequest(gohttp.MethodPost, url, r)
if err != nil {
return "", err
}
req.Header.Set("Content-Type", "multipart/form-data; boundary="+r.Boundary())
resp, err := c.hc.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
if resp.StatusCode != gohttp.StatusOK {
return "", fmt.Errorf("unexpected status code: %v", resp.StatusCode)
}
return v1car.Root().String(), nil
}