-
Notifications
You must be signed in to change notification settings - Fork 7
/
storage_test.go
229 lines (181 loc) · 5.3 KB
/
storage_test.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package storage
import (
"context"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"testing"
"github.com/Filecoin-Titan/titan-storage-sdk/memfile"
)
var (
apiKey = os.Getenv("API_KEY")
locatorURL = os.Getenv("LOCATOR_URL")
)
func TestCalculateCarCID(t *testing.T) {
f, err := os.Open("./example/main.go")
if err != nil {
t.Fatal(err)
}
cid, err := CalculateCid(f)
if err != nil {
t.Fatal(err)
}
t.Log("cid ", cid.String())
}
func TestCreateCarWithFile(t *testing.T) {
input := "./example/example.exe"
output := "./example/example.car"
root, err := createCar(input, output)
if err != nil {
t.Fatal(err)
}
t.Log("root ", root.String())
}
func TestCreateCarWithStream(t *testing.T) {
f, err := os.Open("./example/example.exe")
if err != nil {
t.Fatal(err)
}
defer f.Close()
mFile := memfile.New([]byte{})
root, err := createCarStream(f, mFile)
if err != nil {
t.Fatal(err)
}
t.Log("root ", root.String())
}
func TestUpload(t *testing.T) {
storage, err := Initialize(&Config{TitanURL: locatorURL, APIKey: apiKey})
if err != nil {
t.Fatal("NewStorage error ", err)
}
progress := func(doneSize int64, totalSize int64) {
t.Logf("upload %d of %d", doneSize, totalSize)
}
filePath := "./"
visitFile := func(fp string, fi os.DirEntry, err error) error {
// Check for and handle errors
if err != nil {
fmt.Println(err) // Can be used to handle errors (e.g., permission denied)
return nil
}
if fi.IsDir() {
return nil
} else {
// This is a file, you can perform file-specific operations here
if strings.HasSuffix(fp, ".go") {
path, err := filepath.Abs(fp)
if err != nil {
t.Fatal(err)
}
_, err = storage.UploadFilesWithPath(context.Background(), path, progress, true)
if err != nil {
t.Log("upload file failed ", err.Error())
return nil
}
t.Logf("totalSize %s success", fp)
}
}
return nil
}
err = filepath.WalkDir(filePath, visitFile)
if err != nil {
t.Fatal("WalkDir ", err)
}
}
func TestUploadStream(t *testing.T) {
storage, err := Initialize(&Config{TitanURL: locatorURL, APIKey: apiKey})
if err != nil {
t.Fatal("NewStorage error ", err)
}
progress := func(doneSize int64, totalSize int64) {
t.Logf("upload %d of %d", doneSize, totalSize)
}
filePath := "./storage_test.go"
f, err := os.Open(filePath)
if err != nil {
t.Fatal(err)
}
cid, err := storage.UploadStream(context.Background(), f, f.Name(), progress)
if err != nil {
t.Fatal("upload file failed ", err.Error())
}
t.Logf("totalSize %s success, cid %s", filePath, cid.String())
}
func TestGetFile(t *testing.T) {
s, err := Initialize(&Config{TitanURL: locatorURL, APIKey: apiKey})
if err != nil {
t.Fatal("NewStorage error ", err)
}
// storageObject := s.(*storage)
// t.Log("candidate node ", storageObject.candidateID)
// progress := func(doneSize int64, totalSize int64) {
// t.Logf("upload %d of %d", doneSize, totalSize)
// }
// filePath := "./storage_test.go"
// f, err := os.Open(filePath)
// if err != nil {
// t.Fatal(err)
// }
// cid, err := s.UploadStream(context.Background(), f, f.Name(), progress)
// if err != nil {
// t.Fatal("upload file failed ", err.Error())
// }
// res, err := s.GetURL(context.Background(), cid.String())
// if err != nil {
// t.Fatal("get url ", err)
// }
// t.Log("url:", res.URLs)
reader, fn, err := s.GetFileWithCid(context.Background(), "bafybeibhwmjstnkv3pvlswwcgs2as24bvlbr5tvba6mthtgf77mw4uown4")
if err != nil {
t.Fatal("get url ", err)
}
defer reader.Close()
data, err := io.ReadAll(reader)
if err != nil {
t.Fatal("get url ", err)
}
newFilePath := fmt.Sprintf("./example/%s", fn)
newFile, err := os.Create(newFilePath)
if err != nil {
t.Fatal("Create file", err)
}
defer newFile.Close()
newFile.Write(data)
t.Logf("write file %s %d", fn, len(data))
}
func TestUploadFileWithURL(t *testing.T) {
s, err := Initialize(&Config{TitanURL: locatorURL, APIKey: apiKey})
if err != nil {
t.Fatal("NewStorage error ", err)
}
storageObject := s.(*storage)
t.Log("candidate ", storageObject.candidateID)
url := "https://files.oaiusercontent.com/file-JRBvuyBZDh7g6Garcgd9HQLl?se=2023-11-07T10%3A43%3A54Z&sp=r&sv=2021-08-06&sr=b&rscc=max-age%3D31536000%2C%20immutable&rscd=attachment%3B%20filename%3D27c485be-ad30-4b24-80ca-4d0e9ccfef8d.webp&sig=nzo0wiDe/a3oVAT5JPHLTP%2B7WdMouvVrmCyYkHSVUBE%3D"
name, err := getFileNameFromURL(url)
if err != nil {
t.Fatal(err)
}
t.Log("name:", name)
cid, newURL, err := s.UploadFileWithURL(context.Background(), url, nil)
if err != nil {
t.Fatal(err)
}
t.Logf("cid %s, newURL %s", cid, newURL)
}
func TestListAsset(t *testing.T) {
s, err := Initialize(&Config{TitanURL: locatorURL, APIKey: apiKey})
if err != nil {
t.Fatal("NewStorage error ", err)
}
rsp, err := s.ListUserAssets(context.Background(), 0, 20, 1)
if err != nil {
t.Fatal("ListUserAssets ", err)
}
t.Logf("total assets %d, len:%d", rsp.Total, len(rsp.AssetOverviews))
for _, asset := range rsp.AssetOverviews {
t.Logf("cid:%s name:%s size:%d", asset.AssetRecord.CID, asset.UserAssetDetail.AssetName, asset.AssetRecord.TotalSize)
}
}