From ff72c007c03ff47de21a4d0bf52a1ff1fb35cd89 Mon Sep 17 00:00:00 2001 From: beyondkmp Date: Thu, 17 Jul 2025 13:15:29 +0800 Subject: [PATCH] feat: add data parsing functionality in handleProvidersProtocolUrl (#8218) * feat: add data parsing functionality in handleProvidersProtocolUrl - Introduced a new ParseData function to decode and parse base64 encoded data from the URL parameters. - Added error handling to log when data is null or invalid, improving robustness of the handleProvidersProtocolUrl function. * fix: update data parsing in handleProvidersProtocolUrl and ProvidersList - Modified ParseData function to return a JSON string instead of an object for consistency. - Simplified data extraction in ProvidersList by directly parsing the addProviderData without base64 decoding, improving readability and performance. * fix: improve data parsing in handleProvidersProtocolUrl - Updated ParseData function to log the parsed result for better debugging. - Enhanced data extraction by replacing URL-safe characters back to their original form before parsing, ensuring accurate data retrieval. * fix: enhance error logging in ParseData function - Updated the ParseData function to log errors when parsing fails, improving debugging capabilities and robustness in handling invalid data. * format code --- .../services/urlschema/handle-providers.ts | 23 +++++++++++++++++-- .../pages/settings/ProviderSettings/index.tsx | 11 +-------- 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/src/main/services/urlschema/handle-providers.ts b/src/main/services/urlschema/handle-providers.ts index 9a598fc459..f8b0661370 100644 --- a/src/main/services/urlschema/handle-providers.ts +++ b/src/main/services/urlschema/handle-providers.ts @@ -3,6 +3,17 @@ import Logger from 'electron-log' import { windowService } from '../WindowService' +function ParseData(data: string) { + try { + const result = JSON.parse(Buffer.from(data, 'base64').toString('utf-8')) + + return JSON.stringify(result) + } catch (error) { + Logger.error('ParseData error:', { error }) + return null + } +} + export async function handleProvidersProtocolUrl(url: URL) { switch (url.pathname) { case '/api-keys': { @@ -19,7 +30,13 @@ export async function handleProvidersProtocolUrl(url: URL) { // replace + and / to _ and - because + and / are processed by URLSearchParams const processedSearch = url.search.replaceAll('+', '_').replaceAll('/', '-') const params = new URLSearchParams(processedSearch) - const data = params.get('data') + const data = ParseData(params.get('data')?.replaceAll('_', '+').replaceAll('-', '/') || '') + + if (!data) { + Logger.error('handleProvidersProtocolUrl data is null or invalid') + return + } + const mainWindow = windowService.getMainWindow() const version = params.get('v') if (version == '1') { @@ -33,7 +50,9 @@ export async function handleProvidersProtocolUrl(url: URL) { !mainWindow.isDestroyed() && (await mainWindow.webContents.executeJavaScript(`typeof window.navigate === 'function'`)) ) { - mainWindow.webContents.executeJavaScript(`window.navigate('/settings/provider?addProviderData=${data}')`) + mainWindow.webContents.executeJavaScript( + `window.navigate('/settings/provider?addProviderData=${encodeURIComponent(data)}')` + ) if (isMac) { windowService.showMainWindow() diff --git a/src/renderer/src/pages/settings/ProviderSettings/index.tsx b/src/renderer/src/pages/settings/ProviderSettings/index.tsx index a89719c178..d7a3469a86 100644 --- a/src/renderer/src/pages/settings/ProviderSettings/index.tsx +++ b/src/renderer/src/pages/settings/ProviderSettings/index.tsx @@ -238,16 +238,7 @@ const ProvidersList: FC = () => { } try { - const base64Decode = (base64EncodedString: string) => - new TextDecoder().decode(Uint8Array.from(atob(base64EncodedString), (m) => m.charCodeAt(0))) - const { - id, - apiKey: newApiKey, - baseUrl, - type, - name - } = JSON.parse(base64Decode(addProviderData.replaceAll('_', '+').replaceAll('-', '/'))) - + const { id, apiKey: newApiKey, baseUrl, type, name } = JSON.parse(addProviderData) if (!id || !newApiKey || !baseUrl) { window.message.error(t('settings.models.provider_key_add_failed_by_invalid_data')) window.navigate('/settings/provider')