@cloudbase/storage 2.0.3-alpha.0 → 2.5.0-beta.0

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/src/index.ts CHANGED
@@ -1,6 +1,6 @@
1
- import { constants, utils, helpers } from '@cloudbase/utilities';
2
- import { ICloudbase } from '@cloudbase/types';
3
- import { ICloudbaseComponent } from '@cloudbase/types/component';
1
+ import { constants, utils, helpers } from '@cloudbase/utilities'
2
+ import { ICloudbase } from '@cloudbase/types'
3
+ import { ICloudbaseComponent } from '@cloudbase/types/component'
4
4
  import {
5
5
  ICloudbaseFileMetaDataRes,
6
6
  ICloudbaseFileInfo,
@@ -12,27 +12,27 @@ import {
12
12
  ICloudbaseGetTempFileURLResult,
13
13
  ICloudbaseGetTempFileURLParams,
14
14
  ICloudbaseDownloadFileResult,
15
- ICloudbaseDownloadFileParams
16
- } from '@cloudbase/types/storage';
15
+ ICloudbaseDownloadFileParams,
16
+ } from '@cloudbase/types/storage'
17
17
 
18
- declare const cloudbase: ICloudbase;
18
+ declare const cloudbase: ICloudbase
19
19
 
20
20
  enum EUploadMethod {
21
21
  put = 'put',
22
22
  post = 'post'
23
23
  }
24
24
 
25
- const { getSdkName, ERRORS, COMMUNITY_SITE_URL } = constants;
26
- const { isArray, isString, isPalinObject, execCallback } = utils;
27
- const { catchErrorsDecorator } = helpers;
25
+ const { getSdkName, ERRORS, COMMUNITY_SITE_URL } = constants
26
+ const { isArray, isString, isPalinObject, execCallback } = utils
27
+ const { catchErrorsDecorator } = helpers
28
28
 
29
- const COMPONENT_NAME = 'storage';
29
+ const COMPONENT_NAME = 'storage'
30
30
 
31
31
  class CloudbaseStorage {
32
32
  @catchErrorsDecorator({
33
33
  customInfo: {
34
34
  className: 'Cloudbase',
35
- methodName: 'uploadFile'
35
+ methodName: 'uploadFile',
36
36
  },
37
37
  title: '上传文件失败',
38
38
  messages: [
@@ -40,45 +40,46 @@ class CloudbaseStorage {
40
40
  ' 1 - 调用 uploadFile() 的语法或参数是否正确',
41
41
  ' 2 - 当前域名是否在安全域名列表中:https://console.cloud.tencent.com/tcb/env/safety',
42
42
  ' 3 - 云存储安全规则是否限制了当前登录状态访问',
43
- `如果问题依然存在,建议到官方问答社区提问或寻找帮助:${COMMUNITY_SITE_URL}`
44
- ]
43
+ `如果问题依然存在,建议到官方问答社区提问或寻找帮助:${COMMUNITY_SITE_URL}`,
44
+ ],
45
45
  })
46
46
  public async uploadFile(
47
47
  params: ICloudbaseUploadFileParams,
48
48
  callback?: Function
49
49
  ): Promise<ICloudbaseUploadFileResult> {
50
- const { cloudPath, filePath, onUploadProgress, method = 'post', headers = {} } = params;
50
+ const { cloudPath, filePath, onUploadProgress, method = 'put', headers = {} } = params
51
51
  if (!isString(cloudPath) || !filePath) {
52
52
  throw new Error(JSON.stringify({
53
53
  code: ERRORS.INVALID_PARAMS,
54
- msg: `[${COMPONENT_NAME}.uploadFile] invalid params`
55
- }));
54
+ msg: `[${COMPONENT_NAME}.uploadFile] invalid params`,
55
+ }))
56
56
  }
57
- const uploadMethod = { put: EUploadMethod.put, post: EUploadMethod.post }[method.toLocaleLowerCase()] || EUploadMethod.post;
57
+ const uploadMethod = { put: EUploadMethod.put, post: EUploadMethod.post }[method.toLocaleLowerCase()] || EUploadMethod.put
58
58
 
59
- const action = 'storage.getUploadMetadata';
59
+ const action = 'storage.getUploadMetadata'
60
60
  // @ts-ignore
61
- const request = this.request;
61
+ const { request } = this
62
62
  const metaDataParam = {
63
63
  path: cloudPath,
64
64
  method: uploadMethod,
65
65
  }
66
66
  if (uploadMethod === EUploadMethod.put) {
67
- metaDataParam["headers"] = headers;
67
+ /* eslint-disable-next-line */
68
+ metaDataParam["headers"] = headers
68
69
  }
69
- const metaData: ICloudbaseFileMetaDataRes = await request.send(action, metaDataParam);
70
+ const metaData: ICloudbaseFileMetaDataRes = await request.send(action, metaDataParam)
70
71
 
71
72
  const {
72
- data: { url, authorization, token, fileId, cosFileId, download_url },
73
- requestId
74
- } = metaData;
73
+ data: { url, authorization, token, fileId, cosFileId, download_url: downloadUrl },
74
+ requestId,
75
+ } = metaData
75
76
 
76
77
  const commonParams = {
77
78
  url,
78
79
  file: filePath,
79
80
  name: cloudPath,
80
- onUploadProgress
81
- };
81
+ onUploadProgress,
82
+ }
82
83
 
83
84
  const putParams = {
84
85
  ...commonParams,
@@ -88,47 +89,47 @@ class CloudbaseStorage {
88
89
  authorization,
89
90
  'x-cos-meta-fileid': cosFileId,
90
91
  'x-cos-security-token': token,
91
- }
92
- };
92
+ },
93
+ }
93
94
 
94
95
  const postParams = {
95
96
  ...commonParams,
97
+ method: EUploadMethod.post,
96
98
  data: {
97
99
  key: cloudPath,
98
100
  signature: authorization,
99
101
  'x-cos-meta-fileid': cosFileId,
100
- 'success_action_status': '201',
101
- 'x-cos-security-token': token
102
- }
103
- };
102
+ success_action_status: '201',
103
+ 'x-cos-security-token': token,
104
+ },
105
+ }
104
106
 
105
107
  const uploadConfig = {
106
108
  [EUploadMethod.put]: {
107
109
  params: putParams,
108
- successCode: 200,
110
+ isSuccess: (code: number) => code >= 200 && code < 300,
109
111
  },
110
112
  [EUploadMethod.post]: {
111
113
  params: postParams,
112
- successCode: 201,
113
- }
114
+ isSuccess: (code: number) => code === 201,
115
+ },
114
116
  }
115
117
 
116
- const res = await request.upload(uploadConfig[uploadMethod].params);
118
+ const res = await request.upload(uploadConfig[uploadMethod].params)
117
119
 
118
- if (res.statusCode === uploadConfig[uploadMethod].successCode) {
120
+ if (uploadConfig[uploadMethod].isSuccess(res.statusCode)) {
119
121
  return execCallback(callback, null, {
120
122
  fileID: fileId,
121
- download_url,
122
- requestId
123
- });
124
- } else {
125
- return execCallback(callback, new Error(`[${getSdkName()}][${ERRORS.OPERATION_FAIL}][${COMPONENT_NAME}]:${res.data}`));
123
+ download_url: downloadUrl,
124
+ requestId,
125
+ })
126
126
  }
127
+ return execCallback(callback, new Error(`[${getSdkName()}][${ERRORS.OPERATION_FAIL}][${COMPONENT_NAME}]:${res.data}`))
127
128
  }
128
129
  @catchErrorsDecorator({
129
130
  customInfo: {
130
131
  className: 'Cloudbase',
131
- methodName: 'getUploadMetadata'
132
+ methodName: 'getUploadMetadata',
132
133
  },
133
134
  title: '获取上传元信息失败',
134
135
  messages: [
@@ -136,37 +137,37 @@ class CloudbaseStorage {
136
137
  ' 1 - 调用 getUploadMetadata() 的语法或参数是否正确',
137
138
  ' 2 - 当前域名是否在安全域名列表中:https://console.cloud.tencent.com/tcb/env/safety',
138
139
  ' 3 - 云存储安全规则是否限制了当前登录状态访问',
139
- `如果问题依然存在,建议到官方问答社区提问或寻找帮助:${COMMUNITY_SITE_URL}`
140
- ]
140
+ `如果问题依然存在,建议到官方问答社区提问或寻找帮助:${COMMUNITY_SITE_URL}`,
141
+ ],
141
142
  })
142
143
  public async getUploadMetadata(
143
144
  params: ICloudbaseGetUploadMetadataParams,
144
145
  callback?: Function
145
146
  ): Promise<any> {
146
- const { cloudPath } = params;
147
+ const { cloudPath } = params
147
148
  if (!isString(cloudPath)) {
148
149
  throw new Error(JSON.stringify({
149
150
  code: ERRORS.INVALID_PARAMS,
150
- msg: `[${COMPONENT_NAME}.getUploadMetadata] invalid cloudPath`
151
- }));
151
+ msg: `[${COMPONENT_NAME}.getUploadMetadata] invalid cloudPath`,
152
+ }))
152
153
  }
153
154
  // @ts-ignore
154
- const request = this.request;
155
- const action = 'storage.getUploadMetadata';
155
+ const { request } = this
156
+ const action = 'storage.getUploadMetadata'
156
157
 
157
158
  try {
158
159
  const metaData = await request.send(action, {
159
- path: cloudPath
160
- });
161
- return execCallback(callback, null, metaData);
160
+ path: cloudPath,
161
+ })
162
+ return execCallback(callback, null, metaData)
162
163
  } catch (err) {
163
- return execCallback(callback, err);
164
+ return execCallback(callback, err)
164
165
  }
165
166
  }
166
167
  @catchErrorsDecorator({
167
168
  customInfo: {
168
169
  className: 'Cloudbase',
169
- methodName: 'deleteFile'
170
+ methodName: 'deleteFile',
170
171
  },
171
172
  title: '删除文件失败',
172
173
  messages: [
@@ -174,51 +175,51 @@ class CloudbaseStorage {
174
175
  ' 1 - 调用 deleteFile() 的语法或参数是否正确',
175
176
  ' 2 - 当前域名是否在安全域名列表中:https://console.cloud.tencent.com/tcb/env/safety',
176
177
  ' 3 - 云存储安全规则是否限制了当前登录状态访问',
177
- `如果问题依然存在,建议到官方问答社区提问或寻找帮助:${COMMUNITY_SITE_URL}`
178
- ]
178
+ `如果问题依然存在,建议到官方问答社区提问或寻找帮助:${COMMUNITY_SITE_URL}`,
179
+ ],
179
180
  })
180
181
  public async deleteFile(
181
182
  params: ICloudbaseDeleteFileParams,
182
183
  callback?: Function
183
184
  ): Promise<ICloudbaseDeleteFileResult> {
184
- const { fileList } = params;
185
+ const { fileList } = params
185
186
 
186
187
  if (!fileList || !isArray(fileList) || fileList.length === 0) {
187
188
  throw new Error(JSON.stringify({
188
189
  code: ERRORS.INVALID_PARAMS,
189
- msg: `[${COMPONENT_NAME}.deleteFile] fileList must not be empty`
190
- }));
190
+ msg: `[${COMPONENT_NAME}.deleteFile] fileList must not be empty`,
191
+ }))
191
192
  }
192
193
 
193
194
  for (const fileId of fileList) {
194
195
  if (!fileId || !isString(fileId)) {
195
196
  throw new Error(JSON.stringify({
196
197
  code: ERRORS.INVALID_PARAMS,
197
- msg: `[${COMPONENT_NAME}.deleteFile] fileID must be string`
198
- }));
198
+ msg: `[${COMPONENT_NAME}.deleteFile] fileID must be string`,
199
+ }))
199
200
  }
200
201
  }
201
202
 
202
- const action = 'storage.batchDeleteFile';
203
+ const action = 'storage.batchDeleteFile'
203
204
  // @ts-ignore
204
- const request = this.request;
205
+ const { request } = this
205
206
  const res = await request.send(action, {
206
- fileid_list: fileList
207
- });
207
+ fileid_list: fileList,
208
+ })
208
209
 
209
210
  if (res.code) {
210
- return execCallback(callback, null, res);
211
+ return execCallback(callback, null, res)
211
212
  }
212
213
  const data = {
213
214
  fileList: res.data.delete_list,
214
- requestId: res.requestId
215
- };
216
- return execCallback(callback, null, data);;
215
+ requestId: res.requestId,
216
+ }
217
+ return execCallback(callback, null, data)
217
218
  }
218
219
  @catchErrorsDecorator({
219
220
  customInfo: {
220
221
  className: 'Cloudbase',
221
- methodName: 'getTempFileURL'
222
+ methodName: 'getTempFileURL',
222
223
  },
223
224
  title: '获取文件下载链接',
224
225
  messages: [
@@ -226,67 +227,68 @@ class CloudbaseStorage {
226
227
  ' 1 - 调用 getTempFileURL() 的语法或参数是否正确',
227
228
  ' 2 - 当前域名是否在安全域名列表中:https://console.cloud.tencent.com/tcb/env/safety',
228
229
  ' 3 - 云存储安全规则是否限制了当前登录状态访问',
229
- `如果问题依然存在,建议到官方问答社区提问或寻找帮助:${COMMUNITY_SITE_URL}`
230
- ]
230
+ `如果问题依然存在,建议到官方问答社区提问或寻找帮助:${COMMUNITY_SITE_URL}`,
231
+ ],
231
232
  })
232
233
  public async getTempFileURL(
233
234
  params: ICloudbaseGetTempFileURLParams,
234
235
  callback?: Function
235
236
  ): Promise<ICloudbaseGetTempFileURLResult> {
236
- const { fileList } = params;
237
+ const { fileList } = params
237
238
 
238
239
  if (!fileList || !isArray(fileList) || fileList.length === 0) {
239
240
  throw new Error(JSON.stringify({
240
241
  code: ERRORS.INVALID_PARAMS,
241
- msg: `[${COMPONENT_NAME}.getTempFileURL] fileList must not be empty`
242
- }));
242
+ msg: `[${COMPONENT_NAME}.getTempFileURL] fileList must not be empty`,
243
+ }))
243
244
  }
244
245
 
245
- const file_list = [];
246
+ const convertedFileList = []
246
247
  for (const file of fileList) {
247
248
  if (isPalinObject(file)) {
248
- if (!file.hasOwnProperty('fileID') || !file.hasOwnProperty('maxAge')) {
249
+ if (!Object.prototype.hasOwnProperty.call(file, 'fileID')
250
+ || !Object.prototype.hasOwnProperty.call(file, 'maxAge')) {
249
251
  throw new Error(JSON.stringify({
250
252
  code: ERRORS.INVALID_PARAMS,
251
- msg: `[${COMPONENT_NAME}.getTempFileURL] file info must include fileID and maxAge`
252
- }));
253
+ msg: `[${COMPONENT_NAME}.getTempFileURL] file info must include fileID and maxAge`,
254
+ }))
253
255
  }
254
256
 
255
- file_list.push({
257
+ convertedFileList.push({
256
258
  fileid: (file as ICloudbaseFileInfo).fileID,
257
- max_age: (file as ICloudbaseFileInfo).maxAge
258
- });
259
+ max_age: (file as ICloudbaseFileInfo).maxAge,
260
+ })
259
261
  } else if (isString(file)) {
260
- file_list.push({
261
- fileid: file
262
- });
262
+ convertedFileList.push({
263
+ fileid: file,
264
+ })
263
265
  } else {
264
266
  throw new Error(JSON.stringify({
265
267
  code: ERRORS.INVALID_PARAMS,
266
- msg: `[${COMPONENT_NAME}.getTempFileURL] invalid fileList`
267
- }));
268
+ msg: `[${COMPONENT_NAME}.getTempFileURL] invalid fileList`,
269
+ }))
268
270
  }
269
271
  }
270
272
 
271
- const action = 'storage.batchGetDownloadUrl';
273
+ const action = 'storage.batchGetDownloadUrl'
272
274
  // @ts-ignore
273
- const request = this.request;
275
+ const { request } = this
274
276
 
275
- const res = await request.send(action, { file_list });
277
+ const res = await request.send(action, { file_list: convertedFileList })
276
278
 
277
279
  if (res.code) {
278
- return execCallback(callback, null, res);
280
+ return execCallback(callback, null, res)
279
281
  }
280
282
 
281
283
  return execCallback(callback, null, {
282
284
  fileList: res.data.download_list,
283
- requestId: res.requestId
284
- });
285
+ requestId: res.requestId,
286
+ })
285
287
  }
286
288
  @catchErrorsDecorator({
287
289
  customInfo: {
288
290
  className: 'Cloudbase',
289
- methodName: 'downloadFile'
291
+ methodName: 'downloadFile',
290
292
  },
291
293
  title: '下载文件失败',
292
294
  messages: [
@@ -294,44 +296,44 @@ class CloudbaseStorage {
294
296
  ' 1 - 调用 downloadFile() 的语法或参数是否正确',
295
297
  ' 2 - 当前域名是否在安全域名列表中:https://console.cloud.tencent.com/tcb/env/safety',
296
298
  ' 3 - 云存储安全规则是否限制了当前登录状态访问',
297
- `如果问题依然存在,建议到官方问答社区提问或寻找帮助:${COMMUNITY_SITE_URL}`
298
- ]
299
+ `如果问题依然存在,建议到官方问答社区提问或寻找帮助:${COMMUNITY_SITE_URL}`,
300
+ ],
299
301
  })
300
302
  public async downloadFile(
301
303
  params: ICloudbaseDownloadFileParams,
302
304
  callback?: Function
303
305
  ): Promise<ICloudbaseDownloadFileResult> {
304
- const { fileID } = params;
306
+ const { fileID } = params
305
307
  if (!isString(fileID)) {
306
308
  throw new Error(JSON.stringify({
307
309
  code: ERRORS.INVALID_PARAMS,
308
- msg: `[${COMPONENT_NAME}.getTempFileURL] fileID must be string`
309
- }));
310
+ msg: `[${COMPONENT_NAME}.getTempFileURL] fileID must be string`,
311
+ }))
310
312
  }
311
313
 
312
314
  const tmpUrlRes = await this.getTempFileURL.call(this, {
313
315
  fileList: [{
314
316
  fileID,
315
- maxAge: 600
316
- }]
317
- });
317
+ maxAge: 600,
318
+ }],
319
+ })
318
320
 
319
- const res = tmpUrlRes.fileList[0];
321
+ const res = tmpUrlRes.fileList[0]
320
322
 
321
323
  if (res.code !== 'SUCCESS') {
322
- return execCallback(callback, res);
324
+ return execCallback(callback, res)
323
325
  }
324
326
  // @ts-ignore
325
- const request = this.request;
327
+ const { request } = this
326
328
 
327
- const tmpUrl = encodeURI(res.download_url);
329
+ const tmpUrl = encodeURI(res.download_url)
328
330
 
329
- const result = await request.download({ url: tmpUrl });
330
- return execCallback(callback, null, result);
331
+ const result = await request.download({ url: tmpUrl })
332
+ return execCallback(callback, null, result)
331
333
  }
332
334
  }
333
335
 
334
- const cloudbaseStorage = new CloudbaseStorage();
336
+ const cloudbaseStorage = new CloudbaseStorage()
335
337
  const component: ICloudbaseComponent = {
336
338
  name: COMPONENT_NAME,
337
339
  entity: {
@@ -339,18 +341,18 @@ const component: ICloudbaseComponent = {
339
341
  deleteFile: cloudbaseStorage.deleteFile,
340
342
  getTempFileURL: cloudbaseStorage.getTempFileURL,
341
343
  downloadFile: cloudbaseStorage.downloadFile,
342
- getUploadMetadata: cloudbaseStorage.getUploadMetadata
343
- }
344
+ getUploadMetadata: cloudbaseStorage.getUploadMetadata,
345
+ },
344
346
  }
345
347
 
346
348
  try {
347
- cloudbase.registerComponent(component);
349
+ cloudbase.registerComponent(component)
348
350
  } catch (e) { }
349
351
 
350
352
  export function registerStorage(app: Pick<ICloudbase, 'registerComponent'>) {
351
353
  try {
352
- app.registerComponent(component);
354
+ app.registerComponent(component)
353
355
  } catch (e) {
354
- console.warn(e);
356
+ console.warn(e)
355
357
  }
356
- }
358
+ }
package/.eslintrc DELETED
@@ -1,29 +0,0 @@
1
- {
2
- "extends": [
3
- "eslint-config-alloy/typescript"
4
- ],
5
- "rules": {
6
- "indent": [
7
- "error",
8
- 2,
9
- {
10
- "SwitchCase": 1,
11
- "flatTernaryExpressions": true
12
- }
13
- ],
14
- "guard-for-in": 0,
15
- "no-param-reassign": 0,
16
- "no-undefined": 0,
17
- "@typescript-eslint/explicit-member-accessibility": 0,
18
- "@typescript-eslint/no-invalid-this": 0,
19
- "@typescript-eslint/no-loss-of-precision": 0,
20
- "@typescript-eslint/no-duplicate-imports": 0
21
- },
22
- "parserOptions": {
23
- "ecmaVersion": 6,
24
- "sourceType": "module",
25
- "ecmaFeatures": {
26
- "modules": true
27
- }
28
- }
29
- }