Files
professional-website/scripts/rename-media.ts
Jason Woltje ad811ba70e
All checks were successful
ci/woodpecker/push/web Pipeline was successful
fix(scripts): resolve lint and typecheck errors in utility scripts
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-15 21:13:01 -05:00

76 lines
2.4 KiB
TypeScript

/**
* Rename media — re-uploads each file under its new filename.
* Payload replaces the old file + regenerates thumbnails while keeping the same ID.
*/
import { getPayload } from 'payload'
import config from '@payload-config'
import path from 'node:path'
import fs from 'node:fs'
const imagesDir = path.resolve(process.cwd(), 'images')
const renames: Array<{ alt: string; newFile: string }> = [
{ alt: 'Jason Woltje portrait', newFile: 'jason-portrait.jpg' },
{ alt: 'Stylized portrait — thought leader', newFile: 'thought-leader-city.jpg' },
{ alt: 'Stylized portrait — social', newFile: 'social-neon.jpg' },
{ alt: 'Jason Woltje — tech founder portrait, dark background', newFile: 'tech-founder-dark.jpg' },
{ alt: 'Jason Woltje — social media profile, neon gradient', newFile: 'social-neon.jpg' },
{ alt: 'Jason Woltje — at the desk, documentary style', newFile: 'at-the-desk.jpg' },
{ alt: 'Jason Woltje — illustrated portrait', newFile: 'illustrated-portrait.jpg' },
{ alt: 'Jason Woltje — thought leader portrait, city backdrop', newFile: 'thought-leader-city.jpg' },
{ alt: 'Jason Woltje — fashion editorial portrait', newFile: 'editorial-blazer.jpg' },
]
async function main() {
const payload = await getPayload({ config })
const seen = new Set<number>()
for (const r of renames) {
const { docs } = await payload.find({
collection: 'media',
where: { alt: { equals: r.alt } },
limit: 1,
depth: 0,
})
if (docs.length === 0) {
console.log(` skip — no media with alt "${r.alt}"`)
continue
}
const doc = docs[0]!
if (seen.has(doc.id as number)) continue
seen.add(doc.id as number)
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const currentFilename = (doc as any).filename as string
if (currentFilename === r.newFile) {
console.log(` ↷ id=${doc.id} already named ${r.newFile}`)
continue
}
const filePath = path.join(imagesDir, r.newFile)
if (!fs.existsSync(filePath)) {
console.log(` ⚠ file missing: ${r.newFile}`)
continue
}
await payload.update({
collection: 'media',
id: doc.id,
filePath,
data: {},
})
console.log(` ✓ id=${doc.id}: ${currentFilename}${r.newFile}`)
}
console.log('\nDone.')
process.exit(0)
}
main().catch((err) => {
console.error('Fatal:', err)
process.exit(1)
})