252 lines
6.9 KiB
TypeScript
252 lines
6.9 KiB
TypeScript
/**
|
||
* 恢复缺失的本地视频文件
|
||
*
|
||
* 对于 videoSourceUrl 已经是本地路径但文件不存在的记录,
|
||
* 尝试从 MinIO 下载对应的文件到本地
|
||
*/
|
||
|
||
import { PrismaClient } from '@prisma/client'
|
||
import { S3Client, GetObjectCommand, HeadObjectCommand } from '@aws-sdk/client-s3'
|
||
import { Readable } from 'stream'
|
||
import { mkdir, writeFile } from 'fs/promises'
|
||
import { existsSync } 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,
|
||
requestHandler: {
|
||
requestTimeout: 300000,
|
||
connectionTimeout: 30000,
|
||
},
|
||
maxAttempts: 5,
|
||
retryMode: 'adaptive',
|
||
})
|
||
|
||
const S3_BUCKET = process.env.S3_BUCKET || ''
|
||
const UPLOAD_DIR = process.env.UPLOAD_DIR || './uploads'
|
||
const VIDEO_TYPES = ['主图视频', '安装视频', '种草视频']
|
||
|
||
/**
|
||
* 从 S3 下载整个对象为 Buffer
|
||
*/
|
||
async function downloadS3Object(key: string): Promise<Buffer> {
|
||
const cmd = new GetObjectCommand({
|
||
Bucket: S3_BUCKET,
|
||
Key: key,
|
||
})
|
||
|
||
const response = await s3Client.send(cmd)
|
||
const stream = response.Body as Readable
|
||
|
||
const chunks: Buffer[] = []
|
||
for await (const chunk of stream) {
|
||
chunks.push(chunk)
|
||
}
|
||
|
||
return Buffer.concat(chunks)
|
||
}
|
||
|
||
/**
|
||
* 检查 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
|
||
}
|
||
throw error
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 尝试在 MinIO 中查找文件
|
||
* 可能的路径:
|
||
* 1. 由壹点AI/video-xxx.mp4
|
||
* 2. gongxiangpan/由壹点AI/video-xxx.mp4
|
||
* 3. video-xxx.mp4 (直接在根目录)
|
||
*/
|
||
async function findS3Key(fileName: string): Promise<string | null> {
|
||
const possibleKeys = [
|
||
`由壹点AI/${fileName}`,
|
||
`gongxiangpan/由壹点AI/${fileName}`,
|
||
fileName,
|
||
]
|
||
|
||
for (const key of possibleKeys) {
|
||
try {
|
||
const exists = await checkS3ObjectExists(key)
|
||
if (exists) {
|
||
return key
|
||
}
|
||
} catch (error: any) {
|
||
// 如果是权限错误,继续尝试下一个路径
|
||
if (error.$metadata?.httpStatusCode === 403) {
|
||
continue
|
||
}
|
||
// 其他错误也继续尝试
|
||
}
|
||
}
|
||
|
||
return null
|
||
}
|
||
|
||
async function restoreOneMaterial(material: {
|
||
id: string
|
||
materialType: string
|
||
videoSourceUrl: string | null
|
||
zipFileName: string | null
|
||
}) {
|
||
const { id, materialType, videoSourceUrl, zipFileName } = material
|
||
|
||
// 如果 videoSourceUrl 为 null,跳过
|
||
if (!videoSourceUrl) {
|
||
console.log(`[${id}] 跳过:videoSourceUrl 为 null`)
|
||
return { skipped: true, reason: 'videoSourceUrl_null' }
|
||
}
|
||
|
||
// 提取文件名
|
||
const fileName = videoSourceUrl.replace('/uploads/', '')
|
||
const localPath = path.join(UPLOAD_DIR, fileName)
|
||
|
||
// 检查本地文件是否已存在
|
||
if (existsSync(localPath)) {
|
||
console.log(`[${id}] 跳过:本地文件已存在`)
|
||
return { skipped: true, reason: 'file_exists' }
|
||
}
|
||
|
||
// 尝试从 zipFileName 或 videoSourceUrl 提取文件名
|
||
const searchFileName = zipFileName || fileName
|
||
|
||
console.log(`[${id}] ${materialType}`)
|
||
console.log(` 查找文件: ${searchFileName}`)
|
||
|
||
// 在 MinIO 中查找文件
|
||
const s3Key = await findS3Key(searchFileName)
|
||
|
||
if (!s3Key) {
|
||
console.log(` ⚠️ 未在 MinIO 中找到文件(可能已被删除或路径不对)`)
|
||
return { skipped: true, reason: 'not_found_in_s3' }
|
||
}
|
||
|
||
console.log(` 📍 在 MinIO 中找到: ${s3Key}`)
|
||
|
||
try {
|
||
// 确保 uploads 目录存在
|
||
if (!existsSync(UPLOAD_DIR)) {
|
||
await mkdir(UPLOAD_DIR, { recursive: true })
|
||
}
|
||
|
||
// 从 S3 下载
|
||
console.log(` 📥 正在下载...`)
|
||
const buffer = await downloadS3Object(s3Key)
|
||
console.log(` 📦 下载完成,大小:${buffer.length} 字节`)
|
||
|
||
// 保存到本地
|
||
await writeFile(localPath, buffer)
|
||
console.log(` ✅ 已保存到本地: ${localPath}`)
|
||
console.log(` ✅ videoSourceUrl 无需修改(已是本地路径格式)\n`)
|
||
|
||
return { success: true, s3Key, localPath }
|
||
} catch (error: any) {
|
||
if (error.$metadata?.httpStatusCode === 403) {
|
||
console.error(` ❌ 权限错误(403):无法访问 MinIO,请检查 S3 凭证配置`)
|
||
} else {
|
||
console.error(` ❌ 下载失败:`, error.message || error)
|
||
}
|
||
return { success: false, error: error.message || error }
|
||
}
|
||
}
|
||
|
||
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,
|
||
},
|
||
})
|
||
|
||
console.log(`共找到 ${allVideos.length} 条 videoSourceUrl 为本地路径的记录\n`)
|
||
|
||
// 检查哪些文件缺失
|
||
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} 条本地文件缺失,开始尝试从 MinIO 恢复...\n`)
|
||
console.log(`⚠️ 注意:由于权限限制,可能无法访问所有文件。将尝试处理前 10 条作为测试...\n`)
|
||
|
||
if (missingFiles.length === 0) {
|
||
console.log('✅ 没有需要恢复的文件')
|
||
await prisma.$disconnect()
|
||
return
|
||
}
|
||
|
||
let successCount = 0
|
||
let skippedCount = 0
|
||
let errorCount = 0
|
||
|
||
// 先处理前 10 条作为测试
|
||
const testBatch = missingFiles.slice(0, 10)
|
||
for (const material of testBatch) {
|
||
const result = await restoreOneMaterial(material)
|
||
if (result.success) {
|
||
successCount++
|
||
} else if (result.skipped) {
|
||
skippedCount++
|
||
} else {
|
||
errorCount++
|
||
}
|
||
}
|
||
|
||
console.log('\n==== 测试批次完成 ====')
|
||
console.log(`成功恢复: ${successCount} 条`)
|
||
console.log(`跳过(文件已存在或未找到): ${skippedCount} 条`)
|
||
console.log(`失败: ${errorCount} 条`)
|
||
if (missingFiles.length > 10) {
|
||
console.log(`\n还有 ${missingFiles.length - 10} 条待处理。`)
|
||
console.log(`如果测试成功,可以修改脚本处理全部 ${missingFiles.length} 条。`)
|
||
}
|
||
|
||
await prisma.$disconnect()
|
||
}
|
||
|
||
main().catch((error) => {
|
||
console.error('脚本执行出错:', error)
|
||
process.exit(1)
|
||
})
|
||
|