From 53f74725ed9904a32c9edb60d43c45e39d3cde59 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=BC=98=E7=94=9F?= <8605565+ysicing@users.noreply.github.com> Date: Thu, 17 Apr 2025 21:21:39 +0800 Subject: [PATCH] feat: add hostname retrieval for backup webdav (#5004) fix #4705 Signed-off-by: ysicing --- packages/shared/IpcChannel.ts | 1 + src/main/index.ts | 4 ++++ src/preload/index.d.ts | 1 + src/preload/index.ts | 3 ++- src/renderer/src/components/WebdavModals.tsx | 3 ++- src/renderer/src/services/BackupService.ts | 6 ++++-- 6 files changed, 14 insertions(+), 4 deletions(-) diff --git a/packages/shared/IpcChannel.ts b/packages/shared/IpcChannel.ts index 4e55e0b6..733b0f55 100644 --- a/packages/shared/IpcChannel.ts +++ b/packages/shared/IpcChannel.ts @@ -129,6 +129,7 @@ export enum IpcChannel { // system System_GetDeviceType = 'system:getDeviceType', + System_GetHostname = 'system:getHostname', // events SelectionAction = 'selection-action', diff --git a/src/main/index.ts b/src/main/index.ts index 51022257..7285b1b5 100644 --- a/src/main/index.ts +++ b/src/main/index.ts @@ -58,6 +58,10 @@ if (!app.requestSingleInstanceLock()) { ipcMain.handle(IpcChannel.System_GetDeviceType, () => { return process.platform === 'darwin' ? 'mac' : process.platform === 'win32' ? 'windows' : 'linux' }) + + ipcMain.handle(IpcChannel.System_GetHostname, () => { + return require('os').hostname() + }) }) registerProtocolClient(app) diff --git a/src/preload/index.d.ts b/src/preload/index.d.ts index d7fcd590..b0420112 100644 --- a/src/preload/index.d.ts +++ b/src/preload/index.d.ts @@ -34,6 +34,7 @@ declare global { clearCache: () => Promise<{ success: boolean; error?: string }> system: { getDeviceType: () => Promise<'mac' | 'windows' | 'linux'> + getHostname: () => Promise } zip: { compress: (text: string) => Promise diff --git a/src/preload/index.ts b/src/preload/index.ts index d041d157..fb390466 100644 --- a/src/preload/index.ts +++ b/src/preload/index.ts @@ -23,7 +23,8 @@ const api = { openWebsite: (url: string) => ipcRenderer.invoke(IpcChannel.Open_Website, url), clearCache: () => ipcRenderer.invoke(IpcChannel.App_ClearCache), system: { - getDeviceType: () => ipcRenderer.invoke(IpcChannel.System_GetDeviceType) + getDeviceType: () => ipcRenderer.invoke(IpcChannel.System_GetDeviceType), + getHostname: () => ipcRenderer.invoke(IpcChannel.System_GetHostname) }, zip: { compress: (text: string) => ipcRenderer.invoke(IpcChannel.Zip_Compress, text), diff --git a/src/renderer/src/components/WebdavModals.tsx b/src/renderer/src/components/WebdavModals.tsx index 89ad47aa..5d9b1bd1 100644 --- a/src/renderer/src/components/WebdavModals.tsx +++ b/src/renderer/src/components/WebdavModals.tsx @@ -42,8 +42,9 @@ export function useWebdavBackupModal({ backupMethod }: { backupMethod?: typeof b const showBackupModal = useCallback(async () => { // 获取默认文件名 const deviceType = await window.api.system.getDeviceType() + const hostname = await window.api.system.getHostname() const timestamp = dayjs().format('YYYYMMDDHHmmss') - const defaultFileName = `cherry-studio.${timestamp}.${deviceType}.zip` + const defaultFileName = `cherry-studio.${timestamp}.${hostname}.${deviceType}.zip` setCustomFileName(defaultFileName) setIsModalVisible(true) }, []) diff --git a/src/renderer/src/services/BackupService.ts b/src/renderer/src/services/BackupService.ts index 53ca83bd..464346ba 100644 --- a/src/renderer/src/services/BackupService.ts +++ b/src/renderer/src/services/BackupService.ts @@ -84,13 +84,15 @@ export async function backupToWebdav({ const { webdavHost, webdavUser, webdavPass, webdavPath } = store.getState().settings let deviceType = 'unknown' + let hostname = 'unknown' try { deviceType = (await window.api.system.getDeviceType()) || 'unknown' + hostname = (await window.api.system.getHostname()) || 'unknown' } catch (error) { - Logger.error('[Backup] Failed to get device type:', error) + Logger.error('[Backup] Failed to get device type or hostname:', error) } const timestamp = dayjs().format('YYYYMMDDHHmmss') - const backupFileName = customFileName || `cherry-studio.${timestamp}.${deviceType}.zip` + const backupFileName = customFileName || `cherry-studio.${timestamp}.${deviceType}.${hostname}.zip` const finalFileName = backupFileName.endsWith('.zip') ? backupFileName : `${backupFileName}.zip` const backupData = await getBackupData()