Update file_conversion.py
Browse files- file_conversion.py +15 -9
file_conversion.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
from fastapi import APIRouter, UploadFile, File, HTTPException
|
| 2 |
from fastapi.responses import FileResponse
|
| 3 |
from pdf2docx import Converter
|
| 4 |
import os
|
|
@@ -14,8 +14,13 @@ logger = logging.getLogger(__name__)
|
|
| 14 |
# Define the temp directory
|
| 15 |
TEMP_DIR = "/.tempfiles"
|
| 16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
@router.post("/convert/pdf_to_docx")
|
| 18 |
-
async def convert_pdf_to_docx(file: UploadFile = File(...)):
|
| 19 |
if not file.filename.endswith('.pdf'):
|
| 20 |
raise HTTPException(status_code=400, detail="File must be a PDF")
|
| 21 |
|
|
@@ -43,6 +48,10 @@ async def convert_pdf_to_docx(file: UploadFile = File(...)):
|
|
| 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,
|
|
@@ -51,10 +60,7 @@ async def convert_pdf_to_docx(file: UploadFile = File(...)):
|
|
| 51 |
)
|
| 52 |
except Exception as e:
|
| 53 |
logger.error(f"Conversion failed: {str(e)}")
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
os.unlink(pdf_temp_path)
|
| 59 |
-
if os.path.exists(docx_temp_path):
|
| 60 |
-
os.unlink(docx_temp_path)
|
|
|
|
| 1 |
+
from fastapi import APIRouter, UploadFile, File, HTTPException, BackgroundTasks
|
| 2 |
from fastapi.responses import FileResponse
|
| 3 |
from pdf2docx import Converter
|
| 4 |
import os
|
|
|
|
| 14 |
# Define the temp directory
|
| 15 |
TEMP_DIR = "/.tempfiles"
|
| 16 |
|
| 17 |
+
def remove_file(path: str):
|
| 18 |
+
if os.path.exists(path):
|
| 19 |
+
os.unlink(path)
|
| 20 |
+
logger.info(f"Removed temporary file: {path}")
|
| 21 |
+
|
| 22 |
@router.post("/convert/pdf_to_docx")
|
| 23 |
+
async def convert_pdf_to_docx(file: UploadFile = File(...), background_tasks: BackgroundTasks):
|
| 24 |
if not file.filename.endswith('.pdf'):
|
| 25 |
raise HTTPException(status_code=400, detail="File must be a PDF")
|
| 26 |
|
|
|
|
| 48 |
if not os.path.exists(docx_temp_path):
|
| 49 |
raise FileNotFoundError(f"Converted file not found: {docx_temp_path}")
|
| 50 |
|
| 51 |
+
# Schedule file removal after response is sent
|
| 52 |
+
background_tasks.add_task(remove_file, pdf_temp_path)
|
| 53 |
+
background_tasks.add_task(remove_file, docx_temp_path)
|
| 54 |
+
|
| 55 |
# Return the DOCX file
|
| 56 |
return FileResponse(
|
| 57 |
docx_temp_path,
|
|
|
|
| 60 |
)
|
| 61 |
except Exception as e:
|
| 62 |
logger.error(f"Conversion failed: {str(e)}")
|
| 63 |
+
# Clean up files in case of an error
|
| 64 |
+
remove_file(pdf_temp_path)
|
| 65 |
+
remove_file(docx_temp_path)
|
| 66 |
+
raise HTTPException(status_code=500, detail=f"Conversion failed: {str(e)}")
|
|
|
|
|
|
|
|
|