Update file_conversion.py
Browse files- file_conversion.py +16 -2
file_conversion.py
CHANGED
|
@@ -2,10 +2,15 @@ from fastapi import APIRouter, UploadFile, File, HTTPException
|
|
| 2 |
from fastapi.responses import FileResponse
|
| 3 |
from pdf2docx import Converter
|
| 4 |
import os
|
| 5 |
-
import
|
|
|
|
| 6 |
|
| 7 |
router = APIRouter()
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
# Define the temp directory
|
| 10 |
TEMP_DIR = "/.tempfiles"
|
| 11 |
|
|
@@ -23,13 +28,21 @@ async def convert_pdf_to_docx(file: UploadFile = File(...)):
|
|
| 23 |
try:
|
| 24 |
# Save the uploaded file
|
| 25 |
with open(pdf_temp_path, "wb") as pdf_file:
|
| 26 |
-
|
|
|
|
|
|
|
| 27 |
|
| 28 |
# Convert PDF to DOCX
|
| 29 |
cv = Converter(pdf_temp_path)
|
| 30 |
cv.convert(docx_temp_path)
|
| 31 |
cv.close()
|
| 32 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
# Return the DOCX file
|
| 34 |
return FileResponse(
|
| 35 |
docx_temp_path,
|
|
@@ -37,6 +50,7 @@ async def convert_pdf_to_docx(file: UploadFile = File(...)):
|
|
| 37 |
filename=file.filename.replace('.pdf', '.docx')
|
| 38 |
)
|
| 39 |
except Exception as e:
|
|
|
|
| 40 |
raise HTTPException(status_code=500, detail=f"Conversion failed: {str(e)}")
|
| 41 |
finally:
|
| 42 |
# Clean up temporary files
|
|
|
|
| 2 |
from fastapi.responses import FileResponse
|
| 3 |
from pdf2docx import Converter
|
| 4 |
import os
|
| 5 |
+
import logging
|
| 6 |
+
import shutil
|
| 7 |
|
| 8 |
router = APIRouter()
|
| 9 |
|
| 10 |
+
# Setup logging
|
| 11 |
+
logging.basicConfig(level=logging.INFO)
|
| 12 |
+
logger = logging.getLogger(__name__)
|
| 13 |
+
|
| 14 |
# Define the temp directory
|
| 15 |
TEMP_DIR = "/.tempfiles"
|
| 16 |
|
|
|
|
| 28 |
try:
|
| 29 |
# Save the uploaded file
|
| 30 |
with open(pdf_temp_path, "wb") as pdf_file:
|
| 31 |
+
shutil.copyfileobj(file.file, pdf_file)
|
| 32 |
+
|
| 33 |
+
logger.info(f"Starting conversion of {pdf_temp_path}")
|
| 34 |
|
| 35 |
# Convert PDF to DOCX
|
| 36 |
cv = Converter(pdf_temp_path)
|
| 37 |
cv.convert(docx_temp_path)
|
| 38 |
cv.close()
|
| 39 |
|
| 40 |
+
logger.info(f"Conversion completed. Output file: {docx_temp_path}")
|
| 41 |
+
|
| 42 |
+
# Check if the file exists
|
| 43 |
+
if not os.path.exists(docx_temp_path):
|
| 44 |
+
raise FileNotFoundError(f"Converted file not found: {docx_temp_path}")
|
| 45 |
+
|
| 46 |
# Return the DOCX file
|
| 47 |
return FileResponse(
|
| 48 |
docx_temp_path,
|
|
|
|
| 50 |
filename=file.filename.replace('.pdf', '.docx')
|
| 51 |
)
|
| 52 |
except Exception as e:
|
| 53 |
+
logger.error(f"Conversion failed: {str(e)}")
|
| 54 |
raise HTTPException(status_code=500, detail=f"Conversion failed: {str(e)}")
|
| 55 |
finally:
|
| 56 |
# Clean up temporary files
|