Mail box and OCR done
This commit is contained in:
159
backend/main.py
159
backend/main.py
@@ -1,13 +1,32 @@
|
||||
import io
|
||||
from typing import Optional
|
||||
import datetime
|
||||
from typing import Optional, List
|
||||
from contextlib import asynccontextmanager
|
||||
|
||||
from fastapi import FastAPI, File, UploadFile, HTTPException, Depends
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
from pydantic import BaseModel
|
||||
from pypdf import PdfReader
|
||||
import pytesseract
|
||||
from PIL import Image
|
||||
from pdf2image import convert_from_bytes
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
app = FastAPI()
|
||||
# Internal modules
|
||||
from database import get_db, Email
|
||||
from scheduler import start_scheduler, stop_scheduler
|
||||
from mail_service import fetch_and_store_emails
|
||||
|
||||
# Lifespan for Scheduler
|
||||
@asynccontextmanager
|
||||
async def lifespan(app: FastAPI):
|
||||
# Startup
|
||||
start_scheduler()
|
||||
yield
|
||||
# Shutdown
|
||||
stop_scheduler()
|
||||
|
||||
app = FastAPI(lifespan=lifespan)
|
||||
|
||||
# CORS configuration
|
||||
origins = [
|
||||
@@ -38,13 +57,6 @@ class LoginResponse(BaseModel):
|
||||
class NERResponse(BaseModel):
|
||||
text: str
|
||||
|
||||
# 1. User Login Module (Dummy)
|
||||
@app.post("/api/login", response_model=LoginResponse)
|
||||
async def login(request: LoginRequest):
|
||||
if request.username == "admin" and request.password == "admin":
|
||||
return LoginResponse(token="dummy-jwt-token-123", message="Login Successful")
|
||||
raise HTTPException(status_code=401, detail="Invalid credentials")
|
||||
|
||||
# 2. OCR Module
|
||||
def extract_text_from_pdf(file_bytes: bytes) -> str:
|
||||
try:
|
||||
@@ -68,10 +80,6 @@ def extract_text_from_image(file_bytes: bytes) -> str:
|
||||
print(f"Error reading Image: {e}")
|
||||
return ""
|
||||
|
||||
from pdf2image import convert_from_bytes
|
||||
|
||||
# ... (imports)
|
||||
|
||||
@app.post("/api/ocr/extract", response_model=NERResponse)
|
||||
async def extract_text(file: UploadFile = File(...)):
|
||||
content = await file.read()
|
||||
@@ -100,6 +108,131 @@ async def extract_text(file: UploadFile = File(...)):
|
||||
|
||||
return NERResponse(text=extracted_text)
|
||||
|
||||
import zipfile
|
||||
import mimetypes
|
||||
from fastapi.responses import StreamingResponse
|
||||
|
||||
class EmailDTO(BaseModel):
|
||||
id: int
|
||||
subject: Optional[str]
|
||||
sender: Optional[str]
|
||||
received_date: Optional[datetime.datetime]
|
||||
is_read: bool
|
||||
has_attachments: bool = False
|
||||
|
||||
class AttachmentDTO(BaseModel):
|
||||
id: int
|
||||
filename: str
|
||||
content_type: str
|
||||
|
||||
class EmailListResponse(BaseModel):
|
||||
items: List[EmailDTO]
|
||||
total: int
|
||||
|
||||
class EmailDetailDTO(EmailDTO):
|
||||
body: Optional[str]
|
||||
attachments: List[AttachmentDTO]
|
||||
|
||||
@app.get("/api/emails", response_model=EmailListResponse)
|
||||
def get_emails(skip: int = 0, limit: int = 50, db: Session = Depends(get_db)):
|
||||
total = db.query(Email).count()
|
||||
emails = db.query(Email).order_by(Email.received_date.desc()).offset(skip).limit(limit).all()
|
||||
|
||||
items = [
|
||||
EmailDTO(
|
||||
id=e.id,
|
||||
subject=e.subject,
|
||||
sender=e.sender,
|
||||
received_date=e.received_date,
|
||||
is_read=e.is_read,
|
||||
has_attachments=bool(e.attachments) # Check if list is not empty
|
||||
) for e in emails
|
||||
]
|
||||
return EmailListResponse(items=items, total=total)
|
||||
|
||||
# ...
|
||||
|
||||
@app.get("/api/emails/{email_id}", response_model=EmailDetailDTO)
|
||||
def get_email_details(email_id: int, db: Session = Depends(get_db)):
|
||||
email = db.query(Email).filter(Email.id == email_id).first()
|
||||
if not email:
|
||||
raise HTTPException(status_code=404, detail="Email not found")
|
||||
|
||||
# Mark as read if opened
|
||||
if not email.is_read:
|
||||
email.is_read = True
|
||||
db.commit()
|
||||
|
||||
# Safely handle missing content_type and filename
|
||||
attachments = []
|
||||
for a in email.attachments:
|
||||
fname = a.filename
|
||||
if not fname or fname == "unknown":
|
||||
ext = mimetypes.guess_extension(a.content_type or "") or ".bin"
|
||||
fname = f"attachment_{a.id}{ext}"
|
||||
|
||||
attachments.append(
|
||||
AttachmentDTO(
|
||||
id=a.id,
|
||||
filename=fname,
|
||||
content_type=a.content_type or "application/octet-stream"
|
||||
)
|
||||
)
|
||||
|
||||
return EmailDetailDTO(
|
||||
id=email.id,
|
||||
subject=email.subject,
|
||||
sender=email.sender,
|
||||
received_date=email.received_date,
|
||||
is_read=email.is_read,
|
||||
has_attachments=bool(email.attachments),
|
||||
body=email.body,
|
||||
attachments=attachments
|
||||
)
|
||||
|
||||
@app.get("/api/emails/{email_id}/download-all")
|
||||
def download_all_attachments(email_id: int, db: Session = Depends(get_db)):
|
||||
email = db.query(Email).filter(Email.id == email_id).first()
|
||||
if not email:
|
||||
raise HTTPException(status_code=404, detail="Email not found")
|
||||
|
||||
if not email.attachments:
|
||||
raise HTTPException(status_code=400, detail="No attachments found")
|
||||
|
||||
# Create a zip file in memory
|
||||
zip_buffer = io.BytesIO()
|
||||
with zipfile.ZipFile(zip_buffer, "w", zipfile.ZIP_DEFLATED) as zip_file:
|
||||
for att in email.attachments:
|
||||
# Use filename from DB, ensure unique names if duplicates exist (simple append here)
|
||||
zip_file.writestr(att.filename, att.file_content)
|
||||
|
||||
zip_buffer.seek(0)
|
||||
|
||||
return StreamingResponse(
|
||||
zip_buffer,
|
||||
media_type="application/zip",
|
||||
headers={"Content-Disposition": f"attachment; filename=email-{email_id}-attachments.zip"}
|
||||
)
|
||||
|
||||
@app.get("/api/attachments/{attachment_id}")
|
||||
def download_attachment(attachment_id: int, view: bool = False, db: Session = Depends(get_db)):
|
||||
attachment = db.query(DBAttachment).filter(DBAttachment.id == attachment_id).first()
|
||||
if not attachment:
|
||||
raise HTTPException(status_code=404, detail="Attachment not found")
|
||||
|
||||
disposition = "inline" if view else "attachment"
|
||||
|
||||
return StreamingResponse(
|
||||
io.BytesIO(attachment.file_content),
|
||||
media_type=attachment.content_type,
|
||||
headers={"Content-Disposition": f"{disposition}; filename={attachment.filename}"}
|
||||
)
|
||||
|
||||
@app.post("/api/emails/sync")
|
||||
def sync_emails():
|
||||
result = fetch_and_store_emails()
|
||||
return result
|
||||
|
||||
@app.get("/")
|
||||
def read_root():
|
||||
return {"message": "OCR Backend API is running"}
|
||||
|
||||
Reference in New Issue
Block a user