From 9f9bf03258a0b5b39b62a0bd053e5668b1592815 Mon Sep 17 00:00:00 2001 From: tanmay4l Date: Thu, 13 Jun 2024 13:06:50 +0530 Subject: [PATCH] Update ContentRenderer.tsx --- src/components/admin/ContentRenderer.tsx | 61 ++++++++++++------------ 1 file changed, 30 insertions(+), 31 deletions(-) diff --git a/src/components/admin/ContentRenderer.tsx b/src/components/admin/ContentRenderer.tsx index 5f59fb7de..4e50228f9 100644 --- a/src/components/admin/ContentRenderer.tsx +++ b/src/components/admin/ContentRenderer.tsx @@ -17,6 +17,15 @@ function bunnyUrl(url: string) { ); } +async function isUrlAccessible(url: string): Promise { + try { + const response = await fetch(url, { method: 'HEAD' }); + return response.ok; + } catch (error) { + return false; + } +} + export const getMetadata = async (contentId: number) => { const session = await getServerSession(authOptions); if (!session?.user) { @@ -27,18 +36,16 @@ export const getMetadata = async (contentId: number) => { contentId, }, }); - if (!metadata) { return null; } - - //@ts-ignore + const userId: string = (1).toString(); - const user = await db.user.findFirst({ - where: { - id: session?.user?.id?.toString() || '-1', - }, - }); + // const user = await db.user.findFirst({ + // where: { + // id: session?.user?.id?.toString() || '-1', + // }, + // }); //@ts-ignore // if (metadata.migration_status === 'MIGRATED') { // return { @@ -66,48 +73,41 @@ export const getMetadata = async (contentId: number) => { // } if (user.bunnyProxyEnabled) { return { - //@ts-ignore 1080: bunnyUrl(metadata[`video_1080p_mp4_${userId}`]), - //@ts-ignore 720: bunnyUrl(metadata[`video_720p_mp4_${userId}`]), - //@ts-ignore 360: bunnyUrl(metadata[`video_360p_mp4_${userId}`]), subtitles: metadata['subtitles'], - //@ts-ignore slides: metadata['slides'], - //@ts-ignore segments: metadata['segments'], - // @ts-ignore thumnnails: metadata['thumbnail_mosiac_url'], }; } - return { - //@ts-ignore + + const mainUrls = { 1080: metadata[`video_1080p_mp4_${userId}`], - //@ts-ignore 720: metadata[`video_720p_mp4_${userId}`], - //@ts-ignore 360: metadata[`video_360p_mp4_${userId}`], subtitles: metadata['subtitles'], - //@ts-ignore slides: metadata['slides'], - //@ts-ignore segments: metadata['segments'], }; + + const isMainUrlAccessible = await Promise.all( + Object.values(mainUrls).map(isUrlAccessible) + ); + + if (isMainUrlAccessible.every(Boolean)) { + return mainUrls; + } + + // If any main URL is not accessible, return Bunny URLs return { - //@ts-ignore - 1080: metadata[`video_1080p_${userId}`], - //@ts-ignore - 720: metadata[`video_720p_${userId}`], - //@ts-ignore - 360: metadata[`video_360p_${userId}`], - //@ts-ignore + 1080: bunnyUrl(metadata[`video_1080p_mp4_${userId}`]), + 720: bunnyUrl(metadata[`video_720p_mp4_${userId}`]), + 360: bunnyUrl(metadata[`video_360p_mp4_${userId}`]), subtitles: metadata['subtitles'], - //@ts-ignore slides: metadata['slides'], - //@ts-ignore segments: metadata['segments'], - // @ts-ignore thumnnails: metadata['thumbnail_mosiac_url'], }; }; @@ -133,7 +133,6 @@ export const ContentRenderer = async ({ }; }) => { const metadata = await getMetadata(content.id); - return (