gongxiangp2/scripts/find-audio-materials.ts

79 lines
2.2 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* 查找本地 uploads 目录中音频文件对应的素材
* 使用本地环境变量连接数据库
*/
import { PrismaClient } from '@prisma/client';
import { readdir } from 'fs/promises';
import path from 'path';
const prisma = new PrismaClient();
async function findAudioMaterials() {
try {
console.log('查找音频素材...\n');
// 读取本地 uploads 目录中的音频文件
const uploadsDir = path.join(process.cwd(), 'uploads');
const files = await readdir(uploadsDir);
const audioFiles = files.filter(f => f.endsWith('.mp3'));
console.log(`找到 ${audioFiles.length} 个音频文件\n`);
// 随机抽取 10 个样本
const samples = audioFiles.sort(() => 0.5 - Math.random()).slice(0, 10);
console.log('查询数据库中的对应素材...\n');
const results = [];
for (const fileName of samples) {
const filePath = `uploads/${fileName}`;
const material = await prisma.material.findFirst({
where: {
zipFilePath: filePath,
deletedAt: null,
},
select: {
id: true,
materialType: true,
productModel: true,
authorName: true,
},
});
if (material) {
results.push({
fileName,
...material,
});
}
}
console.log(`✅ 找到 ${results.length} 个可测试的素材\n`);
console.log('='.repeat(100));
results.forEach((r, index) => {
console.log(`\n${index + 1}. ${r.materialType} - ${r.productModel}`);
console.log(` 作者: ${r.authorName}`);
console.log(` 文件: ${r.fileName}`);
console.log(` 🔗 正式环境: http://192.168.58.20:4320/share/${r.id}`);
});
console.log('\n' + '='.repeat(100));
console.log('\n📝 测试步骤:');
console.log('1. 复制上面任意一个链接,在浏览器中打开');
console.log('2. 点击下载按钮');
console.log('3. 验证文件是否能正常下载并播放');
console.log('\n如果下载成功,说明这 93 个音频文件的源文件已成功恢复!');
} catch (error) {
console.error('查询失败:', error);
} finally {
await prisma.$disconnect();
}
}
findAudioMaterials();