117 lines
4.9 KiB
JavaScript
117 lines
4.9 KiB
JavaScript
import { assert } from 'oak-domain/lib/utils/assert';
|
|
import TencentYun from './tencent';
|
|
import { TencentYunSDK } from 'oak-external-sdk';
|
|
import { OakPreConditionUnsetException } from 'oak-domain/lib/types/Exception';
|
|
export default class TencentYunBackend extends TencentYun {
|
|
getConfigAndInstance(application) {
|
|
const { config, account } = this.getConfig(application);
|
|
const instance = TencentYunSDK.getInstance(account.secretId, account.secretKey);
|
|
return {
|
|
config,
|
|
instance,
|
|
};
|
|
}
|
|
async composeFileUrlBackend(options) {
|
|
const { application, extraFile, context, style } = options;
|
|
const { config: tencentCosConfig } = this.getConfig(application);
|
|
if (tencentCosConfig) {
|
|
let bucket = tencentCosConfig.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: tencentYunCosConfig } = this.getConfigAndInstance(application);
|
|
const { buckets } = tencentYunCosConfig;
|
|
let bucket2 = bucket;
|
|
if (!bucket2) {
|
|
const { defaultBucket } = tencentYunCosConfig;
|
|
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: tencentYunCosConfig } = this.getConfigAndInstance(application);
|
|
const b = tencentYunCosConfig.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: tencentYunCosConfig } = this.getConfigAndInstance(application);
|
|
const b = tencentYunCosConfig.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;
|
|
}
|
|
}
|
|
async composeChunkUploadInfo(application, extraFile, context) {
|
|
throw new OakPreConditionUnsetException('腾讯云暂不支持分片上传');
|
|
return {
|
|
uploadId: '',
|
|
chunkSize: 0,
|
|
partCount: 0,
|
|
partSize: 0,
|
|
parts: [],
|
|
};
|
|
}
|
|
/**
|
|
* 完成分片上传后的合并操作
|
|
*/
|
|
async mergeChunkedUpload(application, extraFile, parts, context) {
|
|
// Implementation here
|
|
}
|
|
async abortMultipartUpload(application, extraFile, context) {
|
|
}
|
|
async listMultipartUploads(application, extraFile, context) {
|
|
return {
|
|
parts: [],
|
|
};
|
|
}
|
|
async presignFile(methods, application, extraFile, context) {
|
|
const key = this.formKey(extraFile);
|
|
const { instance, config: tencentYunCosConfig } = this.getConfigAndInstance(application);
|
|
const b = tencentYunCosConfig.buckets.find((ele) => ele.name === extraFile.bucket);
|
|
assert(b, `extraFile中的bucket名称在腾讯云配置中找不到「${extraFile.bucket}」`);
|
|
return await instance.presignObjectUrl(methods, extraFile.bucket, b.zone, key, {
|
|
expires: 24 * 60 * 60, // 1 day
|
|
});
|
|
}
|
|
presignMultiPartUpload(application, extraFile, from, to, context) {
|
|
throw new OakPreConditionUnsetException('腾讯云暂不支持分片上传预签名');
|
|
}
|
|
prepareChunkedUpload(application, extraFile, context) {
|
|
throw new OakPreConditionUnsetException("腾讯云分片上传请使用composeChunkUploadInfo方法获取上传信息", 'extraFile');
|
|
}
|
|
}
|