-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmain.js
More file actions
601 lines (520 loc) · 21.2 KB
/
Copy pathmain.js
File metadata and controls
601 lines (520 loc) · 21.2 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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
const { app, BrowserWindow, session, globalShortcut, Menu, nativeTheme } = require('electron');
const path = require('path');
const fs = require('fs');
// 配置
const CONFIG = {
// 默认 URL 列表(按优先级排序)
editorUrls: [
'http://localhost:8080', // 本地开发优先
'https://web-ai-media-editor.cn', // 主域名
'https://web-ai-media-editor.xyz' // 备用域名
],
editorUrl: null, // 将在启动时动态检测
scraperPort: process.env.SCRAPER_PORT || 3100,
devMode: !app.isPackaged
};
let mainWindow;
let scraperServer;
let scraperConnected = false;
let connectionCheckInterval = null;
// ==================== Scraper HTTP Server ====================
async function startScraperServer() {
const express = require('express');
const cors = require('cors');
const STORAGE_DIR = path.join(__dirname, 'storage');
// 确保存储目录存在
const storageDirs = ['videos', 'music', 'images', 'svg', 'thumbnails'];
storageDirs.forEach(dir => {
const fullPath = path.join(STORAGE_DIR, dir);
if (!fs.existsSync(fullPath)) {
fs.mkdirSync(fullPath, { recursive: true });
}
});
// 动态导入 scraper 模块
let MixkitScraper, IncompetechMusic, PexelsScraper, YtDlpWrapper, handleToolCall;
try {
const scraperPath = CONFIG.devMode
? './scraper-dist'
: path.join(process.resourcesPath, 'scraper-dist');
const mixkitModule = await import(path.join(scraperPath, 'tools/scraper/mixkit.js'));
const incompetechModule = await import(path.join(scraperPath, 'tools/scraper/incompetech.js'));
const pexelsModule = await import(path.join(scraperPath, 'tools/scraper/pexels.js'));
const ytdlpModule = await import(path.join(scraperPath, 'tools/video-platform/ytdlp.js'));
const toolHandlerModule = await import(path.join(scraperPath, 'toolHandler.js'));
MixkitScraper = mixkitModule.MixkitScraper;
IncompetechMusic = incompetechModule.IncompetechMusic;
PexelsScraper = pexelsModule.PexelsScraper;
YtDlpWrapper = ytdlpModule.YtDlpWrapper;
handleToolCall = toolHandlerModule.handleToolCall;
} catch (e) {
console.error('[Scraper] Failed to load scraper modules:', e);
return null;
}
// 创建爬虫实例
const mixkitScraper = new MixkitScraper();
const incompetechMusic = new IncompetechMusic();
const pexelsScraper = new PexelsScraper();
const ytdlpWrapper = new YtDlpWrapper();
// Express 应用
const expressApp = express();
expressApp.use(express.json());
expressApp.use(cors({ origin: '*' }));
expressApp.use('/storage', express.static(STORAGE_DIR));
// API 路由
expressApp.get('/api/status', async (req, res) => {
try {
const ytdlpAvailable = await ytdlpWrapper.checkAvailability();
const countFiles = (dir) => {
const fullPath = path.join(STORAGE_DIR, dir);
if (!fs.existsSync(fullPath)) return 0;
return fs.readdirSync(fullPath).filter(f => !f.startsWith('.')).length;
};
res.json({
status: 'running',
version: '1.0.0',
port: CONFIG.scraperPort,
ytdlp: ytdlpAvailable,
storage: {
videos: countFiles('videos'),
music: countFiles('music'),
images: countFiles('images'),
svg: countFiles('svg'),
}
});
} catch (error) {
res.status(500).json({ error: String(error) });
}
});
expressApp.post('/api/search/video', async (req, res) => {
try {
const { query, source = 'mixkit', maxResults = 10 } = req.body;
if (!query) return res.status(400).json({ error: 'query is required' });
let results = [];
if (source === 'mixkit') {
results = await mixkitScraper.searchVideos(query, maxResults);
}
res.json({ success: true, query, source, count: results.length, results });
} catch (error) {
res.status(500).json({ success: false, error: String(error) });
}
});
expressApp.post('/api/search/image', async (req, res) => {
try {
const { query, source = 'pexels', maxResults = 10 } = req.body;
if (!query) return res.status(400).json({ error: 'query is required' });
let results = [];
if (source === 'pexels') {
results = await pexelsScraper.searchImages(query, maxResults);
}
res.json({ success: true, query, source, count: results.length, results });
} catch (error) {
res.status(500).json({ success: false, error: String(error) });
}
});
expressApp.post('/api/search/music', async (req, res) => {
try {
const { query, source = 'incompetech', genre, mood, maxResults = 10 } = req.body;
if (!query) return res.status(400).json({ error: 'query is required' });
let results = [];
if (source === 'incompetech') {
results = await incompetechMusic.search(query, { genre, mood, maxResults });
} else if (source === 'mixkit') {
results = await mixkitScraper.searchMusic(query, maxResults);
}
res.json({ success: true, query, source, count: results.length, results });
} catch (error) {
res.status(500).json({ success: false, error: String(error) });
}
});
expressApp.post('/api/search/media', async (req, res) => {
try {
const { query, type = 'all', maxResults = 10 } = req.body;
if (!query) return res.status(400).json({ error: 'query is required' });
const results = { videos: [], music: [], images: [] };
const errors = [];
if (type === 'all' || type === 'video') {
try {
results.videos = (await mixkitScraper.searchVideos(query, maxResults)).map(v => ({ ...v, source: 'mixkit' }));
} catch (e) { errors.push(`video: ${e.message}`); }
}
if (type === 'all' || type === 'music') {
try {
const [mixkitMusic, incompetechResults] = await Promise.all([
mixkitScraper.searchMusic(query, Math.ceil(maxResults / 2)).catch(() => []),
incompetechMusic.search(query, { maxResults: Math.ceil(maxResults / 2) }).catch(() => [])
]);
results.music = [
...mixkitMusic.map(m => ({ ...m, source: 'mixkit' })),
...incompetechResults.map(m => ({ ...m, source: 'incompetech' }))
];
} catch (e) { errors.push(`music: ${e.message}`); }
}
if (type === 'all' || type === 'image') {
try {
results.images = (await pexelsScraper.searchImages(query, maxResults)).map(i => ({ ...i, source: 'pexels' }));
} catch (e) { errors.push(`image: ${e.message}`); }
}
const totalCount = results.videos.length + results.music.length + results.images.length;
res.json({
success: true, query, type,
counts: { videos: results.videos.length, music: results.music.length, images: results.images.length, total: totalCount },
results,
errors: errors.length > 0 ? errors : undefined
});
} catch (error) {
res.status(500).json({ success: false, error: String(error) });
}
});
expressApp.post('/api/download', async (req, res) => {
try {
const { url, type = 'video', filename } = req.body;
if (!url) return res.status(400).json({ error: 'url is required' });
const result = await handleToolCall('download_media', { url, type, filename }, null);
if (result.isError) {
const errorText = result.content[0]?.text || 'Unknown error';
return res.status(500).json({ success: false, error: errorText });
}
const data = JSON.parse(result.content[0]?.text || '{}');
let normalizedPath = data.localPath?.replace(/\\/g, '/') || '';
const storageIndex = normalizedPath.indexOf('/storage/');
if (storageIndex !== -1) {
normalizedPath = normalizedPath.substring(storageIndex);
} else if (normalizedPath.startsWith('./storage')) {
normalizedPath = normalizedPath.replace('./storage', '/storage');
} else if (normalizedPath.startsWith('storage')) {
normalizedPath = '/' + normalizedPath;
}
const localUrl = `http://localhost:${CONFIG.scraperPort}${normalizedPath}`;
res.json({
success: true,
localPath: data.localPath,
localUrl,
size: data.size,
filename: normalizedPath.split('/').pop() || filename || 'unknown',
type
});
} catch (error) {
res.status(500).json({ success: false, error: String(error) });
}
});
expressApp.post('/api/ytdlp', async (req, res) => {
try {
const { url, format = 'best', audioOnly = false } = req.body;
if (!url) return res.status(400).json({ error: 'url is required' });
const isAvailable = await ytdlpWrapper.checkAvailability();
if (!isAvailable) {
return res.status(503).json({ success: false, error: 'yt-dlp is not installed' });
}
const result = await ytdlpWrapper.download(url, {
outputDir: path.join(STORAGE_DIR, 'videos'),
format,
audioOnly
});
if (!result.success) {
return res.status(500).json({ success: false, error: result.error });
}
const localUrl = `http://localhost:${CONFIG.scraperPort}/storage/videos/${path.basename(result.localPath || '')}`;
res.json({ success: true, localPath: result.localPath, localUrl, title: result.title, duration: result.duration });
} catch (error) {
res.status(500).json({ success: false, error: String(error) });
}
});
expressApp.post('/api/ytdlp/info', async (req, res) => {
try {
const { url } = req.body;
if (!url) return res.status(400).json({ error: 'url is required' });
const isAvailable = await ytdlpWrapper.checkAvailability();
if (!isAvailable) {
return res.status(503).json({ success: false, error: 'yt-dlp is not installed' });
}
const info = await ytdlpWrapper.getVideoInfo(url);
res.json({ success: true, info });
} catch (error) {
res.status(500).json({ success: false, error: String(error) });
}
});
expressApp.get('/api/files/:type', (req, res) => {
try {
const { type } = req.params;
const validTypes = ['videos', 'music', 'images', 'svg'];
if (!validTypes.includes(type)) {
return res.status(400).json({ error: `Invalid type` });
}
const dirPath = path.join(STORAGE_DIR, type);
if (!fs.existsSync(dirPath)) return res.json({ files: [] });
const files = fs.readdirSync(dirPath)
.filter(f => !f.startsWith('.'))
.map(f => {
const filePath = path.join(dirPath, f);
const stats = fs.statSync(filePath);
return {
name: f,
size: stats.size,
url: `http://localhost:${CONFIG.scraperPort}/storage/${type}/${f}`,
createdAt: stats.birthtime,
modifiedAt: stats.mtime
};
});
res.json({ files });
} catch (error) {
res.status(500).json({ error: String(error) });
}
});
expressApp.delete('/api/files/:type/:filename', (req, res) => {
try {
const { type, filename } = req.params;
const validTypes = ['videos', 'music', 'images', 'svg'];
if (!validTypes.includes(type)) return res.status(400).json({ error: 'Invalid type' });
if (filename.includes('..') || filename.includes('/') || filename.includes('\\')) {
return res.status(400).json({ error: 'Invalid filename' });
}
const filePath = path.join(STORAGE_DIR, type, filename);
if (!fs.existsSync(filePath)) return res.status(404).json({ error: 'File not found' });
fs.unlinkSync(filePath);
res.json({ success: true, message: `Deleted ${filename}` });
} catch (error) {
res.status(500).json({ error: String(error) });
}
});
// 启动服务器
return new Promise((resolve) => {
scraperServer = expressApp.listen(CONFIG.scraperPort, () => {
console.log(`[Scraper] HTTP Server running on port ${CONFIG.scraperPort}`);
resolve(scraperServer);
});
});
}
// ==================== URL 可用性检测 ====================
async function checkUrlAvailability(url, timeout = 3000) {
const http = url.startsWith('https') ? require('https') : require('http');
return new Promise((resolve) => {
const req = http.get(url, { timeout }, (res) => {
// 2xx 或 3xx 都算成功
resolve(res.statusCode >= 200 && res.statusCode < 400);
});
req.on('error', () => resolve(false));
req.on('timeout', () => {
req.destroy();
resolve(false);
});
// 额外的超时保护
setTimeout(() => {
req.destroy();
resolve(false);
}, timeout + 500);
});
}
async function detectBestEditorUrl() {
// 如果环境变量指定了 URL,直接使用
if (process.env.EDITOR_URL) {
console.log('[Box] Using EDITOR_URL from environment:', process.env.EDITOR_URL);
return process.env.EDITOR_URL;
}
console.log('[Box] Detecting best editor URL...');
for (const url of CONFIG.editorUrls) {
console.log(`[Box] Checking ${url}...`);
const isAvailable = await checkUrlAvailability(url);
if (isAvailable) {
console.log(`[Box] ✓ ${url} is available`);
return url;
} else {
console.log(`[Box] ✗ ${url} is not available`);
}
}
// 如果都不可用,返回第一个在线域名(用户可能需要等待网络)
console.log('[Box] No URL available, defaulting to primary domain');
return CONFIG.editorUrls[1]; // 返回主域名
}
// ==================== 连接检查 ====================
async function waitForScraper(maxRetries = 10) {
const http = require('http');
for (let i = 0; i < maxRetries; i++) {
try {
await new Promise((resolve, reject) => {
const req = http.get(`http://localhost:${CONFIG.scraperPort}/api/status`, (res) => {
if (res.statusCode === 200) resolve();
else reject(new Error(`Status: ${res.statusCode}`));
});
req.on('error', reject);
req.setTimeout(1000, () => { req.destroy(); reject(new Error('Timeout')); });
});
console.log('[Box] Scraper is ready');
scraperConnected = true;
updateTrayStatus();
return true;
} catch (e) {
console.log(`[Box] Waiting for scraper... (${i + 1}/${maxRetries})`);
await new Promise(r => setTimeout(r, 500));
}
}
console.error('[Box] Scraper failed to start');
scraperConnected = false;
updateTrayStatus();
return false;
}
function startConnectionCheck() {
const http = require('http');
connectionCheckInterval = setInterval(async () => {
try {
await new Promise((resolve, reject) => {
const req = http.get(`http://localhost:${CONFIG.scraperPort}/api/status`, (res) => {
if (res.statusCode === 200) resolve();
else reject(new Error(`Status: ${res.statusCode}`));
});
req.on('error', reject);
req.setTimeout(2000, () => { req.destroy(); reject(new Error('Timeout')); });
});
if (!scraperConnected) {
console.log('[Box] Scraper reconnected');
scraperConnected = true;
updateTrayStatus();
}
} catch (e) {
if (scraperConnected) {
console.log('[Box] Scraper disconnected');
scraperConnected = false;
updateTrayStatus();
}
}
}, 5000);
}
function updateTrayStatus() {
if (mainWindow) {
const statusText = scraperConnected ? '● 已连接' : '○ 未连接';
mainWindow.setTitle(`语剪 · EDITOR ${statusText}`);
}
}
// ==================== 窗口创建 ====================
function createWindow() {
mainWindow = new BrowserWindow({
width: 1920,
height: 1080,
title: '语剪 · EDITOR',
backgroundColor: '#1a1a2e',
titleBarStyle: 'default',
webPreferences: {
allowRunningInsecureContent: true,
preload: path.join(__dirname, 'preload.js'),
nodeIntegration: false,
contextIsolation: true
}
});
// 中文菜单栏
const menuTemplate = [
{
label: '文件',
submenu: [
{ label: '刷新页面', accelerator: 'CmdOrCtrl+R', click: () => mainWindow?.reload() },
{ label: '强制刷新', accelerator: 'CmdOrCtrl+Shift+R', click: () => mainWindow?.webContents.reloadIgnoringCache() },
{ type: 'separator' },
{ label: '退出', accelerator: 'Alt+F4', click: () => app.quit() }
]
},
{
label: '编辑',
submenu: [
{ label: '撤销', accelerator: 'CmdOrCtrl+Z', role: 'undo' },
{ label: '重做', accelerator: 'CmdOrCtrl+Y', role: 'redo' },
{ type: 'separator' },
{ label: '剪切', accelerator: 'CmdOrCtrl+X', role: 'cut' },
{ label: '复制', accelerator: 'CmdOrCtrl+C', role: 'copy' },
{ label: '粘贴', accelerator: 'CmdOrCtrl+V', role: 'paste' },
{ label: '全选', accelerator: 'CmdOrCtrl+A', role: 'selectAll' }
]
},
{
label: '视图',
submenu: [
{ label: '开发者工具', accelerator: 'F12', click: () => mainWindow?.webContents.toggleDevTools() },
{ type: 'separator' },
{ label: '全屏', accelerator: 'F11', click: () => mainWindow?.setFullScreen(!mainWindow.isFullScreen()) },
{ type: 'separator' },
{ label: '放大', accelerator: 'CmdOrCtrl+Plus', role: 'zoomIn' },
{ label: '缩小', accelerator: 'CmdOrCtrl+-', role: 'zoomOut' },
{ label: '重置缩放', accelerator: 'CmdOrCtrl+0', role: 'resetZoom' }
]
},
{
label: '窗口',
submenu: [
{ label: '最小化', accelerator: 'CmdOrCtrl+M', role: 'minimize' },
{ label: '关闭', accelerator: 'CmdOrCtrl+W', role: 'close' }
]
},
{
label: '帮助',
submenu: [
{
label: '关于语剪',
click: () => {
const { dialog } = require('electron');
dialog.showMessageBox(mainWindow, {
type: 'info',
title: '关于语剪',
message: '语剪 · EDITOR v1.0.0',
detail: 'AI 驱动的视频剪辑工具\n\n本地服务端口: ' + CONFIG.scraperPort
});
}
}
]
}
];
Menu.setApplicationMenu(Menu.buildFromTemplate(menuTemplate));
session.defaultSession.webRequest.onHeadersReceived((details, callback) => {
callback({ responseHeaders: { ...details.responseHeaders, 'Content-Security-Policy': [''] } });
});
mainWindow.webContents.on('did-fail-load', (event, errorCode, errorDescription, validatedURL) => {
console.error(`[Box] Failed to load ${validatedURL}: ${errorDescription}`);
// 构建备用 URL 按钮
const alternativeButtons = CONFIG.editorUrls
.filter(url => url !== validatedURL)
.map(url => `<button onclick="location.href='${url}'" style="margin:5px">🔗 ${url}</button>`)
.join('');
const errorHtml = `<!DOCTYPE html><html><head><meta charset="UTF-8"><title>连接失败</title>
<style>body{font-family:sans-serif;background:#1a1a2e;color:#fff;display:flex;justify-content:center;align-items:center;height:100vh;margin:0}
.container{text-align:center;padding:40px;background:rgba(255,255,255,0.1);border-radius:16px;max-width:600px}
h1{color:#ff6b6b}button{background:#3b82f6;color:white;border:none;padding:12px 24px;border-radius:8px;cursor:pointer;margin-top:10px;font-size:14px}
button:hover{background:#2563eb}.alternatives{margin-top:20px;padding-top:20px;border-top:1px solid rgba(255,255,255,0.2)}
.alternatives h3{color:#94a3b8;font-size:14px;margin-bottom:10px}</style></head>
<body><div class="container"><h1>⚠️ 无法连接到 Editor</h1><p>地址: ${validatedURL}</p><p>错误: ${errorDescription}</p>
<button onclick="location.reload()">🔄 重试当前地址</button>
<div class="alternatives"><h3>尝试其他地址:</h3>${alternativeButtons}</div>
</div></body></html>`;
mainWindow.loadURL(`data:text/html;charset=utf-8,${encodeURIComponent(errorHtml)}`);
});
console.log('[Box] Loading editor:', CONFIG.editorUrl);
mainWindow.loadURL(CONFIG.editorUrl);
mainWindow.on('closed', () => { mainWindow = null; });
}
// ==================== 应用启动 ====================
app.whenReady().then(async () => {
console.log('[Box] Starting V-Editor Box...');
console.log('[Box] Dev mode:', CONFIG.devMode);
nativeTheme.themeSource = 'dark';
// 1. 检测最佳 Editor URL
CONFIG.editorUrl = await detectBestEditorUrl();
console.log('[Box] Selected editor URL:', CONFIG.editorUrl);
// 2. 启动内置 scraper 服务器
await startScraperServer();
// 3. 等待 scraper 就绪
await waitForScraper();
// 4. 创建窗口
createWindow();
// 5. 启动连接状态检查
startConnectionCheck();
// 5. 注册快捷键
globalShortcut.register('F12', () => BrowserWindow.getFocusedWindow()?.webContents.toggleDevTools());
globalShortcut.register('CommandOrControl+Shift+I', () => BrowserWindow.getFocusedWindow()?.webContents.toggleDevTools());
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) createWindow();
});
});
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') app.quit();
});
app.on('quit', () => {
globalShortcut.unregisterAll();
if (connectionCheckInterval) clearInterval(connectionCheckInterval);
if (scraperServer) scraperServer.close();
});
process.on('uncaughtException', (err) => console.error('[Box] Uncaught exception:', err));