61 lines
2.4 KiB
JavaScript
61 lines
2.4 KiB
JavaScript
import { assert } from 'oak-domain/lib/utils/assert';
|
|
import CTYun from './ctyun';
|
|
import { CTYunSDK } from 'oak-external-sdk';
|
|
export default class CTYunBackend extends CTYun {
|
|
getConfigAndInstance(application) {
|
|
const { config, account } = this.getConfig(application);
|
|
const instance = CTYunSDK.getInstance(account.accessKey, account.securityKey);
|
|
return {
|
|
config,
|
|
instance,
|
|
};
|
|
}
|
|
async composeFileUrlBackend(application, extraFile, context, style) {
|
|
return this.composeFileUrl(application, extraFile, style);
|
|
}
|
|
async formUploadMeta(application, extraFile) {
|
|
const { bucket } = extraFile;
|
|
// 构造文件上传所需的key
|
|
const key = this.formKey(extraFile);
|
|
const { instance, config: ctYunConfig } = this.getConfigAndInstance(application);
|
|
const { buckets } = ctYunConfig;
|
|
let bucket2 = bucket;
|
|
if (!bucket2) {
|
|
const { defaultBucket } = ctYunConfig;
|
|
bucket2 = defaultBucket;
|
|
}
|
|
assert(bucket2);
|
|
const b = buckets.find((ele) => ele.name === bucket2);
|
|
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: ctyunCosConfig } = this.getConfigAndInstance(application);
|
|
const b = ctyunCosConfig.buckets.find((ele) => ele.name === extraFile.bucket);
|
|
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: ctyunCosConfig } = this.getConfigAndInstance(application);
|
|
const b = ctyunCosConfig.buckets.find((ele) => ele.name === extraFile.bucket);
|
|
assert(b, `extraFile中的bucket名称在天翼云配置中找不到「「${extraFile.bucket}」`);
|
|
try {
|
|
await instance.removeFile(extraFile.bucket, b.zone, key);
|
|
}
|
|
catch (err) {
|
|
throw err;
|
|
}
|
|
}
|
|
}
|