Spaces:
Sleeping
Sleeping
Update app/api/upload.py
Browse files- app/api/upload.py +99 -99
app/api/upload.py
CHANGED
|
@@ -1,99 +1,99 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import uuid
|
| 3 |
-
import tempfile
|
| 4 |
-
from fastapi import APIRouter, UploadFile, File, HTTPException, Depends, BackgroundTasks
|
| 5 |
-
from sqlalchemy.orm import Session
|
| 6 |
-
|
| 7 |
-
from app.db.database import get_db
|
| 8 |
-
from app.core.dependencies import get_current_user
|
| 9 |
-
from app.models.user import User
|
| 10 |
-
from app.models.document import Document
|
| 11 |
-
from app.services.indexing_service import index_document
|
| 12 |
-
from app.utils.hash import generate_file_hash
|
| 13 |
-
from app.services.storage_service import storage_service
|
| 14 |
-
|
| 15 |
-
router = APIRouter()
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
@router.post("/")
|
| 19 |
-
async def upload_pdf(
|
| 20 |
-
background_tasks: BackgroundTasks,
|
| 21 |
-
file: UploadFile = File(...),
|
| 22 |
-
current_user: User = Depends(get_current_user),
|
| 23 |
-
db: Session = Depends(get_db)
|
| 24 |
-
):
|
| 25 |
-
filename = file.filename
|
| 26 |
-
|
| 27 |
-
if not filename:
|
| 28 |
-
raise HTTPException(status_code=400, detail="Filename is missing")
|
| 29 |
-
|
| 30 |
-
if not filename.lower().endswith(".pdf"):
|
| 31 |
-
raise HTTPException(status_code=400, detail="Only PDF files are allowed")
|
| 32 |
-
|
| 33 |
-
# Read file content
|
| 34 |
-
content = await file.read()
|
| 35 |
-
|
| 36 |
-
# Save to a temporary file to generate hash
|
| 37 |
-
with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmp:
|
| 38 |
-
tmp.write(content)
|
| 39 |
-
temp_path = tmp.name
|
| 40 |
-
|
| 41 |
-
try:
|
| 42 |
-
file_hash = generate_file_hash(temp_path)
|
| 43 |
-
|
| 44 |
-
existing_doc = db.query(Document).filter(
|
| 45 |
-
Document.user_id == current_user.id,
|
| 46 |
-
Document.file_hash == file_hash
|
| 47 |
-
).first()
|
| 48 |
-
|
| 49 |
-
if existing_doc:
|
| 50 |
-
os.remove(temp_path)
|
| 51 |
-
return {
|
| 52 |
-
"message": "File already uploaded",
|
| 53 |
-
"document_id": existing_doc.id,
|
| 54 |
-
"filename": existing_doc.filename,
|
| 55 |
-
"status": existing_doc.status
|
| 56 |
-
}
|
| 57 |
-
|
| 58 |
-
# Create document entry first to get ID
|
| 59 |
-
new_document = Document(
|
| 60 |
-
filename=filename,
|
| 61 |
-
file_path="pending", # Will update after
|
| 62 |
-
user_id=current_user.id,
|
| 63 |
-
file_hash=file_hash,
|
| 64 |
-
status="processing"
|
| 65 |
-
)
|
| 66 |
-
db.add(new_document)
|
| 67 |
-
db.commit()
|
| 68 |
-
db.refresh(new_document)
|
| 69 |
-
|
| 70 |
-
# Upload to
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
upload_success = storage_service.upload_file(content,
|
| 74 |
-
|
| 75 |
-
if not upload_success:
|
| 76 |
-
db.delete(new_document)
|
| 77 |
-
db.commit()
|
| 78 |
-
raise HTTPException(status_code=500, detail="Failed to upload file to cloud storage")
|
| 79 |
-
|
| 80 |
-
new_document.file_path =
|
| 81 |
-
db.commit()
|
| 82 |
-
|
| 83 |
-
background_tasks.add_task(
|
| 84 |
-
index_document,
|
| 85 |
-
new_document.id,
|
| 86 |
-
|
| 87 |
-
filename,
|
| 88 |
-
current_user.id
|
| 89 |
-
)
|
| 90 |
-
|
| 91 |
-
return {
|
| 92 |
-
"message": "PDF uploaded successfully. Indexing started.",
|
| 93 |
-
"document_id": new_document.id,
|
| 94 |
-
"filename": filename,
|
| 95 |
-
"status": new_document.status
|
| 96 |
-
}
|
| 97 |
-
finally:
|
| 98 |
-
if os.path.exists(temp_path):
|
| 99 |
-
os.remove(temp_path)
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import uuid
|
| 3 |
+
import tempfile
|
| 4 |
+
from fastapi import APIRouter, UploadFile, File, HTTPException, Depends, BackgroundTasks
|
| 5 |
+
from sqlalchemy.orm import Session
|
| 6 |
+
|
| 7 |
+
from app.db.database import get_db
|
| 8 |
+
from app.core.dependencies import get_current_user
|
| 9 |
+
from app.models.user import User
|
| 10 |
+
from app.models.document import Document
|
| 11 |
+
from app.services.indexing_service import index_document
|
| 12 |
+
from app.utils.hash import generate_file_hash
|
| 13 |
+
from app.services.storage_service import storage_service
|
| 14 |
+
|
| 15 |
+
router = APIRouter()
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
@router.post("/")
|
| 19 |
+
async def upload_pdf(
|
| 20 |
+
background_tasks: BackgroundTasks,
|
| 21 |
+
file: UploadFile = File(...),
|
| 22 |
+
current_user: User = Depends(get_current_user),
|
| 23 |
+
db: Session = Depends(get_db)
|
| 24 |
+
):
|
| 25 |
+
filename = file.filename
|
| 26 |
+
|
| 27 |
+
if not filename:
|
| 28 |
+
raise HTTPException(status_code=400, detail="Filename is missing")
|
| 29 |
+
|
| 30 |
+
if not filename.lower().endswith(".pdf"):
|
| 31 |
+
raise HTTPException(status_code=400, detail="Only PDF files are allowed")
|
| 32 |
+
|
| 33 |
+
# Read file content
|
| 34 |
+
content = await file.read()
|
| 35 |
+
|
| 36 |
+
# Save to a temporary file to generate hash
|
| 37 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmp:
|
| 38 |
+
tmp.write(content)
|
| 39 |
+
temp_path = tmp.name
|
| 40 |
+
|
| 41 |
+
try:
|
| 42 |
+
file_hash = generate_file_hash(temp_path)
|
| 43 |
+
|
| 44 |
+
existing_doc = db.query(Document).filter(
|
| 45 |
+
Document.user_id == current_user.id,
|
| 46 |
+
Document.file_hash == file_hash
|
| 47 |
+
).first()
|
| 48 |
+
|
| 49 |
+
if existing_doc:
|
| 50 |
+
os.remove(temp_path)
|
| 51 |
+
return {
|
| 52 |
+
"message": "File already uploaded",
|
| 53 |
+
"document_id": existing_doc.id,
|
| 54 |
+
"filename": existing_doc.filename,
|
| 55 |
+
"status": existing_doc.status
|
| 56 |
+
}
|
| 57 |
+
|
| 58 |
+
# Create document entry first to get ID
|
| 59 |
+
new_document = Document(
|
| 60 |
+
filename=filename,
|
| 61 |
+
file_path="pending", # Will update after Supabase upload
|
| 62 |
+
user_id=current_user.id,
|
| 63 |
+
file_hash=file_hash,
|
| 64 |
+
status="processing"
|
| 65 |
+
)
|
| 66 |
+
db.add(new_document)
|
| 67 |
+
db.commit()
|
| 68 |
+
db.refresh(new_document)
|
| 69 |
+
|
| 70 |
+
# Upload to Supabase: storage/user_id/doc_id_filename.pdf
|
| 71 |
+
storage_path = f"uploads/user_{current_user.id}/{new_document.id}_{filename}"
|
| 72 |
+
|
| 73 |
+
upload_success = storage_service.upload_file(content, storage_path)
|
| 74 |
+
|
| 75 |
+
if not upload_success:
|
| 76 |
+
db.delete(new_document)
|
| 77 |
+
db.commit()
|
| 78 |
+
raise HTTPException(status_code=500, detail="Failed to upload file to cloud storage")
|
| 79 |
+
|
| 80 |
+
new_document.file_path = storage_path
|
| 81 |
+
db.commit()
|
| 82 |
+
|
| 83 |
+
background_tasks.add_task(
|
| 84 |
+
index_document,
|
| 85 |
+
new_document.id,
|
| 86 |
+
storage_path,
|
| 87 |
+
filename,
|
| 88 |
+
current_user.id
|
| 89 |
+
)
|
| 90 |
+
|
| 91 |
+
return {
|
| 92 |
+
"message": "PDF uploaded successfully. Indexing started.",
|
| 93 |
+
"document_id": new_document.id,
|
| 94 |
+
"filename": filename,
|
| 95 |
+
"status": new_document.status
|
| 96 |
+
}
|
| 97 |
+
finally:
|
| 98 |
+
if os.path.exists(temp_path):
|
| 99 |
+
os.remove(temp_path)
|