Skip to content

Commit

Permalink
Merge pull request #781 from TanmayDhobale/lol
Browse files Browse the repository at this point in the history
feat: Add fallback to Bunny URLs if main URLs are not accessible
  • Loading branch information
siinghd authored Jun 14, 2024
2 parents 7232c99 + cd9ccc9 commit a198c79
Showing 1 changed file with 43 additions and 41 deletions.
84 changes: 43 additions & 41 deletions src/components/admin/ContentRenderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ function bunnyUrl(url: string) {
);
}

async function isUrlAccessible(url: string): Promise<boolean> {
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) {
Expand All @@ -27,19 +36,18 @@ 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',
},
});
//@ts-ignore

// //@ts-ignore
// if (metadata.migration_status === 'MIGRATED') {
// return {
// //@ts-ignore
Expand All @@ -64,52 +72,47 @@ export const getMetadata = async (contentId: number) => {
// segments: metadata['segments'],
// };
// }

const bunnyUrls = {
1080: bunnyUrl(metadata[`video_1080p_mp4_${userId}`]),
720: bunnyUrl(metadata[`video_720p_mp4_${userId}`]),
360: bunnyUrl(metadata[`video_360p_mp4_${userId}`]),
subtitles: metadata['subtitles'],
slides: metadata['slides'],
segments: metadata['segments'],
thumbnails: metadata['thumbnail_mosiac_url'],
};

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 bunnyUrls;
}
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'],
thumbnails: metadata['thumbnail_mosiac_url'],
};
return {
//@ts-ignore
1080: metadata[`video_1080p_${userId}`],
//@ts-ignore
720: metadata[`video_720p_${userId}`],
//@ts-ignore
360: metadata[`video_360p_${userId}`],
//@ts-ignore
subtitles: metadata['subtitles'],
//@ts-ignore
slides: metadata['slides'],
//@ts-ignore
segments: metadata['segments'],
// @ts-ignore
thumnnails: metadata['thumbnail_mosiac_url'],
};

const isHighestQualityUrlAccessible = await isUrlAccessible(mainUrls['1080']);

if (isHighestQualityUrlAccessible) {
return mainUrls;
}

const otherQualities = ['720', '360'];
for (const quality of otherQualities) {
const isAccessible = await isUrlAccessible(mainUrls[quality]);
if (isAccessible) {
return mainUrls;
}
}

// If none of the main URLs are accessible, return Bunny URLs
return bunnyUrls;
};

export const ContentRenderer = async ({
Expand All @@ -133,7 +136,6 @@ export const ContentRenderer = async ({
};
}) => {
const metadata = await getMetadata(content.id);

return (
<div>
<ContentRendererClient
Expand Down

0 comments on commit a198c79

Please sign in to comment.