-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathipfsbatcher.go
79 lines (67 loc) · 1.6 KB
/
ipfsbatcher.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
package ipfsbatcher
import (
"context"
"ipfsbatcher/generator"
"ipfsbatcher/ipfs"
"log"
"os"
"path"
"path/filepath"
"code.cloudfoundry.org/bytefmt"
"github.com/ipfs/go-cid"
httpapi "github.com/ipfs/go-ipfs-http-client"
)
func Upload(ctx context.Context, i *httpapi.HttpApi, paths []string) []cid.Cid {
var cids []cid.Cid
for _, p := range paths {
c, err := ipfs.UploadAndPin(ctx, i, p)
if err != nil {
log.Printf("error uploading file: %s", err)
continue
}
cids = append(cids, c)
}
return cids
}
func Do() error {
pwd, err := os.Getwd()
if err != nil {
return err
}
dataPath := path.Join(pwd, "data")
err = os.Mkdir(dataPath, 0755)
if err != nil && !os.IsExist(err) {
return err
}
paths := []string{
filepath.Join(dataPath, "test-file-1"),
filepath.Join(dataPath, "test-file-2"),
filepath.Join(dataPath, "test-file-3"),
filepath.Join(dataPath, "test-file-4"),
filepath.Join(dataPath, "test-file-5"),
filepath.Join(dataPath, "test-file-6"),
}
err = generator.NewTestData(paths)
if err != nil {
return err
}
ctx := context.Background()
i, err := ipfs.InitIPFSClient("/ip4/127.0.0.1/tcp/5001")
if err != nil {
return err
}
cids := Upload(ctx, i, paths)
batch, err := ipfs.CreateBatch(ctx, i, cids)
if err != nil {
return err
}
log.Printf("batch cid: %s", batch.String())
size, err := ipfs.ClientDealPieceCID(ctx, i, batch)
if err != nil {
return err
}
log.Printf("piece cid: %s", size.PieceCID)
log.Printf("payload size: %s", bytefmt.ByteSize(uint64(size.PayloadSize)))
log.Printf("piece size: %s", bytefmt.ByteSize(uint64(size.PieceSize)))
return nil
}