-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
181 lines (158 loc) · 4.45 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
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package main
import (
"context"
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"path/filepath"
"github.com/ipfs/go-ipfs-files"
"github.com/ipfs/go-ipfs-http-client"
"github.com/ipfs/ipfs-cluster/api"
ipfsCluster "github.com/ipfs/ipfs-cluster/api/rest/client"
ma "github.com/multiformats/go-multiaddr"
)
const (
UploadPathEnvVar = "IPFS_MOBILE_HELPER_UPLOAD_PATH"
UploadPathSingleNodeSuffix = "single"
UploadPathClusterSuffix = "cluster"
)
func main() {
addr, err := ma.NewMultiaddr("/ip4/127.0.0.1/tcp/5001")
if err != nil {
log.Fatalf("could not make NewMultiaddr: %v", err)
}
ipfs, err := httpapi.NewApi(addr)
if err != nil {
log.Fatalf("could not create ipfs client: %v", err)
}
uploadPath := os.Getenv(UploadPathEnvVar)
if uploadPath == "" {
log.Fatalf("missing env var: %s", UploadPathEnvVar)
}
clusterCfg := &ipfsCluster.Config{}
cluster, err := ipfsCluster.NewDefaultClient(clusterCfg)
if err != nil {
log.Fatalf("could not create ipfs-cluster client: %v", err)
}
addHandler := NewAddHandler(ipfs, uploadPath)
http.Handle("/add", addHandler)
clusterAddHandler := NewClusterAddHandler(cluster, uploadPath)
http.Handle("/cluster/add", clusterAddHandler)
log.Fatal(http.ListenAndServe(":9999", nil))
}
type ClusterAddHandler struct {
client ipfsCluster.Client
uploadPath string
}
func NewClusterAddHandler(client ipfsCluster.Client, uploadPath string) ClusterAddHandler {
return ClusterAddHandler{
client: client,
uploadPath: filepath.Join(uploadPath, UploadPathClusterSuffix),
}
}
func (h ClusterAddHandler) ServeHTTP(w http.ResponseWriter, _ *http.Request) {
var addPaths []string
err := filepath.Walk(h.uploadPath, func(path string, info os.FileInfo, err error) error {
if !info.Mode().IsRegular() {
return nil
}
addPaths = append(addPaths, path)
return nil
})
if err != nil {
log.Printf("error walking upload path %q: %v", h.uploadPath, err)
w.WriteHeader(500)
return
}
ctx := context.Background()
addChan := make(chan *api.AddedOutput)
errChan := make(chan error)
var results []*api.AddedOutput
go func() {
errChan <- h.client.Add(ctx, addPaths, api.DefaultAddParams(), addChan)
}()
for {
select {
case res := <-addChan:
if res == nil {
continue
}
log.Printf("cluster add res: %+v", res)
results = append(results, res)
case err := <-errChan:
if err != nil {
log.Printf("error calling cluster Add: %v", err)
w.WriteHeader(500)
return
}
resBytes, err := json.Marshal(results)
if err != nil {
log.Printf("error marshaling json %v in ClusterAddHandler: %v", results, err)
w.WriteHeader(500)
return
}
n, err := fmt.Fprint(w, string(resBytes))
if err != nil {
log.Printf("error writing ClusterAddHandler respsonse after %d bytes written: %v", n, err)
}
return
}
}
}
type AddResult struct {
Path string `json:"path"`
CID string `json:"cid"`
Err error `json:"err,omitempty"`
}
type AddHandler struct {
ipfs *httpapi.HttpApi
uploadPath string
}
func NewAddHandler(ipfs *httpapi.HttpApi, uploadPath string) AddHandler {
return AddHandler{
ipfs: ipfs,
uploadPath: filepath.Join(uploadPath, UploadPathSingleNodeSuffix),
}
}
func (h AddHandler) ServeHTTP(w http.ResponseWriter, _ *http.Request) {
var results []AddResult
err := filepath.Walk(h.uploadPath, newFileAdder(h.ipfs, &results))
if err != nil {
log.Printf("error walking upload path %q: %v", h.uploadPath, err)
w.WriteHeader(500)
return
}
resBytes, err := json.Marshal(results)
if err != nil {
log.Printf("error marshaling json %v in AddHandler: %v", results, err)
w.WriteHeader(500)
return
}
n, err := fmt.Fprint(w, string(resBytes))
if err != nil {
log.Printf("error writing AddHandler response after %d bytes written: %v", n, err)
}
}
func newFileAdder(ipfs *httpapi.HttpApi, results *[]AddResult) func(path string, info os.FileInfo, err error) error {
return func(path string, info os.FileInfo, err error) error {
if err != nil {
return fmt.Errorf("error accessing path %q: %w", path, err)
}
if !info.Mode().IsRegular() {
return nil
}
node, err := files.NewSerialFile(path, false, info)
if err != nil {
return fmt.Errorf("could not create new serial file from %q: %w", path, err)
}
ctx := context.Background()
res, err := ipfs.Unixfs().Add(ctx, node)
if err != nil {
return fmt.Errorf("error adding file %q: %w", path, err)
}
*results = append(*results, AddResult{path, res.Cid().String(), nil})
return nil
}
}