Skip to content

Commit

Permalink
♻️ assetId and playbackId compatible function
Browse files Browse the repository at this point in the history
  • Loading branch information
Mario-SO committed Aug 10, 2024
1 parent b6fa800 commit a62e49c
Showing 1 changed file with 26 additions and 11 deletions.
37 changes: 26 additions & 11 deletions packages/app/lib/actions/livepeer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,39 @@ export const getVideoPhaseAction = async (assetId: string) => {
}
};

/**
* Retrieves the video URL for a given identifier, supporting both playbackId and assetId.
* This function makes all videos playable, including older ones that don't have an assetId.
*
* @param {string} identifier - The unique identifier for the video. This can be either a playbackId or an assetId.
* @param {'assetId' | 'playbackId'} type - Specifies whether the identifier is an 'assetId' or a 'playbackId'.
* @returns {Promise<string | null>} A promise that resolves to the video URL if successful, or null if unsuccessful.
*
* @example
* // Using with a playbackId
* const videoUrl = await getVideoUrlAction(session.playbackId, 'playbackId');
*
* @example
* // Using with an assetId
* const videoUrl = await getVideoUrlAction(session.assetId!, 'assetId');
*
* @throws {Error} If there's an issue fetching the asset or building the URL.
*/
export const getVideoUrlAction = async (
identifier: string | { assetId?: string; playbackId?: string }
identifier: string,
type: 'assetId' | 'playbackId'
): Promise<string | null> => {
try {
if (typeof identifier === 'string') {
// If identifier is a string, treat it as a playbackId
return `https://lp-playback.com/hls/${identifier}/index.m3u8`;
}

// If identifier is an object
if (identifier.assetId) {
const asset = await fetchAsset({ assetId: identifier.assetId });
if (type === 'playbackId') {
// If type is playbackId, simply build the full URL
return `https://vod-cdn.lp-playback.studio/raw/jxf4iblf6wlsyor6526t4tcmtmqa/catalyst-vod-com/hls/${identifier}/index.m3u8`;
} else if (type === 'assetId') {
// If type is assetId, use the fetch call to retrieve the videoUrl directly
const asset = await fetchAsset({ assetId: identifier });
console.log(asset);
if (asset?.playbackUrl) {
return asset.playbackUrl;
}
} else if (identifier.playbackId) {
return `https://lp-playback.com/hls/${identifier.playbackId}/index.m3u8`;
}

return null;
Expand Down

0 comments on commit a62e49c

Please sign in to comment.