Implement code changes to enhance functionality and improve performance
This commit is contained in:
+56
-6
@@ -318,38 +318,60 @@ async def upload_multiple_documents(
|
||||
This endpoint accepts multiple image files and returns file IDs
|
||||
that can be used with the /process/{file_id} endpoint.
|
||||
"""
|
||||
|
||||
try:
|
||||
responses = []
|
||||
|
||||
for file in files:
|
||||
# Validate file type
|
||||
allowed_types = ["jpg", "jpeg", "png", "gif", "bmp", "pdf"]
|
||||
file_extension = file.filename.split(".")[-1].lower()
|
||||
|
||||
allowed_types = ["jpg", "jpeg", "png", "gif", "bmp", "pdf"]
|
||||
|
||||
file_extension = file.filename.split(".")[-1].lower()
|
||||
google_file_id, file_name = file.filename.split("|")
|
||||
if file_extension not in allowed_types:
|
||||
raise HTTPException(
|
||||
status_code=400,
|
||||
detail=f"Unsupported file type for {file.filename}. Allowed: {allowed_types}",
|
||||
)
|
||||
logger.info(f"filename: {file_name}, extension: {file_extension}, google_file_id: {google_file_id}")
|
||||
|
||||
# Generate unique file ID
|
||||
file_id = str(uuid.uuid4())
|
||||
file_id = google_file_id # Using Google Drive file ID as file_id
|
||||
|
||||
# Check if file already exists in database
|
||||
existing_file = get_uploaded_file_from_db(db, file_id)
|
||||
|
||||
if existing_file:
|
||||
# File already exists, return existing record
|
||||
logger.info(f"File {file_name} with ID {file_id} already exists, returning existing record")
|
||||
responses.append(
|
||||
DocumentUploadResponse(
|
||||
file_id=existing_file.file_id,
|
||||
filename=existing_file.filename,
|
||||
file_type=existing_file.file_type,
|
||||
upload_date=existing_file.upload_date,
|
||||
status=existing_file.status,
|
||||
)
|
||||
)
|
||||
else:
|
||||
# File doesn't exist, create new record
|
||||
# Read file content and save to disk
|
||||
content = await file.read()
|
||||
file_path = await document_processor.save_uploaded_file(
|
||||
content, file.filename
|
||||
content, file_name
|
||||
)
|
||||
|
||||
# Create database record for uploaded file
|
||||
db_uploaded_file = DBUploadedFile(
|
||||
file_id=file_id,
|
||||
filename=file.filename,
|
||||
filename=file_name,
|
||||
file_path=file_path,
|
||||
file_type=file_extension,
|
||||
upload_date=datetime.now(),
|
||||
status="uploaded",
|
||||
)
|
||||
logger.info(f"Uploaded new file {file_name} with ID {file_id}")
|
||||
|
||||
# Add to database
|
||||
db.add(db_uploaded_file)
|
||||
@@ -357,7 +379,7 @@ async def upload_multiple_documents(
|
||||
responses.append(
|
||||
DocumentUploadResponse(
|
||||
file_id=file_id,
|
||||
filename=file.filename,
|
||||
filename=file_name,
|
||||
file_type=file_extension,
|
||||
upload_date=datetime.now(),
|
||||
status="uploaded",
|
||||
@@ -409,6 +431,34 @@ async def process_document(
|
||||
for rule in request.ai_rules
|
||||
]
|
||||
|
||||
# Check if receipt already exists for this file_id
|
||||
existing_receipt = get_receipt_from_db(db, file_id)
|
||||
|
||||
if existing_receipt:
|
||||
# Receipt already processed, return existing data
|
||||
logger.info(f"Receipt for file {file_id} already exists, returning existing record")
|
||||
return DocumentProcessResponse(
|
||||
file_id=file_id,
|
||||
receipt_id=existing_receipt.receipt_id,
|
||||
extraction_success=existing_receipt.extraction_success == "True",
|
||||
vendor=existing_receipt.vendor,
|
||||
description=existing_receipt.description,
|
||||
total_amount=existing_receipt.amount,
|
||||
tax_amount=existing_receipt.tax_amount,
|
||||
date=existing_receipt.date.strftime("%Y-%m-%d"),
|
||||
category=existing_receipt.category,
|
||||
confidence=existing_receipt.confidence,
|
||||
error=existing_receipt.error_message,
|
||||
receipt_currency=existing_receipt.receipt_currency,
|
||||
receipt_location=existing_receipt.receipt_location,
|
||||
calculated_tax=existing_receipt.calculated_tax,
|
||||
is_depreciable=existing_receipt.is_depreciable == "True" if existing_receipt.is_depreciable else None,
|
||||
name_of_asset=existing_receipt.name_of_asset,
|
||||
cca_rate=existing_receipt.cca_rate,
|
||||
useful_life=existing_receipt.useful_life,
|
||||
residual_value=existing_receipt.residual_value,
|
||||
)
|
||||
|
||||
# Process the file using the stored file path
|
||||
receipt_data = await document_processor.process_file(
|
||||
db_uploaded_file.file_path,
|
||||
|
||||
Reference in New Issue
Block a user