-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.js
More file actions
341 lines (297 loc) · 10.1 KB
/
main.js
File metadata and controls
341 lines (297 loc) · 10.1 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
import { app, BrowserWindow, ipcMain, dialog } from 'electron'
import path from 'path'
import { fileURLToPath } from 'url'
import fs from 'fs'
import crypto from 'crypto'
import bencode from 'bencode'
import Handlebars from 'handlebars'
const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)
// 模板文件路径
const TEMPLATES = {
forumTitle: path.join(__dirname, 'templates/forum-title.hbs'),
forumContent: path.join(__dirname, 'templates/forum-content.hbs'),
allianceTitle: path.join(__dirname, 'templates/alliance-title.hbs'),
allianceContent: path.join(__dirname, 'templates/alliance-content.hbs')
}
// 创建窗口
function createWindow() {
const mainWindow = new BrowserWindow({
width: 1000,
height: 800,
icon: './assets/icons/icon.png',
webPreferences: {
preload: path.join(__dirname, 'preload.mjs'),
contextIsolation: true,
nodeIntegration: false,
sandbox: false,
enableRemoteModule: false,
experimentalFeatures: true,
nodeIntegrationInWorker: true,
nodeIntegrationInSubFrames: true,
webSecurity: false
}
})
mainWindow.loadFile('index.html')
// 在点击关闭按钮时立即退出程序
mainWindow.on('close', () => {
app.quit();
});
}
app.whenReady().then(() => {
createWindow()
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow()
}
})
})
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit()
}
})
// 处理系统信息请求
ipcMain.handle('get-system-info', async () => {
return {
isMacOS: process.platform === 'darwin',
isWindows: process.platform === 'win32',
isLinux: process.platform === 'linux'
}
})
// 递归获取所有文件路径
const getAllFiles = async (filePath) => {
const stats = await fs.promises.stat(filePath)
if (stats.isFile()) {
return [filePath]
}
const files = []
const items = await fs.promises.readdir(filePath)
for (const item of items) {
// 忽略.DS_Store等系统文件
if (item.startsWith('.') || item === 'Thumbs.db') {
continue
}
const fullPath = path.join(filePath, item)
files.push(...await getAllFiles(fullPath))
}
return files
}
// 处理文件/文件夹选择
ipcMain.handle('select-files', async () => {
let properties = ['multiSelections'];
// 根据系统设置不同属性
if (process.platform === 'darwin') {
properties.push('openFile', 'openDirectory');
} else {
// Windows/Linux系统需要用户选择模式
const { response } = await dialog.showMessageBox({
type: 'question',
buttons: ['选择文件', '选择文件夹', '取消'],
message: '请选择模式',
detail: 'Windows/Linux系统不支持同时选择文件和文件夹'
});
if (response === 0) {
properties.push('openFile');
} else if (response === 1) {
properties.push('openDirectory');
} else {
return []; // 用户取消
}
}
const result = await dialog.showOpenDialog({ properties });
const allFiles = []
for (const filePath of result.filePaths) {
allFiles.push(...await getAllFiles(filePath))
}
return allFiles
})
// 处理拖拽文件/文件夹
ipcMain.handle('handle-drop', async (event, filePaths) => {
const allFiles = []
for (const filePath of filePaths) {
allFiles.push(...await getAllFiles(filePath))
}
return allFiles
})
// 计算CRC32校验值
function calculateCRC32(filePath) {
return new Promise((resolve, reject) => {
const hash = crypto.createHash('crc32')
const stream = fs.createReadStream(filePath)
stream.on('data', chunk => hash.update(chunk))
stream.on('end', () => resolve(hash.digest('hex')))
stream.on('error', reject)
})
}
// 计算文件总大小
ipcMain.handle('calculateTotalSize', async (event, files) => {
let totalSize = 0;
for (const file of files) {
const stats = await fs.promises.stat(file);
totalSize += stats.size;
}
// Convert to MB with 2 decimal places
return `${(totalSize / 1024 / 1024).toFixed(2)} MB`;
});
// 生成torrent和magnet链接
ipcMain.handle('createTorrent', async (event, files, pieceLength) => {
try {
console.log('Received files:', files);
console.log('Piece length:', pieceLength);
if (!files || files.length === 0) {
throw new Error('No files selected');
}
let torrentName;
if (files.length === 1) {
const stats = fs.statSync(files[0]);
if (stats.isDirectory()) {
torrentName = path.basename(files[0]);
} else {
torrentName = path.basename(files[0]);
}
} else {
// 如果选择的是多个文件/文件夹,使用第一个文件的父目录名
const firstFile = files[0];
const parentDir = path.dirname(firstFile);
const parentStats = fs.statSync(parentDir);
if (parentStats.isDirectory()) {
torrentName = path.basename(parentDir);
} else {
// 如果父目录不是文件夹(不太可能的情况),使用第一个文件名
torrentName = path.basename(firstFile);
}
}
// Default trackers
const defaultTrackers = [
'https://tr.bangumi.moe:9696/announce',
'http://t.acg.rip:6699/announce',
'http://tracker.dmhy.org:8000/announce',
'http://tracker.skyts.net:6969/announce',
'http://open.acgtracker.com:1096/announce',
'http://tr.bangumi.moe:6969/announce',
'http://tracker.lintk.me:2710/announce',
'http://open.nyaatorrents.info:6544/announce',
'http://tracker.kuroy.me:5944/announce',
'http://tracker.skyts.cn:6969/announce',
'http://t.nyaatracker.com/announce',
'http://nyaa.tracker.wf:7777/announce',
'http://tracker.kamigami.org:2710/announce',
'udp://tracker.dmhy.org:8000/announce',
'udp://tr.bangumi.moe:6969/announce',
'udp://tracker.skyts.net:6969/announce',
'http://tracker.skyts.net:6969/announce',
'udp://tracker.skyts.cc:6969/announce',
'udp://tracker.ink:6969/announce',
'udp://asia.tracker.ink:6969/announce',
'udp://open.tracker.ink:6969/announce',
'https://tracker.ghostchu-services.top/announce',
'udp://utracker.ghostchu-services.top:6969'
];
const torrent = {
announce: defaultTrackers[0],
'announce-list': [defaultTrackers],
info: {
'piece length': pieceLength,
name: torrentName,
...(files.length === 1 ? {
length: fs.statSync(files[0]).size,
pieces: (() => {
const data = fs.readFileSync(files[0]);
const pieces = [];
let offset = 0;
while (offset < data.length) {
const pieceData = data.slice(offset, offset + pieceLength);
const hash = crypto.createHash('sha1').update(pieceData).digest();
pieces.push(hash);
offset += pieceLength;
}
return Buffer.concat(pieces);
})()
} : {
files: files.map(file => {
console.log('Processing file:', file);
const fileStats = fs.statSync(file);
if (!fileStats.isFile()) {
throw new Error(`Path is not a file: ${file}`);
}
return {
path: [path.relative(path.dirname(files[0]), file)],
length: fileStats.size
};
}),
// Generate pieces
pieces: (() => {
const pieces = [];
let piece = Buffer.alloc(0);
for (const file of files) {
const data = fs.readFileSync(file);
piece = Buffer.concat([piece, data]);
while (piece.length >= pieceLength) {
const pieceData = piece.slice(0, pieceLength);
const hash = crypto.createHash('sha1').update(pieceData).digest();
pieces.push(hash);
piece = piece.slice(pieceLength);
}
}
// Add remaining data if any
if (piece.length > 0) {
const hash = crypto.createHash('sha1').update(piece).digest();
pieces.push(hash);
}
return Buffer.concat(pieces);
})()
})
}
}
console.log('Created torrent structure:', torrent);
// 计算info hash
const infoBuffer = bencode.encode(torrent.info);
console.log('Encoded info buffer:', infoBuffer);
const infoHash = crypto.createHash('sha1').update(infoBuffer).digest('hex');
console.log('Generated info hash:', infoHash);
// 生成magnet链接
const magnetLink = `magnet:?xt=urn:btih:${infoHash}&dn=${encodeURIComponent(torrent.info.name)}&tr=${defaultTrackers.map(encodeURIComponent).join('&tr=')}`;
console.log('Generated magnet link:', magnetLink);
// 创建.torrent文件
const torrentFileName = `${torrent.info.name}.torrent`;
// 显示保存对话框
const { filePath } = await dialog.showSaveDialog({
title: '保存种子文件',
defaultPath: torrentFileName,
filters: [
{ name: 'Torrent Files', extensions: ['torrent'] },
{ name: 'All Files', extensions: ['*'] }
]
});
if (!filePath) {
throw new Error('保存操作已取消');
}
console.log('Writing torrent file to:', filePath);
await fs.promises.writeFile(filePath, bencode.encode(torrent));
console.log('Successfully wrote torrent file');
return {
infoHash,
magnetLink,
torrentFilePath: filePath,
torrentFileName: path.basename(filePath)
}
} catch (error) {
console.error('Error in createTorrent:', error);
throw error;
}
})
// 渲染模板
async function renderTemplate(templatePath, data) {
const templateContent = await fs.promises.readFile(templatePath, 'utf8')
const template = Handlebars.compile(templateContent)
return template(data)
}
// 处理模板生成请求
ipcMain.handle('render-templates', async (event, data) => {
const results = {}
for (const [key, templatePath] of Object.entries(TEMPLATES)) {
results[key] = await renderTemplate(templatePath, data)
}
return results
})