@tweedegolf/sab-adapter-backblaze-b2 3.0.0 → 3.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/changelog.md +13 -0
- package/dist/AbstractAdapter.d.ts +20 -75
- package/dist/AbstractAdapter.js +337 -259
- package/dist/AbstractAdapter.js.map +1 -1
- package/dist/AdapterBackblazeB2.d.ts +2 -2
- package/dist/AdapterBackblazeB2.js +461 -406
- package/dist/AdapterBackblazeB2.js.map +1 -1
- package/dist/types/backblaze-b2.d.ts +182 -0
- package/dist/types/general.d.ts +23 -78
- package/dist/types/result.d.ts +9 -9
- package/dist/util.d.ts +2 -1
- package/dist/util.js +28 -22
- package/dist/util.js.map +1 -1
- package/package.json +1 -1
|
@@ -1,4 +1,20 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __asyncValues = (this && this.__asyncValues) || function (o) {
|
|
12
|
+
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
|
|
13
|
+
var m = o[Symbol.asyncIterator], i;
|
|
14
|
+
return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
|
|
15
|
+
function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
|
|
16
|
+
function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
|
|
17
|
+
};
|
|
2
18
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
19
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
20
|
};
|
|
@@ -13,21 +29,25 @@ class AdapterBackblazeB2 extends AbstractAdapter_1.AbstractAdapter {
|
|
|
13
29
|
super(config);
|
|
14
30
|
this._provider = general_1.Provider.B2;
|
|
15
31
|
this._configError = null;
|
|
16
|
-
this._client = null;
|
|
17
32
|
this.authorized = false;
|
|
18
33
|
this.versioning = false;
|
|
19
34
|
if (typeof config !== "string") {
|
|
20
|
-
this._config = {
|
|
35
|
+
this._config = Object.assign({}, config);
|
|
21
36
|
}
|
|
22
37
|
else {
|
|
23
38
|
const { value, error } = (0, util_1.parseUrl)(config);
|
|
24
|
-
if (
|
|
39
|
+
if (value === null) {
|
|
25
40
|
this._configError = `[configError] ${error}`;
|
|
26
41
|
}
|
|
27
42
|
else {
|
|
28
43
|
const { protocol: type, username: applicationKeyId, password: applicationKey, host: bucketName, searchParams, } = value;
|
|
44
|
+
if (applicationKey === null || applicationKeyId === null) {
|
|
45
|
+
this._configError =
|
|
46
|
+
'Please provide both a value for "applicationKey" and "applicationKeyId"';
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
29
49
|
if (searchParams !== null) {
|
|
30
|
-
this._config = { type, applicationKeyId, applicationKey,
|
|
50
|
+
this._config = Object.assign({ type, applicationKeyId, applicationKey }, searchParams);
|
|
31
51
|
}
|
|
32
52
|
else {
|
|
33
53
|
this._config = { type, applicationKeyId, applicationKey };
|
|
@@ -38,460 +58,495 @@ class AdapterBackblazeB2 extends AbstractAdapter_1.AbstractAdapter {
|
|
|
38
58
|
}
|
|
39
59
|
// console.log(this._config);
|
|
40
60
|
}
|
|
41
|
-
|
|
42
|
-
this.
|
|
61
|
+
try {
|
|
62
|
+
this._client = new backblaze_b2_1.default(this._config);
|
|
43
63
|
}
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
this._client = new backblaze_b2_1.default(this._config);
|
|
47
|
-
}
|
|
48
|
-
catch (e) {
|
|
49
|
-
this._configError = `[configError] ${e.message}`;
|
|
50
|
-
}
|
|
64
|
+
catch (e) {
|
|
65
|
+
this._configError = `[configError] ${(0, util_1.getErrorMessage)(e)}`;
|
|
51
66
|
}
|
|
52
67
|
if (typeof this.config.bucketName !== "undefined") {
|
|
53
68
|
this._bucketName = this.config.bucketName;
|
|
54
69
|
}
|
|
55
70
|
}
|
|
56
71
|
// util members
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
72
|
+
authorize() {
|
|
73
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
74
|
+
if (this.authorized) {
|
|
75
|
+
return { value: "ok", error: null };
|
|
76
|
+
}
|
|
77
|
+
try {
|
|
78
|
+
const { data: _data } = yield this._client.authorize();
|
|
79
|
+
this.authorized = true;
|
|
80
|
+
return { value: "ok", error: null };
|
|
81
|
+
}
|
|
82
|
+
catch (e) {
|
|
83
|
+
return { value: null, error: (0, util_1.getErrorMessage)(e) };
|
|
84
|
+
}
|
|
85
|
+
});
|
|
69
86
|
}
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
87
|
+
getBucket(name) {
|
|
88
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
89
|
+
try {
|
|
90
|
+
const { data } = yield this._client.getBucket({ bucketName: name });
|
|
91
|
+
if (data.buckets.length > 0) {
|
|
92
|
+
const { bucketId, bucketName } = data.buckets[0];
|
|
93
|
+
return { value: { id: bucketId, name: bucketName }, error: null };
|
|
94
|
+
}
|
|
95
|
+
return { value: null, error: `Could not find bucket "${name}"` };
|
|
76
96
|
}
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
}
|
|
97
|
+
catch (e) {
|
|
98
|
+
return { value: null, error: (0, util_1.getErrorMessage)(e) };
|
|
99
|
+
}
|
|
100
|
+
});
|
|
82
101
|
}
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
102
|
+
getUploadUrl(bucketId) {
|
|
103
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
104
|
+
try {
|
|
105
|
+
const { data } = yield this._client.getUploadUrl({ bucketId });
|
|
106
|
+
if (typeof data.uploadUrl === "undefined") {
|
|
107
|
+
return { value: null, error: data.message };
|
|
108
|
+
}
|
|
109
|
+
return { value: data, error: null };
|
|
88
110
|
}
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
}
|
|
111
|
+
catch (e) {
|
|
112
|
+
return { value: null, error: (0, util_1.getErrorMessage)(e) };
|
|
113
|
+
}
|
|
114
|
+
});
|
|
94
115
|
}
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
try {
|
|
101
|
-
let data; //eslint-disable-line
|
|
102
|
-
// const options: ListFileVersionsOpts = {
|
|
103
|
-
const options = {
|
|
104
|
-
bucketId: bucket.id,
|
|
105
|
-
maxFileCount: numFiles,
|
|
106
|
-
};
|
|
107
|
-
if (versioning) {
|
|
108
|
-
({ data } = await this._client.listFileVersions(options));
|
|
116
|
+
getFiles(bucketName_1) {
|
|
117
|
+
return __awaiter(this, arguments, void 0, function* (bucketName, versioning = this.versioning, numFiles = 1000) {
|
|
118
|
+
const { value: bucket, error } = yield this.getBucket(bucketName);
|
|
119
|
+
if (error !== null) {
|
|
120
|
+
return { value: null, error };
|
|
109
121
|
}
|
|
110
|
-
|
|
111
|
-
|
|
122
|
+
if (bucket === null) {
|
|
123
|
+
return { value: null, error: `can't find bucket '${bucketName}'` };
|
|
112
124
|
}
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
125
|
+
try {
|
|
126
|
+
let data;
|
|
127
|
+
// const options: ListFileVersionsOpts = {
|
|
128
|
+
const options = {
|
|
129
|
+
bucketId: bucket.id,
|
|
130
|
+
maxFileCount: numFiles,
|
|
131
|
+
};
|
|
132
|
+
if (versioning) {
|
|
133
|
+
({ data } = yield this._client.listFileVersions(options));
|
|
134
|
+
}
|
|
135
|
+
else {
|
|
136
|
+
({ data } = yield this._client.listFileNames(options));
|
|
137
|
+
}
|
|
138
|
+
return {
|
|
139
|
+
value: data.files.map(({ fileId, fileName, contentType, contentLength }) => {
|
|
140
|
+
return {
|
|
141
|
+
id: fileId,
|
|
142
|
+
name: fileName,
|
|
143
|
+
contentType,
|
|
144
|
+
contentLength,
|
|
145
|
+
};
|
|
146
|
+
}),
|
|
147
|
+
error: null,
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
catch (e) {
|
|
151
|
+
return { value: null, error: (0, util_1.getErrorMessage)(e) };
|
|
152
|
+
}
|
|
153
|
+
});
|
|
131
154
|
}
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
for (let i = 0; i < files.length; i++) {
|
|
138
|
-
const file = files[i];
|
|
139
|
-
if (file.name === name) {
|
|
140
|
-
return { value: file, error: null };
|
|
155
|
+
getFile(bucketName, name) {
|
|
156
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
157
|
+
const { value: files, error } = yield this.getFiles(bucketName, false);
|
|
158
|
+
if (error !== null) {
|
|
159
|
+
return { value: null, error };
|
|
141
160
|
}
|
|
142
|
-
|
|
143
|
-
|
|
161
|
+
if (files === null) {
|
|
162
|
+
return { value: null, error: `Could not find file '${name}' in bucket '${bucketName}'.` };
|
|
163
|
+
}
|
|
164
|
+
for (let i = 0; i < files.length; i++) {
|
|
165
|
+
const file = files[i];
|
|
166
|
+
if (file.name === name) {
|
|
167
|
+
return { value: file, error: null };
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
return { value: null, error: `Could not find file '${name}' in bucket '${bucketName}'.` };
|
|
171
|
+
});
|
|
144
172
|
}
|
|
145
173
|
// protected, called by methods of public API via AbstractAdapter
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
const { error } = await this.authorize();
|
|
162
|
-
let bucketType = "allPrivate";
|
|
163
|
-
if (typeof options.bucketType !== "undefined") {
|
|
164
|
-
bucketType = options.bucketType;
|
|
165
|
-
}
|
|
166
|
-
else if (options.public === true) {
|
|
167
|
-
options.bucketType = "allPublic";
|
|
168
|
-
}
|
|
169
|
-
if (bucketType !== "allPrivate" && bucketType !== "allPublic") {
|
|
170
|
-
return {
|
|
171
|
-
value: null, error: `${bucketType} is not valid: bucket type must be either 'allPrivate' or 'allPublic'`
|
|
172
|
-
};
|
|
173
|
-
}
|
|
174
|
-
try {
|
|
175
|
-
const { data } = await this._client.createBucket({
|
|
176
|
-
...options,
|
|
177
|
-
bucketName: name,
|
|
178
|
-
bucketType,
|
|
179
|
-
});
|
|
180
|
-
const { bucketType: _type } = data;
|
|
181
|
-
// console.log(_type);
|
|
182
|
-
return { value: "ok", error: null };
|
|
183
|
-
}
|
|
184
|
-
catch (e) {
|
|
185
|
-
return { value: null, error: e.response.data.message };
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
|
-
async _addFile(params) {
|
|
189
|
-
const { error } = await this.authorize();
|
|
190
|
-
if (error !== null) {
|
|
191
|
-
return { value: null, error };
|
|
192
|
-
}
|
|
193
|
-
const { bucketName, targetPath } = params;
|
|
194
|
-
const data1 = await this.getBucket(bucketName);
|
|
195
|
-
if (data1.error !== null) {
|
|
196
|
-
return { value: null, error: data1.error };
|
|
197
|
-
}
|
|
198
|
-
const { value: { id: bucketId }, } = data1;
|
|
199
|
-
const data2 = await this.getUploadUrl(bucketId);
|
|
200
|
-
if (data2.error !== null) {
|
|
201
|
-
return { value: null, error: data2.error };
|
|
202
|
-
}
|
|
203
|
-
const { value: { uploadUrl, authorizationToken } } = data2;
|
|
204
|
-
let { options } = params;
|
|
205
|
-
if (typeof options === "undefined") {
|
|
206
|
-
options = {};
|
|
207
|
-
}
|
|
208
|
-
try {
|
|
209
|
-
let buffer;
|
|
210
|
-
if (typeof params.buffer !== "undefined") {
|
|
211
|
-
buffer = params.buffer;
|
|
212
|
-
}
|
|
213
|
-
else if (typeof params.stream !== "undefined") {
|
|
214
|
-
const buffers = []; // eslint-disable-line
|
|
215
|
-
for await (const data of params.stream) {
|
|
216
|
-
buffers.push(data);
|
|
217
|
-
}
|
|
218
|
-
buffer = Buffer.concat(buffers);
|
|
219
|
-
}
|
|
220
|
-
const { data: _data } = await this._client.uploadFile({
|
|
221
|
-
uploadUrl,
|
|
222
|
-
uploadAuthToken: authorizationToken,
|
|
223
|
-
fileName: targetPath,
|
|
224
|
-
data: buffer,
|
|
225
|
-
...options,
|
|
226
|
-
});
|
|
227
|
-
// console.log(_data);
|
|
228
|
-
return { value: "ok", error: null };
|
|
229
|
-
}
|
|
230
|
-
catch (e) {
|
|
231
|
-
// console.log(e.toJSON());
|
|
232
|
-
return { value: null, error: e.message };
|
|
233
|
-
}
|
|
174
|
+
_listBuckets() {
|
|
175
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
176
|
+
const { error } = yield this.authorize();
|
|
177
|
+
if (error !== null) {
|
|
178
|
+
return { value: null, error };
|
|
179
|
+
}
|
|
180
|
+
try {
|
|
181
|
+
const { data } = yield this._client.listBuckets();
|
|
182
|
+
const value = data.buckets.map(({ bucketName }) => bucketName);
|
|
183
|
+
return { value, error: null };
|
|
184
|
+
}
|
|
185
|
+
catch (e) {
|
|
186
|
+
return { value: null, error: (0, util_1.getErrorMessage)(e) };
|
|
187
|
+
}
|
|
188
|
+
});
|
|
234
189
|
}
|
|
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
|
-
"Content-Type": file.contentType,
|
|
265
|
-
Range: range,
|
|
266
|
-
},
|
|
267
|
-
...options,
|
|
268
|
-
},
|
|
269
|
-
});
|
|
270
|
-
return { value: data, error: null };
|
|
271
|
-
}
|
|
272
|
-
catch (e) {
|
|
273
|
-
return { value: null, error: e.message };
|
|
274
|
-
}
|
|
190
|
+
_createBucket(name, options) {
|
|
191
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
192
|
+
const { error } = yield this.authorize();
|
|
193
|
+
if (error !== null) {
|
|
194
|
+
return { value: null, error };
|
|
195
|
+
}
|
|
196
|
+
let bucketType = "allPrivate";
|
|
197
|
+
if (options.public === true) {
|
|
198
|
+
options.bucketType = "allPublic";
|
|
199
|
+
}
|
|
200
|
+
if (typeof options.bucketType !== "undefined") {
|
|
201
|
+
bucketType = options.bucketType;
|
|
202
|
+
}
|
|
203
|
+
if (bucketType !== "allPrivate" && bucketType !== "allPublic") {
|
|
204
|
+
return {
|
|
205
|
+
value: null,
|
|
206
|
+
error: `Bucket type '${options.bucketType}' is not valid: must be either 'allPrivate' or 'allPublic'`,
|
|
207
|
+
};
|
|
208
|
+
}
|
|
209
|
+
try {
|
|
210
|
+
const { data } = yield this._client.createBucket(Object.assign(Object.assign({}, options), { bucketName: name, bucketType }));
|
|
211
|
+
const { bucketType: _type } = data;
|
|
212
|
+
// console.log(_type);
|
|
213
|
+
return { value: "ok", error: null };
|
|
214
|
+
}
|
|
215
|
+
catch (e) {
|
|
216
|
+
return { value: null, error: e.response.data.message };
|
|
217
|
+
}
|
|
218
|
+
});
|
|
275
219
|
}
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
220
|
+
_addFile(params) {
|
|
221
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
222
|
+
var _a, e_1, _b, _c;
|
|
223
|
+
const { error } = yield this.authorize();
|
|
279
224
|
if (error !== null) {
|
|
280
225
|
return { value: null, error };
|
|
281
226
|
}
|
|
282
|
-
|
|
283
|
-
|
|
227
|
+
const { bucketName, targetPath } = params;
|
|
228
|
+
const data1 = yield this.getBucket(bucketName);
|
|
229
|
+
if (data1.error !== null) {
|
|
230
|
+
return { value: null, error: data1.error };
|
|
284
231
|
}
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
error
|
|
289
|
-
|
|
232
|
+
const { value: bucket } = data1;
|
|
233
|
+
const { id: bucketId } = bucket;
|
|
234
|
+
const data2 = yield this.getUploadUrl(bucketId);
|
|
235
|
+
if (data2.error !== null) {
|
|
236
|
+
return { value: null, error: data2.error };
|
|
237
|
+
}
|
|
238
|
+
const { value: { uploadUrl, authorizationToken }, } = data2;
|
|
239
|
+
let { options } = params;
|
|
240
|
+
if (typeof options === "undefined") {
|
|
241
|
+
options = {};
|
|
242
|
+
}
|
|
243
|
+
try {
|
|
244
|
+
let buffer;
|
|
245
|
+
if (typeof params.buffer !== "undefined") {
|
|
246
|
+
buffer = params.buffer;
|
|
247
|
+
}
|
|
248
|
+
else if (typeof params.stream !== "undefined") {
|
|
249
|
+
const buffers = []; // eslint-disable-line
|
|
250
|
+
try {
|
|
251
|
+
for (var _d = true, _e = __asyncValues(params.stream), _f; _f = yield _e.next(), _a = _f.done, !_a; _d = true) {
|
|
252
|
+
_c = _f.value;
|
|
253
|
+
_d = false;
|
|
254
|
+
const data = _c;
|
|
255
|
+
buffers.push(data);
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
259
|
+
finally {
|
|
260
|
+
try {
|
|
261
|
+
if (!_d && !_a && (_b = _e.return)) yield _b.call(_e);
|
|
262
|
+
}
|
|
263
|
+
finally { if (e_1) throw e_1.error; }
|
|
264
|
+
}
|
|
265
|
+
buffer = Buffer.concat(buffers);
|
|
266
|
+
}
|
|
267
|
+
if (typeof buffer === "undefined") {
|
|
268
|
+
return { value: null, error: "Could get file buffer" };
|
|
269
|
+
}
|
|
270
|
+
const { data: _data } = yield this._client.uploadFile(Object.assign({ uploadUrl, uploadAuthToken: authorizationToken, fileName: targetPath, data: buffer }, options));
|
|
271
|
+
// console.log(_data);
|
|
272
|
+
return { value: "ok", error: null };
|
|
273
|
+
}
|
|
274
|
+
catch (e) {
|
|
275
|
+
// console.log(e.toJSON());
|
|
276
|
+
return { value: null, error: (0, util_1.getErrorMessage)(e) };
|
|
277
|
+
}
|
|
278
|
+
});
|
|
290
279
|
}
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
const
|
|
280
|
+
_getFileAsStream(bucketName_1, fileName_1) {
|
|
281
|
+
return __awaiter(this, arguments, void 0, function* (bucketName, fileName, options = { start: 0 }) {
|
|
282
|
+
const { error } = yield this.authorize();
|
|
283
|
+
if (error !== null) {
|
|
284
|
+
return { value: null, error };
|
|
285
|
+
}
|
|
286
|
+
const data = yield this.getFile(bucketName, fileName);
|
|
294
287
|
if (data.error !== null) {
|
|
295
288
|
return { value: null, error: data.error };
|
|
296
289
|
}
|
|
297
|
-
const { value:
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
}
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
290
|
+
const { value: file } = data;
|
|
291
|
+
if (file === null) {
|
|
292
|
+
return { value: null, error: `Could not find file '${fileName}' in bucket '${bucketName}'.` };
|
|
293
|
+
}
|
|
294
|
+
const { start, end } = options;
|
|
295
|
+
let range = `bytes=${start}-${end}`;
|
|
296
|
+
if (typeof start === "undefined" && typeof end === "undefined") {
|
|
297
|
+
range = undefined;
|
|
298
|
+
}
|
|
299
|
+
else if (typeof start === "undefined") {
|
|
300
|
+
range = `bytes=0-${end}`;
|
|
301
|
+
}
|
|
302
|
+
else if (typeof end === "undefined") {
|
|
303
|
+
range = `bytes=${start}-`;
|
|
304
|
+
}
|
|
305
|
+
delete options.start;
|
|
306
|
+
delete options.end;
|
|
307
|
+
try {
|
|
308
|
+
const { data } = yield this._client.downloadFileById({
|
|
309
|
+
fileId: file.id,
|
|
310
|
+
responseType: "stream",
|
|
311
|
+
axios: Object.assign({ headers: {
|
|
312
|
+
"Content-Type": file.contentType,
|
|
313
|
+
Range: range,
|
|
314
|
+
} }, options),
|
|
315
|
+
});
|
|
316
|
+
return { value: data, error: null };
|
|
317
|
+
}
|
|
318
|
+
catch (e) {
|
|
319
|
+
return { value: null, error: (0, util_1.getErrorMessage)(e) };
|
|
320
|
+
}
|
|
321
|
+
});
|
|
322
|
+
}
|
|
323
|
+
_getPublicURL(bucketName, fileName, _options) {
|
|
324
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
308
325
|
return {
|
|
309
|
-
value: `${this._client.downloadUrl}/file/${bucketName}/${fileName}
|
|
326
|
+
value: `${this._client.downloadUrl}/file/${bucketName}/${fileName}`,
|
|
310
327
|
error: null,
|
|
311
328
|
};
|
|
312
|
-
}
|
|
313
|
-
catch (e) {
|
|
314
|
-
return {
|
|
315
|
-
value: null,
|
|
316
|
-
error: e,
|
|
317
|
-
};
|
|
318
|
-
}
|
|
329
|
+
});
|
|
319
330
|
}
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
if (error !== null) {
|
|
323
|
-
return { value: null, error };
|
|
324
|
-
}
|
|
325
|
-
const data = await this.getFiles(bucketName, true);
|
|
326
|
-
if (error !== null) {
|
|
327
|
-
return { value: null, error };
|
|
328
|
-
}
|
|
329
|
-
const { value: files } = data;
|
|
330
|
-
const index = files.findIndex(({ name }) => name === fileName);
|
|
331
|
-
if (index === -1) {
|
|
332
|
-
return { value: null, error: `No file '${fileName}' found in bucket '${bucketName}'` };
|
|
333
|
-
}
|
|
334
|
-
if (this.versioning) {
|
|
335
|
-
// delete the file, if the file has more versions, delete the most recent version
|
|
336
|
-
const file = files[index];
|
|
331
|
+
_getSignedURL(bucketName, fileName, options) {
|
|
332
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
337
333
|
try {
|
|
338
|
-
|
|
334
|
+
const data = yield this.getBucket(bucketName);
|
|
335
|
+
if (data.error !== null) {
|
|
336
|
+
return { value: null, error: data.error };
|
|
337
|
+
}
|
|
338
|
+
const { value: bucket } = data;
|
|
339
|
+
const { id: bucketId } = bucket;
|
|
340
|
+
let expiresIn = 300; // 5 * 60
|
|
341
|
+
if (typeof options.expiresIn !== "undefined") {
|
|
342
|
+
expiresIn = Number.parseInt(options.expiresIn, 10);
|
|
343
|
+
}
|
|
344
|
+
const r = yield this._client.getDownloadAuthorization({
|
|
345
|
+
bucketId,
|
|
346
|
+
fileNamePrefix: fileName,
|
|
347
|
+
validDurationInSeconds: expiresIn,
|
|
348
|
+
});
|
|
349
|
+
const { data: { authorizationToken }, } = r;
|
|
350
|
+
return {
|
|
351
|
+
value: `${this._client.downloadUrl}/file/${bucketName}/${fileName}?Authorization=${authorizationToken}`,
|
|
352
|
+
error: null,
|
|
353
|
+
};
|
|
354
|
+
}
|
|
355
|
+
catch (e) {
|
|
356
|
+
return { value: null, error: (0, util_1.getErrorMessage)(e) };
|
|
357
|
+
}
|
|
358
|
+
});
|
|
359
|
+
}
|
|
360
|
+
_removeFile(bucketName, fileName) {
|
|
361
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
362
|
+
const { error } = yield this.authorize();
|
|
363
|
+
if (error !== null) {
|
|
364
|
+
return { value: null, error };
|
|
365
|
+
}
|
|
366
|
+
const data = yield this.getFiles(bucketName, true);
|
|
367
|
+
if (data.value === null) {
|
|
368
|
+
return { value: null, error };
|
|
369
|
+
}
|
|
370
|
+
const { value: files } = data;
|
|
371
|
+
const index = files.findIndex(({ name }) => name === fileName);
|
|
372
|
+
if (index === -1) {
|
|
373
|
+
return { value: null, error: `No file '${fileName}' found in bucket '${bucketName}'` };
|
|
374
|
+
}
|
|
375
|
+
if (this.versioning) {
|
|
376
|
+
// delete the file, if the file has more versions, delete the most recent version
|
|
377
|
+
const file = files[index];
|
|
378
|
+
try {
|
|
379
|
+
yield this._client.deleteFileVersion({
|
|
380
|
+
fileId: file.id,
|
|
381
|
+
fileName: file.name,
|
|
382
|
+
});
|
|
383
|
+
return { value: "ok", error: null };
|
|
384
|
+
}
|
|
385
|
+
catch (e) {
|
|
386
|
+
return { value: null, error: (0, util_1.getErrorMessage)(e) };
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
else {
|
|
390
|
+
// delete all versions of the file
|
|
391
|
+
try {
|
|
392
|
+
yield Promise.all(files
|
|
393
|
+
.filter((f) => f.name === fileName)
|
|
394
|
+
.map(({ id: fileId, name: fileName }) => {
|
|
395
|
+
// console.log(fileName, fileId);
|
|
396
|
+
return this._client.deleteFileVersion({
|
|
397
|
+
fileId,
|
|
398
|
+
fileName,
|
|
399
|
+
});
|
|
400
|
+
}));
|
|
401
|
+
return { value: "ok", error: null };
|
|
402
|
+
}
|
|
403
|
+
catch (e) {
|
|
404
|
+
return { value: null, error: (0, util_1.getErrorMessage)(e) };
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
});
|
|
408
|
+
}
|
|
409
|
+
_clearBucket(name) {
|
|
410
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
411
|
+
const { value, error } = yield this.authorize();
|
|
412
|
+
if (error !== null) {
|
|
413
|
+
return { value: null, error };
|
|
414
|
+
}
|
|
415
|
+
const data = yield this.getFiles(name, true);
|
|
416
|
+
if (data.value === null) {
|
|
417
|
+
return { value: null, error: data.error };
|
|
418
|
+
}
|
|
419
|
+
const { value: files } = data;
|
|
420
|
+
try {
|
|
421
|
+
const _data = yield Promise.all(files.map((file) => this._client.deleteFileVersion({
|
|
339
422
|
fileId: file.id,
|
|
340
423
|
fileName: file.name,
|
|
341
|
-
});
|
|
424
|
+
})));
|
|
425
|
+
// console.log("[clearBucket]", _data);
|
|
342
426
|
return { value: "ok", error: null };
|
|
343
427
|
}
|
|
344
428
|
catch (e) {
|
|
345
|
-
return { value: null, error: e
|
|
429
|
+
return { value: null, error: (0, util_1.getErrorMessage)(e) };
|
|
430
|
+
}
|
|
431
|
+
});
|
|
432
|
+
}
|
|
433
|
+
_deleteBucket(name) {
|
|
434
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
435
|
+
const { error, value: bucket } = yield this.getBucket(name);
|
|
436
|
+
if (bucket === null) {
|
|
437
|
+
return { value: null, error: error };
|
|
346
438
|
}
|
|
347
|
-
}
|
|
348
|
-
else {
|
|
349
|
-
// delete all versions of the file
|
|
350
439
|
try {
|
|
351
|
-
|
|
352
|
-
.filter((f) => f.name === fileName)
|
|
353
|
-
.map(({ id: fileId, name: fileName }) => {
|
|
354
|
-
// console.log(fileName, fileId);
|
|
355
|
-
return this._client.deleteFileVersion({
|
|
356
|
-
fileId,
|
|
357
|
-
fileName,
|
|
358
|
-
});
|
|
359
|
-
}));
|
|
440
|
+
yield this._client.deleteBucket({ bucketId: bucket.id });
|
|
360
441
|
return { value: "ok", error: null };
|
|
361
442
|
}
|
|
362
443
|
catch (e) {
|
|
363
|
-
return { value: null, error: e
|
|
444
|
+
return { value: null, error: (0, util_1.getErrorMessage)(e) };
|
|
364
445
|
}
|
|
365
|
-
}
|
|
366
|
-
}
|
|
367
|
-
async _clearBucket(name) {
|
|
368
|
-
const { value, error } = await this.authorize();
|
|
369
|
-
if (error !== null) {
|
|
370
|
-
return { value: null, error };
|
|
371
|
-
}
|
|
372
|
-
const data = await this.getFiles(name, true);
|
|
373
|
-
if (data.error !== null) {
|
|
374
|
-
return { value: null, error: data.error };
|
|
375
|
-
}
|
|
376
|
-
const { value: files } = data;
|
|
377
|
-
try {
|
|
378
|
-
const _data = await Promise.all(files.map((file) => this._client.deleteFileVersion({
|
|
379
|
-
fileId: file.id,
|
|
380
|
-
fileName: file.name,
|
|
381
|
-
})));
|
|
382
|
-
// console.log("[clearBucket]", _data);
|
|
383
|
-
return { value: "ok", error: null };
|
|
384
|
-
}
|
|
385
|
-
catch (e) {
|
|
386
|
-
return { value: null, error: e.message };
|
|
387
|
-
}
|
|
446
|
+
});
|
|
388
447
|
}
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
return { value: null, error };
|
|
410
|
-
}
|
|
411
|
-
const data = await this.getFiles(bucketName, this.versioning, numFiles);
|
|
412
|
-
if (data.error === null) {
|
|
413
|
-
const { value: files } = data;
|
|
414
|
-
return {
|
|
415
|
-
value: files.map((f) => {
|
|
416
|
-
return [f.name, f.contentLength];
|
|
417
|
-
}),
|
|
418
|
-
error: null,
|
|
419
|
-
};
|
|
420
|
-
}
|
|
421
|
-
else {
|
|
422
|
-
return { value: null, error: data.error };
|
|
423
|
-
}
|
|
448
|
+
_listFiles(bucketName, numFiles) {
|
|
449
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
450
|
+
const { error } = yield this.authorize();
|
|
451
|
+
if (error !== null) {
|
|
452
|
+
return { value: null, error };
|
|
453
|
+
}
|
|
454
|
+
const data = yield this.getFiles(bucketName, this.versioning, numFiles);
|
|
455
|
+
if (data.value !== null) {
|
|
456
|
+
const { value: files } = data;
|
|
457
|
+
return {
|
|
458
|
+
value: files.map((f) => {
|
|
459
|
+
return [f.name, f.contentLength];
|
|
460
|
+
}),
|
|
461
|
+
error: null,
|
|
462
|
+
};
|
|
463
|
+
}
|
|
464
|
+
else {
|
|
465
|
+
return { value: null, error: data.error };
|
|
466
|
+
}
|
|
467
|
+
});
|
|
424
468
|
}
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
469
|
+
_sizeOf(bucketName, fileName) {
|
|
470
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
471
|
+
const { error } = yield this.authorize();
|
|
472
|
+
if (error !== null) {
|
|
473
|
+
return { value: null, error };
|
|
474
|
+
}
|
|
475
|
+
const data = yield this.getFile(bucketName, fileName);
|
|
476
|
+
if (data.value !== null) {
|
|
477
|
+
const { value: file } = data;
|
|
478
|
+
return { value: file.contentLength, error: null };
|
|
479
|
+
}
|
|
480
|
+
else {
|
|
481
|
+
return { value: null, error: data.error };
|
|
482
|
+
}
|
|
483
|
+
});
|
|
438
484
|
}
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
485
|
+
_bucketExists(bucketName) {
|
|
486
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
487
|
+
const { error } = yield this.authorize();
|
|
488
|
+
if (error !== null) {
|
|
489
|
+
return { value: null, error };
|
|
490
|
+
}
|
|
491
|
+
const data = yield this.getBucket(bucketName);
|
|
492
|
+
if (data.error === null) {
|
|
493
|
+
return { value: true, error: null };
|
|
494
|
+
}
|
|
495
|
+
else if (data.error.startsWith("Could not find bucket")) {
|
|
496
|
+
return { value: false, error: null };
|
|
497
|
+
}
|
|
498
|
+
else {
|
|
499
|
+
return { value: null, error: data.error };
|
|
500
|
+
}
|
|
501
|
+
});
|
|
454
502
|
}
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
try {
|
|
461
|
-
const { data } = await this._client.listBuckets();
|
|
462
|
-
const index = data.buckets.findIndex((bucket) => bucket.bucketName === bucketName);
|
|
463
|
-
if (index === -1) {
|
|
464
|
-
return { value: null, error: `Could not find the bucket "${bucketName}"` };
|
|
503
|
+
_bucketIsPublic(bucketName) {
|
|
504
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
505
|
+
const { error } = yield this.authorize();
|
|
506
|
+
if (error !== null) {
|
|
507
|
+
return { value: null, error };
|
|
465
508
|
}
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
509
|
+
try {
|
|
510
|
+
const { data } = yield this._client.listBuckets();
|
|
511
|
+
const index = data.buckets.findIndex((bucket) => bucket.bucketName === bucketName);
|
|
512
|
+
if (index === -1) {
|
|
513
|
+
return { value: null, error: `Could not find the bucket "${bucketName}"` };
|
|
514
|
+
}
|
|
515
|
+
const bucket = data.buckets[index];
|
|
516
|
+
return { value: bucket.bucketType === "allPublic", error: null };
|
|
517
|
+
}
|
|
518
|
+
catch (e) {
|
|
519
|
+
return { value: null, error: (0, util_1.getErrorMessage)(e) };
|
|
520
|
+
}
|
|
521
|
+
});
|
|
472
522
|
}
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
523
|
+
_fileExists(bucketName, fileName) {
|
|
524
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
525
|
+
const { error, value } = yield this._sizeOf(bucketName, fileName);
|
|
526
|
+
if (error === null) {
|
|
527
|
+
return { value: true, error: null };
|
|
528
|
+
}
|
|
529
|
+
else {
|
|
530
|
+
return { value: false, error: null };
|
|
531
|
+
}
|
|
532
|
+
});
|
|
481
533
|
}
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
534
|
+
_getPresignedUploadURL(bucketName, _fileName, _options) {
|
|
535
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
536
|
+
try {
|
|
537
|
+
const data = yield this.getBucket(bucketName);
|
|
538
|
+
if (data.error !== null) {
|
|
539
|
+
return { value: null, error: data.error };
|
|
540
|
+
}
|
|
541
|
+
const { value: bucket } = data;
|
|
542
|
+
const { id: bucketId } = bucket;
|
|
543
|
+
const { value: { uploadUrl, authorizationToken }, } = (yield this.getUploadUrl(bucketId));
|
|
544
|
+
return { value: { url: uploadUrl, authToken: authorizationToken }, error: null };
|
|
487
545
|
}
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
}
|
|
492
|
-
catch (e) {
|
|
493
|
-
return { value: null, error: e.message };
|
|
494
|
-
}
|
|
546
|
+
catch (e) {
|
|
547
|
+
return { value: null, error: (0, util_1.getErrorMessage)(e) };
|
|
548
|
+
}
|
|
549
|
+
});
|
|
495
550
|
}
|
|
496
551
|
// public
|
|
497
552
|
get config() {
|