-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpatch_search_main.py
More file actions
125 lines (106 loc) · 3.23 KB
/
Copy pathpatch_search_main.py
File metadata and controls
125 lines (106 loc) · 3.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import re
with open('/workspace/search-main.js', 'r') as f:
content = f.read()
# Replace getFileInfoFast to be async
old_getFileInfo = """// 快速获取文件信息 - 使用同步方法提高性能
function getFileInfoFast(filePath) {
try {
const stats = fsSync.statSync(filePath);
const parsedPath = path.parse(filePath);
return {
name: parsedPath.base,
path: filePath,
dir: parsedPath.dir,
ext: parsedPath.ext,
size: stats.size,
mtime: stats.mtime,
birthtime: stats.birthtime,
atime: stats.atime
};
} catch (error) {
if (error.code !== 'EPERM' && error.code !== 'EACCES') {
console.error('获取文件信息失败:', error);
}
return null;
}
}"""
new_getFileInfo = """// 快速获取文件信息 - 使用异步方法不阻塞主线程
async function getFileInfoFast(filePath) {
try {
const stats = await fs.stat(filePath);
const parsedPath = path.parse(filePath);
return {
name: parsedPath.base,
path: filePath,
dir: parsedPath.dir,
ext: parsedPath.ext,
size: stats.size,
mtime: stats.mtime,
birthtime: stats.birthtime,
atime: stats.atime
};
} catch (error) {
if (error.code !== 'EPERM' && error.code !== 'EACCES') {
console.error('获取文件信息失败:', error);
}
return null;
}
}"""
content = content.replace(old_getFileInfo, new_getFileInfo)
# Add export-files IPC handler
export_files_code = """
// 导出文件
ipcMain.handle('export-files', async (event, { files, destFolder }) => {
let successCount = 0;
let failCount = 0;
// 限制并发数量
const limit = 10;
let active = 0;
let index = 0;
return new Promise((resolve, reject) => {
const next = async () => {
if (index >= files.length) {
if (active === 0) {
resolve({ successCount, failCount });
}
return;
}
const file = files[index++];
active++;
try {
let targetPath = path.join(destFolder, file.name);
// 处理文件名冲突
let counter = 1;
while (fsSync.existsSync(targetPath)) {
const parsed = path.parse(file.name);
targetPath = path.join(destFolder, `${parsed.name} (${counter})${parsed.ext}`);
counter++;
}
// 使用原生的 copyFile,它在底层使用高效的系统调用
await fs.copyFile(file.path, targetPath, fsSync.constants.COPYFILE_FICLONE);
successCount++;
} catch (err) {
console.error('Copy failed:', err);
failCount++;
} finally {
active--;
// 发送进度
if ((successCount + failCount) % 10 === 0 || (successCount + failCount) === files.length) {
event.sender.send('export-progress', {
current: successCount + failCount,
total: files.length
});
}
next();
}
};
for (let i = 0; i < limit && i < files.length; i++) {
next();
}
});
});
"""
# Insert export-files before // 打开文件夹
content = content.replace("// 打开文件夹", export_files_code + "\n// 打开文件夹")
with open('/workspace/search-main.js', 'w') as f:
f.write(content)