@@ -6,17 +6,45 @@ import { createMessageSender } from './messageHandler';
66// 定义支持的文件扩展名常量
77export const MEDIA_EXTENSIONS = [
88 // 视频格式
9- '.mp4' , '.avi' , '.mov' , '.mkv' , '.flv' , '.wmv' , '.webm' ,
9+ '.mp4' ,
10+ '.avi' ,
11+ '.mov' ,
12+ '.mkv' ,
13+ '.flv' ,
14+ '.wmv' ,
15+ '.webm' ,
1016 // 音频格式
11- '.mp3' , '.wav' , '.ogg' , '.aac' , '.wma' , '.flac' , '.m4a' ,
12- '.aiff' , '.ape' , '.opus' , '.ac3' , '.amr' , '.au' , '.mid' ,
17+ '.mp3' ,
18+ '.wav' ,
19+ '.ogg' ,
20+ '.aac' ,
21+ '.wma' ,
22+ '.flac' ,
23+ '.m4a' ,
24+ '.aiff' ,
25+ '.ape' ,
26+ '.opus' ,
27+ '.ac3' ,
28+ '.amr' ,
29+ '.au' ,
30+ '.mid' ,
1331 // 其他常见视频格式
14- '.3gp' , '.asf' , '.rm' , '.rmvb' , '.vob' , '.ts' , '.mts' , '.m2ts' ,
32+ '.3gp' ,
33+ '.asf' ,
34+ '.rm' ,
35+ '.rmvb' ,
36+ '.vob' ,
37+ '.ts' ,
38+ '.mts' ,
39+ '.m2ts' ,
1540] ;
1641
1742export const SUBTITLE_EXTENSIONS = [
1843 // 字幕格式
19- '.srt' , '.vtt' , '.ass' , '.ssa' ,
44+ '.srt' ,
45+ '.vtt' ,
46+ '.ass' ,
47+ '.ssa' ,
2048] ;
2149
2250// 判断文件是否为媒体文件
@@ -32,23 +60,30 @@ export function isSubtitleFile(filePath: string): boolean {
3260}
3361
3462// 递归获取文件夹中的符合任务类型的文件
35- async function getMediaFilesFromDirectory ( directoryPath : string , taskType : string ) : Promise < string [ ] > {
63+ async function getMediaFilesFromDirectory (
64+ directoryPath : string ,
65+ taskType : string ,
66+ ) : Promise < string [ ] > {
3667 // 根据任务类型选择扩展名
37- const supportedExtensions = taskType === 'translate'
38- ? SUBTITLE_EXTENSIONS
39- : MEDIA_EXTENSIONS ;
40-
68+ const supportedExtensions =
69+ taskType === 'translate' ? SUBTITLE_EXTENSIONS : MEDIA_EXTENSIONS ;
70+
4171 const files : string [ ] = [ ] ;
42-
72+
4373 try {
44- const entries = await fs . promises . readdir ( directoryPath , { withFileTypes : true } ) ;
45-
74+ const entries = await fs . promises . readdir ( directoryPath , {
75+ withFileTypes : true ,
76+ } ) ;
77+
4678 for ( const entry of entries ) {
4779 const fullPath = path . join ( directoryPath , entry . name ) ;
48-
80+
4981 if ( entry . isDirectory ( ) ) {
5082 // 递归处理子目录
51- const subDirFiles = await getMediaFilesFromDirectory ( fullPath , taskType ) ;
83+ const subDirFiles = await getMediaFilesFromDirectory (
84+ fullPath ,
85+ taskType ,
86+ ) ;
5287 files . push ( ...subDirFiles ) ;
5388 } else if ( entry . isFile ( ) ) {
5489 // 检查文件扩展名是否受支持
@@ -61,7 +96,7 @@ async function getMediaFilesFromDirectory(directoryPath: string, taskType: strin
6196 } catch ( error ) {
6297 console . error ( `读取目录 ${ directoryPath } 时出错:` , error ) ;
6398 }
64-
99+
65100 return files ;
66101}
67102
@@ -74,12 +109,13 @@ export function setupIpcHandlers(mainWindow: BrowserWindow) {
74109 const { fileType } = data ;
75110 console . log ( fileType , 'fileType' ) ;
76111 const name = fileType === 'srt' ? 'Subtitle Files' : 'Media Files' ;
77-
112+
78113 // 使用已定义的常量获取扩展名
79- const extensions = fileType === 'srt'
80- ? SUBTITLE_EXTENSIONS . map ( ext => ext . substring ( 1 ) ) // 移除前面的点
81- : MEDIA_EXTENSIONS . map ( ext => ext . substring ( 1 ) ) ;
82-
114+ const extensions =
115+ fileType === 'srt'
116+ ? SUBTITLE_EXTENSIONS . map ( ( ext ) => ext . substring ( 1 ) ) // 移除前面的点
117+ : MEDIA_EXTENSIONS . map ( ( ext ) => ext . substring ( 1 ) ) ;
118+
83119 const result = await dialog . showOpenDialog ( {
84120 properties : [ 'openFile' , 'multiSelections' ] ,
85121 filters : [
@@ -107,20 +143,25 @@ export function setupIpcHandlers(mainWindow: BrowserWindow) {
107143 ipcMain . handle ( 'getDroppedFiles' , async ( event , { files, taskType } ) => {
108144 // 处理文件和文件夹
109145 const allValidPaths : string [ ] = [ ] ;
110-
146+
111147 for ( const filePath of files ) {
112148 try {
113149 const stats = await fs . promises . stat ( filePath ) ;
114-
150+
115151 if ( stats . isDirectory ( ) ) {
116152 // 如果是文件夹,递归获取所有符合任务类型的文件
117- const filteredFiles = await getMediaFilesFromDirectory ( filePath , taskType ) ;
153+ const filteredFiles = await getMediaFilesFromDirectory (
154+ filePath ,
155+ taskType ,
156+ ) ;
118157 allValidPaths . push ( ...filteredFiles ) ;
119158 } else if ( stats . isFile ( ) ) {
120159 // 如果是文件,根据任务类型过滤
121160 // 根据任务类型决定添加哪种文件
122- if ( ( taskType === 'translate' && isSubtitleFile ( filePath ) ) ||
123- ( taskType !== 'translate' && isMediaFile ( filePath ) ) ) {
161+ if (
162+ ( taskType === 'translate' && isSubtitleFile ( filePath ) ) ||
163+ ( taskType !== 'translate' && isMediaFile ( filePath ) )
164+ ) {
124165 allValidPaths . push ( filePath ) ;
125166 }
126167 }
@@ -129,7 +170,7 @@ export function setupIpcHandlers(mainWindow: BrowserWindow) {
129170 continue ;
130171 }
131172 }
132-
173+
133174 return allValidPaths ;
134175 } ) ;
135176
0 commit comments