oak-general-business/lib/utils/cos/local.js

72 lines
2.6 KiB
JavaScript

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const assert_1 = require("oak-domain/lib/utils/assert");
const Exception_1 = require("../../types/Exception");
const Exception_2 = require("oak-domain/lib/types/Exception");
class Local {
name = 'local';
autoInform() {
return false;
}
getConfig(application) {
const { system } = application;
const { config } = system;
const localConfig = config.Cos?.local;
(0, assert_1.assert)(localConfig);
const { accessKey } = localConfig;
const account = config.Account?.local?.find((ele) => ele.accessKey === accessKey);
(0, assert_1.assert)(account);
return {
config: localConfig,
account
};
}
formKey(extraFile) {
const { id, extension, objectId } = extraFile;
(0, assert_1.assert)(objectId);
return `extraFile/${objectId}${extension ? '.' + extension : ''}`;
}
async upload(options) {
const { extraFile, uploadFn, file, uploadToAspect, getPercent } = options;
(0, assert_1.assert)(extraFile.enableChunkedUpload !== true, '暂不支持分片上传');
let result;
const { applicationId, type, extra2, id } = extraFile;
try {
result = (await uploadToAspect(file, 'file', 'uploadExtraFile', {
applicationId,
extraFileId: id,
}, true));
}
catch (err) {
// 网络错误
throw new Exception_2.OakNetworkException('网络异常,请求失败');
}
const isSuccess = result.success === true;
if (isSuccess) {
return;
}
else {
throw new Exception_1.OakUploadException('文件上传服务器失败');
}
}
composeFileUrl(options) {
const { application, extraFile, style } = options;
const { config: localConfig } = this.getConfig(application);
let bucket = localConfig.buckets.find((ele) => ele.name === extraFile.bucket);
if (bucket) {
const { domain, protocol, nginxPath } = bucket;
let protocol2 = protocol;
if (protocol instanceof Array) {
// protocol存在https: 说明域名有证书
const index = protocol.includes('https:')
? protocol.findIndex((ele) => ele === 'https:')
: 0;
protocol2 = protocol[index];
}
return `${protocol2}//${domain}${nginxPath ? '/' + nginxPath : ''}/${this.formKey(extraFile)}${style ? style : ''}`;
}
return '';
}
}
exports.default = Local;