composeFileUrl application未取得时 先返回空

This commit is contained in:
wkj 2023-12-15 11:11:25 +08:00
parent ebf3210c8f
commit 1504718329
6 changed files with 140 additions and 70 deletions

View File

@ -24,7 +24,7 @@ export default class CTYun {
bucket2 = defaultBucket;
}
assert(bucket2);
const b = buckets.find(ele => ele.name === bucket2);
const b = buckets.find((ele) => ele.name === bucket2);
assert(b, `${bucket2}不是一个有效的桶配置`);
Object.assign(extraFile, {
bucket: bucket2,
@ -35,41 +35,50 @@ export default class CTYun {
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);
}
catch (err) {
response = await uploadFn(
file,
'file',
uploadMeta.uploadHost,
{
key: uploadMeta.key,
Policy: uploadMeta.policy,
AWSAccessKeyId: uploadMeta.accessKey,
signature: uploadMeta.signature,
},
true
);
} catch (err) {
// 网络错误
throw new OakNetworkException('网络异常,请求失败');
}
let isSuccess = false;
if (process.env.OAK_PLATFORM === 'wechatMp') {
// 小程序端上传 使用wx.uploadFile
// 小程序端上传 使用wx.uploadFile
// 待测试
if (response.errMsg === 'uploadFile:ok') {
const data = JSON.parse(response.data);
isSuccess = !!(data.status === 204);
}
}
else {
} else {
isSuccess = !!(response.status === 204);
}
// 解析回调
if (isSuccess) {
return;
}
else {
} else {
throw new OakUploadException('图片上传天翼云失败');
}
}
composeFileUrl(extraFile, context, style) {
const application = context.getApplication();
if (!application) {
return '';
}
const { config } = getConfig(context, 'Cos', 'ctyun');
if (config) {
let bucket = config.buckets.find((ele) => ele.name === extraFile.bucket);
let bucket = config.buckets.find(
(ele) => ele.name === extraFile.bucket
);
if (bucket) {
const { domain, protocol } = bucket;
let protocol2 = protocol;
@ -88,7 +97,6 @@ export default class CTYun {
async checkWhetherSuccess(extraFile, context) {
return true;
}
async removeFile(extraFile, context) {
}
async removeFile(extraFile, context) {}
}
;

View File

@ -24,7 +24,7 @@ export default class Qiniu {
bucket2 = defaultBucket;
}
assert(bucket2);
const b = buckets.find(ele => ele.name === bucket2);
const b = buckets.find((ele) => ele.name === bucket2);
assert(b, `${bucket2}不是一个有效的桶配置`);
Object.assign(extraFile, {
bucket: bucket2,
@ -35,12 +35,17 @@ export default class Qiniu {
const uploadMeta = extraFile.uploadMeta;
let response;
try {
response = await uploadFn(file, 'file', uploadMeta.uploadHost, {
key: uploadMeta.key,
token: uploadMeta.uploadToken,
}, true);
}
catch (err) {
response = await uploadFn(
file,
'file',
uploadMeta.uploadHost,
{
key: uploadMeta.key,
token: uploadMeta.uploadToken,
},
true
);
} catch (err) {
// 网络错误
throw new OakNetworkException('网络异常,请求失败');
}
@ -51,23 +56,27 @@ export default class Qiniu {
const data = JSON.parse(response.data);
isSuccess = !!(data.success === true || data.key);
}
}
else {
} else {
const data = await response.json();
isSuccess = !!(data.success === true || data.key);
}
// 解析回调
if (isSuccess) {
return;
}
else {
} else {
throw new OakUploadException('图片上传七牛失败');
}
}
composeFileUrl(extraFile, context, style) {
const application = context.getApplication();
if (!application) {
return '';
}
const { config } = getConfig(context, 'Cos', 'qiniu');
if (config) {
let bucket = config.buckets.find((ele) => ele.name === extraFile.bucket);
let bucket = config.buckets.find(
(ele) => ele.name === extraFile.bucket
);
if (bucket) {
const { domain, protocol } = bucket;
let protocol2 = protocol;
@ -87,15 +96,23 @@ export default class Qiniu {
const key = this.formKey(extraFile);
const { instance, config } = getConfig(context, 'Cos', 'qiniu');
// web环境下访问不了七牛接口用mockData过
const mockData = process.env.OAK_PLATFORM === 'web' ? { fsize: 100 } : undefined;
const b = config.buckets.find(ele => ele.name === extraFile.bucket);
assert(b, `extraFile中的bucket名称在七牛配置中找不到「${extraFile.bucket}`);
const mockData =
process.env.OAK_PLATFORM === 'web' ? { fsize: 100 } : undefined;
const b = config.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 result = await instance.getKodoFileStat(
extraFile.bucket,
b.zone,
key,
mockData
);
const { fsize } = result;
return fsize > 0;
}
catch (err) {
} catch (err) {
// 七牛如果文件不存在会抛出status 612的异常
if (err instanceof OakExternalException) {
const data = err.data;
@ -111,12 +128,19 @@ export default class Qiniu {
const { instance, config } = getConfig(context, 'Cos', 'qiniu');
// web环境下访问不了七牛接口用mockData过
const mockData = process.env.OAK_PLATFORM === 'web' ? true : undefined;
const b = config.buckets.find(ele => ele.name === extraFile.bucket);
assert(b, `extraFile中的bucket名称在七牛配置中找不到「${extraFile.bucket}`);
const b = config.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) {
await instance.removeKodoFile(
extraFile.bucket,
b.zone,
key,
mockData
);
} catch (err) {
// 七牛如果文件不存在会抛出status 612的异常
if (err instanceof OakExternalException) {
const data = err.data;

View File

@ -69,6 +69,10 @@ class CTYun {
}
}
composeFileUrl(extraFile, context, style) {
const application = context.getApplication();
if (!application) {
return '';
}
const { config } = (0, getContextConfig_1.getConfig)(context, 'Cos', 'ctyun');
if (config) {
let bucket = config.buckets.find((ele) => ele.name === extraFile.bucket);

View File

@ -67,6 +67,10 @@ class Qiniu {
}
}
composeFileUrl(extraFile, context, style) {
const application = context.getApplication();
if (!application) {
return '';
}
const { config } = (0, getContextConfig_1.getConfig)(context, 'Cos', 'qiniu');
if (config) {
let bucket = config.buckets.find((ele) => ele.name === extraFile.bucket);

View File

@ -108,6 +108,10 @@ export default class CTYun implements Cos<ED, BRC, FRC> {
context: FRC,
style?: string,
) {
const application = context.getApplication();
if (!application) {
return ''
}
const { config } = getConfig<ED, BRC, FRC>(context, 'Cos', 'ctyun');
if (config) {

View File

@ -25,14 +25,15 @@ export default class Qiniu implements Cos<ED, BRC, FRC> {
return `extraFile/${objectId}${extension ? '.' + extension : ''}`;
}
async formUploadMeta(
extraFile: OpSchema,
context: BRC
) {
async formUploadMeta(extraFile: OpSchema, context: BRC) {
const { bucket } = extraFile;
// 构造文件上传所需的key
const key = this.formKey(extraFile);
const { instance, config } = getConfig<ED, BRC, FRC>(context, 'Cos', 'qiniu');
const { instance, config } = getConfig<ED, BRC, FRC>(
context,
'Cos',
'qiniu'
);
const { buckets } = config as QiniuCosConfig;
let bucket2 = bucket;
@ -41,7 +42,7 @@ export default class Qiniu implements Cos<ED, BRC, FRC> {
bucket2 = defaultBucket!;
}
assert(bucket2);
const b = buckets.find(ele => ele.name === bucket2);
const b = buckets.find((ele) => ele.name === bucket2);
assert(b, `${bucket2}不是一个有效的桶配置`);
Object.assign(extraFile, {
bucket: bucket2,
@ -88,10 +89,9 @@ export default class Qiniu implements Cos<ED, BRC, FRC> {
const data = JSON.parse(response.data);
isSuccess = !!(data.success === true || data.key);
}
}
else {
const data = await response.json();
isSuccess = !!(data.success === true || data.key);
} else {
const data = await response.json();
isSuccess = !!(data.success === true || data.key);
}
// 解析回调
if (isSuccess) {
@ -104,12 +104,18 @@ export default class Qiniu implements Cos<ED, BRC, FRC> {
composeFileUrl(
extraFile: ED['extraFile']['OpSchema'],
context: FRC,
style?: string,
style?: string
) {
const application = context.getApplication();
if (!application) {
return '';
}
const { config } = getConfig<ED, BRC, FRC>(context, 'Cos', 'qiniu');
if (config) {
let bucket = (config.buckets as QiniuCosConfig['buckets']).find((ele) => ele.name === extraFile.bucket!);
let bucket = (config.buckets as QiniuCosConfig['buckets']).find(
(ele) => ele.name === extraFile.bucket!
);
if (bucket) {
const { domain, protocol } = bucket!;
let protocol2 = protocol;
@ -128,26 +134,34 @@ export default class Qiniu implements Cos<ED, BRC, FRC> {
return '';
}
async checkWhetherSuccess(
extraFile: OpSchema,
context: BRC
) {
async checkWhetherSuccess(extraFile: OpSchema, context: BRC) {
const key = this.formKey(extraFile);
const { instance, config } = getConfig<ED, BRC, FRC>(context, 'Cos', 'qiniu');
const { instance, config } = getConfig<ED, BRC, FRC>(
context,
'Cos',
'qiniu'
);
// web环境下访问不了七牛接口用mockData过
const mockData = process.env.OAK_PLATFORM === 'web' ? { fsize: 100 } : undefined;
const mockData =
process.env.OAK_PLATFORM === 'web' ? { fsize: 100 } : undefined;
const b = (config as QiniuCosConfig).buckets.find(ele => ele.name === extraFile.bucket);
assert(b, `extraFile中的bucket名称在七牛配置中找不到「${extraFile.bucket}`);
const b = (config as QiniuCosConfig).buckets.find(
(ele) => ele.name === extraFile.bucket
);
assert(
b,
`extraFile中的bucket名称在七牛配置中找不到「${extraFile.bucket}`
);
try {
const result = await (instance as QiniuCloudInstance).getKodoFileStat(extraFile.bucket!, b.zone, key, mockData);
const result = await (
instance as QiniuCloudInstance
).getKodoFileStat(extraFile.bucket!, b.zone, key, mockData);
const { fsize } = result;
return fsize > 0;
}
catch (err: any) {
} catch (err: any) {
// 七牛如果文件不存在会抛出status 612的异常
if (err instanceof OakExternalException) {
const data = err.data;
@ -159,19 +173,31 @@ export default class Qiniu implements Cos<ED, BRC, FRC> {
}
}
async removeFile(extraFile: OpSchema, context: BRC) {
const key = this.formKey(extraFile);
const { instance, config } = getConfig<ED, BRC, FRC>(context, 'Cos', 'qiniu');
const { instance, config } = getConfig<ED, BRC, FRC>(
context,
'Cos',
'qiniu'
);
// web环境下访问不了七牛接口用mockData过
const mockData = process.env.OAK_PLATFORM === 'web' ? true : undefined;
const b = (config as QiniuCosConfig).buckets.find(ele => ele.name === extraFile.bucket);
assert(b, `extraFile中的bucket名称在七牛配置中找不到「${extraFile.bucket}`);
const b = (config as QiniuCosConfig).buckets.find(
(ele) => ele.name === extraFile.bucket
);
assert(
b,
`extraFile中的bucket名称在七牛配置中找不到「${extraFile.bucket}`
);
try {
await (instance as QiniuCloudInstance).removeKodoFile(extraFile.bucket!, b.zone, key, mockData);
}
catch (err: any) {
await (instance as QiniuCloudInstance).removeKodoFile(
extraFile.bucket!,
b.zone,
key,
mockData
);
} catch (err: any) {
// 七牛如果文件不存在会抛出status 612的异常
if (err instanceof OakExternalException) {
const data = err.data;