-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBaseBucket.ts
456 lines (389 loc) · 13.8 KB
/
BaseBucket.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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
import {Callbacks, nbLogger, _promisify, _createError, _errorText, JsonObject} from "./Head";
import {Nebula} from "./Nebula";
import {NebulaService} from "./NebulaService";
import {Acl} from "./Acl";
import {HttpRequest} from "./HttpRequest";
import {ApiRequest} from "./HttpRequest";
import {_SdeRequest} from "./SdeRequest";
import {Promise} from "es6-promise";
/**
* @class バケット基底クラス
* @private
*/
export class BaseBucket {
_service: NebulaService;
_type: string;
_name: string;
_acl: Acl;
_noAcl: boolean;
_contentAcl: Acl;
_description: string;
_mode: number;
/**
* コンストラクタ
* @param service NebulaService
* @param name バケット名
* @param type 種別 ("object" or "file")
* @param mode バケットモード(BUCKET_MODE_*)
*/
constructor(service: NebulaService, name: string, type: string, mode: number = Nebula.BUCKET_MODE_ONLINE) {
this._service = service;
this._type = type;
this._name = name;
this._acl = null;
this._noAcl = null;
this._contentAcl = null;
this._description = null;
if (mode !== Nebula.BUCKET_MODE_ONLINE && mode !== Nebula.BUCKET_MODE_REPLICA && mode !== Nebula.BUCKET_MODE_LOCAL) {
this._mode = Nebula.BUCKET_MODE_ONLINE;
} else {
this._mode = mode;
}
}
/**
* @description バケットの読み込み
* @private
*/
protected static _baseLoadBucket(type: string, service: NebulaService, name: string, mode: number, callbacks: Callbacks): Promise<BaseBucket> {
nbLogger("BaseBucket.loadBucket(), name=" + name + ", type=" + type + ", callbacks=" + callbacks + ", mode=" + mode);
let req: ApiRequest;
if (service.isOffline()) {
req = new _SdeRequest(BaseBucket.getClassName(type), "loadBucket");
const body: JsonObject = {
bucketName: name
};
if (mode !== Nebula.BUCKET_MODE_ONLINE && mode !== Nebula.BUCKET_MODE_REPLICA && mode !== Nebula.BUCKET_MODE_LOCAL) {
mode = Nebula.BUCKET_MODE_ONLINE;
}
body.bucketMode = mode;
req.setData(body);
} else {
const path = "/buckets/" + type + "/" + encodeURIComponent(name);
req = new HttpRequest(service, path);
req.setMethod("GET");
req.setContentType("application/json");
}
const promise = req.execute().then(response => {
try {
nbLogger("BaseBucket.loadBucket(), success : " + response);
const resObj = JSON.parse(response);
const resName = resObj.name;
if (resName == null) {
nbLogger("BaseBucket.loadBucket(), invalid bucket name, name=" + resName);
const error = _createError(0, "Invalid response", "", name);
return Promise.reject(error);
}
nbLogger("BaseBucket.loadBucket(), new Bucket, name=" + resName);
let bucket: BaseBucket;
if (type === "file") {
bucket = new service.FileBucket(resName);
} else {
bucket = new service.ObjectBucket(resName, mode);
}
const resAcl = resObj.ACL;
if (resAcl != null) {
nbLogger("BaseBucket.loadBucket(), acl=" + resAcl);
const acl = new Acl();
acl._set(resAcl);
bucket.setAcl(acl);
}
const resContentAcl = resObj.contentACL;
if (resContentAcl != null) {
nbLogger("BaseBucket.loadBucket(), contentAcl=" + resContentAcl);
const contentAcl = new Acl();
contentAcl._set(resContentAcl);
bucket.setContentAcl(contentAcl);
}
const resDescription = resObj.description;
if (resDescription != null) {
nbLogger("BaseBucket.loadBucket(), description=" + resDescription);
bucket.setDescription(resDescription);
}
return Promise.resolve(bucket);
} catch (e) {
nbLogger("BaseBucket.loadBucket(), error : exception" + e);
const error = _createError(0, e, e.toString(), name);
return Promise.reject(error);
}
}, error => {
nbLogger(("BaseBucket.loadBucket(), error: " + (_errorText(error))));
error.data = name;
return Promise.reject(error);
});
return _promisify(promise, callbacks);
}
/**
* @description バケット一覧の取得
* @private
*/
protected static _baseGetBucketList(type: string, service: NebulaService, local: boolean, callbacks?: Callbacks): Promise<string[]> {
nbLogger("BaseBucket.getBucketList(), type=" + type + ", callbacks=" + callbacks);
let req: ApiRequest;
if (service.isOffline()) {
let method = "getBucketList";
if (local) {
method = "getLocalBucketList";
}
req = new _SdeRequest(BaseBucket.getClassName(type), method);
} else {
const path = "/buckets/" + type;
req = new HttpRequest(service, path);
req.setMethod("GET");
req.setContentType("application/json");
}
const promise = req.execute().then(response => {
try {
nbLogger("BaseBucket.getBucketList(), success : " + response);
const resObj = JSON.parse(response);
const buckets = resObj.results;
if (buckets != null) {
nbLogger("BaseBucket.getBucketList(), buckets=" + buckets);
const bucketNames: string[] = [];
for (const bucket of buckets) {
nbLogger("BaseBucket.getBucketList(), success : add bucketName = " + bucket.name);
if (bucket.name != null) {
bucketNames.push(bucket.name);
} else {
nbLogger("BaseBucket.getBucketList(), [WARNING] No name in response");
}
}
return Promise.resolve(bucketNames);
} else {
nbLogger("BaseBucket.getBucketList(), invalid response");
const error = _createError(0, "Invalid response", "", name);
return Promise.reject(error);
}
} catch (e) {
nbLogger("BaseBucket.getBucketList(), error : exception" + e);
const error = _createError(0, e, e);
return Promise.reject(error);
}
}, error => {
nbLogger(("BaseBucket.getBucketList(), error: " + (_errorText(error))));
return Promise.reject(error);
});
return _promisify(promise, callbacks);
}
/**
* @description バケットの保存
* @private
*/
saveBucket(callbacks?: Callbacks): Promise<BaseBucket> {
nbLogger("BaseBucket.saveBucket(), callbacks=" + callbacks);
let req: ApiRequest;
if (this._service.isOffline()) {
req = new _SdeRequest(BaseBucket.getClassName(this._type), "saveBucket");
} else {
const path = this.getPath();
req = new HttpRequest(this._service, path);
req.setMethod("PUT");
req.setContentType("application/json");
}
const body: JsonObject = {};
if (this._acl != null) {
body.ACL = this._acl._toJsonObject();
}
if (this._contentAcl != null) {
body.contentACL = this._contentAcl._toJsonObject();
}
if (this._description != null) {
body.description = this._description;
}
if (this._service.isOffline()) {
body.bucketName = this.getBucketName();
body.bucketMode = this.getBucketMode();
}
if (this._noAcl != null) {
body.noAcl = this._noAcl;
}
req.setData(body);
nbLogger("BaseBucket.saveBucket(), body=" + JSON.stringify(body));
const promise = req.execute().then(response => {
nbLogger("success : " + response);
nbLogger("bucketName : " + this.getBucketName());
try {
nbLogger("success : " + response);
const resObj = JSON.parse(response);
if (resObj.ACL != null) {
if (!this._acl) {
this._acl = new Acl();
}
this._acl._set(resObj.ACL);
}
if (resObj.contentACL != null) {
if (!this._contentAcl) {
this._contentAcl = new Acl();
}
this._contentAcl._set(resObj.contentACL);
}
return Promise.resolve(this);
} catch (e) {
nbLogger("error : " + e);
return Promise.reject(e);
}
}, err => {
nbLogger(("error: " + (_errorText(err))));
err.data = this;
return Promise.reject(err);
});
return _promisify(promise, callbacks);
}
/**
* @description バケットの削除
* @private
*/
deleteBucket(callbacks?: Callbacks): Promise<BaseBucket> {
nbLogger("BaseBucket.deleteBucket(), callbacks=" + callbacks);
let req: ApiRequest;
if (this._service.isOffline()) {
req = new _SdeRequest(BaseBucket.getClassName(this._type), "deleteBucket");
const body = {
bucketName: this.getBucketName(),
bucketMode: this.getBucketMode()
};
req.setData(body);
} else {
const path = this.getPath();
req = new HttpRequest(this._service, path);
req.setMethod("DELETE");
req.setContentType("application/json");
}
const promise = req.execute().then(() => {
nbLogger("BaseBucket.deleteBucket(), success");
return this;
}, err => {
nbLogger(("BaseBucket.deleteBucket(), error: " + (_errorText(err))));
err.data = this;
return Promise.reject(err);
});
return _promisify(promise, callbacks);
}
/**
* @description ACLの設定
* @private
*/
setAcl(acl: Acl) {
if (acl != null && !(acl instanceof Acl)) {
throw new Error("setAcl: not Acl instance!");
}
this._acl = acl;
}
/**
* @description ACLの取得
* @private
*/
getAcl(): Acl {
return this._acl;
}
/**
* @description ACLレスモードの設定
* @private
*/
setNoAcl(noAcl: boolean) {
this._noAcl = noAcl;
}
/**
* @description ACLレスモードの取得
* @private
*/
isNoAcl(): boolean {
return this._noAcl;
}
/**
* @description コンテンツACLの設定
* @param {@link acl} acl 設定するACL
* @private
*/
setContentAcl(acl: Acl) {
if (acl != null && !(acl instanceof Acl)) {
throw new Error("setContentAcl: not Acl instance!");
}
this._contentAcl = acl;
}
/**
* @description コンテンツACLの取得
* @private
*/
getContentAcl(): Acl {
return this._contentAcl;
}
/**
* @description バケットの説明文の設定
* @private
*/
setDescription(description: string) {
this._description = description;
}
/**
* @description バケットの説明文の取得
* @private
*/
getDescription(): string {
return this._description;
}
/**
* @description バケット名の取得
* @private
*/
getBucketName(): string {
return this._name;
}
/**
* @description バケット名の設定
* @private
*/
setBucketName(name: string) {
this._name = name;
}
/**
* @description バケットモードの取得
* @private
*/
getBucketMode(): number {
return this._mode;
}
/**
* @description バケットに関するREST APIのパスの取得
* @private
*/
getPath(option?: string): string {
let path = "/buckets" + "/" + this._type + "/" + encodeURIComponent(this._name);
if (option != null) {
path = path + option;
}
return path;
}
/**
* @description バケットに含まれるデータ(ファイルまたはオブジェクト)に関するREST APIのパスの取得
* @private
*/
getDataPath(option?: string): string {
let dataType = "objects";
if (this._type === "file") {
dataType = "files";
}
let path = "/" + dataType + "/" + encodeURIComponent(this._name);
if (option != null) {
path = path + option;
}
return path;
}
/**
* @description バケット種別からネイティブクラス名を取得する(オフライン限定)
* @private
*/
protected static getClassName(type?: string): string {
let className = "NebulaObjectBucket";
if (type === "file") {
className = "NebulaFileBucket";
}
return className;
}
/**
* @description バケット種別からネイティブクラス名を取得する(オフライン限定)
* @private
*/
protected getClassName(): string {
return BaseBucket.getClassName(this._type);
}
}