88 lines
2.3 KiB
JavaScript
88 lines
2.3 KiB
JavaScript
#!/usr/bin/env node
|
||
|
||
/**
|
||
* 脚本:更新已有素材的文件大小信息
|
||
* 用途:扫描所有素材的压缩包文件,更新数据库中的 zipFileSize 字段
|
||
*/
|
||
|
||
const { PrismaClient } = require('@prisma/client')
|
||
const fs = require('fs')
|
||
const path = require('path')
|
||
|
||
const prisma = new PrismaClient()
|
||
|
||
async function updateFileSizes() {
|
||
try {
|
||
console.log('🚀 开始扫描素材文件...\n')
|
||
|
||
// 获取所有素材
|
||
const materials = await prisma.material.findMany({
|
||
select: {
|
||
id: true,
|
||
zipFilePath: true,
|
||
zipFileName: true,
|
||
zipFileSize: true
|
||
}
|
||
})
|
||
|
||
console.log(`📊 找到 ${materials.length} 个素材\n`)
|
||
|
||
let successCount = 0
|
||
let skipCount = 0
|
||
let errorCount = 0
|
||
|
||
for (const material of materials) {
|
||
try {
|
||
// 如果已经有大小信息且不为0,跳过
|
||
if (material.zipFileSize && BigInt(material.zipFileSize) > 0) {
|
||
console.log(`⏭️ 跳过: ${material.zipFileName} (已有大小信息)`)
|
||
skipCount++
|
||
continue
|
||
}
|
||
|
||
// 检查文件是否存在
|
||
if (!fs.existsSync(material.zipFilePath)) {
|
||
console.log(`❌ 文件不存在: ${material.zipFilePath}`)
|
||
errorCount++
|
||
continue
|
||
}
|
||
|
||
// 获取文件大小
|
||
const stats = fs.statSync(material.zipFilePath)
|
||
const fileSize = BigInt(stats.size)
|
||
|
||
// 更新数据库
|
||
await prisma.material.update({
|
||
where: { id: material.id },
|
||
data: { zipFileSize: fileSize }
|
||
})
|
||
|
||
const sizeMB = (Number(fileSize) / 1024 / 1024).toFixed(2)
|
||
console.log(`✅ 已更新: ${material.zipFileName} (${sizeMB} MB)`)
|
||
successCount++
|
||
|
||
} catch (error) {
|
||
console.error(`❌ 处理失败: ${material.zipFileName}`, error.message)
|
||
errorCount++
|
||
}
|
||
}
|
||
|
||
console.log('\n📈 统计结果:')
|
||
console.log(` ✅ 成功更新: ${successCount}`)
|
||
console.log(` ⏭️ 已跳过: ${skipCount}`)
|
||
console.log(` ❌ 失败: ${errorCount}`)
|
||
console.log(` 📊 总计: ${materials.length}`)
|
||
console.log('\n✨ 完成!')
|
||
|
||
} catch (error) {
|
||
console.error('❌ 执行失败:', error)
|
||
process.exit(1)
|
||
} finally {
|
||
await prisma.$disconnect()
|
||
}
|
||
}
|
||
|
||
// 执行脚本
|
||
updateFileSizes()
|
||
|