feat: manage network people media
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import 'dotenv/config'
|
||||
|
||||
import config from '@payload-config'
|
||||
import crypto from 'crypto'
|
||||
import { getPayload, type Payload } from 'payload'
|
||||
|
||||
import { netzwerkContent } from '@/spa/netzwerkContent'
|
||||
@@ -15,6 +16,29 @@ const mediaByFilename = async (payload: Payload, filename: string) => {
|
||||
return result.docs[0]?.id
|
||||
}
|
||||
|
||||
const safeRemoteName = (url: string) => {
|
||||
const parsed = new URL(url)
|
||||
const ext = parsed.pathname.split('/').pop()?.includes('.') ? `.${parsed.pathname.split('.').pop()}` : '.jpg'
|
||||
const hash = crypto.createHash('sha1').update(url).digest('hex').slice(0, 10)
|
||||
return `${parsed.hostname.replace(/[^a-z0-9]+/gi, '-')}-${hash}${ext}`.toLowerCase()
|
||||
}
|
||||
|
||||
const filenameForSource = (source: string) => {
|
||||
if (source.startsWith('http')) return safeRemoteName(source)
|
||||
return source.split('?')[0]?.split('/').filter(Boolean).pop()
|
||||
}
|
||||
|
||||
const itemsWithImageUploads = async <T extends { image?: string }>(payload: Payload, items: T[]) =>
|
||||
Promise.all(
|
||||
items.map(async (item) => {
|
||||
const filename = item.image ? filenameForSource(item.image) : undefined
|
||||
return {
|
||||
...item,
|
||||
imageUpload: filename ? await mediaByFilename(payload, filename) : undefined,
|
||||
}
|
||||
}),
|
||||
)
|
||||
|
||||
async function main() {
|
||||
const payload = await getPayload({ config })
|
||||
|
||||
@@ -36,6 +60,10 @@ async function main() {
|
||||
const sponsoringImage = await mediaByFilename(payload, netzwerkContent.sponsoring.imageFilename)
|
||||
const { imageFilename: _heroFilename, ...heroContent } = netzwerkContent.hero
|
||||
const { imageFilename: _sponsoringFilename, ...sponsoringContent } = netzwerkContent.sponsoring
|
||||
const [patronPeople, juryMembers] = await Promise.all([
|
||||
itemsWithImageUploads(payload, netzwerkContent.patronage.people),
|
||||
itemsWithImageUploads(payload, netzwerkContent.jury.members),
|
||||
])
|
||||
|
||||
await payload.update({
|
||||
collection: 'pages',
|
||||
@@ -49,6 +77,14 @@ async function main() {
|
||||
...heroContent,
|
||||
image: heroImage,
|
||||
},
|
||||
patronage: {
|
||||
...netzwerkContent.patronage,
|
||||
people: patronPeople,
|
||||
},
|
||||
jury: {
|
||||
...netzwerkContent.jury,
|
||||
members: juryMembers,
|
||||
},
|
||||
sponsoring: {
|
||||
...sponsoringContent,
|
||||
image: sponsoringImage,
|
||||
|
||||
Reference in New Issue
Block a user