test-audio-url-access.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. const { PrismaClient } = require('@prisma/client');
  2. const prisma = new PrismaClient();
  3. const http = require('http');
  4. async function testAudioUrl() {
  5. console.log('\n📊 测试音频URL可访问性:\n');
  6. const chapter = await prisma.bookChapter.findUnique({
  7. where: { id: 1214 }
  8. });
  9. if (!chapter || !chapter.audioUrl) {
  10. console.log('❌ 没有音频URL\n');
  11. await prisma.$disconnect();
  12. return;
  13. }
  14. console.log(`章节: ${chapter.title}`);
  15. console.log(`audioUrl: ${chapter.audioUrl}\n`);
  16. // 测试本地文件是否存在
  17. const fs = require('fs');
  18. const path = require('path');
  19. // audioUrl格式: /uploads/{uuid}/output.mp3
  20. const uuid = chapter.audioUrl.match(/\/uploads\/([^\/]+)\//)?.[1];
  21. if (uuid) {
  22. const filePath = path.join(__dirname, 'uploads', uuid, 'output.mp3');
  23. console.log(`文件路径: ${filePath}`);
  24. if (fs.existsSync(filePath)) {
  25. const stats = fs.statSync(filePath);
  26. console.log(`✅ 文件存在,大小: ${(stats.size / 1024).toFixed(2)} KB\n`);
  27. } else {
  28. console.log(`❌ 文件不存在\n`);
  29. }
  30. }
  31. // 测试API访问
  32. console.log('测试HTTP访问: http://localhost:3000' + chapter.audioUrl);
  33. const url = 'http://localhost:3000' + chapter.audioUrl;
  34. return new Promise((resolve) => {
  35. http.get(url, (res) => {
  36. console.log(`\nHTTP响应状态: ${res.statusCode}`);
  37. console.log(`Content-Type: ${res.headers['content-type']}`);
  38. console.log(`Content-Length: ${res.headers['content-length']}\n`);
  39. if (res.statusCode === 200) {
  40. console.log('✅ 音频URL可以通过HTTP访问\n');
  41. } else {
  42. console.log('❌ 音频URL无法通过HTTP访问\n');
  43. }
  44. resolve();
  45. }).on('error', (err) => {
  46. console.log(`❌ HTTP请求失败: ${err.message}\n`);
  47. resolve();
  48. });
  49. });
  50. }
  51. testAudioUrl().then(() => prisma.$disconnect()).catch(console.error);