This commit is contained in:
yj 2024-08-08 15:31:06 +08:00
parent c341100d5d
commit 4dc3f10e85
6 changed files with 103 additions and 42 deletions

16
main.js
View File

@ -205,14 +205,26 @@ app.on('ready', () => {
} }
}); });
// 选择文件夹 // 选择文件夹
ipcMain.handle('selectFilePath', async (event) => { ipcMain.handle('selectFilePath', async (event, data) => {
const result = await dialog.showOpenDialog({ const result = await dialog.showOpenDialog({
properties: ['openDirectory'] properties: ['openDirectory']
}); });
if (result.canceled) { if (result.canceled) {
} else { } 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;
}
} }
}); });
// 设置桌面应用基础属性 // 设置桌面应用基础属性

View File

@ -38,11 +38,15 @@ window.electron = {
ipcRenderer.invoke('updateDownload', type) ipcRenderer.invoke('updateDownload', type)
}, },
// 选择文件夹 // 选择文件夹
selectFilePath: () => { selectFilePath: (data) => {
ipcRenderer.invoke('selectFilePath') ipcRenderer.invoke('selectFilePath', data)
}, },
// 发送文件夹路径 // 发送文件夹路径
onFilePath: (callback) => { onFilePath: (callback) => {
ipcRenderer.on('onFilePath', callback) ipcRenderer.on('onFilePath', callback)
}, },
// 下载文件
downFile: (callback) => {
ipcRenderer.on('downFile', callback)
},
} }

View File

@ -16,7 +16,8 @@ import * as CryptoJS from 'crypto-js';
import { PostLogin } from "@/api/Login"; import { PostLogin } from "@/api/Login";
import agora from "@/utils/package/agora"; import agora from "@/utils/package/agora";
import QuitTips from "@/components/QuitTips"; import QuitTips from "@/components/QuitTips";
const fs = require('fs').promises;
const { exec } = require('child_process');
const App: React.FC = () => { const App: React.FC = () => {
const navigate = useNavigate(); const navigate = useNavigate();
const { state } = useLocation(); const { state } = useLocation();
@ -98,7 +99,6 @@ const App: React.FC = () => {
window.dispatchEvent(event); window.dispatchEvent(event);
}; };
window.addEventListener('customStorageChange', handleCustomStorageChange); window.addEventListener('customStorageChange', handleCustomStorageChange);
return () => { return () => {
window.removeEventListener('resize', handleResize); window.removeEventListener('resize', handleResize);
window.removeEventListener('customStorageChange', handleCustomStorageChange); window.removeEventListener('customStorageChange', handleCustomStorageChange);
@ -106,6 +106,30 @@ const App: React.FC = () => {
window.removeEventListener('offline', handleNetworkChange); 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(() => { useEffect(() => {
onInvitation((item: any) => { onInvitation((item: any) => {
switch (item.key) { switch (item.key) {

View File

@ -1,7 +1,6 @@
import styles from '@/components/SharedFilesModel/index.module.scss' import styles from '@/components/SharedFilesModel/index.module.scss'
import { import {
DeleteOutlined, DeleteOutlined,
FolderOutlined,
ProfileOutlined, ProfileOutlined,
ReloadOutlined, ReloadOutlined,
SearchOutlined, SearchOutlined,
@ -277,6 +276,12 @@ const SharedFilesModel = forwardRef((props: any, ref: any) => {
data: fileItem, data: fileItem,
}) })
const setting = await JSON.parse(storage.getItem('setting') as string) const setting = await JSON.parse(storage.getItem('setting') as string)
if (setting.isShareSavePath) {
window.electron.selectFilePath({
fileName: fileItem[fileIndex].fileName,
filePath: res.data
})
} else {
try { try {
await fs.access(setting.shareFilesPath, fs.constants.F_OK) await fs.access(setting.shareFilesPath, fs.constants.F_OK)
const response = await fetch(res.data); const response = await fetch(res.data);
@ -290,7 +295,6 @@ const SharedFilesModel = forwardRef((props: any, ref: any) => {
} else if (process.platform === 'darwin') { } else if (process.platform === 'darwin') {
exec(`open "${setting.shareFilesPath}"`); exec(`open "${setting.shareFilesPath}"`);
} }
getRoomFile()
} catch (error: any) { } catch (error: any) {
if (error.code === 'ENOENT') { if (error.code === 'ENOENT') {
message.error('文件夹不存在!') message.error('文件夹不存在!')
@ -299,6 +303,10 @@ const SharedFilesModel = forwardRef((props: any, ref: any) => {
message.error(error) message.error(error)
} }
} }
}
setTimeout(() => {
getRoomFile()
}, 2000)
}) })
} }
}) })

View File

@ -385,13 +385,17 @@ const RecordingComponents = () => {
} else { } else {
setFilePath(setting.recordingFilesPath); setFilePath(setting.recordingFilesPath);
} }
window.electron.onFilePath(async (_e: any, filePath: string) => { window.addEventListener('customStorageChange', handleCustomStorageChange);
setFilePath(filePath) return () => {
const setting = await JSON.parse(storage.getItem('setting') as string) window.removeEventListener('customStorageChange', handleCustomStorageChange);
setting.recordingFilesPath = filePath };
storage.setItem('setting', JSON.stringify(setting))
})
}, []) }, [])
const handleCustomStorageChange = (e: any): void => {
if (e.key === 'setting') {
const setting = JSON.parse(storage.getItem('setting') as string)
setFilePath(setting.recordingFilesPath)
}
};
return ( return (
<> <>
<div> <div>
@ -412,7 +416,9 @@ const RecordingComponents = () => {
}} }}
/> />
<Button type="primary" onClick={() => { <Button type="primary" onClick={() => {
window.electron.selectFilePath() window.electron.selectFilePath({
key: 'recordingFilesPath',
})
}} style={{ backgroundColor: '#31353A', marginRight: '10px' }}></Button> }} style={{ backgroundColor: '#31353A', marginRight: '10px' }}></Button>
<Button type="primary" onClick={async () => { <Button type="primary" onClick={async () => {
try { try {
@ -449,13 +455,17 @@ const FileComponents = () => {
setFilePath(setting.shareFilesPath); setFilePath(setting.shareFilesPath);
} }
setIsShareSavePath(setting.isShareSavePath) setIsShareSavePath(setting.isShareSavePath)
window.electron.onFilePath(async (_e: any, filePath: string) => { window.addEventListener('customStorageChange', handleCustomStorageChange);
setFilePath(filePath) return () => {
const setting = await JSON.parse(storage.getItem('setting') as string) window.removeEventListener('customStorageChange', handleCustomStorageChange);
setting.shareFilesPath = filePath };
storage.setItem('setting', JSON.stringify(setting))
})
}, []) }, [])
const handleCustomStorageChange = (e: any): void => {
if (e.key === 'setting') {
const setting = JSON.parse(storage.getItem('setting') as string)
setFilePath(setting.shareFilesPath)
}
};
return ( return (
<> <>
<div> <div>
@ -476,7 +486,9 @@ const FileComponents = () => {
}} }}
/> />
<Button type="primary" onClick={() => { <Button type="primary" onClick={() => {
window.electron.selectFilePath() window.electron.selectFilePath({
key: 'shareFilesPath',
})
}} style={{ backgroundColor: '#31353A', marginRight: '10px' }}></Button> }} style={{ backgroundColor: '#31353A', marginRight: '10px' }}></Button>
<Button type="primary" onClick={async () => { <Button type="primary" onClick={async () => {
try { try {

3
src/render.d.ts vendored
View File

@ -8,9 +8,10 @@ export interface IElectronAPI {
onUpdate: (callBack: Function) => void; onUpdate: (callBack: Function) => void;
joinNotification: (data: { name: string, body: string }) => void; joinNotification: (data: { name: string, body: string }) => void;
onDownload: (data: string) => void onDownload: (data: string) => void
selectFilePath: () => void selectFilePath: (data?: any) => void
onFilePath: (callBack: Function) => void; onFilePath: (callBack: Function) => void;
getSources: () => any; getSources: () => any;
downFile: (callBack: Function) => void;
} }
declare global { declare global {