124 lines
5.0 KiB
JavaScript
124 lines
5.0 KiB
JavaScript
import { assert } from 'oak-domain/lib/utils/assert';
|
||
import { QiniuSDK } from 'oak-external-sdk';
|
||
import Qiniu from './qiniu';
|
||
import { OakExternalException, OakPreConditionUnsetException } from 'oak-domain/lib/types/Exception';
|
||
export default class QiniuBackend extends Qiniu {
|
||
getConfigAndInstance(application) {
|
||
const { config, account } = this.getConfig(application);
|
||
const instance = QiniuSDK.getInstance(account.accessKey, account.secretKey);
|
||
return {
|
||
config,
|
||
instance,
|
||
};
|
||
}
|
||
async composeFileUrlBackend(options) {
|
||
const { application, extraFile, context, style } = options;
|
||
const { config: qiniuCosConfig } = this.getConfig(application);
|
||
if (qiniuCosConfig) {
|
||
let bucket = qiniuCosConfig.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: qiniuCosConfig } = this.getConfigAndInstance(application);
|
||
const { buckets } = qiniuCosConfig;
|
||
let bucket2 = bucket;
|
||
if (!bucket2) {
|
||
const { defaultBucket } = qiniuCosConfig;
|
||
bucket2 = defaultBucket;
|
||
}
|
||
assert(bucket2);
|
||
const b = buckets.find((ele) => ele.name === bucket2);
|
||
assert(b, `${bucket2}不是一个有效的桶配置`);
|
||
Object.assign(extraFile, {
|
||
bucket: bucket2,
|
||
uploadMeta: instance.getKodoUploadInfo(bucket2, b.zone, key),
|
||
});
|
||
}
|
||
async checkWhetherSuccess(application, extraFile) {
|
||
const key = this.formKey(extraFile);
|
||
const { instance, config: qiniuCosConfig } = this.getConfigAndInstance(application);
|
||
// web环境下访问不了七牛接口,用mockData过
|
||
const mockData = process.env.OAK_PLATFORM === 'web' ? { fsize: 100 } : undefined;
|
||
const b = qiniuCosConfig.buckets.find((ele) => ele.name === extraFile.bucket);
|
||
assert(b, `extraFile中的bucket名称在七牛配置中找不到「${extraFile.bucket}」`);
|
||
try {
|
||
const result = await instance.getKodoFileStat(extraFile.bucket, b.zone, key, mockData);
|
||
const { fsize } = result;
|
||
return fsize > 0;
|
||
}
|
||
catch (err) {
|
||
// 七牛如果文件不存在会抛出status = 612的异常
|
||
if (err instanceof OakExternalException) {
|
||
const data = err.data;
|
||
if (data && data.status === 612) {
|
||
return false;
|
||
}
|
||
}
|
||
throw err;
|
||
}
|
||
}
|
||
async removeFile(application, extraFile) {
|
||
const key = this.formKey(extraFile);
|
||
const { instance, config: qiniuCosConfig } = this.getConfigAndInstance(application);
|
||
// web环境下访问不了七牛接口,用mockData过
|
||
const mockData = process.env.OAK_PLATFORM === 'web' ? true : undefined;
|
||
const b = qiniuCosConfig.buckets.find((ele) => ele.name === extraFile.bucket);
|
||
assert(b, `extraFile中的bucket名称在七牛配置中找不到「${extraFile.bucket}」`);
|
||
try {
|
||
await instance.removeKodoFile(extraFile.bucket, b.zone, key, mockData);
|
||
}
|
||
catch (err) {
|
||
// 七牛如果文件不存在会抛出status = 612的异常
|
||
if (err instanceof OakExternalException) {
|
||
const data = err.data;
|
||
if (data && data.status === 612) {
|
||
return;
|
||
}
|
||
}
|
||
throw err;
|
||
}
|
||
}
|
||
async composeChunkUploadInfo(application, extraFile, context) {
|
||
throw new OakPreConditionUnsetException('七牛云暂不支持分片上传');
|
||
return {
|
||
uploadId: '',
|
||
chunkSize: 0,
|
||
partCount: 0,
|
||
partSize: 0,
|
||
parts: [],
|
||
};
|
||
}
|
||
/**
|
||
* 完成分片上传后的合并操作
|
||
*/
|
||
async mergeChunkedUpload(application, extraFile, context) {
|
||
// Implementation here
|
||
}
|
||
async abortMultipartUpload(application, extraFile, context) {
|
||
const key = this.formKey(extraFile);
|
||
const { instance, config: aliyunCosConfig } = this.getConfigAndInstance(application);
|
||
}
|
||
async listMultipartUploads(application, extraFile, context) {
|
||
return {
|
||
parts: [],
|
||
};
|
||
}
|
||
}
|
||
;
|