feat(remote): 下载改成本地面板选择目录
This commit is contained in:
@@ -255,15 +255,17 @@ ipcMain.handle('remote-upload-file', async (_event, serverHost: string, port: nu
|
||||
}
|
||||
});
|
||||
|
||||
ipcMain.handle('remote-download-file', async (_event, serverHost: string, port: number, fileName: string, remotePath: string, password?: string) => {
|
||||
ipcMain.handle('remote-download-file', async (_event, serverHost: string, port: number, fileName: string, remotePath: string, localPath: string, password?: string) => {
|
||||
try {
|
||||
log.info('Remote download params:', { serverHost, port, fileName, remotePath, localPath, password });
|
||||
|
||||
const win = electronState.getMainWindow();
|
||||
if (!win) {
|
||||
throw new Error('No window found');
|
||||
}
|
||||
|
||||
const fullPath = remotePath ? `${remotePath}\\${fileName}` : fileName;
|
||||
let url = `http://${serverHost}:${port}/api/files/${encodeURIComponent(fullPath)}`;
|
||||
const fullRemotePath = remotePath ? `${remotePath}\\${fileName}` : fileName;
|
||||
let url = `http://${serverHost}:${port}/api/files/${encodeURIComponent(fullRemotePath)}`;
|
||||
if (password) {
|
||||
url += `?password=${encodeURIComponent(password)}`;
|
||||
}
|
||||
@@ -275,18 +277,16 @@ ipcMain.handle('remote-download-file', async (_event, serverHost: string, port:
|
||||
|
||||
const buffer = await response.arrayBuffer();
|
||||
|
||||
const { filePath } = await dialog.showSaveDialog(win, {
|
||||
title: '保存文件',
|
||||
defaultPath: fileName,
|
||||
});
|
||||
const targetDir = localPath || 'C:\\';
|
||||
const targetPath = path.join(targetDir, fileName);
|
||||
|
||||
if (!filePath) {
|
||||
return { success: false, canceled: true };
|
||||
if (!fs.existsSync(targetDir)) {
|
||||
fs.mkdirSync(targetDir, { recursive: true });
|
||||
}
|
||||
|
||||
fs.writeFileSync(filePath, Buffer.from(buffer));
|
||||
fs.writeFileSync(targetPath, Buffer.from(buffer));
|
||||
|
||||
return { success: true, filePath };
|
||||
return { success: true, filePath: targetPath };
|
||||
} catch (error: any) {
|
||||
log.error('Remote download failed:', error);
|
||||
return { success: false, error: error.message };
|
||||
|
||||
Reference in New Issue
Block a user