-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathproject.ts
295 lines (212 loc) · 6.9 KB
/
project.ts
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/* eslint-disable */
import {BucketInfo, ObjectInfo} from "./types.js";
import bindings = require("bindings");
const uplink = bindings("uplink");
//
import {DownloadResultStruct} from "./download.js";
const errorhandle = require("./error.js");
import {UploadResultStruct} from "./upload.js";
//
//
export class ProjectResultStruct {
project: any;
// Project handle
constructor (project: any) {
this.project = project;
}
/*
* Function closes the project and all associated resources.
* Input : None
* Output : None
*/
async close (): Promise<void> {
await uplink.close_project(this.project).catch((error: any) => {
errorhandle.storjException(
error.error.code,
error.error.message
);
});
}
/*
* Function starts download to the specified key.
* Iutput : Bucket Name (String) , ObjectPath (String) and Download Options (Object)
* Onput : Download (Object)
*/
async downloadObject (bucketName: string, uploadPath: string, downloadOptions: Record<string, any>): Promise<DownloadResultStruct> {
const download = await uplink.download_object(
this.project,
bucketName,
uploadPath,
downloadOptions
).catch((error: any) => {
errorhandle.storjException(
error.error.code,
error.error.message
);
}),
downloadResultReturn = new DownloadResultStruct(download.download);
return downloadResultReturn;
}
/*
* Function starts an upload to the specified key.
* Iutput : Bucket Name (String) , ObjectPath (String) and Download Options (Object)
* Onput : Upload (Object)
*/
async uploadObject (bucketName: string, uploadPath: string, uploadOptions: Record<string, any>): Promise<UploadResultStruct> {
const upload = await uplink.upload_object(
this.project,
bucketName,
uploadPath,
uploadOptions
).catch((error: any) => {
errorhandle.storjException(
error.error.code,
error.error.message
);
}),
uploadResultReturn = new UploadResultStruct(upload.upload);
return uploadResultReturn;
}
/*
* Function returns a list of objects with all its information.
* Input : BucketName (String) , ListObjectOptions (Object)
* Output : ObjectList (Object)
*/
async listObjects (bucketName: string, listObjectsOptions: Record<string, any>) {
const objectlist = await uplink.list_objects(
this.project,
bucketName,
listObjectsOptions
).catch((error: any) => {
errorhandle.storjException(
error.error.code,
error.error.message
);
});
return objectlist;
}
/*
* Function deletes the object at the specific key.
* Input : BucketName (String) , ObjectName (String)
* Output : ObjectInfo (Object)
*/
async deleteObject (bucketName: string, uploadPath: string): Promise<ObjectInfo> {
const objectinfo = await uplink.delete_object(
this.project,
bucketName,
uploadPath
).catch((error: any) => {
errorhandle.storjException(
error.error.code,
error.error.message
);
});
return objectinfo;
}
/*
* Function returns information about an object at the specific key.
* Input : BucketName (String) , ObjectName (String)
* Output : ObjectInfo (Object)
*/
async statObject (bucketName: string, uploadPath: string): Promise<ObjectInfo> {
const objectinfo = await uplink.stat_object(
this.project,
bucketName,
uploadPath
).catch((error: any) => {
errorhandle.storjException(
error.error.code,
error.error.message
);
});
return objectinfo;
}
/*
* Function returns information about a bucket.
* Input : BucketName (String)
* Output : BucketInfo (Object)
*/
async statBucket (bucketName: string): Promise<BucketInfo> {
const bucketInfo = await uplink.stat_bucket(
this.project,
bucketName
).catch((error: any) => {
errorhandle.storjException(
error.error.code,
error.error.message
);
});
return bucketInfo;
}
/*
* Function creates a new bucket.
* Input : BucketName (String)
* Output : BucketInfo (Object)
*/
async createBucket (bucketName: string): Promise<BucketInfo> {
const bucketInfo = await uplink.create_bucket(
this.project,
bucketName
).catch((error: any) => {
errorhandle.storjException(
error.error.code,
error.error.message
);
});
return bucketInfo;
}
/*
* Function ensures that a bucket exists or creates a new one.
* When bucket already exists it returns a valid Bucket and no error
* Input : BucketName (String)
* Output : BucketInfo (Object)
*/
async ensureBucket (bucketName: string): Promise<BucketInfo> {
const bucketInfo = await uplink.ensure_bucket(
this.project,
bucketName
).catch((error: any) => {
errorhandle.storjException(
error.error.code,
error.error.message
);
});
return bucketInfo;
}
/*
* Function returns a list of buckets with all its information.
* Input : ListBucketOptions (Object)
* Output : List of Bucket Info (Object)
*/
async listBuckets (listBucketsOptions: Record<string, any>) {
const bucketListResult = await uplink.list_buckets(
this.project,
listBucketsOptions
).catch((error: any) => {
errorhandle.storjException(
error.error.code,
error.error.message
);
});
return bucketListResult;
}
/*
* Function deletes a bucket.
* When bucket is not empty it throws BucketNotEmptyError exception.
* Input : BucketName (String)
* Output : BucketInfo (Object)
*/
async deleteBucket (bucketName: string): Promise<BucketInfo> {
const bucketInfo = await uplink.delete_bucket(
this.project,
bucketName
).catch((error: any) => {
errorhandle.storjException(
error.error.code,
error.error.message
);
});
return bucketInfo;
}
}
/* eslint-enable */