Spaces:
Running
Running
Update main.py
Browse files
main.py
CHANGED
@@ -120,9 +120,12 @@ import os
|
|
120 |
class HTMLInput(BaseModel):
|
121 |
html: str
|
122 |
|
|
|
|
|
|
|
123 |
@app.post("/convert")
|
124 |
async def convert_html_to_docx(input_data: HTMLInput):
|
125 |
-
|
126 |
try:
|
127 |
# Create a new HtmlToDocx parser
|
128 |
parser = HtmlToDocx()
|
@@ -130,9 +133,8 @@ async def convert_html_to_docx(input_data: HTMLInput):
|
|
130 |
# Parse the HTML string to DOCX
|
131 |
docx = parser.parse_html_string(input_data.html)
|
132 |
|
133 |
-
# Create a temporary
|
134 |
-
|
135 |
-
temp_filename = temp_file.name
|
136 |
|
137 |
# Save the DOCX to the temporary file
|
138 |
docx.save(temp_filename)
|
@@ -150,8 +152,6 @@ async def convert_html_to_docx(input_data: HTMLInput):
|
|
150 |
except Exception as e:
|
151 |
raise HTTPException(status_code=500, detail=str(e))
|
152 |
finally:
|
153 |
-
# Clean up:
|
154 |
-
if
|
155 |
-
|
156 |
-
os.unlink(temp_file.name)
|
157 |
-
|
|
|
120 |
class HTMLInput(BaseModel):
|
121 |
html: str
|
122 |
|
123 |
+
# Define the path to the temporary folder
|
124 |
+
TEMP_FOLDER = "/app/temp"
|
125 |
+
|
126 |
@app.post("/convert")
|
127 |
async def convert_html_to_docx(input_data: HTMLInput):
|
128 |
+
temp_filename = None
|
129 |
try:
|
130 |
# Create a new HtmlToDocx parser
|
131 |
parser = HtmlToDocx()
|
|
|
133 |
# Parse the HTML string to DOCX
|
134 |
docx = parser.parse_html_string(input_data.html)
|
135 |
|
136 |
+
# Create a unique filename in the temporary folder
|
137 |
+
temp_filename = os.path.join(TEMP_FOLDER, f"temp_{os.urandom(8).hex()}.docx")
|
|
|
138 |
|
139 |
# Save the DOCX to the temporary file
|
140 |
docx.save(temp_filename)
|
|
|
152 |
except Exception as e:
|
153 |
raise HTTPException(status_code=500, detail=str(e))
|
154 |
finally:
|
155 |
+
# Clean up: remove the temporary file
|
156 |
+
if temp_filename and os.path.exists(temp_filename):
|
157 |
+
os.remove(temp_filename)
|
|
|
|