mirror of
https://github.com/complexcaresolutions/cms.c2sgmbh.git
synced 2026-03-17 16:14:12 +00:00
feat: auto-download YouTube thumbnails on content create/update
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
52a6bce815
commit
a09f6abc3d
2 changed files with 45 additions and 1 deletions
|
|
@ -8,6 +8,7 @@ import {
|
|||
canAccessAssignedContent,
|
||||
} from '../lib/youtubeAccess'
|
||||
import { createTasksOnStatusChange } from '../hooks/youtubeContent/createTasksOnStatusChange'
|
||||
import { downloadThumbnail } from '../hooks/youtubeContent/downloadThumbnail'
|
||||
// TODO: ScriptSectionBlock causes admin UI rendering issues
|
||||
// import { ScriptSectionBlock } from '../blocks/ScriptSectionBlock'
|
||||
|
||||
|
|
@ -38,7 +39,7 @@ export const YouTubeContent: CollectionConfig = {
|
|||
delete: isYouTubeManager,
|
||||
},
|
||||
hooks: {
|
||||
afterChange: [createTasksOnStatusChange],
|
||||
afterChange: [createTasksOnStatusChange, downloadThumbnail],
|
||||
beforeChange: [
|
||||
// Auto-Slug generieren
|
||||
({ data }) => {
|
||||
|
|
|
|||
43
src/hooks/youtubeContent/downloadThumbnail.ts
Normal file
43
src/hooks/youtubeContent/downloadThumbnail.ts
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
import type { CollectionAfterChangeHook } from 'payload'
|
||||
import { downloadAndUploadImage } from '../../lib/utils/media-download'
|
||||
import { getYouTubeThumbnail } from '../../lib/utils/youtube'
|
||||
|
||||
/**
|
||||
* After a YouTubeContent document is created or updated,
|
||||
* automatically download the YouTube thumbnail if the thumbnail field is empty
|
||||
* and a YouTube videoId is present.
|
||||
*/
|
||||
export const downloadThumbnail: CollectionAfterChangeHook = async ({ doc, req }) => {
|
||||
// Skip if thumbnail already exists (manual upload or previous download)
|
||||
if (doc.thumbnail) return doc
|
||||
|
||||
// Get the YouTube video ID
|
||||
const videoId = doc.youtube?.videoId
|
||||
if (!videoId) return doc
|
||||
|
||||
const thumbnailUrl = getYouTubeThumbnail(videoId, 'hq')
|
||||
const filename = `yt-thumb-${videoId}.jpg`
|
||||
|
||||
const tenantId = typeof doc.tenant === 'object' ? doc.tenant?.id : doc.tenant
|
||||
|
||||
const mediaId = await downloadAndUploadImage(req.payload, {
|
||||
url: thumbnailUrl,
|
||||
filename,
|
||||
alt: doc.title
|
||||
? `Thumbnail: ${typeof doc.title === 'string' ? doc.title : doc.title?.de || doc.title?.en || videoId}`
|
||||
: `YouTube Thumbnail ${videoId}`,
|
||||
tenantId: tenantId || undefined,
|
||||
})
|
||||
|
||||
if (mediaId) {
|
||||
await req.payload.update({
|
||||
collection: 'youtube-content',
|
||||
id: doc.id,
|
||||
data: { thumbnail: mediaId },
|
||||
depth: 0,
|
||||
})
|
||||
console.log(`[yt-thumbnail] Auto-downloaded thumbnail for video ${videoId} (media: ${mediaId})`)
|
||||
}
|
||||
|
||||
return doc
|
||||
}
|
||||
Loading…
Reference in a new issue