Skip to content

Commit

Permalink
feat: x video duration
Browse files Browse the repository at this point in the history
  • Loading branch information
nevo-david committed Nov 7, 2024
1 parent f5637d8 commit 1feff46
Showing 1 changed file with 51 additions and 10 deletions.
61 changes: 51 additions & 10 deletions apps/frontend/src/components/launches/providers/x/x.provider.tsx
Original file line number Diff line number Diff line change
@@ -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<boolean> => {
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.'));
};
});
};

0 comments on commit 1feff46

Please sign in to comment.