222 lines
6.5 KiB
TypeScript
222 lines
6.5 KiB
TypeScript
/**
|
||
* 检查备份位置,查找缺失的视频文件
|
||
*
|
||
* 检查位置:
|
||
* 1. 本地 uploads 目录(当前目录)
|
||
* 2. MinIO S3 中的不同路径
|
||
* 3. 可能的备份目录
|
||
*/
|
||
|
||
import { PrismaClient } from '@prisma/client'
|
||
import { S3Client, ListObjectsV2Command, HeadObjectCommand } from '@aws-sdk/client-s3'
|
||
import { access, readdir } from 'fs/promises'
|
||
import { constants as fsConstants } from 'fs'
|
||
import path from 'path'
|
||
|
||
const prisma = new PrismaClient()
|
||
|
||
// S3 / MinIO 客户端配置
|
||
const s3Client = new S3Client({
|
||
endpoint: process.env.S3_ENDPOINT || '',
|
||
region: process.env.S3_REGION || 'us-east-1',
|
||
credentials: {
|
||
accessKeyId: process.env.S3_ACCESS_KEY_ID!,
|
||
secretAccessKey: process.env.S3_SECRET_ACCESS_KEY!,
|
||
},
|
||
forcePathStyle: true,
|
||
})
|
||
|
||
const S3_BUCKET = process.env.S3_BUCKET || ''
|
||
const UPLOAD_DIR = process.env.UPLOAD_DIR || './uploads'
|
||
const VIDEO_TYPES = ['主图视频', '安装视频', '种草视频']
|
||
|
||
async function fileExists(fullPath: string): Promise<boolean> {
|
||
try {
|
||
await access(fullPath, fsConstants.F_OK)
|
||
return true
|
||
} catch {
|
||
return false
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 检查 S3 对象是否存在
|
||
*/
|
||
async function checkS3ObjectExists(key: string): Promise<boolean> {
|
||
try {
|
||
const cmd = new HeadObjectCommand({
|
||
Bucket: S3_BUCKET,
|
||
Key: key,
|
||
})
|
||
await s3Client.send(cmd)
|
||
return true
|
||
} catch (error: any) {
|
||
if (error.name === 'NotFound' || error.$metadata?.httpStatusCode === 404) {
|
||
return false
|
||
}
|
||
// 权限错误也返回 false,避免中断检查
|
||
if (error.$metadata?.httpStatusCode === 403) {
|
||
return false
|
||
}
|
||
return false
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 列出 S3 中指定前缀的所有对象
|
||
*/
|
||
async function listS3Objects(prefix: string): Promise<string[]> {
|
||
try {
|
||
const cmd = new ListObjectsV2Command({
|
||
Bucket: S3_BUCKET,
|
||
Prefix: prefix,
|
||
MaxKeys: 1000,
|
||
})
|
||
const response = await s3Client.send(cmd)
|
||
return (response.Contents || []).map((obj) => obj.Key || '').filter(Boolean)
|
||
} catch (error: any) {
|
||
if (error.$metadata?.httpStatusCode === 403) {
|
||
console.log(` ⚠️ 权限不足,无法列出 S3 对象(前缀: ${prefix})`)
|
||
}
|
||
return []
|
||
}
|
||
}
|
||
|
||
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,
|
||
createdAt: 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 (!(await fileExists(localPath))) {
|
||
missingFiles.push(m)
|
||
}
|
||
}
|
||
|
||
console.log(`共找到 ${missingFiles.length} 条缺失的文件\n`)
|
||
|
||
if (missingFiles.length === 0) {
|
||
console.log('✅ 没有缺失的文件')
|
||
await prisma.$disconnect()
|
||
return
|
||
}
|
||
|
||
// 取前 10 条作为样本检查
|
||
const sampleFiles = missingFiles.slice(0, 10)
|
||
console.log(`检查前 10 条作为样本:\n`)
|
||
|
||
for (const material of sampleFiles) {
|
||
const fileName = (material.videoSourceUrl as string).replace('/uploads/', '')
|
||
console.log(`[${material.id}] ${material.materialType}`)
|
||
console.log(` 文件名: ${fileName}`)
|
||
console.log(` 创建时间: ${material.createdAt.toISOString()}`)
|
||
|
||
// 1. 检查本地 uploads 目录
|
||
const localPath = path.join(UPLOAD_DIR, fileName)
|
||
const exists = await fileExists(localPath)
|
||
console.log(` 本地文件: ${exists ? '✅ 存在' : '❌ 不存在'} (${localPath})`)
|
||
|
||
// 2. 检查 S3 中的不同路径
|
||
const s3Paths = [
|
||
`由壹点AI/${fileName}`,
|
||
`gongxiangpan/由壹点AI/${fileName}`,
|
||
fileName,
|
||
`video/${fileName}`,
|
||
`videos/${fileName}`,
|
||
]
|
||
|
||
let foundInS3 = false
|
||
for (const s3Path of s3Paths) {
|
||
const exists = await checkS3ObjectExists(s3Path)
|
||
if (exists) {
|
||
console.log(` S3 中找到: ✅ ${s3Path}`)
|
||
foundInS3 = true
|
||
break
|
||
}
|
||
}
|
||
|
||
if (!foundInS3) {
|
||
console.log(` S3 中: ❌ 未找到(已检查 ${s3Paths.length} 个路径)`)
|
||
}
|
||
|
||
console.log('')
|
||
}
|
||
|
||
// 3. 尝试列出 S3 中的 video- 开头的文件,看看实际存储路径
|
||
console.log('\n==== 检查 S3 中的实际存储路径 ====\n')
|
||
console.log('列出 S3 中 video- 开头的文件(最多 20 个)...\n')
|
||
|
||
const s3Prefixes = ['由壹点AI/video-', 'gongxiangpan/由壹点AI/video-', 'video-']
|
||
for (const prefix of s3Prefixes) {
|
||
console.log(`检查前缀: ${prefix}`)
|
||
const objects = await listS3Objects(prefix)
|
||
if (objects.length > 0) {
|
||
console.log(` 找到 ${objects.length} 个对象(显示前 5 个):`)
|
||
objects.slice(0, 5).forEach((key) => {
|
||
console.log(` - ${key}`)
|
||
})
|
||
if (objects.length > 5) {
|
||
console.log(` ... 还有 ${objects.length - 5} 个`)
|
||
}
|
||
} else {
|
||
console.log(` 未找到对象`)
|
||
}
|
||
console.log('')
|
||
}
|
||
|
||
// 4. 检查本地 uploads 目录中实际存在的 video- 文件
|
||
console.log('==== 检查本地 uploads 目录 ====\n')
|
||
try {
|
||
const files = await readdir(UPLOAD_DIR)
|
||
const videoFiles = files.filter((f) => f.startsWith('video-') && f.endsWith('.mp4'))
|
||
console.log(`本地 uploads 目录中有 ${videoFiles.length} 个 video-*.mp4 文件`)
|
||
if (videoFiles.length > 0) {
|
||
console.log(`前 5 个文件:`)
|
||
videoFiles.slice(0, 5).forEach((f) => {
|
||
console.log(` - ${f}`)
|
||
})
|
||
if (videoFiles.length > 5) {
|
||
console.log(` ... 还有 ${videoFiles.length - 5} 个`)
|
||
}
|
||
}
|
||
} catch (error) {
|
||
console.log(`无法读取 uploads 目录: ${error}`)
|
||
}
|
||
|
||
console.log('\n==== 检查完成 ====')
|
||
console.log(`\n💡 建议:`)
|
||
console.log(`1. 如果文件在 S3 中找到,可以使用迁移脚本下载到本地`)
|
||
console.log(`2. 如果文件都不存在,可能是历史数据,文件已被删除`)
|
||
console.log(`3. 检查生产服务器的 /mnt/PoolOne/apps/youyidian-3d/uploads/ 目录`)
|
||
console.log(`4. 检查是否有备份目录:/mnt/PoolOne/uploads_backup_before_flnxi/`)
|
||
|
||
await prisma.$disconnect()
|
||
}
|
||
|
||
main().catch((error) => {
|
||
console.error('脚本执行出错:', error)
|
||
process.exit(1)
|
||
})
|
||
|