fix: add V2 file upload endpoint and update frontend to v2 API
Add missing file upload functionality to V2 API that was removed during V1 to V2 migration. Update frontend to use v2 API endpoints. Backend changes: - Add /api/v2/upload endpoint in main.py for file uploads - Import necessary dependencies (UploadFile, hashlib, TaskFile) - Upload endpoint creates task, saves file, and returns task info - Add UploadResponse schema to task.py schemas - Update tasks router imports for consistency Frontend changes: - Update API_VERSION from 'v1' to 'v2' in api.ts - Update UploadResponse type to match V2 API response format (task_id instead of batch_id, single file instead of array) This fixes the 404 error when uploading files from the frontend. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -125,12 +125,103 @@ async def root():
|
||||
|
||||
# Include V2 API routers
|
||||
from app.routers import auth, tasks, admin
|
||||
from fastapi import UploadFile, File, Depends, HTTPException, status
|
||||
from sqlalchemy.orm import Session
|
||||
import hashlib
|
||||
|
||||
from app.core.deps import get_db, get_current_user
|
||||
from app.models.user import User
|
||||
from app.models.task import TaskFile
|
||||
from app.schemas.task import UploadResponse, TaskStatusEnum
|
||||
from app.services.task_service import task_service
|
||||
|
||||
app.include_router(auth.router)
|
||||
app.include_router(tasks.router)
|
||||
app.include_router(admin.router)
|
||||
|
||||
|
||||
# File upload endpoint
|
||||
@app.post("/api/v2/upload", response_model=UploadResponse, tags=["Upload"], summary="Upload file for OCR")
|
||||
async def upload_file(
|
||||
file: UploadFile = File(..., description="File to upload (PNG, JPG, PDF, etc.)"),
|
||||
db: Session = Depends(get_db),
|
||||
current_user: User = Depends(get_current_user)
|
||||
):
|
||||
"""
|
||||
Upload a file for OCR processing
|
||||
|
||||
Creates a new task and uploads the file
|
||||
|
||||
- **file**: File to upload
|
||||
"""
|
||||
try:
|
||||
# Validate file extension
|
||||
file_ext = Path(file.filename).suffix.lower().lstrip('.')
|
||||
if file_ext not in settings.allowed_extensions_list:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail=f"File type .{file_ext} not allowed. Allowed types: {', '.join(settings.allowed_extensions_list)}"
|
||||
)
|
||||
|
||||
# Read file content
|
||||
file_content = await file.read()
|
||||
file_size = len(file_content)
|
||||
|
||||
# Calculate file hash
|
||||
file_hash = hashlib.sha256(file_content).hexdigest()
|
||||
|
||||
# Create task
|
||||
task = task_service.create_task(
|
||||
db=db,
|
||||
user_id=current_user.id,
|
||||
filename=file.filename,
|
||||
file_type=file.content_type
|
||||
)
|
||||
|
||||
# Save file to disk
|
||||
upload_dir = Path(settings.upload_dir)
|
||||
upload_dir.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
# Create unique filename using task_id
|
||||
unique_filename = f"{task.task_id}_{file.filename}"
|
||||
file_path = upload_dir / unique_filename
|
||||
|
||||
# Write file
|
||||
with open(file_path, "wb") as f:
|
||||
f.write(file_content)
|
||||
|
||||
# Create TaskFile record
|
||||
task_file = TaskFile(
|
||||
task_id=task.id,
|
||||
original_name=file.filename,
|
||||
stored_path=str(file_path),
|
||||
file_size=file_size,
|
||||
mime_type=file.content_type,
|
||||
file_hash=file_hash
|
||||
)
|
||||
db.add(task_file)
|
||||
db.commit()
|
||||
|
||||
logger.info(f"Uploaded file {file.filename} ({file_size} bytes) for task {task.task_id}, user {current_user.email}")
|
||||
|
||||
return {
|
||||
"task_id": task.task_id,
|
||||
"filename": file.filename,
|
||||
"file_size": file_size,
|
||||
"file_type": file.content_type or "application/octet-stream",
|
||||
"status": TaskStatusEnum.PENDING
|
||||
}
|
||||
|
||||
except HTTPException:
|
||||
raise
|
||||
except Exception as e:
|
||||
logger.exception(f"Failed to upload file for user {current_user.id}")
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
detail=f"Failed to upload file: {str(e)}"
|
||||
)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
import uvicorn
|
||||
|
||||
|
||||
Reference in New Issue
Block a user