61 lines
2.4 KiB
JavaScript
61 lines
2.4 KiB
JavaScript
import { assert } from 'oak-domain/lib/utils/assert';
|
|
import ALiYun from './aliyun';
|
|
import { ALiYunSDK } from 'oak-external-sdk';
|
|
export default class ALiYunBackend extends ALiYun {
|
|
getConfigAndInstance(application) {
|
|
const { config, account } = this.getConfig(application);
|
|
const instance = ALiYunSDK.getInstance(account.accessKeyId, account.accessKeySecret);
|
|
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: aliyunCosConfig } = this.getConfigAndInstance(application);
|
|
const { buckets } = aliyunCosConfig;
|
|
let bucket2 = bucket;
|
|
if (!bucket2) {
|
|
const { defaultBucket } = aliyunCosConfig;
|
|
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: aliyunCosConfig } = this.getConfigAndInstance(application);
|
|
const b = aliyunCosConfig.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: aliyunCosConfig } = this.getConfigAndInstance(application);
|
|
const b = aliyunCosConfig.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;
|
|
}
|
|
}
|
|
}
|