100 lines
3.7 KiB
JavaScript
100 lines
3.7 KiB
JavaScript
"use strict";
|
||
Object.defineProperty(exports, "__esModule", { value: true });
|
||
const assert_1 = require("oak-domain/lib/utils/assert");
|
||
const Exception_1 = require("../../types/Exception");
|
||
const Exception_2 = require("oak-domain/lib/types/Exception");
|
||
class Qiniu {
|
||
name = 'qiniu';
|
||
autoInform() {
|
||
return false;
|
||
}
|
||
getConfig(application) {
|
||
const { system } = application;
|
||
const { config } = system;
|
||
const qiniuConfig = config.Cos?.qiniu;
|
||
(0, assert_1.assert)(qiniuConfig);
|
||
const { accessKey } = qiniuConfig;
|
||
const account = config.Account?.qiniu?.find(ele => ele.accessKey === accessKey);
|
||
(0, assert_1.assert)(account);
|
||
return {
|
||
config: qiniuConfig,
|
||
account,
|
||
};
|
||
}
|
||
formKey(extraFile) {
|
||
const { id, extension, objectId } = extraFile;
|
||
(0, assert_1.assert)(objectId);
|
||
return `extraFile/${objectId}${extension ? '.' + extension : ''}`;
|
||
}
|
||
async upload(options) {
|
||
const { extraFile, uploadFn, file, uploadToAspect, getPercent } = options;
|
||
const uploadMeta = extraFile.uploadMeta;
|
||
(0, assert_1.assert)(extraFile.enableChunkedUpload !== true, '暂不支持分片上传');
|
||
let response;
|
||
try {
|
||
response = await uploadFn({
|
||
file,
|
||
name: 'file',
|
||
uploadUrl: uploadMeta.uploadHost,
|
||
formData: {
|
||
key: uploadMeta.key,
|
||
token: uploadMeta.uploadToken,
|
||
},
|
||
autoInform: true,
|
||
getPercent,
|
||
uploadId: extraFile.id,
|
||
});
|
||
}
|
||
catch (err) {
|
||
// 网络错误
|
||
throw new Exception_2.OakNetworkException('网络异常,请求失败');
|
||
}
|
||
let isSuccess = false;
|
||
if (process.env.OAK_PLATFORM === 'wechatMp') {
|
||
// 小程序端上传 使用wx.uploadFile
|
||
if (response.errMsg === 'uploadFile:ok') {
|
||
const data = JSON.parse(response.data);
|
||
isSuccess = !!(data.success === true || data.key);
|
||
}
|
||
}
|
||
else {
|
||
const data = await response.json();
|
||
isSuccess = !!(data.success === true || data.key);
|
||
}
|
||
// 解析回调
|
||
if (isSuccess) {
|
||
return;
|
||
}
|
||
else {
|
||
throw new Exception_1.OakUploadException('文件上传七牛失败');
|
||
}
|
||
}
|
||
// style 多媒体样式
|
||
// 访问处理后的文件
|
||
// 对于图片文件 1.png ,访问样式链接 https://{domain}/1.png-small.jpg,即可获得处理后的 jpg 格式图片
|
||
// "-"是默认的样式分隔符,
|
||
// 查看文档 https://developer.qiniu.com/kodo/8558/set-the-picture-style
|
||
composeFileUrl(options) {
|
||
const { application, extraFile, 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 '';
|
||
}
|
||
}
|
||
exports.default = Qiniu;
|
||
;
|