diff --git a/src/aspects/AspectDict.ts b/src/aspects/AspectDict.ts index 7b23674b0..d26e9f44b 100644 --- a/src/aspects/AspectDict.ts +++ b/src/aspects/AspectDict.ts @@ -1,7 +1,7 @@ import { NativeEnv, WebEnv, WechatMpEnv } from 'oak-domain/lib/types/Environment'; import { AppType } from '../oak-app-domain/Application/Schema'; import { EntityDict } from '../oak-app-domain'; -import { Config, Origin } from '../types/Config'; +import { Config, AccountOrigin } from '../types/Config'; import { Style } from '../types/Style'; import { MediaType, MaterialType } from '../types/WeChat'; import { BackendRuntimeContext } from '../context/BackendRuntimeContext'; diff --git a/src/entities/Application.ts b/src/entities/Application.ts index 4a43591e4..8d180f961 100644 --- a/src/entities/Application.ts +++ b/src/entities/Application.ts @@ -6,6 +6,7 @@ import { Schema as Domain } from './Domain'; import { Style } from '../types/Style'; import { EntityDesc } from 'oak-domain/lib/types/EntityDesc'; +import { CosOrigin } from '../types/Config'; export type AppType = 'web' | 'wechatMp' | 'wechatPublic' | 'native'; export type WechatMpConfig = { @@ -21,6 +22,9 @@ export type WechatMpConfig = { mode: 'clear' | 'compatible' | 'safe'; //消息加解密方式 明文模式 兼容模式 安全模式 dataFormat: 'json' | 'xml'; }; + cos?: { + defaultOrigin: CosOrigin; //默认使用的cos存储源 + }; }; export type WebConfig = { @@ -39,7 +43,10 @@ export type WebConfig = { protocol: 'http:' | 'https:'; hostname: string; port: string; - } + }; + cos?: { + defaultOrigin: CosOrigin; //默认使用的cos存储源 + }; }; @@ -64,7 +71,10 @@ export type WechatPublicConfig = { protocol: 'http:' | 'https:'; hostname: string; port: string; - } + }; + cos?: { + defaultOrigin: CosOrigin; //默认使用的cos存储源 + }; }; export type NativeConfig = { @@ -78,7 +88,10 @@ export type NativeConfig = { protocol: 'http:' | 'https:'; hostname: string; port: string; - } + }; + cos?: { + defaultOrigin: CosOrigin; //默认使用的cos存储源 + }; }; type Versions = string[]; diff --git a/src/entities/ExtraFile.ts b/src/entities/ExtraFile.ts index 5981ca0f6..a18f3fcae 100644 --- a/src/entities/ExtraFile.ts +++ b/src/entities/ExtraFile.ts @@ -2,9 +2,10 @@ import { String, Int, Text, Float, Boolean } from 'oak-domain/lib/types/DataType import { EntityShape } from 'oak-domain/lib/types/Entity'; import { EntityDesc } from 'oak-domain/lib/types/EntityDesc'; import { Schema as Application } from './Application'; +import { CosOrigin } from '../types/Config'; export interface Schema extends EntityShape { - origin: 'qiniu' | 'wechat' | 'ctyun' | 'aliyun' | 'tencent' | 'local' | 'unknown' | 's3'; + origin: CosOrigin; type: 'image' | 'video' | 'audio' | 'file'; bucket?: String<32>; // 七牛、腾讯、阿里、天翼,其它cos可忽略 objectId?: String<64>; // 七牛、腾讯、阿里、天翼,其它cos可忽略 diff --git a/src/triggers/extraFile.ts b/src/triggers/extraFile.ts index c43a21f52..10b3eeec5 100644 --- a/src/triggers/extraFile.ts +++ b/src/triggers/extraFile.ts @@ -6,6 +6,7 @@ import { OakException } from 'oak-domain/lib/types/Exception'; import { RemoveTrigger } from 'oak-domain/lib/types/Trigger'; import { BRC } from '../types/RuntimeCxt'; import { applicationProjection } from '../types/Projection'; +import assert from 'assert'; const triggers: Trigger>[] = [ { @@ -16,20 +17,56 @@ const triggers: Trigger>[] = [ fn: async ({ operation }, context) => { const { data } = operation; + const [application] = await context.select("application", { + data: { + config: { + cos: { + defaultOrigin: 1, + } + }, + system: { + config: { + Cos: { + defaultOrigin: 1, + } + }, + platform: { + config: { + Cos: { + defaultOrigin: 1, + } + } + } + } + }, + filter: { + id: context.getApplicationId()!, + }, + }, {}); + + assert(application, `找不到 当前应用程序`); + const formMeta = async (data: EntityDict['extraFile']['OpSchema']): Promise => { - const { origin } = data; + + const configOrigin = data.origin || + application!.config?.cos?.defaultOrigin || + application!.system?.config?.Cos?.defaultOrigin || + application!.system?.platform?.config?.Cos?.defaultOrigin; + + assert(configOrigin, `extraFile的origin未指定,且应用程序、系统配置、平台配置中也没有默认的Cos源,请检查`); + if (!data.applicationId) { data.applicationId = context.getApplicationId()!; } - if (origin === 'unknown') { + if (configOrigin === 'unknown') { Object.assign(data, { uploadState: 'success', }); return; } - const cos = getCosBackend(origin!); + const cos = getCosBackend(configOrigin!); if (!cos) { - throw new OakException(`origin为${origin}的extraFile没有定义Cos类,请调用registerCos注入`); + throw new OakException(`origin为${configOrigin}的extraFile没有定义Cos类,请调用registerCos注入`); } await cos.formUploadMeta(context.getApplication() as EntityDict['application']['Schema'], data); Object.assign(data, { diff --git a/src/types/Config.ts b/src/types/Config.ts index 74167dc4c..75eea59dc 100644 --- a/src/types/Config.ts +++ b/src/types/Config.ts @@ -204,6 +204,7 @@ export type Config = { tencent?: TencentYunCosConfig; local?: LocalCosConfig; s3?: S3CosConfig; + defaultOrigin?: CosOrigin; //默认存储源 }; Live?: { qiniu?: QiniuLiveConfig; @@ -238,5 +239,6 @@ export type Config = { } }; -export type Origin = 'ali' | 'tencent' | 'qiniu' | 'amap' | 'ctyun' | 'local'; +export type AccountOrigin = 'ali' | 'tencent' | 'qiniu' | 'amap' | 'ctyun' | 'local' | 's3'; +export type CosOrigin = 'qiniu' | 'wechat' | 'ctyun' | 'aliyun' | 'tencent' | 'local' | 'unknown' | 's3'; export type Service = 'Map' | 'Cos' | 'Live' | 'Sms' | 'Emails'; \ No newline at end of file diff --git a/src/utils/getContextConfig.ts b/src/utils/getContextConfig.ts index e0a26d24c..fcd7f93b2 100644 --- a/src/utils/getContextConfig.ts +++ b/src/utils/getContextConfig.ts @@ -1,16 +1,17 @@ import { assert } from 'oak-domain/lib/utils/assert'; import { OakDataException } from 'oak-domain/lib/types/Exception'; -import { AmapSDK, QiniuSDK, CTYunSDK, ALiYunSDK, TencentYunSDK, LocalSDK } from 'oak-external-sdk'; +import { AmapSDK, QiniuSDK, CTYunSDK, ALiYunSDK, TencentYunSDK, LocalSDK, S3SDK } from 'oak-external-sdk'; import { AliCloudConfig, AmapCloudConfig, - Origin, + AccountOrigin, QiniuCloudConfig, Service, TencentCloudConfig, CTYunCloudConfig, Config, LocalCloudConfig, + S3AccountConfig, } from '../types/Config'; /** @@ -19,7 +20,7 @@ import { * @param origin * @returns */ -export function getConfig(systemConfig: Config, service: Service, origin: Origin) { +export function getConfig(systemConfig: Config, service: Service, origin: AccountOrigin) { const serviceConfig = systemConfig[service]; let originConfig = serviceConfig && (serviceConfig as any)[origin]; let originCloudAccounts = originConfig && systemConfig.Account && systemConfig.Account[origin]; @@ -115,6 +116,23 @@ export function getConfig(systemConfig: Config, service: Service, origin: Origin config: originConfig, }; } + case 's3': { + const s3Account = ( + originCloudAccounts as S3AccountConfig[] + ).find((ele) => ele.accessKey === originConfig.accessKey); + assert( + s3Account, + `调用的服务${service}源${origin}找不到相应的平台帐号,请联系管理员` + ); + const s3Instance = S3SDK.getInstance( + s3Account!.accessKey, + s3Account!.secretKey + ); + return { + instance: s3Instance, + config: originConfig, + }; + } default: { assert(origin === 'amap'); const amapAccount = (originCloudAccounts as AmapCloudConfig[]).find( diff --git a/src/utils/livestream.ts b/src/utils/livestream.ts index 754d85f38..d531b1bf8 100644 --- a/src/utils/livestream.ts +++ b/src/utils/livestream.ts @@ -2,7 +2,7 @@ import { EntityDict as BaseEntityDict } from 'oak-domain/lib/types/Entity'; import { assert } from 'oak-domain/lib/utils/assert'; import { EntityDict } from '../oak-app-domain'; import { Schema as Livestream } from '../oak-app-domain/Livestream/Schema'; -import { Origin, QiniuLiveConfig } from '../types/Config'; +import { AccountOrigin, QiniuLiveConfig } from '../types/Config'; import { getConfig } from './getContextConfig'; import { QiniuCloudInstance } from 'oak-external-sdk/es/service/qiniu/QiniuCloud'; import { BackendRuntimeContext } from '../context/BackendRuntimeContext'; @@ -17,7 +17,7 @@ import { cloneDeep } from 'oak-domain/lib/utils/lodash'; */ export async function getLivestream>( params: { - origin: Origin; + origin: AccountOrigin; streamTitle: string, expireAt: number, }, @@ -61,7 +61,7 @@ export async function getLivestream>( params: { - origin: Origin; + origin: AccountOrigin; streamTitle: string; expireAt: number; }, @@ -98,7 +98,7 @@ export async function getStreamObj>( params: { - origin: Origin; + origin: AccountOrigin; streamTitle: string; start: number; end: number;