143 lines
6.2 KiB
JavaScript
143 lines
6.2 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 oak_external_sdk_1 = require("oak-external-sdk");
|
||
const qiniu_1 = tslib_1.__importDefault(require("./qiniu"));
|
||
const Exception_1 = require("oak-domain/lib/types/Exception");
|
||
class QiniuBackend extends qiniu_1.default {
|
||
getConfigAndInstance(application) {
|
||
const { config, account } = this.getConfig(application);
|
||
const instance = oak_external_sdk_1.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;
|
||
}
|
||
(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.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);
|
||
(0, assert_1.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 Exception_1.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);
|
||
(0, assert_1.assert)(b, `extraFile中的bucket名称在七牛配置中找不到「${extraFile.bucket}」`);
|
||
try {
|
||
await instance.removeKodoFile(extraFile.bucket, b.zone, key, mockData);
|
||
}
|
||
catch (err) {
|
||
// 七牛如果文件不存在会抛出status = 612的异常
|
||
if (err instanceof Exception_1.OakExternalException) {
|
||
const data = err.data;
|
||
if (data && data.status === 612) {
|
||
return;
|
||
}
|
||
}
|
||
throw err;
|
||
}
|
||
}
|
||
async composeChunkUploadInfo(application, extraFile, context) {
|
||
throw new Exception_1.OakPreConditionUnsetException('七牛云暂不支持分片上传');
|
||
return {
|
||
uploadId: '',
|
||
chunkSize: 0,
|
||
partCount: 0,
|
||
partSize: 0,
|
||
parts: [],
|
||
};
|
||
}
|
||
/**
|
||
* 完成分片上传后的合并操作
|
||
*/
|
||
async mergeChunkedUpload(application, extraFile, parts, 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: [],
|
||
};
|
||
}
|
||
async presignFile(methods, application, extraFile, context) {
|
||
const key = this.formKey(extraFile);
|
||
const { instance, config: qiniuCosConfig } = this.getConfigAndInstance(application);
|
||
const b = qiniuCosConfig.buckets.find((ele) => ele.name === extraFile.bucket);
|
||
(0, assert_1.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 Exception_1.OakPreConditionUnsetException('七牛云暂不支持分片上传预签名');
|
||
}
|
||
prepareChunkedUpload(application, extraFile, context) {
|
||
throw new Exception_1.OakPreConditionUnsetException("七牛云分片上传请使用composeChunkUploadInfo方法获取上传信息", 'extraFile');
|
||
}
|
||
}
|
||
exports.default = QiniuBackend;
|
||
;
|