-
Notifications
You must be signed in to change notification settings - Fork 19
/
main.go
129 lines (110 loc) · 3.1 KB
/
main.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
package main
import (
"context"
"encoding/base64"
"fmt"
"net/http"
"os"
"os/signal"
"time"
ipfsFiles "github.com/ipfs/go-ipfs-files"
httpapi "github.com/ipfs/go-ipfs-http-client"
coreiface "github.com/ipfs/interface-go-ipfs-core"
caopts "github.com/ipfs/interface-go-ipfs-core/options"
ipfsPath "github.com/ipfs/interface-go-ipfs-core/path"
flag "github.com/spf13/pflag"
)
const infuraAPI = "https://ipfs.infura.io:5001"
func main() {
projectId := flag.String("id", "", "your Infura ProjectID")
projectSecret := flag.String("secret", "", "your Infura ProjectSecret")
api := flag.String("url", infuraAPI, "the API URL")
pin := flag.Bool("pin", true, "whether or not to pin the data")
verbose := flag.Bool("verbose", false, "whether or not to print full upload information")
flag.Parse()
if *projectId == "" {
_, _ = fmt.Fprintln(os.Stderr, "parameter --id is required")
os.Exit(1)
}
if *projectSecret == "" {
_, _ = fmt.Fprintln(os.Stderr, "parameter --secret is required")
os.Exit(1)
}
httpClient := &http.Client{}
client, err := httpapi.NewURLApiWithClient(*api, httpClient)
if err != nil {
_, _ = fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
client.Headers.Add("Authorization", "Basic "+basicAuth(*projectId, *projectSecret))
args := flag.Args()
if len(args) != 1 {
_, _ = fmt.Fprintln(os.Stderr, "file or directory path required as an argument")
os.Exit(1)
}
path := args[0]
stat, err := os.Lstat(path)
if err != nil {
_, _ = fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
// also support directory
file, err := ipfsFiles.NewSerialFile(path, false, stat)
if err != nil {
_, _ = fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
// trap Ctrl+C and call cancel on the context
ctx, cancel := context.WithCancel(context.Background())
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
defer func() {
signal.Stop(c)
cancel()
}()
go func() {
select {
case <-c:
cancel()
case <-ctx.Done():
}
}()
var res ipfsPath.Resolved
errCh := make(chan error, 1)
events := make(chan interface{}, 8)
start := time.Now()
go func() {
var err error
defer close(events)
res, err = client.Unixfs().Add(ctx, file, caopts.Unixfs.Pin(*pin), caopts.Unixfs.Progress(true), caopts.Unixfs.Events(events))
errCh <- err
}()
for event := range events {
output, ok := event.(*coreiface.AddEvent)
if !ok {
panic("unknown event type")
}
if output.Path != nil && output.Name != "" {
if *verbose {
_, _ = fmt.Fprintln(os.Stderr, fmt.Sprintf("Added %v %v | Bytes: %v | Size: %v", output.Name, output.Path, output.Bytes, output.Size))
} else {
_, _ = fmt.Fprintln(os.Stderr, fmt.Sprintf("Added %v", output.Name))
}
}
}
if err := <-errCh; err != nil {
_, _ = fmt.Fprintln(os.Stderr, err)
exit(start, 1)
}
_, _ = fmt.Fprintln(os.Stdout, res.Cid().String())
exit(start, 0)
}
func exit(start time.Time, exitCode int) {
duration := time.Since(start)
_, _ = fmt.Fprintln(os.Stderr, duration)
os.Exit(exitCode)
}
func basicAuth(projectId, projectSecret string) string {
auth := projectId + ":" + projectSecret
return base64.StdEncoding.EncodeToString([]byte(auth))
}