feat: Improve file display, timezone handling, and LOT management
Changes: - Fix datetime serialization with UTC 'Z' suffix for correct timezone display - Add PDF upload support with extension fallback for MIME detection - Fix LOT add/remove by creating new list for SQLAlchemy JSON change detection - Add file message components (FileMessage, ImageLightbox, UploadPreview) - Add multi-file upload support with progress tracking - Link uploaded files to chat messages via message_id - Include file attachments in AI report generation - Update specs for file-storage, realtime-messaging, and ai-report-generation 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
"""File validation utilities"""
|
||||
import magic
|
||||
import os
|
||||
from fastapi import UploadFile, HTTPException
|
||||
from typing import Set
|
||||
from typing import Set, Dict
|
||||
import logging
|
||||
|
||||
from app.core.config import get_settings
|
||||
@@ -17,7 +18,15 @@ IMAGE_TYPES: Set[str] = {
|
||||
}
|
||||
|
||||
DOCUMENT_TYPES: Set[str] = {
|
||||
"application/pdf"
|
||||
"application/pdf",
|
||||
"application/x-pdf", # Some systems detect PDF as x-pdf
|
||||
}
|
||||
|
||||
# Extensions that can be accepted even if MIME detection fails
|
||||
EXTENSION_FALLBACK: Dict[str, str] = {
|
||||
".pdf": "application/pdf",
|
||||
".doc": "application/msword",
|
||||
".docx": "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
|
||||
}
|
||||
|
||||
LOG_TYPES: Set[str] = {
|
||||
@@ -67,6 +76,17 @@ def validate_file_type(file: UploadFile, allowed_types: Set[str]) -> str:
|
||||
detected_mime = detect_mime_type(header)
|
||||
|
||||
if detected_mime not in allowed_types:
|
||||
# Try extension fallback for known safe file types
|
||||
filename = file.filename or ""
|
||||
_, ext = os.path.splitext(filename.lower())
|
||||
|
||||
if ext in EXTENSION_FALLBACK:
|
||||
logger.info(
|
||||
f"MIME detection returned {detected_mime} for {filename}, "
|
||||
f"using extension fallback: {EXTENSION_FALLBACK[ext]}"
|
||||
)
|
||||
return EXTENSION_FALLBACK[ext]
|
||||
|
||||
raise HTTPException(
|
||||
status_code=400,
|
||||
detail=f"File type not allowed: {detected_mime}. Allowed types: {', '.join(allowed_types)}"
|
||||
@@ -115,9 +135,12 @@ def get_file_type_and_limits(mime_type: str) -> tuple[str, int]:
|
||||
Raises:
|
||||
HTTPException if MIME type not recognized
|
||||
"""
|
||||
# Include extension fallback types as documents
|
||||
document_types_extended = DOCUMENT_TYPES | set(EXTENSION_FALLBACK.values())
|
||||
|
||||
if mime_type in IMAGE_TYPES:
|
||||
return ("image", settings.get_image_max_size_bytes())
|
||||
elif mime_type in DOCUMENT_TYPES:
|
||||
elif mime_type in document_types_extended:
|
||||
return ("document", settings.get_document_max_size_bytes())
|
||||
elif mime_type in LOG_TYPES:
|
||||
return ("log", settings.get_log_max_size_bytes())
|
||||
|
||||
Reference in New Issue
Block a user