feat: add hostname retrieval for backup webdav (#5004)

fix #4705

Signed-off-by: ysicing <i@ysicing.me>
This commit is contained in:
缘生 2025-04-17 21:21:39 +08:00 committed by GitHub
parent 4fa04a801a
commit 53f74725ed
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 14 additions and 4 deletions

View File

@ -129,6 +129,7 @@ export enum IpcChannel {
// system // system
System_GetDeviceType = 'system:getDeviceType', System_GetDeviceType = 'system:getDeviceType',
System_GetHostname = 'system:getHostname',
// events // events
SelectionAction = 'selection-action', SelectionAction = 'selection-action',

View File

@ -58,6 +58,10 @@ if (!app.requestSingleInstanceLock()) {
ipcMain.handle(IpcChannel.System_GetDeviceType, () => { ipcMain.handle(IpcChannel.System_GetDeviceType, () => {
return process.platform === 'darwin' ? 'mac' : process.platform === 'win32' ? 'windows' : 'linux' return process.platform === 'darwin' ? 'mac' : process.platform === 'win32' ? 'windows' : 'linux'
}) })
ipcMain.handle(IpcChannel.System_GetHostname, () => {
return require('os').hostname()
})
}) })
registerProtocolClient(app) registerProtocolClient(app)

View File

@ -34,6 +34,7 @@ declare global {
clearCache: () => Promise<{ success: boolean; error?: string }> clearCache: () => Promise<{ success: boolean; error?: string }>
system: { system: {
getDeviceType: () => Promise<'mac' | 'windows' | 'linux'> getDeviceType: () => Promise<'mac' | 'windows' | 'linux'>
getHostname: () => Promise<string>
} }
zip: { zip: {
compress: (text: string) => Promise<Buffer> compress: (text: string) => Promise<Buffer>

View File

@ -23,7 +23,8 @@ const api = {
openWebsite: (url: string) => ipcRenderer.invoke(IpcChannel.Open_Website, url), openWebsite: (url: string) => ipcRenderer.invoke(IpcChannel.Open_Website, url),
clearCache: () => ipcRenderer.invoke(IpcChannel.App_ClearCache), clearCache: () => ipcRenderer.invoke(IpcChannel.App_ClearCache),
system: { system: {
getDeviceType: () => ipcRenderer.invoke(IpcChannel.System_GetDeviceType) getDeviceType: () => ipcRenderer.invoke(IpcChannel.System_GetDeviceType),
getHostname: () => ipcRenderer.invoke(IpcChannel.System_GetHostname)
}, },
zip: { zip: {
compress: (text: string) => ipcRenderer.invoke(IpcChannel.Zip_Compress, text), compress: (text: string) => ipcRenderer.invoke(IpcChannel.Zip_Compress, text),

View File

@ -42,8 +42,9 @@ export function useWebdavBackupModal({ backupMethod }: { backupMethod?: typeof b
const showBackupModal = useCallback(async () => { const showBackupModal = useCallback(async () => {
// 获取默认文件名 // 获取默认文件名
const deviceType = await window.api.system.getDeviceType() const deviceType = await window.api.system.getDeviceType()
const hostname = await window.api.system.getHostname()
const timestamp = dayjs().format('YYYYMMDDHHmmss') const timestamp = dayjs().format('YYYYMMDDHHmmss')
const defaultFileName = `cherry-studio.${timestamp}.${deviceType}.zip` const defaultFileName = `cherry-studio.${timestamp}.${hostname}.${deviceType}.zip`
setCustomFileName(defaultFileName) setCustomFileName(defaultFileName)
setIsModalVisible(true) setIsModalVisible(true)
}, []) }, [])

View File

@ -84,13 +84,15 @@ export async function backupToWebdav({
const { webdavHost, webdavUser, webdavPass, webdavPath } = store.getState().settings const { webdavHost, webdavUser, webdavPass, webdavPath } = store.getState().settings
let deviceType = 'unknown' let deviceType = 'unknown'
let hostname = 'unknown'
try { try {
deviceType = (await window.api.system.getDeviceType()) || 'unknown' deviceType = (await window.api.system.getDeviceType()) || 'unknown'
hostname = (await window.api.system.getHostname()) || 'unknown'
} catch (error) { } 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 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 finalFileName = backupFileName.endsWith('.zip') ? backupFileName : `${backupFileName}.zip`
const backupData = await getBackupData() const backupData = await getBackupData()