legal-doc-masker/backend/app/services/file_service.py

54 lines
1.6 KiB
Python

from celery import Celery
from ..core.config import settings
from ..models.file import File, FileStatus
from sqlalchemy.orm import Session
from ..core.database import SessionLocal
import sys
import os
from ..core.services.document_service import DocumentService
from pathlib import Path
celery = Celery(
'file_service',
broker=settings.CELERY_BROKER_URL,
backend=settings.CELERY_RESULT_BACKEND
)
@celery.task
def process_file(file_id: str):
db = SessionLocal()
try:
file = db.query(File).filter(File.id == file_id).first()
if not file:
return
# Update status to processing
file.status = FileStatus.PROCESSING
db.commit()
try:
# Process the file using your existing masking system
process_service = DocumentService()
# Determine output path
input_path = Path(file.original_path)
output_filename = f"processed_{input_path.name}"
output_path = str(settings.PROCESSED_FOLDER / output_filename)
# Process document with both input and output paths
process_service.process_document(file.original_path, output_path)
# Update file record with processed path
file.processed_path = output_path
file.status = FileStatus.SUCCESS
db.commit()
except Exception as e:
file.status = FileStatus.FAILED
file.error_message = str(e)
db.commit()
raise
finally:
db.close()