96 lines
3.9 KiB
JavaScript
96 lines
3.9 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 CTYun {
|
||
name = 'ctyun';
|
||
autoInform() {
|
||
return false;
|
||
}
|
||
getConfig(application) {
|
||
const { system } = application;
|
||
const { config } = system;
|
||
const ctYunConfig = config.Cos?.ctyun;
|
||
(0, assert_1.assert)(ctYunConfig);
|
||
const { accessKey } = ctYunConfig;
|
||
const account = config.Account?.ctyun?.find(ele => ele.accessKey === accessKey);
|
||
(0, assert_1.assert)(account);
|
||
return {
|
||
config: ctYunConfig,
|
||
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;
|
||
let response;
|
||
try {
|
||
response = await uploadFn(file, 'file', uploadMeta.uploadHost, {
|
||
key: uploadMeta.key,
|
||
Policy: uploadMeta.policy,
|
||
AWSAccessKeyId: uploadMeta.accessKey,
|
||
signature: uploadMeta.signature,
|
||
}, true, getPercent, 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.status === 204);
|
||
}
|
||
}
|
||
else {
|
||
isSuccess = !!(response.status === 204);
|
||
}
|
||
// 解析回调
|
||
if (isSuccess) {
|
||
return;
|
||
}
|
||
else {
|
||
throw new Exception_1.OakUploadException('文件上传天翼云失败');
|
||
}
|
||
}
|
||
// style 多媒体样式
|
||
// demo: http://endpoint/bucket/object@oosImage|100w_100h_90q.jpg
|
||
// @oosImage|100w_100h_90q.jpg 多媒体样式
|
||
// 转换字符串分为3部分:初始操作、转换参数、转换格式:
|
||
// 初始操作(如@oosImage|)是一个“@”符号 +“oosImage”+“|”管道符号,后面都为转换字符串。
|
||
// 转换参数(如120w_120h_90q)由一个或多个键值对(以"_"连接)组成,“值”在前“键”在后,“值”为数字类型,“键”为一位字母。
|
||
// 转换格式(如.jpg)是指定图片转换的输出格式,通过指定转换格式,可以对原图处理并返回指定的图片格式。支持的转换格式为: jpg、webp、png、bmp。
|
||
// 对象存储文档 https://www.ctyun.cn/document/10026693/10026878
|
||
composeFileUrl(options) {
|
||
const { application, extraFile, style } = options;
|
||
const { config: ctYunCosConfig } = this.getConfig(application);
|
||
if (ctYunCosConfig) {
|
||
let bucket = ctYunCosConfig.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 = CTYun;
|
||
;
|