From 1504718329ee808c81167982bbb126eb10df6413 Mon Sep 17 00:00:00 2001 From: wkj <278599135@.com> Date: Fri, 15 Dec 2023 11:11:25 +0800 Subject: [PATCH] =?UTF-8?q?composeFileUrl=20application=E6=9C=AA=E5=8F=96?= =?UTF-8?q?=E5=BE=97=E6=97=B6=20=E5=85=88=E8=BF=94=E5=9B=9E=E7=A9=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- es/utils/cos/ctyun.js | 42 ++++++++++++--------- es/utils/cos/qiniu.js | 70 +++++++++++++++++++++++----------- lib/utils/cos/ctyun.js | 4 ++ lib/utils/cos/qiniu.js | 4 ++ src/utils/cos/ctyun.ts | 4 ++ src/utils/cos/qiniu.ts | 86 +++++++++++++++++++++++++++--------------- 6 files changed, 140 insertions(+), 70 deletions(-) diff --git a/es/utils/cos/ctyun.js b/es/utils/cos/ctyun.js index 6feeabec5..770a7c05c 100644 --- a/es/utils/cos/ctyun.js +++ b/es/utils/cos/ctyun.js @@ -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) {} } ; diff --git a/es/utils/cos/qiniu.js b/es/utils/cos/qiniu.js index 1d268784f..cd1f60aa5 100644 --- a/es/utils/cos/qiniu.js +++ b/es/utils/cos/qiniu.js @@ -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; diff --git a/lib/utils/cos/ctyun.js b/lib/utils/cos/ctyun.js index c0b440ff5..9c2bf2b60 100644 --- a/lib/utils/cos/ctyun.js +++ b/lib/utils/cos/ctyun.js @@ -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); diff --git a/lib/utils/cos/qiniu.js b/lib/utils/cos/qiniu.js index c56df9a63..625576631 100644 --- a/lib/utils/cos/qiniu.js +++ b/lib/utils/cos/qiniu.js @@ -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); diff --git a/src/utils/cos/ctyun.ts b/src/utils/cos/ctyun.ts index fb2fe7345..5860cad47 100644 --- a/src/utils/cos/ctyun.ts +++ b/src/utils/cos/ctyun.ts @@ -108,6 +108,10 @@ export default class CTYun implements Cos { context: FRC, style?: string, ) { + const application = context.getApplication(); + if (!application) { + return '' + } const { config } = getConfig(context, 'Cos', 'ctyun'); if (config) { diff --git a/src/utils/cos/qiniu.ts b/src/utils/cos/qiniu.ts index 9e0855908..b9ce6e6e5 100644 --- a/src/utils/cos/qiniu.ts +++ b/src/utils/cos/qiniu.ts @@ -25,14 +25,15 @@ export default class Qiniu implements Cos { 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(context, 'Cos', 'qiniu'); + const { instance, config } = getConfig( + context, + 'Cos', + 'qiniu' + ); const { buckets } = config as QiniuCosConfig; let bucket2 = bucket; @@ -41,7 +42,7 @@ export default class Qiniu implements Cos { 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 { 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 { composeFileUrl( extraFile: ED['extraFile']['OpSchema'], context: FRC, - style?: string, + style?: string ) { + const application = context.getApplication(); + if (!application) { + return ''; + } const { config } = getConfig(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 { return ''; } - async checkWhetherSuccess( - extraFile: OpSchema, - context: BRC - ) { + async checkWhetherSuccess(extraFile: OpSchema, context: BRC) { const key = this.formKey(extraFile); - const { instance, config } = getConfig(context, 'Cos', 'qiniu'); + const { instance, config } = getConfig( + 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 { } } - async removeFile(extraFile: OpSchema, context: BRC) { const key = this.formKey(extraFile); - const { instance, config } = getConfig(context, 'Cos', 'qiniu'); + const { instance, config } = getConfig( + 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;