diff --git a/main.js b/main.js index 8b0f058..b248fda 100644 --- a/main.js +++ b/main.js @@ -205,14 +205,26 @@ app.on('ready', () => { } }); // 选择文件夹 - ipcMain.handle('selectFilePath', async (event) => { + ipcMain.handle('selectFilePath', async (event, data) => { const result = await dialog.showOpenDialog({ properties: ['openDirectory'] }); if (result.canceled) { } else { - mainWindow.webContents.send('onFilePath', result.filePaths[0]); + switch (data.key) { + case 'shareFilesPath': + case 'recordingFilesPath': + mainWindow.webContents.send('onFilePath', result.filePaths[0] + '\\', data.key); + break; + default: + mainWindow.webContents.send('downFile', { + downFilePaths: result.filePaths[0] + '\\', + fileName: data.fileName, + filePath: data.filePath, + }); + break; + } } }); // 设置桌面应用基础属性 diff --git a/preload.js b/preload.js index b6f3c3b..a5e9a40 100644 --- a/preload.js +++ b/preload.js @@ -38,11 +38,15 @@ window.electron = { ipcRenderer.invoke('updateDownload', type) }, // 选择文件夹 - selectFilePath: () => { - ipcRenderer.invoke('selectFilePath') + selectFilePath: (data) => { + ipcRenderer.invoke('selectFilePath', data) }, // 发送文件夹路径 onFilePath: (callback) => { ipcRenderer.on('onFilePath', callback) }, + // 下载文件 + downFile: (callback) => { + ipcRenderer.on('downFile', callback) + }, } diff --git a/src/App.tsx b/src/App.tsx index d93feee..528a2c6 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -16,7 +16,8 @@ import * as CryptoJS from 'crypto-js'; import { PostLogin } from "@/api/Login"; import agora from "@/utils/package/agora"; import QuitTips from "@/components/QuitTips"; - +const fs = require('fs').promises; +const { exec } = require('child_process'); const App: React.FC = () => { const navigate = useNavigate(); const { state } = useLocation(); @@ -98,7 +99,6 @@ const App: React.FC = () => { window.dispatchEvent(event); }; window.addEventListener('customStorageChange', handleCustomStorageChange); - return () => { window.removeEventListener('resize', handleResize); window.removeEventListener('customStorageChange', handleCustomStorageChange); @@ -106,6 +106,30 @@ const App: React.FC = () => { window.removeEventListener('offline', handleNetworkChange); }; }, []); + useEffect(() => { + window.electron.downFile(async (_e: any, data: any) => { + const response = await fetch(data.filePath); + const arrayBuffer = await response.arrayBuffer(); + const buffer = Buffer.from(arrayBuffer); + fs.writeFile(`${data.downFilePaths}${data.fileName}`, buffer, {}); + message.success(`下载成功!文件已保存至:${data.downFilePaths}`) + await fs.access(data.downFilePaths, fs.constants.F_OK); + if (process.platform === 'win32') { + exec(`explorer "${data.downFilePaths}"`); + } else if (process.platform === 'darwin') { + exec(`open "${data.downFilePaths}"`); + } + }) + window.electron.onFilePath(async (_e: any, filePath: string, key: string) => { + const setting = await JSON.parse(storage.getItem('setting') as string) + if (key === 'recordingFilesPath') { + setting.recordingFilesPath = filePath + } else { + setting.shareFilesPath = filePath + } + storage.setItem('setting', JSON.stringify(setting)) + }) + }, []) useEffect(() => { onInvitation((item: any) => { switch (item.key) { diff --git a/src/components/SharedFilesModel/index.tsx b/src/components/SharedFilesModel/index.tsx index 3b38744..d237db2 100644 --- a/src/components/SharedFilesModel/index.tsx +++ b/src/components/SharedFilesModel/index.tsx @@ -1,7 +1,6 @@ import styles from '@/components/SharedFilesModel/index.module.scss' import { DeleteOutlined, - FolderOutlined, ProfileOutlined, ReloadOutlined, SearchOutlined, @@ -277,28 +276,37 @@ const SharedFilesModel = forwardRef((props: any, ref: any) => { data: fileItem, }) const setting = await JSON.parse(storage.getItem('setting') as string) - try { - await fs.access(setting.shareFilesPath, fs.constants.F_OK) - const response = await fetch(res.data); - const arrayBuffer = await response.arrayBuffer(); - const buffer = Buffer.from(arrayBuffer); - fs.writeFile(`${setting.shareFilesPath}${fileItem[fileIndex].fileName}`, buffer, {}); - message.success(`下载成功!文件已保存至:${setting.shareFilesPath}`) - await fs.access(setting.shareFilesPath, fs.constants.F_OK); - if (process.platform === 'win32') { - exec(`explorer "${setting.shareFilesPath}"`); - } else if (process.platform === 'darwin') { - exec(`open "${setting.shareFilesPath}"`); - } - getRoomFile() - } catch (error: any) { - if (error.code === 'ENOENT') { - message.error('文件夹不存在!') - return - } else { - message.error(error) + if (setting.isShareSavePath) { + window.electron.selectFilePath({ + fileName: fileItem[fileIndex].fileName, + filePath: res.data + }) + } else { + try { + await fs.access(setting.shareFilesPath, fs.constants.F_OK) + const response = await fetch(res.data); + const arrayBuffer = await response.arrayBuffer(); + const buffer = Buffer.from(arrayBuffer); + fs.writeFile(`${setting.shareFilesPath}${fileItem[fileIndex].fileName}`, buffer, {}); + message.success(`下载成功!文件已保存至:${setting.shareFilesPath}`) + await fs.access(setting.shareFilesPath, fs.constants.F_OK); + if (process.platform === 'win32') { + exec(`explorer "${setting.shareFilesPath}"`); + } else if (process.platform === 'darwin') { + exec(`open "${setting.shareFilesPath}"`); + } + } catch (error: any) { + if (error.code === 'ENOENT') { + message.error('文件夹不存在!') + return + } else { + message.error(error) + } } } + setTimeout(() => { + getRoomFile() + }, 2000) }) } }) diff --git a/src/components/StupWizard/index.tsx b/src/components/StupWizard/index.tsx index c216f4d..91ede85 100644 --- a/src/components/StupWizard/index.tsx +++ b/src/components/StupWizard/index.tsx @@ -385,13 +385,17 @@ const RecordingComponents = () => { } else { setFilePath(setting.recordingFilesPath); } - window.electron.onFilePath(async (_e: any, filePath: string) => { - setFilePath(filePath) - const setting = await JSON.parse(storage.getItem('setting') as string) - setting.recordingFilesPath = filePath - storage.setItem('setting', JSON.stringify(setting)) - }) + window.addEventListener('customStorageChange', handleCustomStorageChange); + return () => { + window.removeEventListener('customStorageChange', handleCustomStorageChange); + }; }, []) + const handleCustomStorageChange = (e: any): void => { + if (e.key === 'setting') { + const setting = JSON.parse(storage.getItem('setting') as string) + setFilePath(setting.recordingFilesPath) + } + }; return ( <>