-
Notifications
You must be signed in to change notification settings - Fork 0
/
pinata.go
179 lines (140 loc) · 4.25 KB
/
pinata.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
package pinata
import (
"encoding/json"
"fmt"
"sync"
)
type Method string
type Url string
const (
DELETE Method = "DELETE"
GET Method = "GET"
POST Method = "POST"
PUT Method = "PUT"
)
const (
PINFILE Url = "https://api.pinata.cloud/pinning/pinFileToIPFS"
PINJSON Url = "https://api.pinata.cloud/pinning/pinJSONToIPFS"
PINBYCID Url = "https://api.pinata.cloud/pinning/pinByHash"
LISTPIN Url = "https://api.pinata.cloud/pinning/pinJobs"
UPDATEMETA Url = "https://api.pinata.cloud/pinning/hashMetadata"
UNPIN Url = "https://api.pinata.cloud/pinning/unpin/"
DATAUSAGE Url = "https://api.pinata.cloud/data/userPinnedDataTotal"
QUERYFILES Url = "https://api.pinata.cloud/data/pinList"
)
type Pinata struct {
authentication string
pinataOptions *PinataOptions
Loading bool
}
type PinataOptions struct {
CidVersion int8 `json:"cidVersion"`
WrapWithDirectory bool `json:"wrapWithDirectory"`
}
type PinataMetadata struct {
Name string `json:"name"`
KeyValues *map[string]string `json:"keyvalues"`
}
type PinataRegion struct {
CurrentReplicationCount int64 `json:"currentReplicationCount"`
DesiredReplicationCount int64 `json:"desiredReplicationCount"`
RegionId string `json:"regionId"`
}
type PinataRow struct {
Id string `json:"id"`
IpfsPinHash string `json:"ipfs_pin_hash"`
Size int64 `json:"size"`
UserId string `json:"user_id"`
DatePinned string `json:"date_pinned"`
DateUnpinned string `json:"date_unpinned"`
MetaData PinataMetadata `json:"metadata"`
Regions []PinataRegion `json:"regions"`
}
type PinataBody struct {
Count int64 `json:"count"`
Rows []PinataRow `json:"rows"`
}
type PinataUsageData struct {
PinCount int `json:"pin_count"`
PinSizeTotal int `json:"pin_size_total"`
PinSizeWithReplicationsTotal int `json:"pin_size_with_replications_total"`
}
func CreatePinata(auth string, cidVersion int8, wrapWithDirectory bool) *Pinata {
return &Pinata{
authentication: auth,
pinataOptions: &PinataOptions{CidVersion: cidVersion, WrapWithDirectory: wrapWithDirectory},
Loading: false,
}
}
func (pinata *Pinata) PinFile(fileLoc string, name string, keyvalues *map[string]string) string {
_, err := pinata.uploadPinFile(fileLoc, name, keyvalues)
if err != nil {
return "Error Pin File"
}
return "Successful Pin File"
}
func (pinata *Pinata) PinJSON(pinataOptions string, pinataMetaData PinataMetadata, pinataContent interface{}) string {
_, err := pinata.uploadJson(pinataOptions, pinataMetaData, pinataContent)
if err != nil {
return "Error Pin JSON"
}
return "Successful Pin JSON"
}
func (pinata *Pinata) PinByCID(cid string, pinataMetaData PinataMetadata) string {
_, err := pinata.pinByCid(cid, pinataMetaData)
if err != nil {
return "Error Pin By CID"
}
return "Successful Pin By CID"
}
func (pinata *Pinata) ListPinByCID() string {
body, err := pinata.listPinByCid()
if err != nil {
return err.Error()
}
return string(body)
}
func (pinata *Pinata) UpdateMetaData(cid string, name string, keyValues *map[string]string) string {
_, err := pinata.updateMetadata(cid, name, keyValues)
if err != nil {
return "Error update metadata"
}
return "Successful update metadata"
}
func (pinata *Pinata) RemoveByHash(hash string) {
pinata.removeFile(hash)
fmt.Println("Finished remove file with hash " + hash)
}
func (pinata *Pinata) RemoveFiles(rows []PinataRow) {
pinata.Loading = true
var wg sync.WaitGroup
wg.Add(len(rows))
for _, row := range rows {
go func(hash string) {
defer wg.Done()
pinata.removeFile(hash)
fmt.Println("Remove file with hash: " + hash)
}(row.IpfsPinHash)
}
wg.Wait()
fmt.Println("Finished remove files")
pinata.Loading = false
}
func (pinata *Pinata) DataUsage() PinataUsageData {
bodyPinataUsage := &PinataUsageData{}
body, err := pinata.statUsage()
if err != nil {
fmt.Println("Error")
}
json.Unmarshal(body, bodyPinataUsage)
return *bodyPinataUsage
}
func (pinata *Pinata) QueryFiles(query string) PinataBody {
bodyPinata := &PinataBody{}
body, err := pinata.queryPinata(&query)
if err != nil {
fmt.Println("Error")
}
json.Unmarshal(body, bodyPinata)
return *bodyPinata
}