Drop in a screenshot and convert it to clean code (HTML/Tailwind/React/Vue)
Python
Fix video MIME type detection for uploads
Abi Rajac8efadfe7e14
diff --git a/backend/models/gemini.py b/backend/models/gemini.pyindex 530b35e9..2762832d 100644--- a/backend/models/gemini.py+++ b/backend/models/gemini.py@@ -25,6 +25,38 @@ def extract_text_from_content(content: str | List[Dict[str, Any]]) -> str:return ""+def detect_mime_type_from_base64(base64_data: str) -> str | None:+ """+ Detect MIME type from base64 data by checking magic bytes.+ Returns detected MIME type or None if unknown.+ """+ try:+ # Decode first few bytes to check magic numbers+ decoded = base64.b64decode(base64_data[:32])++ # Check for common image formats+ if decoded[:8] == b'\x89PNG\r\n\x1a\n':+ return "image/png"+ if decoded[:2] == b'\xff\xd8':+ return "image/jpeg"+ if decoded[:6] in (b'GIF87a', b'GIF89a'):+ return "image/gif"+ if decoded[:4] == b'RIFF' and decoded[8:12] == b'WEBP':+ return "image/webp"++ # Check for video formats+ if decoded[4:8] == b'ftyp':+ # MP4/MOV file+ return "video/mp4"+ if decoded[:4] == b'\x1aE\xdf\xa3':+ # WebM file+ return "video/webm"+ except Exception:+ pass++ return None++def extract_image_from_content(content: str | List[Dict[str, Any]]) -> Dict[str, str] | None:"""Extracts image data from message content.@@ -47,6 +79,17 @@ def extract_image_from_content(content: str | List[Dict[str, Any]]) -> Dict[str,# Extract base64 data and mime type for data URLsmime_type = image_url.split(";")[0].split(":")[1]base64_data = image_url.split(",")[1]++ # If MIME type is generic, try to detect from data+ if mime_type == "application/octet-stream":+ detected_mime = detect_mime_type_from_base64(base64_data)+ if detected_mime:+ mime_type = detected_mime+ else:+ # Skip this content if we can't determine the type+ print(f"Warning: Could not detect MIME type for data URL, skipping")+ continue+return {"mime_type": mime_type, "data": base64_data}else:# Handle regular URLsdiff --git a/frontend/src/components/ImageUpload.tsx b/frontend/src/components/ImageUpload.tsxindex e2d44bc1..bfa2413b 100644--- a/frontend/src/components/ImageUpload.tsx+++ b/frontend/src/components/ImageUpload.tsx@@ -38,10 +38,24 @@ const rejectStyle = {};// TODO: Move to a separate file-function fileToDataURL(file: File) {+function fileToDataURL(file: File): Promise<string> {return new Promise((resolve, reject) => {const reader = new FileReader();- reader.onload = () => resolve(reader.result);+ reader.onload = () => {+ const result = reader.result as string;+ // Check if the MIME type is correctly set in the data URL+ // Some browsers return application/octet-stream for video files+ if (result.startsWith("data:application/octet-stream") && file.type) {+ // Replace with the correct MIME type from the file+ const correctedResult = result.replace(+ "data:application/octet-stream",+ `data:${file.type}`+ );+ resolve(correctedResult);+ } else {+ resolve(result);+ }+ };reader.onerror = (error) => reject(error);reader.readAsDataURL(file);});@@ -142,14 +156,20 @@ function ImageUpload({ setReferenceImages, onUploadStateChange }: Props) {) as FileWithPreview[]);+ // Determine input mode from file type (more reliable than checking data URL)+ const firstFile = acceptedFiles[0];+ const isVideo = firstFile?.type?.startsWith("video/") ||+ [".mp4", ".mov", ".webm"].some(ext => firstFile?.name?.toLowerCase().endsWith(ext));+// Convert images to data URLs and store them (don't trigger generation yet)Promise.all(acceptedFiles.map((file) => fileToDataURL(file))).then((dataUrls) => {if (dataUrls.length > 0) {- const inputMode = (dataUrls[0] as string).startsWith("data:video")+ // Use file type detection as primary, fall back to data URL check+ const inputMode = isVideo || (dataUrls[0] as string).startsWith("data:video")? "video": "image";- setUploadedDataUrls(dataUrls.map((dataUrl) => dataUrl as string));+ setUploadedDataUrls(dataUrls as string[]);setUploadedInputMode(inputMode);// Focus the text input after uploadsetTimeout(() => textInputRef.current?.focus(), 100);