Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add fallback to Bunny URLs if main URLs are not accessible #781

Merged
merged 10 commits into from
Jun 14, 2024
93 changes: 61 additions & 32 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,18 +36,17 @@ 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
// if (metadata.migration_status === 'MIGRATED') {
// return {
Expand All @@ -64,50 +72,72 @@ export const getMetadata = async (contentId: number) => {
// segments: metadata['segments'],
// };
// }

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'],
};
}

// Check the highest quality video URL (1080p) first
const highestQualityUrl = metadata[`video_1080p_mp4_${userId}`];
const isHighestQualityUrlAccessible = await isUrlAccessible(highestQualityUrl);

if (isHighestQualityUrlAccessible) {
// If the highest quality URL is accessible, return all main URLs
return {
1080: highestQualityUrl,
720: metadata[`video_720p_mp4_${userId}`],
360: metadata[`video_360p_mp4_${userId}`],
subtitles: metadata['subtitles'],
slides: metadata['slides'],
segments: metadata['segments'],
};
}

// If the highest quality URL is not accessible, check the 720p URL
const mediumQualityUrl = metadata[`video_720p_mp4_${userId}`];
TanmayDhobale marked this conversation as resolved.
Show resolved Hide resolved
const isMediumQualityUrlAccessible = await isUrlAccessible(mediumQualityUrl);

if (isMediumQualityUrlAccessible) {
// If the 720p URL is accessible, return main URLs for 720p and 360p
return {
720: mediumQualityUrl,
360: metadata[`video_360p_mp4_${userId}`],
subtitles: metadata['subtitles'],
slides: metadata['slides'],
segments: metadata['segments'],
};
}

// If the 720p URL is not accessible, check the 360p URL
const lowestQualityUrl = metadata[`video_360p_mp4_${userId}`];
const isLowestQualityUrlAccessible = await isUrlAccessible(lowestQualityUrl);

if (isLowestQualityUrlAccessible) {
// If the 360p URL is accessible, return the main URL for 360p
return {
360: lowestQualityUrl,
subtitles: metadata['subtitles'],
slides: metadata['slides'],
segments: metadata['segments'],
};
}

// If none of the main URLs are accessible, return Bunny URLs
return {
//@ts-ignore
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'],
};
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'],
};
};
Expand All @@ -133,7 +163,6 @@ export const ContentRenderer = async ({
};
}) => {
const metadata = await getMetadata(content.id);

return (
<div>
<ContentRendererClient
Expand Down
Loading