82 lines
3.5 KiB
JavaScript
82 lines
3.5 KiB
JavaScript
"use strict";
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
const tslib_1 = require("tslib");
|
|
const assert_1 = require("oak-domain/lib/utils/assert");
|
|
const aliyun_1 = tslib_1.__importDefault(require("./aliyun"));
|
|
const oak_external_sdk_1 = require("oak-external-sdk");
|
|
class ALiYunBackend extends aliyun_1.default {
|
|
getConfigAndInstance(application) {
|
|
const { config, account } = this.getConfig(application);
|
|
const instance = oak_external_sdk_1.ALiYunSDK.getInstance(account.accessKeyId, account.accessKeySecret);
|
|
return {
|
|
config,
|
|
instance,
|
|
};
|
|
}
|
|
async composeFileUrlBackend(options) {
|
|
const { application, extraFile, context, style } = options;
|
|
const { config: aliyunCosConfig } = this.getConfig(application);
|
|
if (aliyunCosConfig) {
|
|
let bucket = aliyunCosConfig.buckets.find((ele) => ele.name === extraFile.bucket);
|
|
if (bucket) {
|
|
const { domain, protocol } = 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}/${this.formKey(extraFile)}${style ? style : ''}`;
|
|
}
|
|
}
|
|
return '';
|
|
}
|
|
async formUploadMeta(application, extraFile) {
|
|
const { bucket } = extraFile;
|
|
// 构造文件上传所需的key
|
|
const key = this.formKey(extraFile);
|
|
const { instance, config: aliyunCosConfig } = this.getConfigAndInstance(application);
|
|
const { buckets } = aliyunCosConfig;
|
|
let bucket2 = bucket;
|
|
if (!bucket2) {
|
|
const { defaultBucket } = aliyunCosConfig;
|
|
bucket2 = defaultBucket;
|
|
}
|
|
(0, assert_1.assert)(bucket2);
|
|
const b = buckets.find((ele) => ele.name === bucket2);
|
|
(0, assert_1.assert)(b, `${bucket2}不是一个有效的桶配置`);
|
|
Object.assign(extraFile, {
|
|
bucket: bucket2,
|
|
uploadMeta: instance.getUploadInfo(bucket2, b.zone, key),
|
|
});
|
|
}
|
|
async checkWhetherSuccess(application, extraFile) {
|
|
const key = this.formKey(extraFile);
|
|
const { instance, config: aliyunCosConfig } = this.getConfigAndInstance(application);
|
|
const b = aliyunCosConfig.buckets.find((ele) => ele.name === extraFile.bucket);
|
|
(0, assert_1.assert)(b, `extraFile中的bucket名称在阿里云配置中找不到「${extraFile.bucket}」`);
|
|
try {
|
|
const result = await instance.isExistObject(extraFile.bucket, b.zone, key);
|
|
return result;
|
|
}
|
|
catch (err) {
|
|
throw err;
|
|
}
|
|
}
|
|
async removeFile(application, extraFile) {
|
|
const key = this.formKey(extraFile);
|
|
const { instance, config: aliyunCosConfig } = this.getConfigAndInstance(application);
|
|
const b = aliyunCosConfig.buckets.find((ele) => ele.name === extraFile.bucket);
|
|
(0, assert_1.assert)(b, `extraFile中的bucket名称在阿里云配置中找不到「${extraFile.bucket}」`);
|
|
try {
|
|
await instance.removeFile(extraFile.bucket, b.zone, key);
|
|
}
|
|
catch (err) {
|
|
throw err;
|
|
}
|
|
}
|
|
}
|
|
exports.default = ALiYunBackend;
|