/** * 生成缺失文件列表,用于在生产服务器上检查 */ import { PrismaClient } from '@prisma/client' import { existsSync } from 'fs' import { writeFile } from 'fs/promises' import path from 'path' const prisma = new PrismaClient() const UPLOAD_DIR = process.env.UPLOAD_DIR || './uploads' const VIDEO_TYPES = ['主图视频', '安装视频', '种草视频'] async function main() { console.log('==== 生成缺失文件列表 ====\n') // 查询所有 videoSourceUrl 是本地路径但文件不存在的视频素材 const allVideos = await prisma.material.findMany({ where: { deletedAt: null, materialType: { in: VIDEO_TYPES } as any, videoSourceUrl: { not: null, startsWith: '/uploads/video-', } as any, }, select: { id: true, materialType: true, videoSourceUrl: true, zipFileName: true, }, }) // 找出缺失的文件 const missingFiles: typeof allVideos = [] for (const m of allVideos) { const fileName = (m.videoSourceUrl as string).replace('/uploads/', '') const localPath = path.join(UPLOAD_DIR, fileName) if (!existsSync(localPath)) { missingFiles.push(m) } } console.log(`共找到 ${missingFiles.length} 条缺失的文件\n`) // 生成文件列表(每行一个文件名) const fileList = missingFiles.map((m) => { const fileName = (m.videoSourceUrl as string).replace('/uploads/', '') return fileName }).join('\n') // 保存到文件 const outputFile = 'missing-video-files.txt' await writeFile(outputFile, fileList, 'utf-8') console.log(`✅ 文件列表已保存到: ${outputFile}`) console.log(` 包含 ${missingFiles.length} 个文件名\n`) // 生成详细信息(JSON 格式) const detailList = missingFiles.map((m) => ({ id: m.id, materialType: m.materialType, videoSourceUrl: m.videoSourceUrl, zipFileName: m.zipFileName, fileName: (m.videoSourceUrl as string).replace('/uploads/', ''), })) const detailFile = 'missing-video-files-detail.json' await writeFile(detailFile, JSON.stringify(detailList, null, 2), 'utf-8') console.log(`✅ 详细信息已保存到: ${detailFile}`) await prisma.$disconnect() } main().catch((error) => { console.error('脚本执行出错:', error) process.exit(1) })