Upload main.py
Browse files- app/main.py +14 -4
app/main.py
CHANGED
|
@@ -49,18 +49,28 @@ async def convert_file(request: Request, file: UploadFile = File(...)):
|
|
| 49 |
|
| 50 |
# Unique ID for request
|
| 51 |
request_id = str(uuid.uuid4())
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
try:
|
| 55 |
-
# Save uploaded file to temp dir
|
| 56 |
input_path = os.path.join(tmp_dir, file.filename)
|
| 57 |
with open(input_path, "wb") as buffer:
|
| 58 |
shutil.copyfileobj(file.file, buffer)
|
| 59 |
|
| 60 |
-
# Convert to PDF (
|
| 61 |
pdf_path = convert_to_pdf(input_path, tmp_dir)
|
| 62 |
|
| 63 |
-
# Move final PDF to public static dir
|
|
|
|
| 64 |
final_filename = f"{request_id}_{os.path.basename(pdf_path)}"
|
| 65 |
final_path = PDF_DIR / final_filename
|
| 66 |
shutil.move(pdf_path, final_path)
|
|
|
|
| 49 |
|
| 50 |
# Unique ID for request
|
| 51 |
request_id = str(uuid.uuid4())
|
| 52 |
+
|
| 53 |
+
# PERFORMANCE HACK: Use /dev/shm (RAM Disk) if available for extreme speed
|
| 54 |
+
# This avoids writing the input file to the slow container disk
|
| 55 |
+
ram_disk = "/dev/shm"
|
| 56 |
+
if os.path.exists(ram_disk) and os.access(ram_disk, os.W_OK):
|
| 57 |
+
base_tmp_dir = ram_disk
|
| 58 |
+
else:
|
| 59 |
+
base_tmp_dir = None # Default to /tmp handling
|
| 60 |
+
|
| 61 |
+
tmp_dir = tempfile.mkdtemp(dir=base_tmp_dir)
|
| 62 |
|
| 63 |
try:
|
| 64 |
+
# Save uploaded file to temp dir (RAM)
|
| 65 |
input_path = os.path.join(tmp_dir, file.filename)
|
| 66 |
with open(input_path, "wb") as buffer:
|
| 67 |
shutil.copyfileobj(file.file, buffer)
|
| 68 |
|
| 69 |
+
# Convert to PDF (Reading/Writing from RAM)
|
| 70 |
pdf_path = convert_to_pdf(input_path, tmp_dir)
|
| 71 |
|
| 72 |
+
# Move final PDF to public static dir (Disk)
|
| 73 |
+
# Only the final result touches the persistent disk
|
| 74 |
final_filename = f"{request_id}_{os.path.basename(pdf_path)}"
|
| 75 |
final_path = PDF_DIR / final_filename
|
| 76 |
shutil.move(pdf_path, final_path)
|