UNPKG

@cloudbase/storage

Version:
963 lines 176 kB
var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (g && (g = 0, op[0] && (_ = 0)), _) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; import { isStorageError, StorageError } from './supabase/errors'; import { constants, utils, helpers } from '@cloudbase/utilities'; var EUploadMethod; (function (EUploadMethod) { EUploadMethod["put"] = "put"; EUploadMethod["post"] = "post"; })(EUploadMethod || (EUploadMethod = {})); var getSdkName = constants.getSdkName, ERRORS = constants.ERRORS, COMMUNITY_SITE_URL = constants.COMMUNITY_SITE_URL; var isArray = utils.isArray, isString = utils.isString, isPalinObject = utils.isPalinObject, execCallback = utils.execCallback; var catchErrorsDecorator = helpers.catchErrorsDecorator; export var COMPONENT_NAME = 'storage'; var storageGateWay = { getUploadInfo: function (request, params, customReqOpts) { return __awaiter(void 0, void 0, void 0, function () { var res, data; var _a; return __generator(this, function (_b) { switch (_b.label) { case 0: return [4, request.gateWay({ path: 'storages', name: 'get-objects-upload-info', data: [ __assign({ objectId: params.path }, (params.headers ? { signedHeader: Object.keys(params.headers).reduce(function (res, v) { var _a; return (__assign(__assign({}, res), (_a = {}, _a[v] = [params.headers[v]], _a))); }, {}) } : {})), ], }, customReqOpts)]; case 1: res = _b.sent(); data = ((_a = res.data) === null || _a === void 0 ? void 0 : _a[0]) || {}; res = __assign(__assign({}, res), { data: __assign(__assign({}, (data.code ? __assign({}, data) : {})), { authorization: data.authorization, token: data.token, url: data.uploadUrl, fileId: data.cloudObjectId, cosFileId: data.cloudObjectMeta, download_url: data.downloadUrl }) }); return [2, res]; } }); }); }, getDownLoadInfo: function (request, params, customReqOpts) { return __awaiter(void 0, void 0, void 0, function () { var res; var _a; return __generator(this, function (_b) { switch (_b.label) { case 0: return [4, request.gateWay({ path: 'storages', name: 'get-objects-download-info', data: params.convertedFileList.map(function (v) { return ({ cloudObjectId: v.fileid }); }), }, customReqOpts)]; case 1: res = _b.sent(); res = __assign(__assign({}, res), { data: { download_list: (_a = res.data) === null || _a === void 0 ? void 0 : _a.map(function (v) { return ({ code: v.code || 'SUCCESS', message: v.message, fileid: v.cloudObjectId, download_url: v.downloadUrl, fileID: v.cloudObjectId, tempFileURL: v.downloadUrl, }); }), } }); return [2, res]; } }); }); }, delete: function (request, params, customReqOpts) { return __awaiter(void 0, void 0, void 0, function () { var res; var _a; return __generator(this, function (_b) { switch (_b.label) { case 0: return [4, request.gateWay({ path: 'storages', name: 'delete-objects', data: params.fileList.map(function (v) { return ({ cloudObjectId: v }); }), }, customReqOpts)]; case 1: res = _b.sent(); res = __assign(__assign({}, res), { data: { delete_list: (_a = res.data) === null || _a === void 0 ? void 0 : _a.map(function (v) { return ({ code: v.code || 'SUCCESS', fileID: v.cloudObjectId, message: v.message, }); }), } }); return [2, res]; } }); }); }, copyFile: function (request, params, customReqOpts) { return __awaiter(void 0, void 0, void 0, function () { var res; var _a; return __generator(this, function (_b) { switch (_b.label) { case 0: return [4, request.gateWay({ path: 'storages', name: 'copy-objects', data: params.convertedFileList.map(function (v) { return ({ srcPath: v.src_path, dstPath: v.dst_path, overwrite: v.overwrite, removeOriginal: v.remove_original, }); }), }, customReqOpts)]; case 1: res = _b.sent(); res = __assign(__assign({}, res), { data: { copy_list: (_a = res.data) === null || _a === void 0 ? void 0 : _a.map(function (v) { return ({ code: v.code || 'SUCCESS', fileID: v.cloudObjectId, message: v.message, }); }), } }); return [2, res]; } }); }); }, }; export var CloudbaseStorage = (function () { function CloudbaseStorage() { } CloudbaseStorage.prototype.isGateWay = function () { var config = this.config; var endPointMode = config.endPointMode || 'CLOUD_API'; return endPointMode === 'GATEWAY'; }; CloudbaseStorage.prototype.uploadFile = function (params, callback) { return __awaiter(this, void 0, void 0, function () { var cloudPath, filePath, onUploadProgress, method, headers, fileContent, uploadMethod, metadataResult, metadata, requestId, url, authorization, token, fileId, cosFileId, downloadUrl, commonParams, putParams, postParams, uploadConfig, res; var _a, _b, _c; return __generator(this, function (_d) { switch (_d.label) { case 0: cloudPath = params.cloudPath, filePath = params.filePath, onUploadProgress = params.onUploadProgress, method = (_a = params.method, _a === void 0 ? 'put' : _a), headers = (_b = params.headers, _b === void 0 ? {} : _b), fileContent = params.fileContent; if (!isString(cloudPath) || (!filePath && !fileContent)) { throw new Error(JSON.stringify({ code: ERRORS.INVALID_PARAMS, msg: "[".concat(COMPONENT_NAME, ".uploadFile] invalid params"), })); } uploadMethod = { put: EUploadMethod.put, post: EUploadMethod.post }[method.toLocaleLowerCase()] || EUploadMethod.put; return [4, this.getUploadMetadata({ cloudPath: cloudPath, method: uploadMethod, headers: uploadMethod === EUploadMethod.put ? headers : undefined, customReqOpts: params.customReqOpts, })]; case 1: metadataResult = _d.sent(); metadata = metadataResult.data, requestId = metadataResult.requestId; url = metadata.url, authorization = metadata.authorization, token = metadata.token, fileId = metadata.fileId, cosFileId = metadata.cosFileId, downloadUrl = metadata.download_url; commonParams = { url: url, file: filePath, name: cloudPath, onUploadProgress: onUploadProgress, fileContent: fileContent, fileId: fileId, requestId: requestId, }; putParams = __assign(__assign({}, commonParams), { method: EUploadMethod.put, headers: __assign(__assign({}, headers), { authorization: authorization, 'x-cos-meta-fileid': cosFileId, 'x-cos-security-token': token }) }); postParams = __assign(__assign({}, commonParams), { method: EUploadMethod.post, data: { key: cloudPath, signature: authorization, 'x-cos-meta-fileid': cosFileId, success_action_status: '201', 'x-cos-security-token': token, } }); uploadConfig = (_c = {}, _c[EUploadMethod.put] = { params: putParams, isSuccess: function (code) { return code >= 200 && code < 300; }, }, _c[EUploadMethod.post] = { params: postParams, isSuccess: function (code) { return code === 201; }, }, _c); return [4, this.request.upload(uploadConfig[uploadMethod].params)]; case 2: res = _d.sent(); if (uploadConfig[uploadMethod].isSuccess(res.statusCode)) { return [2, execCallback(callback, null, { fileID: fileId, download_url: downloadUrl, requestId: requestId, })]; } return [2, execCallback(callback, new Error("[".concat(getSdkName(), "][").concat(ERRORS.OPERATION_FAIL, "][").concat(COMPONENT_NAME, "]:").concat(res.data)))]; } }); }); }; CloudbaseStorage.prototype.getUploadMetadata = function (params, callback) { return __awaiter(this, void 0, void 0, function () { var cloudPath, method, headers, request, action, metaData, metaDataParam, err_1; return __generator(this, function (_a) { switch (_a.label) { case 0: cloudPath = params.cloudPath, method = params.method, headers = params.headers; if (!isString(cloudPath)) { throw new Error(JSON.stringify({ code: ERRORS.INVALID_PARAMS, msg: "[".concat(COMPONENT_NAME, ".getUploadMetadata] invalid cloudPath"), })); } request = this.request; action = 'storage.getUploadMetadata'; _a.label = 1; case 1: _a.trys.push([1, 6, , 7]); metaData = void 0; metaDataParam = { path: cloudPath }; if (method) { metaDataParam.method = method; } if (method === EUploadMethod.put && headers) { metaDataParam.headers = headers; } if (!this.isGateWay()) return [3, 3]; return [4, storageGateWay.getUploadInfo(request, metaDataParam, params.customReqOpts)]; case 2: metaData = _a.sent(); return [3, 5]; case 3: return [4, request.send(action, metaDataParam, params.customReqOpts)]; case 4: metaData = _a.sent(); _a.label = 5; case 5: return [2, execCallback(callback, null, metaData)]; case 6: err_1 = _a.sent(); return [2, execCallback(callback, err_1)]; case 7: return [2]; } }); }); }; CloudbaseStorage.prototype.deleteFile = function (params, callback) { return __awaiter(this, void 0, void 0, function () { var fileList, _i, fileList_1, fileId, action, request, res, data; return __generator(this, function (_a) { switch (_a.label) { case 0: fileList = params.fileList; if (!fileList || !isArray(fileList) || fileList.length === 0) { throw new Error(JSON.stringify({ code: ERRORS.INVALID_PARAMS, msg: "[".concat(COMPONENT_NAME, ".deleteFile] fileList must not be empty"), })); } for (_i = 0, fileList_1 = fileList; _i < fileList_1.length; _i++) { fileId = fileList_1[_i]; if (!fileId || !isString(fileId)) { throw new Error(JSON.stringify({ code: ERRORS.INVALID_PARAMS, msg: "[".concat(COMPONENT_NAME, ".deleteFile] fileID must be string"), })); } } action = 'storage.batchDeleteFile'; request = this.request; res = {}; if (!this.isGateWay()) return [3, 2]; return [4, storageGateWay.delete(request, { fileList: fileList }, params.customReqOpts)]; case 1: res = _a.sent(); return [3, 4]; case 2: return [4, request.send(action, { fileid_list: fileList, }, params.customReqOpts)]; case 3: res = _a.sent(); _a.label = 4; case 4: if (res.code) { return [2, execCallback(callback, null, res)]; } data = { fileList: res.data.delete_list, requestId: res.requestId, }; return [2, execCallback(callback, null, data)]; } }); }); }; CloudbaseStorage.prototype.getTempFileURL = function (params, callback) { return __awaiter(this, void 0, void 0, function () { var fileList, convertedFileList, _i, fileList_2, file, action, request, res; return __generator(this, function (_a) { switch (_a.label) { case 0: fileList = params.fileList; if (!fileList || !isArray(fileList) || fileList.length === 0) { throw new Error(JSON.stringify({ code: ERRORS.INVALID_PARAMS, msg: "[".concat(COMPONENT_NAME, ".getTempFileURL] fileList must not be empty"), })); } convertedFileList = []; for (_i = 0, fileList_2 = fileList; _i < fileList_2.length; _i++) { file = fileList_2[_i]; if (isPalinObject(file)) { if (!Object.prototype.hasOwnProperty.call(file, 'fileID')) { throw new Error(JSON.stringify({ code: ERRORS.INVALID_PARAMS, msg: "[".concat(COMPONENT_NAME, ".getTempFileURL] file info must include fileID"), })); } convertedFileList.push({ fileid: file.fileID, max_age: file.maxAge || 7200, }); } else if (isString(file)) { convertedFileList.push({ fileid: file, }); } else { throw new Error(JSON.stringify({ code: ERRORS.INVALID_PARAMS, msg: "[".concat(COMPONENT_NAME, ".getTempFileURL] invalid fileList"), })); } } action = 'storage.batchGetDownloadUrl'; request = this.request; res = {}; if (!this.isGateWay()) return [3, 2]; return [4, storageGateWay.getDownLoadInfo(request, { convertedFileList: convertedFileList }, params.customReqOpts)]; case 1: res = _a.sent(); return [3, 4]; case 2: return [4, request.send(action, { file_list: convertedFileList }, params.customReqOpts)]; case 3: res = _a.sent(); _a.label = 4; case 4: if (res.code) { return [2, execCallback(callback, null, res)]; } return [2, execCallback(callback, null, { fileList: res.data.download_list, requestId: res.requestId, })]; } }); }); }; CloudbaseStorage.prototype.downloadFile = function (params, callback) { return __awaiter(this, void 0, void 0, function () { var fileID, tmpUrlRes, res, request, tmpUrl, result; return __generator(this, function (_a) { switch (_a.label) { case 0: fileID = params.fileID; if (!isString(fileID)) { throw new Error(JSON.stringify({ code: ERRORS.INVALID_PARAMS, msg: "[".concat(COMPONENT_NAME, ".getTempFileURL] fileID must be string"), })); } return [4, this.getTempFileURL.call(this, { fileList: [ { fileID: fileID, maxAge: 600, }, ], customReqOpts: params.customReqOpts, })]; case 1: tmpUrlRes = _a.sent(); res = tmpUrlRes.fileList[0]; if (res.code !== 'SUCCESS') { return [2, execCallback(callback, res)]; } request = this.request; tmpUrl = encodeURI(res.download_url); return [4, request.download({ url: tmpUrl, tempFilePath: params.tempFilePath })]; case 2: result = _a.sent(); return [2, execCallback(callback, null, result)]; } }); }); }; CloudbaseStorage.prototype.copyFile = function (params, callback) { return __awaiter(this, void 0, void 0, function () { var fileList, convertedFileList, _i, fileList_3, file, srcPath, dstPath, request, res, action; return __generator(this, function (_a) { switch (_a.label) { case 0: fileList = params.fileList; if (!fileList || !isArray(fileList) || fileList.length === 0) { throw new Error(JSON.stringify({ code: ERRORS.INVALID_PARAMS, msg: "[".concat(COMPONENT_NAME, ".copyFile] fileList must not be empty"), })); } convertedFileList = []; for (_i = 0, fileList_3 = fileList; _i < fileList_3.length; _i++) { file = fileList_3[_i]; srcPath = file.srcPath, dstPath = file.dstPath; if (!srcPath || !dstPath || typeof srcPath !== 'string' || typeof dstPath !== 'string') { throw new Error(JSON.stringify({ code: ERRORS.INVALID_PARAMS, msg: "[".concat(COMPONENT_NAME, ".copyFile] srcPath and dstPath may not be empty"), })); } if (srcPath === dstPath) { throw new Error(JSON.stringify({ code: ERRORS.INVALID_PARAMS, msg: "[".concat(COMPONENT_NAME, ".copyFile] srcPath and dstPath can not be the same"), })); } convertedFileList.push({ src_path: srcPath, dst_path: dstPath, overwrite: file.overwrite, remove_original: file.removeOriginal, }); } request = this.request; res = {}; if (!this.isGateWay()) return [3, 2]; return [4, storageGateWay.copyFile(request, { convertedFileList: convertedFileList }, params.customReqOpts)]; case 1: res = _a.sent(); return [3, 4]; case 2: action = 'storage.batchCopyFile'; return [4, request.send(action, { file_list: convertedFileList }, params.customReqOpts)]; case 3: res = _a.sent(); _a.label = 4; case 4: if (res.code) { return [2, execCallback(callback, null, res)]; } return [2, execCallback(callback, null, { fileList: res.data.copy_list, requestId: res.requestId, })]; } }); }); }; CloudbaseStorage.prototype.getFileInfo = function (params) { var _a; return __awaiter(this, void 0, void 0, function () { var fileInfo, fileList; var _this = this; return __generator(this, function (_b) { switch (_b.label) { case 0: return [4, this.getTempFileURL(params)]; case 1: fileInfo = _b.sent(); if (!((fileInfo === null || fileInfo === void 0 ? void 0 : fileInfo.fileList) && ((_a = fileInfo === null || fileInfo === void 0 ? void 0 : fileInfo.fileList) === null || _a === void 0 ? void 0 : _a.length) > 0)) return [3, 3]; return [4, Promise.all(fileInfo.fileList.map(function (item) { return __awaiter(_this, void 0, void 0, function () { var request, res, header, fileSize, contentType, fileInfo_1, e_1; return __generator(this, function (_a) { switch (_a.label) { case 0: if (item.code !== 'SUCCESS') { return [2, { code: item.code, fileID: item.fileID, tempFileURL: item.tempFileURL, message: item.message, }]; } _a.label = 1; case 1: _a.trys.push([1, 3, , 4]); request = this.request; return [4, request.fetch({ url: item.tempFileURL, method: 'HEAD' })]; case 2: res = _a.sent(); header = res.header; if (Headers && header instanceof Headers) { header = Object.fromEntries(res.header.entries()); } fileSize = parseInt(header['content-length']) || 0; contentType = header['content-type'] || ''; if ([400, 404].includes(Number(res.statusCode))) { return [2, { code: 'FILE_NOT_FOUND', fileID: item.fileID, tempFileURL: item.tempFileURL, message: 'file not found', }]; } fileInfo_1 = { code: item.code, fileID: item.fileID, tempFileURL: item.tempFileURL, cloudId: item.fileID, fileName: item.fileID.split('/').pop(), contentType: contentType, mime: contentType.split(';')[0].trim(), size: fileSize, etag: header.etag || '', lastModified: header['last-modified'] || '', cacheControl: header['cache-control'] || '', }; return [2, fileInfo_1]; case 3: e_1 = _a.sent(); return [2, { code: 'FETCH_FILE_INFO_ERROR', fileID: item.fileID, tempFileURL: item.tempFileURL, message: e_1.message, }]; case 4: return [2]; } }); }); }))]; case 2: fileList = _b.sent(); return [2, { fileList: fileList, requestId: fileInfo.requestId, }]; case 3: return [2, { fileList: [], requestId: fileInfo.requestId, }]; } }); }); }; __decorate([ catchErrorsDecorator({ customInfo: { className: 'Cloudbase', methodName: 'uploadFile', }, title: '上传文件失败', messages: [ '请确认以下各项:', ' 1 - 调用 uploadFile() 的语法或参数是否正确', ' 2 - 当前域名是否在安全域名列表中:https://console.cloud.tencent.com/tcb/env/safety', ' 3 - 云存储安全规则是否限制了当前登录状态访问', "\u5982\u679C\u95EE\u9898\u4F9D\u7136\u5B58\u5728\uFF0C\u5EFA\u8BAE\u5230\u5B98\u65B9\u95EE\u7B54\u793E\u533A\u63D0\u95EE\u6216\u5BFB\u627E\u5E2E\u52A9\uFF1A".concat(COMMUNITY_SITE_URL), ], }), __metadata("design:type", Function), __metadata("design:paramtypes", [Object, Function]), __metadata("design:returntype", Promise) ], CloudbaseStorage.prototype, "uploadFile", null); __decorate([ catchErrorsDecorator({ customInfo: { className: 'Cloudbase', methodName: 'getUploadMetadata', }, title: '获取上传元信息失败', messages: [ '请确认以下各项:', ' 1 - 调用 getUploadMetadata() 的语法或参数是否正确', ' 2 - 当前域名是否在安全域名列表中:https://console.cloud.tencent.com/tcb/env/safety', ' 3 - 云存储安全规则是否限制了当前登录状态访问', "\u5982\u679C\u95EE\u9898\u4F9D\u7136\u5B58\u5728\uFF0C\u5EFA\u8BAE\u5230\u5B98\u65B9\u95EE\u7B54\u793E\u533A\u63D0\u95EE\u6216\u5BFB\u627E\u5E2E\u52A9\uFF1A".concat(COMMUNITY_SITE_URL), ], }), __metadata("design:type", Function), __metadata("design:paramtypes", [Object, Function]), __metadata("design:returntype", Promise) ], CloudbaseStorage.prototype, "getUploadMetadata", null); __decorate([ catchErrorsDecorator({ customInfo: { className: 'Cloudbase', methodName: 'deleteFile', }, title: '删除文件失败', messages: [ '请确认以下各项:', ' 1 - 调用 deleteFile() 的语法或参数是否正确', ' 2 - 当前域名是否在安全域名列表中:https://console.cloud.tencent.com/tcb/env/safety', ' 3 - 云存储安全规则是否限制了当前登录状态访问', "\u5982\u679C\u95EE\u9898\u4F9D\u7136\u5B58\u5728\uFF0C\u5EFA\u8BAE\u5230\u5B98\u65B9\u95EE\u7B54\u793E\u533A\u63D0\u95EE\u6216\u5BFB\u627E\u5E2E\u52A9\uFF1A".concat(COMMUNITY_SITE_URL), ], }), __metadata("design:type", Function), __metadata("design:paramtypes", [Object, Function]), __metadata("design:returntype", Promise) ], CloudbaseStorage.prototype, "deleteFile", null); __decorate([ catchErrorsDecorator({ customInfo: { className: 'Cloudbase', methodName: 'getTempFileURL', }, title: '获取文件下载链接', messages: [ '请确认以下各项:', ' 1 - 调用 getTempFileURL() 的语法或参数是否正确', ' 2 - 当前域名是否在安全域名列表中:https://console.cloud.tencent.com/tcb/env/safety', ' 3 - 云存储安全规则是否限制了当前登录状态访问', "\u5982\u679C\u95EE\u9898\u4F9D\u7136\u5B58\u5728\uFF0C\u5EFA\u8BAE\u5230\u5B98\u65B9\u95EE\u7B54\u793E\u533A\u63D0\u95EE\u6216\u5BFB\u627E\u5E2E\u52A9\uFF1A".concat(COMMUNITY_SITE_URL), ], }), __metadata("design:type", Function), __metadata("design:paramtypes", [Object, Function]), __metadata("design:returntype", Promise) ], CloudbaseStorage.prototype, "getTempFileURL", null); __decorate([ catchErrorsDecorator({ customInfo: { className: 'Cloudbase', methodName: 'downloadFile', }, title: '下载文件失败', messages: [ '请确认以下各项:', ' 1 - 调用 downloadFile() 的语法或参数是否正确', ' 2 - 当前域名是否在安全域名列表中:https://console.cloud.tencent.com/tcb/env/safety', ' 3 - 云存储安全规则是否限制了当前登录状态访问', "\u5982\u679C\u95EE\u9898\u4F9D\u7136\u5B58\u5728\uFF0C\u5EFA\u8BAE\u5230\u5B98\u65B9\u95EE\u7B54\u793E\u533A\u63D0\u95EE\u6216\u5BFB\u627E\u5E2E\u52A9\uFF1A".concat(COMMUNITY_SITE_URL), ], }), __metadata("design:type", Function), __metadata("design:paramtypes", [Object, Function]), __metadata("design:returntype", Promise) ], CloudbaseStorage.prototype, "downloadFile", null); __decorate([ catchErrorsDecorator({ customInfo: { className: 'Cloudbase', methodName: 'copyFile', }, title: '批量复制文件', messages: [ '请确认以下各项:', ' 1 - 调用 copyFile() 的语法或参数是否正确', ' 2 - 当前域名是否在安全域名列表中:https://console.cloud.tencent.com/tcb/env/safety', ' 3 - 云存储安全规则是否限制了当前登录状态访问', "\u5982\u679C\u95EE\u9898\u4F9D\u7136\u5B58\u5728\uFF0C\u5EFA\u8BAE\u5230\u5B98\u65B9\u95EE\u7B54\u793E\u533A\u63D0\u95EE\u6216\u5BFB\u627E\u5E2E\u52A9\uFF1A".concat(COMMUNITY_SITE_URL), ], }), __metadata("design:type", Function), __metadata("design:paramtypes", [Object, Function]), __metadata("design:returntype", Promise) ], CloudbaseStorage.prototype, "copyFile", null); return CloudbaseStorage; }()); var storage = new CloudbaseStorage(); var component = { name: COMPONENT_NAME, entity: { uploadFile: storage.uploadFile, deleteFile: storage.deleteFile, getTempFileURL: storage.getTempFileURL, downloadFile: storage.downloadFile, getUploadMetadata: storage.getUploadMetadata, copyFile: storage.copyFile, getFileInfo: storage.getFileInfo, isGateWay: storage.isGateWay, }, }; var SupabaseFileAPILikeStorage = (function (_super) { __extends(SupabaseFileAPILikeStorage, _super); function SupabaseFileAPILikeStorage(context) { var _this = _super.call(this) || this; _this.shouldThrowOnError = false; _this.bucketId = ''; _this.context = context; return _this; } Object.defineProperty(SupabaseFileAPILikeStorage.prototype, "config", { get: function () { var _a; return (_a = this.context) === null || _a === void 0 ? void 0 : _a.config; }, enumerable: false, configurable: true }); Object.defineProperty(SupabaseFileAPILikeStorage.prototype, "request", { get: function () { var _a; return (_a = this.context) === null || _a === void 0 ? void 0 : _a.request; }, enumerable: false, configurable: true }); SupabaseFileAPILikeStorage.prototype.throwOnError = function () { this.shouldThrowOnError = true; return this; }; SupabaseFileAPILikeStorage.prototype.from = function (bucket) { this.bucketId = bucket || ''; return this; }; SupabaseFileAPILikeStorage.prototype.upload = function (path, fileBody, fileOptions) { return __awaiter(this, void 0, void 0, function () { var options, cacheControl, contentType, metadata, cloudPath, uploadFileParams, headers, result, error_1; return __generator(this, function (_a) { switch (_a.label) { case 0: options = __assign({ upsert: true }, fileOptions); cacheControl = options.cacheControl, contentType = options.contentType, metadata = options.metadata; _a.label = 1; case 1: _a.trys.push([1, 3, , 4]); cloudPath = this._getCloudPath(path); uploadFileParams = { cloudPath: cloudPath, filePath: fileBody, }; if (cacheControl || contentType || metadata) { headers = {}; if (cacheControl) { headers['cache-control'] = cacheControl; } if (contentType) { headers['content-type'] = contentType; } if (metadata) { headers['x-cos-metadata-metadata'] = this.toBase64(JSON.stringify(metadata)); } uploadFileParams.headers = headers; } return [4, this.uploadFile(uploadFileParams)]; case 2: result = _a.sent(); if (!result.fileID) { throw new Error(JSON.stringify({ code: ERRORS.OPERATION_FAIL, msg: "[".concat(COMPONENT_NAME, ".update] no fileID returned"), })); } return [2, { data: { id: result.fileID, path: path, fullPath: path, }, error: null, }]; case 3: error_1 = _a.sent(); if (this.shouldThrowOnError) throw error_1; if (isStorageError(error_1)) { return [2, { data: null, error: error_1, }]; } throw error_1; case 4: return [2]; } }); }); }; SupabaseFileAPILikeStorage.prototype.uploadToSignedUrl = function (path, _token, fileBody, fileOptions) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { return [2, this.upload(path, fileBody, fileOptions)]; }); }); }; SupabaseFileAPILikeStorage.prototype.createSignedUploadUrl = function (path) { return __awaiter(this, void 0, void 0, function () { var cloudPath, metadata, error_2; return __generator(this, function (_a) { switch (_a.label) { case 0: _a.trys.push([0, 2, , 3]); cloudPath = this._getCloudPath(path); return [4, this.getUploadMetadata({ cloudPath: cloudPath })]; case 1: metadata = (_a.sent()).data; return [2, { data: { signedUrl: metadata.url, token: metadata.token, path: path, authorization: metadata.authorization, id: metadata.fileId, cosFileId: metadata.cosFileId, downloadUrl: metadata.download_url, }, error: null, }]; case 2: error_2 = _a.sent(); if (this.shouldThrowOnError) throw error_2; return [2, { data: null, error: error_2 instanceof StorageError ? error_2 : new StorageError(error_2.message), }]; case 3: return [2]; } }); }); }; SupabaseFileAPILikeStorage.prototype.update = function (path, fileBody, fileOptions) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { return [2, this.upload(path, fileBody, __assign(__assign({}, fileOptions), { upsert: true }))]; }); }); }; SupabaseFileAPILikeStorage.prototype.move = function (fromPath, toPath) { return __awaiter(this, void 0, void 0, function () { var result, error_3; return __generator(this, function (_a) { switch (_a.label) { case 0: _a.trys.push([0, 2, , 3]); return [4, this.copyFile({ fileList: [ { srcPath: this._getCloudPath(fromPath), dstPath: this._getCloudPath(toPath), overwrite: true, removeOriginal: true, }, ], })]; case 1: result = _a.sent(); if (result.fileList[0].code && result.fileList[0].code !== 'SUCCESS') { throw new StorageError(result.fileList[0].message || 'Move failed'); } return [2, { data: { message: "File moved from ".concat(fromPath, " to ").concat(toPath) }, error: null, }]; case 2: error_3 = _a.sent(); if (this.shouldThrowOnError) throw error_3; if (isStorageError(error_3)) { return [2, { data: null, error: error_3, }]; } throw error_3; case 3: return [2]; } }); }); }; SupabaseFileAPILikeStorage.prototype.copy = function (fromPath, toPath) { return __awaiter(this, void 0, void 0, function () { var result, error_4; return __generator(this, function (_a) { switch (_a.label) { case 0: _a.trys.push([0, 2, , 3]); return [4, this.copyFile({ fileList: [ { srcPath: this._getCloudPath(fromPath), dstPath: this._getCloudPath(toPath), overwrite: true, removeOriginal: false, }, ], })]; case 1: result = _a.sent(); if (result.fileList[0].code && result.fileList[0].code !== 'SUCCESS') { throw new StorageError(result.fileList[0].message || 'Copy failed'); } return [2, { data: { path: this._getCloudPath(toPath) }, error: null, }]; case 2: error_4 = _a.sent(); if (this.shouldThrowOnError) throw error_4; if (isStorageError(error_4)) { return [2, { data: null, error: error_4 }]; } throw error_4; case 3: return [2]; } }); }); }; SupabaseFileAPILikeStorage.prototype.createSignedUrl = function (path, expiresIn, options) { return __awaiter(this, void 0, void 0, function () { var cloudPath, result, signedUrl, queryParams, transformQuery, separator, error_5; return __generator(this, function (_a) { switch (_a.label) { case 0: _a.t