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
This commit is contained in:
beyondkmp 2025-07-17 13:15:29 +08:00 committed by GitHub
parent 6bdb157af3
commit ff72c007c0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 22 additions and 12 deletions

View File

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

View File

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