200 lines
4.8 KiB
TypeScript
200 lines
4.8 KiB
TypeScript
import 'dotenv/config'
|
|
|
|
import { prisma } from '../src/lib/prisma'
|
|
import { assetCenterService } from '../src/server/services/asset-center.service'
|
|
import {
|
|
ASSET_VARIANT_THUMBNAIL_PLACEHOLDER_URL,
|
|
assetVariantThumbnailService,
|
|
} from '../src/server/services/asset-variant-thumbnail.service'
|
|
|
|
interface BackfillOptions {
|
|
renderBaseUrl: string
|
|
force: boolean
|
|
concurrency: number
|
|
limit: number | null
|
|
}
|
|
|
|
interface BackfillTask {
|
|
id: string
|
|
code: string
|
|
name: string
|
|
modelUrl: string
|
|
thumbnailUrl: string | null
|
|
}
|
|
|
|
function readFlag(name: string): boolean {
|
|
return process.argv.includes(name)
|
|
}
|
|
|
|
function readOption(name: string): string | null {
|
|
const direct = process.argv.find((arg) => arg.startsWith(`${name}=`))
|
|
if (direct) {
|
|
return direct.slice(name.length + 1).trim() || null
|
|
}
|
|
|
|
const index = process.argv.findIndex((arg) => arg === name)
|
|
if (index >= 0) {
|
|
const next = process.argv[index + 1]
|
|
if (next && !next.startsWith('--')) {
|
|
return next.trim() || null
|
|
}
|
|
}
|
|
|
|
return null
|
|
}
|
|
|
|
function resolveOptions(): BackfillOptions {
|
|
const renderBaseUrl =
|
|
readOption('--base-url') ||
|
|
process.env.ASSET_THUMBNAIL_RENDER_BASE_URL ||
|
|
process.env.NEXTAUTH_URL ||
|
|
'http://127.0.0.1:3200'
|
|
|
|
const concurrency = Number(readOption('--concurrency') || 2)
|
|
const limitInput = readOption('--limit')
|
|
const parsedLimit = limitInput ? Number(limitInput) : null
|
|
|
|
return {
|
|
renderBaseUrl,
|
|
force: readFlag('--force'),
|
|
concurrency: Number.isFinite(concurrency) && concurrency > 0 ? Math.floor(concurrency) : 2,
|
|
limit:
|
|
parsedLimit !== null && Number.isFinite(parsedLimit) && parsedLimit > 0
|
|
? Math.floor(parsedLimit)
|
|
: null,
|
|
}
|
|
}
|
|
|
|
function buildTaskWhere(force: boolean) {
|
|
const modelUrlWhere = {
|
|
modelUrl: { not: '' },
|
|
}
|
|
|
|
if (force) {
|
|
return modelUrlWhere
|
|
}
|
|
|
|
return {
|
|
...modelUrlWhere,
|
|
OR: [
|
|
{ thumbnailUrl: null },
|
|
{ thumbnailUrl: '' },
|
|
{ thumbnailUrl: ASSET_VARIANT_THUMBNAIL_PLACEHOLDER_URL },
|
|
],
|
|
}
|
|
}
|
|
|
|
async function loadTasks(options: BackfillOptions): Promise<BackfillTask[]> {
|
|
const tasks = await prisma.assetVariant.findMany({
|
|
where: buildTaskWhere(options.force),
|
|
select: {
|
|
id: true,
|
|
code: true,
|
|
name: true,
|
|
modelUrl: true,
|
|
thumbnailUrl: true,
|
|
},
|
|
orderBy: [{ updatedAt: 'desc' }],
|
|
...(options.limit ? { take: options.limit } : {}),
|
|
})
|
|
|
|
return tasks.filter((task) => {
|
|
const modelUrl = task.modelUrl.trim()
|
|
return (
|
|
modelUrl.startsWith('/') ||
|
|
modelUrl.startsWith('http://') ||
|
|
modelUrl.startsWith('https://')
|
|
)
|
|
})
|
|
}
|
|
|
|
async function runWithConcurrency<T>(
|
|
items: T[],
|
|
concurrency: number,
|
|
worker: (item: T, index: number) => Promise<void>,
|
|
) {
|
|
const running = new Set<Promise<void>>()
|
|
|
|
for (let index = 0; index < items.length; index += 1) {
|
|
const promise = worker(items[index], index).finally(() => {
|
|
running.delete(promise)
|
|
})
|
|
running.add(promise)
|
|
|
|
if (running.size >= concurrency) {
|
|
await Promise.race(running)
|
|
}
|
|
}
|
|
|
|
await Promise.all(running)
|
|
}
|
|
|
|
async function main() {
|
|
const options = resolveOptions()
|
|
|
|
console.log('开始批量补生成资产缩略图。')
|
|
console.log(JSON.stringify(options, null, 2))
|
|
|
|
const tasks = await loadTasks(options)
|
|
|
|
if (tasks.length === 0) {
|
|
console.log('没有需要处理的规格款。')
|
|
return
|
|
}
|
|
|
|
let successCount = 0
|
|
let failureCount = 0
|
|
let fallbackCount = 0
|
|
|
|
await runWithConcurrency(tasks, options.concurrency, async (task, index) => {
|
|
const prefix = `[${index + 1}/${tasks.length}]`
|
|
try {
|
|
const variant = await assetCenterService.regenerateVariantThumbnail(task.id, {
|
|
renderBaseUrl: options.renderBaseUrl,
|
|
updatedBy: null,
|
|
})
|
|
if (variant.thumbnailUrl === ASSET_VARIANT_THUMBNAIL_PLACEHOLDER_URL) {
|
|
fallbackCount += 1
|
|
console.log(`${prefix} 已回退占位图 ${task.code} ${task.name}`)
|
|
} else {
|
|
successCount += 1
|
|
console.log(
|
|
`${prefix} 已完成 ${task.code} ${task.name} -> ${variant.thumbnailUrl || '无缩略图'}`
|
|
)
|
|
}
|
|
} catch (error) {
|
|
failureCount += 1
|
|
console.error(
|
|
`${prefix} 失败 ${task.code} ${task.name}: ${
|
|
error instanceof Error ? error.message : String(error)
|
|
}`
|
|
)
|
|
}
|
|
})
|
|
|
|
console.log(
|
|
JSON.stringify(
|
|
{
|
|
total: tasks.length,
|
|
successCount,
|
|
fallbackCount,
|
|
failureCount,
|
|
force: options.force,
|
|
},
|
|
null,
|
|
2,
|
|
),
|
|
)
|
|
}
|
|
|
|
main()
|
|
.catch((error) => {
|
|
console.error('批量补生成资产缩略图失败:')
|
|
console.error(error)
|
|
process.exitCode = 1
|
|
})
|
|
.finally(async () => {
|
|
await assetVariantThumbnailService.shutdown()
|
|
await prisma.$disconnect()
|
|
})
|