From 1feff46239c6c312ad246a661759e01319c33c7a Mon Sep 17 00:00:00 2001 From: Nevo David Date: Thu, 7 Nov 2024 23:55:21 +0700 Subject: [PATCH] feat: x video duration --- .../launches/providers/x/x.provider.tsx | 61 ++++++++++++++++--- 1 file changed, 51 insertions(+), 10 deletions(-) diff --git a/apps/frontend/src/components/launches/providers/x/x.provider.tsx b/apps/frontend/src/components/launches/providers/x/x.provider.tsx index ac5a8dda..a9ffc5fe 100644 --- a/apps/frontend/src/components/launches/providers/x/x.provider.tsx +++ b/apps/frontend/src/components/launches/providers/x/x.provider.tsx @@ -1,11 +1,52 @@ import { withProvider } from '@gitroom/frontend/components/launches/providers/high.order.provider'; -export default withProvider(null, undefined, undefined, async (posts) => { - if (posts.some(p => p.length > 4)) { - return 'There can be maximum 4 pictures in a post.'; - } - - if (posts.some(p => p.some(m => m.path.indexOf('mp4') > -1) && p.length > 1)) { - return 'There can be maximum 1 video in a post.'; - } - return true; -}, 280); +export default withProvider( + null, + undefined, + undefined, + async (posts) => { + if (posts.some((p) => p.length > 4)) { + return 'There can be maximum 4 pictures in a post.'; + } + + if ( + posts.some( + (p) => p.some((m) => m.path.indexOf('mp4') > -1) && p.length > 1 + ) + ) { + return 'There can be maximum 1 video in a post.'; + } + + for (const load of posts.flatMap((p) => p.flatMap((a) => a.path))) { + if (load.indexOf('mp4') > -1) { + const isValid = await checkVideoDuration(load); + if (!isValid) { + return 'Video duration must be less than or equal to 140 seconds.'; + } + } + } + return true; + }, + 280 +); + +const checkVideoDuration = async (url: string): Promise => { + return new Promise((resolve, reject) => { + const video = document.createElement('video'); + video.src = url; + video.preload = 'metadata'; + + video.onloadedmetadata = () => { + // Check if the duration is less than or equal to 140 seconds + const duration = video.duration; + if (duration <= 140) { + resolve(true); // Video duration is acceptable + } else { + resolve(false); // Video duration exceeds 140 seconds + } + }; + + video.onerror = () => { + reject(new Error('Failed to load video metadata.')); + }; + }); +};