114 lines
3.3 KiB
TypeScript
114 lines
3.3 KiB
TypeScript
import { readFile } from 'node:fs/promises'
|
|
import path from 'node:path'
|
|
import { doesObjectExist, uploadBufferToObjectStorage } from '../src/lib/storage'
|
|
|
|
const PRESETS_FILE = path.join(process.cwd(), 'src/lib/3d/material-pbr-presets.ts')
|
|
const URL_PATTERN = /https:\/\/(?:dl\.polyhaven\.org|polyhaven\.com|cdn\.polyhaven\.com)\/[^\s'"]+/g
|
|
const CONFIRM_TOKEN = 'sync-material-textures'
|
|
|
|
function toObjectKey(remoteUrl: string): string | null {
|
|
try {
|
|
const parsed = new URL(remoteUrl)
|
|
const segments = parsed.pathname.split('/').filter(Boolean)
|
|
const fileName = segments.at(-1)
|
|
if (!fileName) return null
|
|
const assetName = segments.at(-2) || 'misc'
|
|
return `materials/pbr/polyhaven/${assetName}/${fileName}`
|
|
} catch {
|
|
return null
|
|
}
|
|
}
|
|
|
|
function contentTypeFromObjectKey(objectKey: string): string {
|
|
const extension = path.extname(objectKey).toLowerCase()
|
|
switch (extension) {
|
|
case '.jpg':
|
|
case '.jpeg':
|
|
return 'image/jpeg'
|
|
case '.png':
|
|
return 'image/png'
|
|
case '.webp':
|
|
return 'image/webp'
|
|
case '.svg':
|
|
return 'image/svg+xml'
|
|
case '.avif':
|
|
return 'image/avif'
|
|
default:
|
|
return 'application/octet-stream'
|
|
}
|
|
}
|
|
|
|
async function uploadTexture(remoteUrl: string, objectKey: string): Promise<void> {
|
|
const response = await fetch(remoteUrl)
|
|
if (!response.ok) {
|
|
throw new Error(`下载失败 ${response.status}: ${remoteUrl}`)
|
|
}
|
|
const buffer = Buffer.from(await response.arrayBuffer())
|
|
await uploadBufferToObjectStorage(buffer, objectKey, contentTypeFromObjectKey(objectKey))
|
|
}
|
|
|
|
function readOption(name: string): string {
|
|
const inline = process.argv.find((arg) => arg.startsWith(`${name}=`))
|
|
if (inline) return inline.slice(name.length + 1).trim()
|
|
const index = process.argv.findIndex((arg) => arg === name)
|
|
const value = index >= 0 ? process.argv[index + 1] : ''
|
|
return value && !value.startsWith('--') ? value.trim() : ''
|
|
}
|
|
|
|
function assertApplyAllowed() {
|
|
if (!process.argv.includes('--apply')) {
|
|
throw new Error('材质贴图同步默认禁止上传对象存储;确认目标环境后请显式追加 --apply。')
|
|
}
|
|
if (readOption('--confirm') !== CONFIRM_TOKEN) {
|
|
throw new Error(`材质贴图同步真实上传必须显式传入 --confirm=${CONFIRM_TOKEN}。`)
|
|
}
|
|
}
|
|
|
|
async function main() {
|
|
assertApplyAllowed()
|
|
|
|
const source = await readFile(PRESETS_FILE, 'utf8')
|
|
const matches = source.match(URL_PATTERN) || []
|
|
const remoteUrls = Array.from(new Set(matches))
|
|
|
|
if (remoteUrls.length === 0) {
|
|
console.log('未发现需要同步的远程贴图 URL。')
|
|
return
|
|
}
|
|
|
|
let downloaded = 0
|
|
let skipped = 0
|
|
let failed = 0
|
|
|
|
for (const remoteUrl of remoteUrls) {
|
|
const objectKey = toObjectKey(remoteUrl)
|
|
if (!objectKey) {
|
|
failed += 1
|
|
console.warn(`无法解析路径: ${remoteUrl}`)
|
|
continue
|
|
}
|
|
|
|
if (await doesObjectExist(objectKey)) {
|
|
skipped += 1
|
|
continue
|
|
}
|
|
|
|
try {
|
|
await uploadTexture(remoteUrl, objectKey)
|
|
downloaded += 1
|
|
console.log(`已上传: ${objectKey}`)
|
|
} catch (error) {
|
|
failed += 1
|
|
console.warn(`下载失败: ${remoteUrl}`)
|
|
console.warn(error)
|
|
}
|
|
}
|
|
|
|
console.log(`同步完成:下载 ${downloaded},跳过 ${skipped},失败 ${failed}`)
|
|
}
|
|
|
|
main().catch((error) => {
|
|
console.error('贴图同步脚本执行失败', error)
|
|
process.exit(1)
|
|
})
|