feat: auto-download YouTube channel images on create/update

Adds channelThumbnailUrl field to store YouTube API URL.
afterChange hook downloads image to Payload Media when branding.logo is empty.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Martin Porwoll 2026-02-14 12:30:48 +00:00
parent a09f6abc3d
commit 2872f32635
2 changed files with 55 additions and 0 deletions

View file

@ -2,6 +2,7 @@
import type { CollectionConfig } from 'payload'
import { isYouTubeManager, hasYouTubeAccess } from '../lib/youtubeAccess'
import { downloadChannelImage } from '../hooks/youtubeChannels/downloadChannelImage'
/**
* YouTubeChannels Collection
@ -27,6 +28,9 @@ export const YouTubeChannels: CollectionConfig = {
update: isYouTubeManager,
delete: isYouTubeManager,
},
hooks: {
afterChange: [downloadChannelImage],
},
fields: [
{
name: 'name',
@ -108,6 +112,16 @@ export const YouTubeChannels: CollectionConfig = {
position: 'sidebar',
},
},
{
name: 'channelThumbnailUrl',
type: 'text',
label: 'Kanal-Thumbnail URL',
admin: {
readOnly: true,
description: 'Profil-Thumbnail URL von YouTube (automatisch befüllt)',
position: 'sidebar',
},
},
// Branding
{
name: 'branding',

View file

@ -0,0 +1,41 @@
import type { CollectionAfterChangeHook } from 'payload'
import { downloadAndUploadImage } from '../../lib/utils/media-download'
/**
* After a YouTubeChannels document is created or updated,
* automatically download the channel profile image if the branding.logo field is empty
* and a channelThumbnailUrl is available.
*/
export const downloadChannelImage: CollectionAfterChangeHook = async ({ doc, req }) => {
// Skip if logo already exists
if (doc.branding?.logo) return doc
// Need channelThumbnailUrl (set from YouTube API or manually)
const thumbnailUrl = doc.channelThumbnailUrl
if (!thumbnailUrl) return doc
const channelId = doc.youtubeChannelId
if (!channelId) return doc
const filename = `yt-channel-${channelId}.jpg`
const tenantId = typeof doc.tenant === 'object' ? doc.tenant?.id : doc.tenant
const mediaId = await downloadAndUploadImage(req.payload, {
url: thumbnailUrl,
filename,
alt: doc.name ? `Kanal: ${typeof doc.name === 'string' ? doc.name : doc.name?.de || channelId}` : `YouTube Channel ${channelId}`,
tenantId: tenantId || undefined,
})
if (mediaId) {
await req.payload.update({
collection: 'youtube-channels',
id: doc.id,
data: { branding: { ...(doc.branding || {}), logo: mediaId } },
depth: 0,
})
console.log(`[yt-thumbnail] Auto-downloaded channel image for ${channelId} (media: ${mediaId})`)
}
return doc
}