files.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. function registerFileRoutes(app, deps) {
  2. app.get('/api/files/whisper/:videoId', (req, res) => {
  3. try {
  4. const manifest = deps.readManifest();
  5. const video = manifest.find((item) => item.id === req.params.videoId);
  6. if (!video) {
  7. return res.status(404).json({ error: `未找到视频: ${req.params.videoId}` });
  8. }
  9. const baseName = video.filename.replace(/\.[^.]+$/, '');
  10. const outputDir = deps.path.join(deps.whisperDir, baseName);
  11. if (!deps.fs.existsSync(outputDir)) {
  12. return res.json({ files: [], exists: false });
  13. }
  14. const files = deps.fs.readdirSync(outputDir).map((fileName) => ({
  15. name: fileName,
  16. path: `Whisper/${baseName}/${fileName}`,
  17. size: deps.fs.statSync(deps.path.join(outputDir, fileName)).size
  18. }));
  19. res.json({ files, exists: true });
  20. } catch (err) {
  21. res.status(500).json({ error: err.message });
  22. }
  23. });
  24. app.get('/api/files/read', (req, res) => {
  25. const filePath = req.query.path;
  26. if (!filePath) {
  27. return res.status(400).json({ error: '缺少 path 参数' });
  28. }
  29. const fullPath = deps.path.join(deps.projectRoot, filePath);
  30. if (!fullPath.startsWith(deps.projectRoot)) {
  31. return res.status(403).json({ error: '路径不在项目目录内' });
  32. }
  33. if (!deps.fs.existsSync(fullPath)) {
  34. return res.status(404).json({ error: '文件不存在' });
  35. }
  36. const ext = deps.path.extname(fullPath).toLowerCase();
  37. if (ext === '.json') {
  38. return res.json(JSON.parse(deps.fs.readFileSync(fullPath, 'utf-8')));
  39. }
  40. res.type('text/plain').send(deps.fs.readFileSync(fullPath, 'utf-8'));
  41. });
  42. }
  43. module.exports = { registerFileRoutes };