From b2beb0a00b5b4987cd33a73f1a40ea2201ac0c0b Mon Sep 17 00:00:00 2001 From: zhujingyang <72259332+zjy365@users.noreply.github.com> Date: Wed, 27 Mar 2024 13:24:30 +0800 Subject: [PATCH] feat:template support base64 (#4627) * feat:template support base64 Signed-off-by: jingyang <3161362058@qq.com> * add sharp Signed-off-by: jingyang <3161362058@qq.com> --------- Signed-off-by: jingyang <3161362058@qq.com> --- frontend/pnpm-lock.yaml | 3 +++ frontend/providers/template/package.json | 1 + .../template/src/pages/api/listTemplate.ts | 2 +- .../providers/template/src/utils/json-yaml.ts | 21 +++++++++++++++++++ 4 files changed, 26 insertions(+), 1 deletion(-) diff --git a/frontend/pnpm-lock.yaml b/frontend/pnpm-lock.yaml index 13ff91df64b..e515a165c9a 100644 --- a/frontend/pnpm-lock.yaml +++ b/frontend/pnpm-lock.yaml @@ -1849,6 +1849,9 @@ importers: sealos-desktop-sdk: specifier: workspace:^ version: link:../../packages/client-sdk + sharp: + specifier: ^0.32.6 + version: 0.32.6 swiper: specifier: ^11.0.7 version: 11.0.7 diff --git a/frontend/providers/template/package.json b/frontend/providers/template/package.json index 5da8b8ef0c5..1d6318cae6c 100644 --- a/frontend/providers/template/package.json +++ b/frontend/providers/template/package.json @@ -59,6 +59,7 @@ "remark-unwrap-images": "^3.0.1", "sass": "^1.68.0", "sealos-desktop-sdk": "workspace:^", + "sharp": "^0.32.6", "swiper": "^11.0.7", "zustand": "^4.4.1" }, diff --git a/frontend/providers/template/src/pages/api/listTemplate.ts b/frontend/providers/template/src/pages/api/listTemplate.ts index 20a2564a833..c5cd9913d59 100644 --- a/frontend/providers/template/src/pages/api/listTemplate.ts +++ b/frontend/providers/template/src/pages/api/listTemplate.ts @@ -77,6 +77,6 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse< jsonRes(res, { data: { templates: templates, menuKeys: topKeys.join(',') }, code: 200 }); } catch (error) { - jsonRes(res, { code: 500, data: 'api listTemplate error' }); + jsonRes(res, { code: 500, data: 'api listTemplate error', error: error }); } } diff --git a/frontend/providers/template/src/utils/json-yaml.ts b/frontend/providers/template/src/utils/json-yaml.ts index 112b9bb1c93..0f8e0f995ee 100644 --- a/frontend/providers/template/src/utils/json-yaml.ts +++ b/frontend/providers/template/src/utils/json-yaml.ts @@ -34,12 +34,33 @@ export const parseTemplateString = ( inputs: Record; } ) => { + // support function list + const functionHandlers = [ + { + name: 'base64', + handler: (value: string) => { + const regex = /base64\((.*?)\)/; + const match = value.match(regex); + if (match) { + const token = match[1]; + const value = token.split('.').reduce((obj: any, prop: string) => obj[prop], dataSource); + return Buffer.from(value).toString('base64'); + } + return value; + } + } + ]; + try { const replacedString = sourceString.replace(regex, (match: string, key: string) => { if (dataSource[key] && key.indexOf('.') === -1) { return dataSource[key]; } if (key.indexOf('.') !== -1) { + const hasMatchingFunction = functionHandlers.find(({ name }) => key.includes(`${name}(`)); + if (hasMatchingFunction) { + return hasMatchingFunction.handler(key); + } const value = key.split('.').reduce((obj: any, prop: string) => obj[prop], dataSource); return value !== undefined ? value : match; }