@or-sdk/files 0.20.1-createfilespackage.0 → 0.20.1-createfilespackage.12

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/dist/cjs/Files.js CHANGED
@@ -14,6 +14,17 @@ var __extends = (this && this.__extends) || (function () {
14
14
  d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15
15
  };
16
16
  })();
17
+ var __assign = (this && this.__assign) || function () {
18
+ __assign = Object.assign || function(t) {
19
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
20
+ s = arguments[i];
21
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
22
+ t[p] = s[p];
23
+ }
24
+ return t;
25
+ };
26
+ return __assign.apply(this, arguments);
27
+ };
17
28
  var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
18
29
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
19
30
  return new (P || (P = Promise))(function (resolve, reject) {
@@ -59,18 +70,21 @@ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
59
70
  }
60
71
  return to.concat(ar || Array.prototype.slice.call(from));
61
72
  };
73
+ var __importDefault = (this && this.__importDefault) || function (mod) {
74
+ return (mod && mod.__esModule) ? mod : { "default": mod };
75
+ };
62
76
  Object.defineProperty(exports, "__esModule", { value: true });
63
77
  exports.Files = void 0;
78
+ var axios_1 = __importDefault(require("axios"));
64
79
  var base_1 = require("@or-sdk/base");
65
80
  var constants_1 = require("./constants");
66
81
  var Files = (function (_super) {
67
82
  __extends(Files, _super);
68
83
  function Files(params) {
69
- var token = params.token, discoveryUrl = params.discoveryUrl, accountId = params.accountId, serviceUrl = params.serviceUrl;
84
+ var token = params.token, discoveryUrl = params.discoveryUrl, serviceUrl = params.serviceUrl;
70
85
  return _super.call(this, {
71
86
  token: token,
72
87
  discoveryUrl: discoveryUrl,
73
- accountId: accountId,
74
88
  serviceUrl: serviceUrl,
75
89
  serviceKey: constants_1.SERVICE_KEY,
76
90
  }) || this;
@@ -81,7 +95,7 @@ var Files = (function (_super) {
81
95
  return __generator(this, function (_a) {
82
96
  switch (_a.label) {
83
97
  case 0: return [4, this.callApi({
84
- method: 'GET',
98
+ method: 'get',
85
99
  route: 'api/folder',
86
100
  params: { key: key },
87
101
  })];
@@ -92,7 +106,7 @@ var Files = (function (_super) {
92
106
  });
93
107
  });
94
108
  };
95
- Files.prototype.getItemsList = function (isPublic, prefix) {
109
+ Files.prototype.getItemsList = function (isPublic, treePrefix) {
96
110
  return __awaiter(this, void 0, void 0, function () {
97
111
  var nextToken, totalItems, queryParams, _a, items, nextTokenResponse;
98
112
  return __generator(this, function (_b) {
@@ -102,10 +116,17 @@ var Files = (function (_super) {
102
116
  totalItems = [];
103
117
  _b.label = 1;
104
118
  case 1:
105
- queryParams = { isPublic: isPublic, prefix: prefix };
119
+ queryParams = {
120
+ isPublic: isPublic,
121
+ prefix: treePrefix,
122
+ };
106
123
  if (nextToken)
107
124
  queryParams.nextToken = nextToken;
108
- return [4, this.callApi({ method: 'GET', route: 'api/files', params: queryParams })];
125
+ return [4, this.callApi({
126
+ method: 'GET',
127
+ route: 'api/files',
128
+ params: queryParams,
129
+ })];
109
130
  case 2:
110
131
  _a = _b.sent(), items = _a.items, nextTokenResponse = _a.nextTokenResponse;
111
132
  totalItems = __spreadArray(__spreadArray([], totalItems, true), items, true);
@@ -121,20 +142,185 @@ var Files = (function (_super) {
121
142
  };
122
143
  Files.prototype.createFolder = function (folderName) {
123
144
  return __awaiter(this, void 0, void 0, function () {
124
- var e_1;
125
145
  return __generator(this, function (_a) {
126
146
  switch (_a.label) {
127
- case 0:
128
- _a.trys.push([0, 2, , 3]);
129
- return [4, this.callApi({ method: 'post', route: 'api/files', data: { key: folderName + '/' } })];
147
+ case 0: return [4, this.callApi({
148
+ method: 'post',
149
+ route: 'api/files',
150
+ data: { key: folderName + '/' },
151
+ })];
130
152
  case 1:
131
153
  _a.sent();
132
- return [3, 3];
133
- case 2:
134
- e_1 = _a.sent();
135
- console.log(e_1);
136
- return [3, 3];
137
- case 3: return [2];
154
+ return [2];
155
+ }
156
+ });
157
+ });
158
+ };
159
+ Files.prototype.renameFile = function (key, newKey, isPublic) {
160
+ return __awaiter(this, void 0, void 0, function () {
161
+ return __generator(this, function (_a) {
162
+ switch (_a.label) {
163
+ case 0: return [4, this.callApi({
164
+ method: 'put',
165
+ route: 'api/files',
166
+ params: {
167
+ key: key,
168
+ isPublic: isPublic,
169
+ },
170
+ data: { newKey: newKey },
171
+ })];
172
+ case 1:
173
+ _a.sent();
174
+ return [2];
175
+ }
176
+ });
177
+ });
178
+ };
179
+ Files.prototype.duplicateFile = function (key, newKey, isPublic) {
180
+ return __awaiter(this, void 0, void 0, function () {
181
+ return __generator(this, function (_a) {
182
+ switch (_a.label) {
183
+ case 0: return [4, this.callApi({
184
+ method: 'put',
185
+ route: 'api/files/duplicate',
186
+ params: {
187
+ key: key,
188
+ isPublic: isPublic,
189
+ },
190
+ data: { newKey: newKey },
191
+ })];
192
+ case 1:
193
+ _a.sent();
194
+ return [2];
195
+ }
196
+ });
197
+ });
198
+ };
199
+ Files.prototype.changePrivacy = function (key, newPrivacy, isPublic) {
200
+ return __awaiter(this, void 0, void 0, function () {
201
+ return __generator(this, function (_a) {
202
+ switch (_a.label) {
203
+ case 0: return [4, this.callApi({
204
+ method: 'patch',
205
+ route: 'api/files',
206
+ params: {
207
+ key: key,
208
+ isPublic: isPublic,
209
+ },
210
+ data: { newPrivacy: newPrivacy },
211
+ })];
212
+ case 1:
213
+ _a.sent();
214
+ return [2];
215
+ }
216
+ });
217
+ });
218
+ };
219
+ Files.prototype.deleteFile = function (key, isPublic) {
220
+ return __awaiter(this, void 0, void 0, function () {
221
+ return __generator(this, function (_a) {
222
+ switch (_a.label) {
223
+ case 0: return [4, this.callApi({
224
+ method: 'delete',
225
+ route: 'api/files',
226
+ params: {
227
+ key: key,
228
+ isPublic: isPublic,
229
+ },
230
+ })];
231
+ case 1:
232
+ _a.sent();
233
+ return [2];
234
+ }
235
+ });
236
+ });
237
+ };
238
+ Files.prototype.deleteFolder = function (key) {
239
+ return __awaiter(this, void 0, void 0, function () {
240
+ return __generator(this, function (_a) {
241
+ switch (_a.label) {
242
+ case 0: return [4, this.callApi({
243
+ method: 'delete',
244
+ route: 'api/folder',
245
+ data: { key: key },
246
+ })];
247
+ case 1:
248
+ _a.sent();
249
+ return [2];
250
+ }
251
+ });
252
+ });
253
+ };
254
+ Files.prototype.getDownloadUrl = function (key, isPublic) {
255
+ return __awaiter(this, void 0, void 0, function () {
256
+ return __generator(this, function (_a) {
257
+ switch (_a.label) {
258
+ case 0: return [4, this.callApi({
259
+ method: 'get',
260
+ route: 'api/url',
261
+ params: {
262
+ key: key,
263
+ isPublic: isPublic,
264
+ },
265
+ })];
266
+ case 1: return [2, _a.sent()];
267
+ }
268
+ });
269
+ });
270
+ };
271
+ Files.prototype.getObjectInfo = function (key, isPublic) {
272
+ return __awaiter(this, void 0, void 0, function () {
273
+ return __generator(this, function (_a) {
274
+ switch (_a.label) {
275
+ case 0: return [4, this.callApi({
276
+ method: 'get',
277
+ route: 'api/file',
278
+ params: {
279
+ key: key,
280
+ isPublic: isPublic,
281
+ },
282
+ })];
283
+ case 1: return [2, _a.sent()];
284
+ }
285
+ });
286
+ });
287
+ };
288
+ Files.prototype.getUploadUrl = function (data, isPublic) {
289
+ return this.callApi({
290
+ method: 'post',
291
+ route: 'api/url',
292
+ params: {
293
+ isPublic: isPublic,
294
+ prefix: '',
295
+ },
296
+ data: __assign({}, data),
297
+ });
298
+ };
299
+ Files.prototype.uploadFile = function (file) {
300
+ return __awaiter(this, void 0, void 0, function () {
301
+ var _a, type, name, contentType, _b, url, fields, formData;
302
+ return __generator(this, function (_c) {
303
+ switch (_c.label) {
304
+ case 0:
305
+ _a = file.fileModel, type = _a.type, name = _a.name;
306
+ contentType = type || 'binary/octet-stream';
307
+ return [4, this.getUploadUrl({
308
+ contentType: contentType,
309
+ key: file.prefix + name,
310
+ cacheControl: 'no-cache',
311
+ }, file.isPublic)];
312
+ case 1:
313
+ _b = _c.sent(), url = _b.url, fields = _b.fields;
314
+ formData = new FormData();
315
+ Object.entries(fields).forEach(function (value) {
316
+ formData.append(value[0], value[1]);
317
+ });
318
+ formData.append('cache-control', 'no-cache');
319
+ formData.append('content-type', contentType);
320
+ formData.append('File', file.fileModel, name);
321
+ return [2, axios_1.default.post(url, formData, {
322
+ onUploadProgress: function (event) { return file.progress(event, name); },
323
+ })];
138
324
  }
139
325
  });
140
326
  });
@@ -1 +1 @@
1
- {"version":3,"file":"Files.js","sourceRoot":"","sources":["../../src/Files.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,qCAAoC;AAEpC,yCAA0C;AAS1C;IAA2B,yBAAI;IAO7B,eAAY,MAAmB;QACrB,IAAA,KAAK,GAA0C,MAAM,MAAhD,EAAE,YAAY,GAA4B,MAAM,aAAlC,EAAE,SAAS,GAAiB,MAAM,UAAvB,EAAE,UAAU,GAAK,MAAM,WAAX,CAAY;eAE9D,kBAAM;YACJ,KAAK,OAAA;YACL,YAAY,cAAA;YACZ,SAAS,WAAA;YACT,UAAU,YAAA;YACV,UAAU,EAAE,uBAAW;SACxB,CAAC;IACJ,CAAC;IAMK,6BAAa,GAAnB,UAAqB,GAAW;;;;;4BACb,WAAM,IAAI,CAAC,OAAO,CAAC;4BAClC,MAAM,EAAE,KAAK;4BACb,KAAK,EAAE,YAAY;4BACnB,MAAM,EAAE,EAAE,GAAG,KAAA,EAAE;yBAChB,CAAC,EAAA;;wBAJM,IAAI,GAAK,CAAA,SAIf,CAAA,KAJU;wBAMZ,WAAO,IAAI,EAAA;;;;KACZ;IACK,4BAAY,GAAlB,UAAoB,QAAiB,EAAE,MAAc;;;;;;wBAC/C,SAAS,GAAkB,IAAI,CAAC;wBAChC,UAAU,GAAe,EAAE,CAAC;;;wBAGx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YAAA;wBAMb,QAAQ,GAAG,IAAI,QAAQ,EAAE,CAAC;wBAEhC,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,OAAO,CAAC,UAAA,KAAK;4BAGlC,QAAQ,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC;wBACtC,CAAC,CAAC,CAAC;wBAEH,QAAQ,CAAC,MAAM,CAAC,eAAe,EAAE,UAAU,CAAC,CAAC;wBAC7C,QAAQ,CAAC,MAAM,CAAC,cAAc,EAAE,WAAW,CAAC,CAAC;wBAC7C,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,IAAI,CAAC,SAAS,EAAE,IAAI,CAAC,CAAC;wBAE9C,WAAO,eAAK,CAAC,IAAI,CAAC,GAAG,EAAE,QAAQ,EAAE;gCAC/B,gBAAgB,EAAE,UAAA,KAAK,IAAI,OAAA,IAAI,CAAC,QAAQ,CAAC,KAAK,EAAE,IAAI,CAAC,EAA1B,CAA0B;6BACtD,CAAC,EAAC;;;;KACJ;IACH,YAAC;AAAD,CAAC,AAhPD,CAA2B,WAAI,GAgP9B;AAhPY,sBAAK"}
package/dist/esm/Files.js CHANGED
@@ -7,15 +7,15 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
7
7
  step((generator = generator.apply(thisArg, _arguments || [])).next());
8
8
  });
9
9
  };
10
+ import axios from 'axios';
10
11
  import { Base } from '@or-sdk/base';
11
12
  import { SERVICE_KEY } from './constants';
12
13
  export class Files extends Base {
13
14
  constructor(params) {
14
- const { token, discoveryUrl, accountId, serviceUrl } = params;
15
+ const { token, discoveryUrl, serviceUrl } = params;
15
16
  super({
16
17
  token,
17
18
  discoveryUrl,
18
- accountId,
19
19
  serviceUrl,
20
20
  serviceKey: SERVICE_KEY,
21
21
  });
@@ -23,22 +23,29 @@ export class Files extends Base {
23
23
  getFolderSize(key) {
24
24
  return __awaiter(this, void 0, void 0, function* () {
25
25
  const { size } = yield this.callApi({
26
- method: 'GET',
26
+ method: 'get',
27
27
  route: 'api/folder',
28
28
  params: { key },
29
29
  });
30
30
  return size;
31
31
  });
32
32
  }
33
- getItemsList(isPublic, prefix) {
33
+ getItemsList(isPublic, treePrefix) {
34
34
  return __awaiter(this, void 0, void 0, function* () {
35
35
  let nextToken = null;
36
36
  let totalItems = [];
37
37
  do {
38
- const queryParams = { isPublic, prefix };
38
+ const queryParams = {
39
+ isPublic,
40
+ prefix: treePrefix,
41
+ };
39
42
  if (nextToken)
40
43
  queryParams.nextToken = nextToken;
41
- const { items, nextTokenResponse } = yield this.callApi({ method: 'GET', route: 'api/files', params: queryParams });
44
+ const { items, nextTokenResponse } = yield this.callApi({
45
+ method: 'GET',
46
+ route: 'api/files',
47
+ params: queryParams,
48
+ });
42
49
  totalItems = [...totalItems, ...items];
43
50
  nextToken = nextTokenResponse;
44
51
  } while (nextToken);
@@ -47,12 +54,127 @@ export class Files extends Base {
47
54
  }
48
55
  createFolder(folderName) {
49
56
  return __awaiter(this, void 0, void 0, function* () {
50
- try {
51
- yield this.callApi({ method: 'post', route: 'api/files', data: { key: folderName + '/' } });
52
- }
53
- catch (e) {
54
- console.log(e);
55
- }
57
+ yield this.callApi({
58
+ method: 'post',
59
+ route: 'api/files',
60
+ data: { key: folderName + '/' },
61
+ });
62
+ });
63
+ }
64
+ renameFile(key, newKey, isPublic) {
65
+ return __awaiter(this, void 0, void 0, function* () {
66
+ yield this.callApi({
67
+ method: 'put',
68
+ route: 'api/files',
69
+ params: {
70
+ key,
71
+ isPublic,
72
+ },
73
+ data: { newKey },
74
+ });
75
+ });
76
+ }
77
+ duplicateFile(key, newKey, isPublic) {
78
+ return __awaiter(this, void 0, void 0, function* () {
79
+ yield this.callApi({
80
+ method: 'put',
81
+ route: 'api/files/duplicate',
82
+ params: {
83
+ key,
84
+ isPublic,
85
+ },
86
+ data: { newKey },
87
+ });
88
+ });
89
+ }
90
+ changePrivacy(key, newPrivacy, isPublic) {
91
+ return __awaiter(this, void 0, void 0, function* () {
92
+ yield this.callApi({
93
+ method: 'patch',
94
+ route: 'api/files',
95
+ params: {
96
+ key,
97
+ isPublic,
98
+ },
99
+ data: { newPrivacy },
100
+ });
101
+ });
102
+ }
103
+ deleteFile(key, isPublic) {
104
+ return __awaiter(this, void 0, void 0, function* () {
105
+ yield this.callApi({
106
+ method: 'delete',
107
+ route: 'api/files',
108
+ params: {
109
+ key,
110
+ isPublic,
111
+ },
112
+ });
113
+ });
114
+ }
115
+ deleteFolder(key) {
116
+ return __awaiter(this, void 0, void 0, function* () {
117
+ yield this.callApi({
118
+ method: 'delete',
119
+ route: 'api/folder',
120
+ data: { key },
121
+ });
122
+ });
123
+ }
124
+ getDownloadUrl(key, isPublic) {
125
+ return __awaiter(this, void 0, void 0, function* () {
126
+ return yield this.callApi({
127
+ method: 'get',
128
+ route: 'api/url',
129
+ params: {
130
+ key,
131
+ isPublic,
132
+ },
133
+ });
134
+ });
135
+ }
136
+ getObjectInfo(key, isPublic) {
137
+ return __awaiter(this, void 0, void 0, function* () {
138
+ return yield this.callApi({
139
+ method: 'get',
140
+ route: 'api/file',
141
+ params: {
142
+ key,
143
+ isPublic,
144
+ },
145
+ });
146
+ });
147
+ }
148
+ getUploadUrl(data, isPublic) {
149
+ return this.callApi({
150
+ method: 'post',
151
+ route: 'api/url',
152
+ params: {
153
+ isPublic,
154
+ prefix: '',
155
+ },
156
+ data: Object.assign({}, data),
157
+ });
158
+ }
159
+ uploadFile(file) {
160
+ return __awaiter(this, void 0, void 0, function* () {
161
+ const { type, name } = file.fileModel;
162
+ const contentType = type || 'binary/octet-stream';
163
+ const { url, fields } = yield this.getUploadUrl({
164
+ contentType,
165
+ key: file.prefix + name,
166
+ cacheControl: 'no-cache',
167
+ }, file.isPublic);
168
+ const formData = new FormData();
169
+ Object.entries(fields).forEach(value => {
170
+ formData.append(value[0], value[1]);
171
+ });
172
+ formData.append('cache-control', 'no-cache');
173
+ formData.append('content-type', contentType);
174
+ formData.append('File', file.fileModel, name);
175
+ return axios.post(url, formData, {
176
+ onUploadProgress: event => file.progress(event, name),
177
+ });
56
178
  });
57
179
  }
58
180
  }
@@ -1 +1 @@
1
- {"version":3,"file":"Files.js","sourceRoot":"","sources":["../../src/Files.ts"],"names":[],"mappings":";;;;;;;;;AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,cAAc,CAAC;AAEpC,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAS1C,MAAM,OAAO,KAAM,SAAQ,IAAI;IAO7B,YAAY,MAAmB;QAC7B,MAAM,EAAE,KAAK,EAAE,YAAY,EAAE,SAAS,EAAE,UAAU,EAAE,GAAG,MAAM,CAAC;QAE9D,KAAK,CAAC;YACJ,KAAK;YACL,YAAY;YACZ,SAAS;YACT,UAAU;YACV,UAAU,EAAE,WAAW;SACxB,CAAC,CAAC;IACL,CAAC;IAMK,aAAa,CAAE,GAAW;;YAC9B,MAAM,EAAE,IAAI,EAAE,GAAG,MAAM,IAAI,CAAC,OAAO,CAAC;gBAClC,MAAM,EAAE,KAAK;gBACb,KAAK,EAAE,YAAY;gBACnB,MAAM,EAAE,EAAE,GAAG,EAAE;aAChB,CAAC,CAAA;YAEF,OAAO,IAAI,CAAA;QACb,CAAC;KAAA;IACK,YAAY,CAAE,QAAiB,EAAE,MAAc;;YACnD,IAAI,SAAS,GAAkB,IAAI,CAAC;YACpC,IAAI,UAAU,GAAe,EAAE,CAAC;YAEhC,GAAG;gBACD,MAAM,WAAW,GAAY,EAAE,QAAQ,EAAE,MAAM,EAAE,CAAC;gBAClD,IAAI,SAAS;oBAAE,WAAW,CAAC,SAAS,GAAG,SAAS,CAAC;gBAEjD,MAAM,EAAC,KAAK,EAAE,iBAAiB,EAAC,GAAyB,MAAM,IAAI,CAAC,OAAO,CAAC,EAAC,MAAM,EAAE,KAAK,EAAE,KAAK,EAAE,WAAW,EAAE,MAAM,EAAE,WAAW,EAAC,CAAC,CAAC;gBACtI,UAAU,GAAG,C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Item, UploadFileProps, HeadData } from './types';
3
3
  export declare class Files extends Base {
4
4
  constructor(params: FilesConfig);
5
5
  getFolderSize(key: string): Promise<number>;
6
- getItemsList(isPublic: boolean, prefix: string): Promise<FileItem[]>;
6
+ getItemsList(isPublic: boolean, treePrefix: string): Promise<FileItem[]>;
7
7
  createFolder(folderName: string): Promise<void>;
8
+ renameFile(key: string, newKey: string, isPublic: boolean): Promise<void>;
9
+ duplicateFile(key: string, newKey: string, isPublic: boolean): Promise<void>;
10
+ changePrivacy(key: string, newPrivacy: 'private' | 'public', isPublic: boolean): Promise<void>;
11
+ deleteFile(key: string, isPublic: boolean): Promise<void>;
12
+ deleteFolder(key: string): Promise<void>;
13
+ getDownloadUrl(key: string, isPublic: boolean): Promise<string>;
14
+ getObjectInfo(key: string, isPublic: boolean): Promise<HeadData>;
15
+ private getUploadUrl;
16
+ uploadFile(file: UploadFileProps): Promise<void>;
8
17
  }
@@ -5,7 +5,7 @@ export declare type FilesConfig = {
5
5
  accountId?: string;
6
6
  serviceUrl?: string;
7
7
  };
8
- export declare type LsQuery = {
8
+ export declare type GetItemsQuery = {
9
9
  isPublic: boolean;
10
10
  prefix: string;
11
11
  nextToken?: string;
@@ -20,9 +20,41 @@ export declare type FilesPageState = {
20
20
  discoveryUrl: string;
21
21
  serviceUrl: string;
22
22
  accountId: string;
23
+ renameFrom: string;
24
+ renameTo: string;
23
25
  items: FileItem[];
24
26
  };
25
- export declare type getItemsListResponse = {
27
+ export declare type HeadData = {
28
+ contentLength: number;
29
+ contentType: string;
30
+ metaData: object;
31
+ };
32
+ export declare type GetItemsListResponse = {
26
33
  items: FileItem[];
27
34
  nextTokenResponse: string | null;
28
35
  };
36
+ export declare type UploadUrlProps = {
37
+ key: string;
38
+ contentType: string;
39
+ maxFileSize?: number;
40
+ cacheControl: string;
41
+ };
42
+ export declare type UploadUrlResponse = {
43
+ url: string;
44
+ fields: {
45
+ key: string;
46
+ bucket: string;
47
+ Policy: string;
48
+ 'X-Amz-Date': string;
49
+ 'X-Amz-Signature': string;
50
+ 'X-Amz-Algorithm': string;
51
+ 'X-Amz-Credential': string;
52
+ 'X-Amz-Security-Token': string;
53
+ }[];
54
+ };
55
+ export declare type UploadFileProps = {
56
+ fileModel: File;
57
+ prefix: string;
58
+ isPublic: boolean;
59
+ progress: Function;
60
+ };
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "0.20.1-createfilespackage.0",
2
+ "version": "0.20.1-createfilespackage.12",
3
3
  "name": "@or-sdk/files",
4
4
  "main": "dist/cjs/index.js",
5
5
  "module": "dist/esm/index.js",
@@ -23,7 +23,8 @@
23
23
  "access": "public"
24
24
  },
25
25
  "dependencies": {
26
- "@or-sdk/base": "^0.20.1-createfilespackage.0"
26
+ "@or-sdk/base": "^0.20.1-createfilespackage.12",
27
+ "axios": "^0.24.0"
27
28
  },
28
- "gitHead": "64b4356c7b747020f554242be7c3885b2f651ad3"
29
+ "gitHead": "8a3bbdc7dc6531f428cda28632723231d62319da"
29
30
  }
package/src/Files.ts CHANGED
@@ -1,6 +1,7 @@
1
+ import axios from 'axios';
1
2
  import { Base } from '@or-sdk/base';
2
- import { FilesConfig, LsQuery, FileItem, getItemsListResponse } from './types';
3
3
  import { SERVICE_KEY } from './constants';
4
+ import { FilesConfig, GetItemsQuery, FileItem, GetItemsListResponse, UploadUrlProps, UploadFileProps, UploadUrlResponse, HeadData } from './types';
4
5
 
5
6
  /**
6
7
  * OneReach Files service client
@@ -17,12 +18,11 @@ export class Files extends Base {
17
18
  * ```
18
19
  */
19
20
  constructor(params: FilesConfig) {
20
- const { token, discoveryUrl, accountId, serviceUrl } = params;
21
+ const { token, discoveryUrl, serviceUrl } = params;
21
22
 
22
23
  super({
23
24
  token,
24
25
  discoveryUrl,
25
- accountId,
26
26
  serviceUrl,
27
27
  serviceKey: SERVICE_KEY,
28
28
  });
@@ -31,25 +31,40 @@ export class Files extends Base {
31
31
  /**
32
32
  * Get folder size
33
33
  * @param key the name of folder
34
+ * @return total folder size in bytes
34
35
  */
35
- async getFolderSize (key: string): Promise<number> {
36
+ async getFolderSize(key: string): Promise<number> {
36
37
  const { size } = await this.callApi({
37
- method: 'GET',
38
+ method: 'get',
38
39
  route: 'api/folder',
39
40
  params: { key },
40
- })
41
+ });
41
42
 
42
- return size
43
+ return size;
43
44
  }
44
- async getItemsList (isPublic: boolean, prefix: string): Promise<FileItem[]> {
45
+
46
+ /**
47
+ * Get list of public/private FileItems from api
48
+ * @param isPublic does this file public or private
49
+ * @param treePrefix the folder path where GET FileItems, for Root use ''
50
+ * @return list of FileItems
51
+ */
52
+ async getItemsList(isPublic: boolean, treePrefix: string): Promise<FileItem[]> {
45
53
  let nextToken: null | string = null;
46
54
  let totalItems: FileItem[] = [];
47
55
 
48
56
  do {
49
- const queryParams :LsQuery = { isPublic, prefix };
57
+ const queryParams: GetItemsQuery = {
58
+ isPublic,
59
+ prefix: treePrefix,
60
+ };
50
61
  if (nextToken) queryParams.nextToken = nextToken;
51
62
 
52
- const {items, nextTokenResponse}: getItemsListResponse = await this.callApi({method: 'GET', route: 'api/files', params: queryParams});
63
+ const { items, nextTokenResponse }: GetItemsListResponse = await this.callApi({
64
+ method: 'GET',
65
+ route: 'api/files',
66
+ params: queryParams,
67
+ });
53
68
  totalItems = [...totalItems, ...items];
54
69
 
55
70
  nextToken = nextTokenResponse;
@@ -58,11 +73,181 @@ export class Files extends Base {
58
73
  return totalItems;
59
74
  }
60
75
 
61
- async createFolder (folderName: string) {
62
- try {
63
- await this.callApi({method: 'post', route: 'api/files', data: { key : folderName + '/' } })
64
- } catch (e) {
65
- console.log(e)
66
- }
76
+ /**
77
+ * Create new folders
78
+ * @param folderName should have folder path + folder name, example: /main/New folder
79
+ */
80
+ async createFolder(folderName: string): Promise<void> {
81
+ await this.callApi({
82
+ method: 'post',
83
+ route: 'api/files',
84
+ data: { key: folderName + '/' },
85
+ });
86
+ }
87
+
88
+ /**
89
+ * Rename exist file
90
+ * @param key old file name
91
+ * @param newKey new file name
92
+ * @param isPublic does this file public or private
93
+ */
94
+ async renameFile(key: string, newKey: string, isPublic: boolean): Promise<void> {
95
+ await this.callApi({
96
+ method: 'put',
97
+ route: 'api/files',
98
+ params: {
99
+ key,
100
+ isPublic,
101
+ },
102
+ data: { newKey },
103
+ });
104
+ }
105
+
106
+ /**
107
+ * Duplicate file
108
+ * @param key target file name
109
+ * @param newKey new file name
110
+ * @param isPublic does this file public or private
111
+ */
112
+ async duplicateFile(key: string, newKey: string, isPublic: boolean): Promise<void> {
113
+ await this.callApi({
114
+ method: 'put',
115
+ route: 'api/files/duplicate',
116
+ params: {
117
+ key,
118
+ isPublic,
119
+ },
120
+ data: { newKey },
121
+ });
122
+ }
123
+
124
+ /**
125
+ * Change file privacy
126
+ * @param key file name
127
+ * @param newPrivacy new file privacy
128
+ * @param isPublic does this file public or private
129
+ */
130
+ async changePrivacy(key: string, newPrivacy: 'private' | 'public', isPublic: boolean): Promise<void> {
131
+ await this.callApi({
132
+ method: 'patch',
133
+ route: 'api/files',
134
+ params: {
135
+ key,
136
+ isPublic,
137
+ },
138
+ data: { newPrivacy },
139
+ });
140
+ }
141
+
142
+ /**
143
+ * Delete file
144
+ * @param key file name
145
+ * @param isPublic does this file public or private
146
+ */
147
+ async deleteFile(key: string, isPublic: boolean): Promise<void> {
148
+ // TODO: add error handler and status response true
149
+ await this.callApi({
150
+ method: 'delete',
151
+ route: 'api/files',
152
+ params: {
153
+ key,
154
+ isPublic,
155
+ },
156
+ });
157
+ }
158
+
159
+ /**
160
+ * Delete folder
161
+ * @param key folder name
162
+ */
163
+ async deleteFolder(key: string): Promise<void> {
164
+ // TODO: add error handler and status response true
165
+ await this.callApi({
166
+ method: 'delete',
167
+ route: 'api/folder',
168
+ data: { key },
169
+ });
170
+ }
171
+
172
+ /**
173
+ * Get a link to download file, example: for preview
174
+ * @param key folder name
175
+ * @param isPublic does this file public or private
176
+ * @return file url
177
+ */
178
+ async getDownloadUrl(key: string, isPublic: boolean): Promise<string> {
179
+ return await this.callApi({
180
+ method: 'get',
181
+ route: 'api/url',
182
+ params: {
183
+ key,
184
+ isPublic,
185
+ },
186
+ });
187
+ }
188
+
189
+ /**
190
+ * Get object info
191
+ * @param key folder name
192
+ * @param isPublic does this file public or private
193
+ */
194
+ async getObjectInfo(key: string, isPublic: boolean): Promise<HeadData> {
195
+ return await this.callApi({
196
+ method: 'get',
197
+ route: 'api/file',
198
+ params: {
199
+ key,
200
+ isPublic,
201
+ },
202
+ });
203
+ }
204
+
205
+ /**
206
+ * Get a link for uploading specific file, JUST FOR INNER USE
207
+ * @param data has UploadUrlProps values inside
208
+ * @param isPublic does this file public or private
209
+ * @return uploading Url with different header Fields
210
+ */
211
+ private getUploadUrl(data: UploadUrlProps, isPublic: boolean): Promise<UploadUrlResponse> {
212
+ return this.callApi({
213
+ method: 'post',
214
+ route: 'api/url',
215
+ params: {
216
+ isPublic,
217
+ prefix: '',
218
+ },
219
+ data: { ...data },
220
+ });
221
+ }
222
+
223
+ /**
224
+ * Upload file to S3 bucket
225
+ * @param file file itself for uploading with prefix (path) and isPublic boolean
226
+ */
227
+ async uploadFile(file: UploadFileProps): Promise<void> {
228
+ const { type, name } = file.fileModel;
229
+ const contentType = type || 'binary/octet-stream';
230
+
231
+ const { url, fields } = await this.getUploadUrl({
232
+ contentType,
233
+ key: file.prefix + name,
234
+ cacheControl: 'no-cache',
235
+ }, file.isPublic);
236
+
237
+ const formData = new FormData();
238
+
239
+ Object.entries(fields).forEach(value => {
240
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
241
+ // @ts-ignore
242
+ formData.append(value[0], value[1]);
243
+ });
244
+
245
+ formData.append('cache-control', 'no-cache');
246
+ formData.append('content-type', contentType);
247
+ formData.append('File', file.fileModel, name);
248
+
249
+ return axios.post(url, formData, {
250
+ onUploadProgress: event => file.progress(event, name),
251
+ });
67
252
  }
68
253
  }
package/src/types.ts CHANGED
@@ -19,26 +19,65 @@ export type FilesConfig = {
19
19
  serviceUrl?: string;
20
20
  };
21
21
 
22
- export type LsQuery = {
23
- isPublic: boolean,
24
- prefix: string,
25
- nextToken?: string
26
- }
22
+ export type GetItemsQuery = {
23
+ isPublic: boolean;
24
+ prefix: string;
25
+ nextToken?: string;
26
+ };
27
27
 
28
28
  export type FileItem = {
29
- key: string,
30
- size?: number
31
- }
29
+ key: string;
30
+ size?: number;
31
+ };
32
32
 
33
33
  export type FilesPageState = {
34
- filesInstance: any,
35
- userToken: string,
36
- discoveryUrl: string,
37
- serviceUrl: string,
38
- accountId: string,
39
- items: FileItem[]
40
- }
41
- export type getItemsListResponse = {
42
- items: FileItem[],
43
- nextTokenResponse: string | null
44
- }
34
+ filesInstance: any;
35
+ userToken: string;
36
+ discoveryUrl: string;
37
+ serviceUrl: string;
38
+ accountId: string;
39
+ renameFrom: string;
40
+ renameTo: string;
41
+ items: FileItem[];
42
+ };
43
+
44
+ export type HeadData = {
45
+ contentLength: number;
46
+ contentType: string;
47
+ metaData: object;
48
+ };
49
+
50
+ export type GetItemsListResponse = {
51
+ items: FileItem[];
52
+ nextTokenResponse: string | null;
53
+ };
54
+
55
+ export type UploadUrlProps = {
56
+ key: string;
57
+ contentType: string;
58
+ maxFileSize?: number;
59
+ cacheControl: string;
60
+ };
61
+
62
+ export type UploadUrlResponse = {
63
+ url: string;
64
+ fields: {
65
+ key: string;
66
+ bucket: string;
67
+ Policy: string;
68
+ 'X-Amz-Date': string;
69
+ 'X-Amz-Signature': string;
70
+ 'X-Amz-Algorithm': string;
71
+ 'X-Amz-Credential': string;
72
+ 'X-Amz-Security-Token': string;
73
+ }[];
74
+ };
75
+
76
+ export type UploadFileProps = {
77
+ fileModel: File;
78
+ prefix: string;
79
+ isPublic: boolean;
80
+ // eslint-disable-next-line @typescript-eslint/ban-types
81
+ progress: Function;
82
+ };
83
+