@vectorx/cloud-toolkit
Version:
VectorX Cloud Toolkit
105 lines (104 loc) • 4.95 kB
JavaScript
;
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Uploader = void 0;
const cos_nodejs_sdk_v5_1 = __importDefault(require("cos-nodejs-sdk-v5"));
const to_hump_1 = require("./to-hump");
class Uploader {
constructor(args) {
this.bizName = args.bizName;
this.scene = args.scene;
this.getToken = args.getToken;
this.enableResume = args.enableResume;
}
getPermit(file, fileInfo, fileFormat, persistentOps, persistentNotifyUrl) {
return this.getToken().then(({ uploadTempPermits, uploadLimitPolicy }) => {
const fileType = fileInfo.type;
const fileSize = fileInfo.size;
if (uploadLimitPolicy) {
const { fileTypes, maxSize } = uploadLimitPolicy;
if ((fileTypes === null || fileTypes === void 0 ? void 0 : fileTypes.length) &&
!fileTypes.reduce((a, c) => [...a, ...c.split("/")], []).some((f) => fileType.endsWith(f))) {
throw new Error(`文件类型 ${fileType} 不在可上传的文件类型 ${fileTypes.join(", ")} 中。`);
}
if (maxSize !== undefined && fileSize > maxSize) {
throw new Error(`文件尺寸(${fileSize / (1024 * 1024)} mb)超过最大尺寸(${maxSize / (1024 * 1024)} mb)。`);
}
}
if (!(uploadTempPermits === null || uploadTempPermits === void 0 ? void 0 : uploadTempPermits.length)) {
throw new Error("token 获取失败。");
}
const [permit] = uploadTempPermits.sort((a, b) => b.qos - a.qos);
return permit;
});
}
post(options) {
const startTime = Date.now();
const { fileFormat, onInstanceCreated } = options, rest = __rest(options, ["fileFormat", "onInstanceCreated"]);
return this.getPermit(options.Body, options.fileInfo, fileFormat)
.then((permit) => {
const [fileId] = permit.fileIds;
const cosInitParams = {
Domain: permit.uploadAddr,
getAuthorization(_, callback) {
callback({
TmpSecretId: permit.secretId || "null",
TmpSecretKey: permit.secretKey || "null",
SecurityToken: permit.token,
StartTime: Math.floor(startTime / 1000),
ExpiredTime: Math.floor(permit.expireTime / 1000),
});
},
};
if (permit.bucket === "unknown") {
cosInitParams.AppId = "1251524319";
}
const cos = new cos_nodejs_sdk_v5_1.default(cosInitParams);
if (onInstanceCreated) {
onInstanceCreated(cos);
}
return cos
.uploadFile(Object.assign({ Bucket: permit.bucket, Region: permit.region, Key: fileId }, rest))
.then((resp) => {
const payload = Object.assign(Object.assign({}, resp), { bizName: this.bizName, scene: this.scene, cloudType: permit.cloudType, fileId, sliceSize: rest.SliceSize || 1048576, isSlice: options.fileInfo.size > (rest.SliceSize || 1048576), tokenFail: false });
if (permit.cdnDomain) {
const cdnLink = `${permit.cdnDomain}/${fileId}`;
payload.cdnDomain = permit.cdnDomain;
payload.url = cdnLink;
payload.previewUrl = cdnLink;
payload.staticUrl = cdnLink;
}
if (resp === null || resp === void 0 ? void 0 : resp.headers) {
Object.keys(resp.headers).forEach((v) => {
var _a;
if (v.startsWith("x-ros-")) {
const key = (0, to_hump_1.toHump)(v.split("x-ros-")[1]);
payload[key] = (_a = resp.headers) === null || _a === void 0 ? void 0 : _a[v];
}
});
}
return payload;
})
.catch((e) => {
throw e;
});
})
.catch((e) => {
throw e;
});
}
}
exports.Uploader = Uploader;