feat: 在application, system, platform中新增了defaultOrigin的配置,并在extrafile的create-before trigger中获取配置并应用

This commit is contained in:
Pan Qiancheng 2025-10-24 14:04:34 +08:00
parent 71b25a2a54
commit aef6942a57
7 changed files with 88 additions and 17 deletions

View File

@ -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';

View File

@ -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[];

View File

@ -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可忽略

View File

@ -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<EntityDict, 'extraFile', BRC<EntityDict>>[] = [
{
@ -16,20 +17,56 @@ const triggers: Trigger<EntityDict, 'extraFile', BRC<EntityDict>>[] = [
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<void> => {
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, {

View File

@ -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';

View File

@ -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(

View File

@ -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<ED extends EntityDict & BaseEntityDict, Cxt extends BackendRuntimeContext<ED>>(
params: {
origin: Origin;
origin: AccountOrigin;
streamTitle: string,
expireAt: number,
},
@ -61,7 +61,7 @@ export async function getLivestream<ED extends EntityDict & BaseEntityDict, Cxt
*/
export async function getStreamObj<ED extends EntityDict & BaseEntityDict, Cxt extends BackendRuntimeContext<ED>>(
params: {
origin: Origin;
origin: AccountOrigin;
streamTitle: string;
expireAt: number;
},
@ -98,7 +98,7 @@ export async function getStreamObj<ED extends EntityDict & BaseEntityDict, Cxt e
// 生成直播回放
export async function getPlayBackUrl<ED extends EntityDict & BaseEntityDict, Cxt extends BackendRuntimeContext<ED>>(
params: {
origin: Origin;
origin: AccountOrigin;
streamTitle: string;
start: number;
end: number;