From 2f104f5a1b1339d3b8ff17d262175563f8ab2396 Mon Sep 17 00:00:00 2001 From: nevolodia Date: Sun, 25 May 2025 14:14:01 +0200 Subject: [PATCH] Check for valid coordinates added, bugs fixed. --- src/pages/tools/video/crop-video/index.tsx | 44 ++++++++++++++++++++- src/pages/tools/video/crop-video/service.ts | 4 ++ 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/src/pages/tools/video/crop-video/index.tsx b/src/pages/tools/video/crop-video/index.tsx index e756fdc..b1bcbb6 100644 --- a/src/pages/tools/video/crop-video/index.tsx +++ b/src/pages/tools/video/crop-video/index.tsx @@ -24,27 +24,60 @@ export default function CropVideo({ title }: ToolComponentProps) { width: number; height: number; } | null>(null); + const [processingError, setProcessingError] = useState(''); useEffect(() => { if (input) { getVideoDimensions(input) .then((dimensions) => { setVideoDimensions(dimensions); + setProcessingError(''); }) .catch((error) => { console.error('Error getting video dimensions:', error); + setProcessingError('Failed to load video dimensions'); }); } else { setVideoDimensions(null); + setProcessingError(''); } }, [input]); + const validateDimensions = (values: InitialValuesType): string => { + if (!videoDimensions) return ''; + + if (values.x < 0 || values.y < 0) { + return 'X and Y coordinates must be non-negative'; + } + + if (values.width <= 0 || values.height <= 0) { + return 'Width and height must be positive'; + } + + if (values.x + values.width > videoDimensions.width) { + return `Crop area extends beyond video width (${videoDimensions.width}px)`; + } + + if (values.y + values.height > videoDimensions.height) { + return `Crop area extends beyond video height (${videoDimensions.height}px)`; + } + + return ''; + }; + const compute = async ( optionsValues: InitialValuesType, input: File | null ) => { if (!input) return; + const error = validateDimensions(optionsValues); + if (error) { + setProcessingError(error); + return; + } + + setProcessingError(''); setLoading(true); try { @@ -52,12 +85,16 @@ export default function CropVideo({ title }: ToolComponentProps) { setResult(croppedFile); } catch (error) { console.error('Error cropping video:', error); + setProcessingError( + 'Error cropping video. Please check parameters and video file.' + ); } finally { setLoading(false); } }; - const debouncedCompute = useCallback(debounce(compute, 1000), [ + // 2 seconds to avoid starting job half way through + const debouncedCompute = useCallback(debounce(compute, 2000), [ videoDimensions ]); @@ -86,6 +123,11 @@ export default function CropVideo({ title }: ToolComponentProps) { title: 'Crop Coordinates', component: ( + {processingError && ( + + {processingError} + + )}