mirror of
https://github.com/complexcaresolutions/cms.c2sgmbh.git
synced 2026-03-17 17:24:12 +00:00
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:
parent
a09f6abc3d
commit
2872f32635
2 changed files with 55 additions and 0 deletions
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
import type { CollectionConfig } from 'payload'
|
import type { CollectionConfig } from 'payload'
|
||||||
import { isYouTubeManager, hasYouTubeAccess } from '../lib/youtubeAccess'
|
import { isYouTubeManager, hasYouTubeAccess } from '../lib/youtubeAccess'
|
||||||
|
import { downloadChannelImage } from '../hooks/youtubeChannels/downloadChannelImage'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* YouTubeChannels Collection
|
* YouTubeChannels Collection
|
||||||
|
|
@ -27,6 +28,9 @@ export const YouTubeChannels: CollectionConfig = {
|
||||||
update: isYouTubeManager,
|
update: isYouTubeManager,
|
||||||
delete: isYouTubeManager,
|
delete: isYouTubeManager,
|
||||||
},
|
},
|
||||||
|
hooks: {
|
||||||
|
afterChange: [downloadChannelImage],
|
||||||
|
},
|
||||||
fields: [
|
fields: [
|
||||||
{
|
{
|
||||||
name: 'name',
|
name: 'name',
|
||||||
|
|
@ -108,6 +112,16 @@ export const YouTubeChannels: CollectionConfig = {
|
||||||
position: 'sidebar',
|
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
|
// Branding
|
||||||
{
|
{
|
||||||
name: 'branding',
|
name: 'branding',
|
||||||
|
|
|
||||||
41
src/hooks/youtubeChannels/downloadChannelImage.ts
Normal file
41
src/hooks/youtubeChannels/downloadChannelImage.ts
Normal 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
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue