-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpost.go
51 lines (43 loc) · 884 Bytes
/
post.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
package gw3
import (
"bytes"
"fmt"
"net/http"
)
// Post uploads the given data to Gateway3 and returns the corresponding CID.
func (c *Client) Post(data []byte) (string, error) {
url, err := c.AuthPost(len(data))
if err != nil {
return "", err
}
req, err := http.NewRequest(http.MethodPost, 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
}
type redirect struct {
URL string `json:"url"`
}
// AuthPost gets the authorized URL from the Gateway3.
func (c *Client) AuthPost(size int) (string, error) {
req, err := http.NewRequest(
http.MethodPost,
fmt.Sprintf(
"%s/ipfs/?size=%d",
c.endPoint,
size,
),
nil,
)
if err != nil {
return "", err
}
var r redirect
return r.URL, c.callGateway(req, &r)
}