-
Notifications
You must be signed in to change notification settings - Fork 0
/
ipfs.go
85 lines (71 loc) · 1.64 KB
/
ipfs.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
package generate
import (
"fmt"
"net/http"
"os"
shell "github.com/ipfs/go-ipfs-api"
)
func (g *Generate) newIpfsClient() (*shell.Shell, error) {
if g.config.IPFS == nil {
return nil, fmt.Errorf("Missing IPFS configuration")
}
var client *http.Client
if g.config.IPFS.ProjectID != "" && g.config.IPFS.ProjectSecret != "" {
client = &http.Client{
Transport: authTransport{
RoundTripper: http.DefaultTransport,
ProjectId: g.config.IPFS.ProjectID,
ProjectSecret: g.config.IPFS.ProjectSecret,
},
}
}
shell := shell.NewShellWithClient(g.config.IPFS.Endpoint, client)
return shell, nil
}
func (g *Generate) Upload(path string) (string, error) {
var cid string
//Get path info
fileInfo, err := os.Stat(path)
if err != nil {
return cid, err
}
//Use AddDir if the path is a directory
if fileInfo.IsDir() {
cid, err = g.ipfs.AddDir(path)
if err != nil {
return cid, err
}
} else { //Upload single file otherwise
file, err := os.Open(path)
if err != nil {
return cid, err
}
defer file.Close()
cid, err = g.ipfs.Add(file)
if err != nil {
return cid, err
}
}
//Pin the file to prevent garbage collection
err = g.ipfs.Pin(cid)
if err != nil {
return cid, err
}
return cid, nil
}
///Auth Transport for Infura
type authTransport struct {
http.RoundTripper
ProjectId string
ProjectSecret string
}
func (t authTransport) RoundTrip(r *http.Request) (*http.Response, error) {
r.SetBasicAuth(t.ProjectId, t.ProjectSecret)
return t.RoundTripper.RoundTrip(r)
}
func getEnv(key string, _default string) string {
if value, ok := os.LookupEnv(key); ok {
return value
}
return _default
}