gongxiangp2/scripts/check-old-materials-external-server.ts

247 lines
7.4 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.

/**
* 检查老素材在外网服务器上的源文件
* 通过HTTP访问 103.118.245.6 检查文件是否存在
*/
import { PrismaClient } from '@prisma/client'
import { writeFile } from 'fs/promises'
import https from 'https'
import http from 'http'
const prisma = new PrismaClient()
const cutoffDate = new Date('2025-10-27T23:59:59.999Z')
// 外网服务器配置
const EXTERNAL_SERVER = '103.118.245.6'
const EXTERNAL_BASE_URL = `http://${EXTERNAL_SERVER}`
/**
* 判断是否为老素材
*/
function isOldMaterial(material: any): boolean {
if (material.createdAt <= cutoffDate) return true
const previewUrl = material.previewUrl || ''
if (previewUrl.includes('s3.1080p.hk') &&
previewUrl.includes('/jsxiangche/') &&
!previewUrl.includes('由壹点AI/')) {
return true
}
return false
}
/**
* 检查HTTP URL文件是否存在(HEAD请求,更快)
*/
function checkHttpFileExists(url: string): Promise<boolean> {
return new Promise((resolve) => {
const protocol = url.startsWith('https:') ? https : http
const urlObj = new URL(url)
const options = {
hostname: urlObj.hostname,
port: urlObj.port || (urlObj.protocol === 'https:' ? 443 : 80),
path: urlObj.pathname + urlObj.search,
method: 'HEAD',
timeout: 3000,
}
const request = protocol.request(options, (response) => {
const isOk = response.statusCode === 200 || response.statusCode === 301 || response.statusCode === 302
response.destroy()
resolve(isOk)
})
request.on('error', () => resolve(false))
request.on('timeout', () => {
request.destroy()
resolve(false)
})
request.end()
})
}
/**
* 构建外网服务器URL
* 假设文件路径是 uploads/material-xxx.zip
* 外网服务器URL可能是: http://103.118.245.6/uploads/material-xxx.zip
*/
function buildExternalUrl(zipFilePath: string): string {
// 如果已经是HTTP URL,直接返回
if (zipFilePath.startsWith('http://') || zipFilePath.startsWith('https://')) {
return zipFilePath
}
// 如果是相对路径,构建外网服务器URL
const fileName = zipFilePath.replace(/^uploads\//, '').replace(/^\/app\/uploads\//, '')
return `${EXTERNAL_BASE_URL}/uploads/${fileName}`
}
async function main() {
console.log('='.repeat(60))
console.log('检查老素材在外网服务器上的源文件')
console.log('='.repeat(60))
console.log(`外网服务器: ${EXTERNAL_SERVER}`)
console.log()
// 查询所有素材(先查询所有,再过滤老素材)
const allMaterials = await prisma.material.findMany({
where: {
deletedAt: null,
},
select: {
id: true,
materialType: true,
productModel: true,
authorName: true,
categoryName: true,
createdAt: true,
previewUrl: true,
zipFilePath: true,
zipFileName: true,
zipFileSize: true,
},
})
// 使用isOldMaterial过滤出真正的老素材
const oldMaterials = allMaterials.filter(isOldMaterial)
console.log(`总素材数: ${allMaterials.length}`)
console.log(`老素材数: ${oldMaterials.length}`)
console.log()
// 检查外网服务器上的文件
console.log('🔍 检查外网服务器上的文件...')
const results: Array<{
id: string
materialType: string
productModel: string
authorName: string
categoryName: string
zipFilePath: string | null
zipFileName: string | null
zipFileSize: number | null
externalUrl: string
fileExists: boolean
createdAt: Date
}> = []
// 先筛选出需要检查的素材
const materialsToCheck = oldMaterials.filter(m => {
if (!m.zipFilePath || m.zipFilePath === '') return false
return m.zipFilePath.startsWith('uploads/') ||
m.zipFilePath.startsWith('/app/uploads/') ||
m.zipFilePath.startsWith('./uploads/')
})
console.log(` 需要检查的文件数: ${materialsToCheck.length}`)
console.log()
// 批量检查(并发控制,避免过多请求)
const BATCH_SIZE = 20 // 增加并发数,加快速度
let checked = 0
for (let i = 0; i < materialsToCheck.length; i += BATCH_SIZE) {
const batch = materialsToCheck.slice(i, i + BATCH_SIZE)
const batchPromises = batch.map(async (material) => {
const externalUrl = buildExternalUrl(material.zipFilePath!)
const fileExists = await checkHttpFileExists(externalUrl)
return {
id: material.id,
materialType: material.materialType || '',
productModel: material.productModel || '',
authorName: material.authorName || '',
categoryName: material.categoryName || '',
zipFilePath: material.zipFilePath,
zipFileName: material.zipFileName,
zipFileSize: material.zipFileSize ? Number(material.zipFileSize) : null,
externalUrl,
fileExists,
createdAt: material.createdAt,
}
})
const batchResults = await Promise.all(batchPromises)
results.push(...batchResults)
checked += batch.length
const progress = Math.round(checked / materialsToCheck.length * 100)
if (checked % 50 === 0 || checked === materialsToCheck.length) {
console.log(` 已检查: ${checked}/${materialsToCheck.length} (${progress}%)`)
}
}
console.log(` 检查完成: ${checked} 个文件`)
console.log()
// 统计
const existing = results.filter(r => r.fileExists)
const missing = results.filter(r => !r.fileExists)
console.log('📊 统计结果:')
console.log(` 总检查数: ${results.length}`)
console.log(` 外网服务器存在: ${existing.length} 个`)
console.log(` 外网服务器缺失: ${missing.length} 个`)
console.log()
// 显示示例
if (existing.length > 0) {
console.log('✅ 外网服务器存在的文件示例 (前5个):')
existing.slice(0, 5).forEach((r, i) => {
console.log(` ${i + 1}. ${r.productModel} (${r.materialType})`)
console.log(` URL: ${r.externalUrl}`)
})
console.log()
}
if (missing.length > 0) {
console.log('❌ 外网服务器缺失的文件示例 (前5个):')
missing.slice(0, 5).forEach((r, i) => {
console.log(` ${i + 1}. ${r.productModel} (${r.materialType})`)
console.log(` URL: ${r.externalUrl}`)
})
console.log()
}
// 生成CSV报告(只包含缺失的)
const reportRows: string[] = []
reportRows.push('素材ID,素材类型,产品型号,作者,使用店铺,源文件路径,外网服务器URL,文件是否存在,源文件大小(字节),创建时间')
for (const r of results) {
const row = [
r.id,
r.materialType,
r.productModel,
r.authorName,
r.categoryName,
r.zipFilePath || '',
r.externalUrl,
r.fileExists ? '是' : '否',
r.zipFileSize?.toString() || '',
r.createdAt.toISOString(),
]
reportRows.push(row.map(field => {
if (field === null || field === undefined) return ''
const str = String(field)
if (str.includes(',') || str.includes('"') || str.includes('\n')) {
return `"${str.replace(/"/g, '""')}"`
}
return str
}).join(','))
}
const reportFile = '/tmp/老素材外网服务器检查报告.csv'
await writeFile(reportFile, reportRows.join('\n'), 'utf-8')
console.log(`✅ 报告已生成: ${reportFile}`)
console.log(` 包含 ${results.length} 个老素材的检查结果`)
console.log()
await prisma.$disconnect()
}
main().catch((error) => {
console.error('执行失败:', error)
process.exit(1)
})