-
Notifications
You must be signed in to change notification settings - Fork 3
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
Testnets #140
Testnets #140
Conversation
fix: catch unrecognized video uploading
WalkthroughThe changes in this pull request enhance error handling in the Changes
Possibly related PRs
Poem
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
✅ Deploy Preview for proof-of-humanity-v2 ready!
To edit notification comments on pull requests, go to your Netlify site configuration. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 2
🧹 Outside diff range and nitpick comments (2)
src/app/[pohid]/claim/Video.tsx (2)
167-170
: Improve error handling with specific error messages.The current catch block uses a generic error message. Consider providing more specific error messages based on the error type.
Enhance error handling:
} catch (error: any) { - videoError("Unexpected error. Check format/codecs used."); + const errorMessage = error.name === 'NotSupportedError' + ? "Video codec not supported. Please use a different format or re-encode the video." + : error.name === 'InvalidStateError' + ? "Video file is corrupted or invalid." + : "Unexpected error while processing video. Please try again."; + videoError(errorMessage); return console.error(error); }
148-166
: Restructure async operations for better error handling.The current structure mixes synchronous and asynchronous operations. Consider restructuring to handle all validations consistently.
Consider restructuring the validation flow:
- await checkVideoDuration(blob); - checkVideoSize(blob); - const vid = document.createElement("video"); + const validateVideo = (blob: Blob, uri: string) => { + return new Promise((resolve, reject) => { + const vid = document.createElement("video"); + vid.crossOrigin = "anonymous"; + vid.src = uri; + vid.preload = "auto"; + + vid.addEventListener("loadeddata", async () => { + try { + await checkVideoDuration(blob); + checkVideoSize(blob); + + if (vid.videoWidth < MIN_DIMS.width || vid.videoHeight < MIN_DIMS.height) { + reject(new Error(ERROR_MSG.dimensions)); + return; + } + + resolve({ uri, content: blob }); + } catch (error) { + reject(error); + } finally { + vid.remove(); + } + }); + + vid.addEventListener("error", () => { + reject(new Error("Failed to load video")); + vid.remove(); + }); + }); + }; + try { + const result = await validateVideo(blob, uri); + setRecording(false); + video$.set(result); + } catch (error) { + videoError(error.message); + URL.revokeObjectURL(uri); + }
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
📒 Files selected for processing (1)
src/app/[pohid]/claim/Video.tsx
(1 hunks)
🔇 Additional comments (1)
src/app/[pohid]/claim/Video.tsx (1)
Line range hint 19-21
: Consider updating MAX_SIZE constant.
The MAX_SIZE is set to undefined, which effectively disables size validation. This might lead to issues with large video files.
Let's check if there are any size constraints defined elsewhere in the codebase:
Consider:
- Setting a reasonable size limit to prevent memory issues
- Adding server-side validation as well
- Implementing chunked upload for large files
const file = received[0]; | ||
const blob = new Blob([file], { type: file.type }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Add file type validation before processing.
The code should validate the file type before attempting to process it to provide better error messages.
Add file type validation:
+ const ALLOWED_VIDEO_TYPES = ['video/webm', 'video/mp4', 'video/avi', 'video/quicktime'];
const file = received[0];
+ if (!ALLOWED_VIDEO_TYPES.includes(file.type)) {
+ videoError(`Unsupported video format. Please use ${ALLOWED_VIDEO_TYPES.map(t => t.split('/')[1]).join(', ')}`);
+ return;
+ }
const blob = new Blob([file], { type: file.type });
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
const file = received[0]; | |
const blob = new Blob([file], { type: file.type }); | |
const ALLOWED_VIDEO_TYPES = ['video/webm', 'video/mp4', 'video/avi', 'video/quicktime']; | |
const file = received[0]; | |
if (!ALLOWED_VIDEO_TYPES.includes(file.type)) { | |
videoError(`Unsupported video format. Please use ${ALLOWED_VIDEO_TYPES.map(t => t.split('/')[1]).join(', ')}`); | |
return; | |
} | |
const blob = new Blob([file], { type: file.type }); |
try { | ||
const file = received[0]; | ||
const blob = new Blob([file], { type: file.type }); | ||
const uri = URL.createObjectURL(blob); | ||
|
||
await checkVideoDuration(blob); | ||
checkVideoSize(blob); | ||
const vid = document.createElement("video"); | ||
vid.crossOrigin = "anonymous"; | ||
vid.src = uri; | ||
vid.preload = "auto"; | ||
|
||
vid.addEventListener("loadeddata", async () => { | ||
if ( | ||
vid.videoWidth < MIN_DIMS.width || | ||
vid.videoHeight < MIN_DIMS.height | ||
) { | ||
videoError(ERROR_MSG.dimensions); | ||
return console.error(ERROR_MSG.dimensions); | ||
} | ||
|
||
setRecording(false); | ||
video$.set({ uri, content: blob }); | ||
}); | ||
} catch (error: any) { | ||
videoError("Unexpected error. Check format/codecs used."); | ||
return console.error(error); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add proper cleanup to prevent memory leaks.
The video element created for dimension validation is not properly cleaned up, which could lead to memory leaks.
Apply this diff to add cleanup:
try {
const file = received[0];
const blob = new Blob([file], { type: file.type });
const uri = URL.createObjectURL(blob);
await checkVideoDuration(blob);
checkVideoSize(blob);
const vid = document.createElement("video");
vid.crossOrigin = "anonymous";
vid.src = uri;
vid.preload = "auto";
vid.addEventListener("loadeddata", async () => {
if (
vid.videoWidth < MIN_DIMS.width ||
vid.videoHeight < MIN_DIMS.height
) {
videoError(ERROR_MSG.dimensions);
+ URL.revokeObjectURL(uri);
+ vid.remove();
return console.error(ERROR_MSG.dimensions);
}
setRecording(false);
video$.set({ uri, content: blob });
+ vid.remove();
});
} catch (error: any) {
videoError("Unexpected error. Check format/codecs used.");
return console.error(error);
}
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
try { | |
const file = received[0]; | |
const blob = new Blob([file], { type: file.type }); | |
const uri = URL.createObjectURL(blob); | |
await checkVideoDuration(blob); | |
checkVideoSize(blob); | |
const vid = document.createElement("video"); | |
vid.crossOrigin = "anonymous"; | |
vid.src = uri; | |
vid.preload = "auto"; | |
vid.addEventListener("loadeddata", async () => { | |
if ( | |
vid.videoWidth < MIN_DIMS.width || | |
vid.videoHeight < MIN_DIMS.height | |
) { | |
videoError(ERROR_MSG.dimensions); | |
return console.error(ERROR_MSG.dimensions); | |
} | |
setRecording(false); | |
video$.set({ uri, content: blob }); | |
}); | |
} catch (error: any) { | |
videoError("Unexpected error. Check format/codecs used."); | |
return console.error(error); | |
} | |
try { | |
const file = received[0]; | |
const blob = new Blob([file], { type: file.type }); | |
const uri = URL.createObjectURL(blob); | |
await checkVideoDuration(blob); | |
checkVideoSize(blob); | |
const vid = document.createElement("video"); | |
vid.crossOrigin = "anonymous"; | |
vid.src = uri; | |
vid.preload = "auto"; | |
vid.addEventListener("loadeddata", async () => { | |
if ( | |
vid.videoWidth < MIN_DIMS.width || | |
vid.videoHeight < MIN_DIMS.height | |
) { | |
videoError(ERROR_MSG.dimensions); | |
URL.revokeObjectURL(uri); | |
vid.remove(); | |
return console.error(ERROR_MSG.dimensions); | |
} | |
setRecording(false); | |
video$.set({ uri, content: blob }); | |
vid.remove(); | |
}); | |
} catch (error: any) { | |
videoError("Unexpected error. Check format/codecs used."); | |
return console.error(error); | |
} |
Summary by CodeRabbit
New Features
Bug Fixes