/
main.go
105 lines (88 loc) · 2.86 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
//nolint:deadcode,errcheck,unused
package main
import (
"context"
"log"
"os"
"syscall"
"time"
pinner "github.com/covalenthq/ipfs-pinner"
"github.com/covalenthq/ipfs-pinner/core"
client "github.com/covalenthq/ipfs-pinner/pinclient"
"github.com/ipfs/go-cid"
)
var WEB3_JWT = "WEB3_JWT"
var UPLOAD_FILE = "./main.go" // uploading current file itself
func main() {
ctx := context.Background()
clientCreateReq := client.NewClientRequest(core.Web3Storage)
// check if cid compute true works with car uploads
nodeCreateReq := pinner.NewNodeRequest(clientCreateReq, []string{"https://w3s.link/ipfs/%s"}, false).CidVersion(1).CidComputeOnly(false)
node := pinner.NewPinnerNode(ctx, *nodeCreateReq)
//upload(ctx, node)
core.Version()
download(ctx, node)
}
func upload(ctx context.Context, node pinner.PinnerNode) {
file, err := os.Open(UPLOAD_FILE)
if err != nil {
log.Fatalf("%v", err)
}
fcid, err := node.UnixfsService().GenerateDag(ctx, file)
if err != nil {
log.Fatalf("%v", err)
}
carf, err := os.CreateTemp(os.TempDir(), "*.car")
if err != nil {
log.Fatalf("%v", err)
}
err = syscall.Unlink(carf.Name())
if err != nil {
log.Fatalf("%v", err)
}
log.Printf("car file location: %s\n", carf.Name())
err = node.CarExporter().Export(ctx, fcid, carf)
if err != nil {
carf.Close()
log.Fatalf("%v", err)
}
carf.Seek(0, 0) // reset for read
ccid, err := node.PinService().UploadFile(ctx, carf)
if err != nil {
log.Fatalf("%v", err)
}
carf.Close() // should delete the file due to unlink
assertEquals(fcid, ccid)
log.Printf("the two cids match: %s\n", ccid.String())
log.Printf("removing dag...")
curr := time.Now().UnixMilli()
err = node.UnixfsService().RemoveDag(ctx, ccid)
after := time.Now().UnixMilli()
log.Println("time taken:", after-curr)
if err != nil {
log.Fatalf("%v", err)
}
}
func download(ctx context.Context, node pinner.PinnerNode) {
//ccid, err := cid.Parse("bafybeifzst7cbujrqemiulznrkttouzshnqkrajiib5fp5te53ojs5sl5u") // file encapsulated in folder
//ccid, err := cid.Parse("QmeFd8e4UaAPrPnwxBWcpqY3tMpggpWB3XYqftMpyYYLWZ") // straight up file
//ccid, err := cid.Parse("bafybeifzst7cbujrqemiulznrkttouzshnqkrajiib5fp5te53ojs5sl5u")
//ccid, err := cid.Parse("QmZrxsDZwrCKbcJLNf1D6GaX2fobHZpesBc3DwhVBLQ33p")
//ccid, err := cid.Parse("bafkreic7xhzqyube57gex7okhzytg7i5eq6fvz5snpte7swy547s22bs5q")
ccid, err := cid.Parse("bafybeic7nbudupk56j2ixdrczddzmuu3qtmlyrpqjxuy4jkaeeaminxq3e") // took about 12-15 minutes
if err != nil {
log.Fatalf("%v", err)
}
log.Printf("searching for pid: %v\n", ccid)
contents, err := node.UnixfsService().Get(ctx, ccid)
if err != nil {
log.Fatalf("%v", err)
}
// print the first 20 characters to prevent polluting the terminal.
log.Println(string(contents)[:20])
}
func assertEquals(obj1 interface{}, obj2 interface{}) {
if obj1 != obj2 {
log.Fatalf("fail %v and %v doesn't match", obj1, obj2)
}
}