File tree Expand file tree Collapse file tree
sample_solutions/DocSummarization/backend Expand file tree Collapse file tree Original file line number Diff line number Diff line change 99import os
1010import logging
1111import json
12+ import tempfile
1213
1314from services import pdf_service , llm_service
1415import config
@@ -84,14 +85,18 @@ async def summarize_document(
8485
8586 # ========== File Upload (Documents) ==========
8687 if files :
87- # Save file temporarily
88- temp_path = f"/tmp/{ files .filename } "
88+ # Save file temporarily using secure temp file
8989 filename_lower = files .filename .lower ()
9090 logger .info (f"Saving uploaded file: { files .filename } , type={ type } " )
9191
92- with open (temp_path , "wb" ) as buffer :
92+ # Get file extension for temp file
93+ _ , file_ext = os .path .splitext (files .filename )
94+
95+ # Create temporary file with proper cleanup
96+ with tempfile .NamedTemporaryFile (delete = False , suffix = file_ext ) as temp_file :
9397 content = await files .read ()
94- buffer .write (content )
98+ temp_file .write (content )
99+ temp_path = temp_file .name
95100
96101 try :
97102 # ===== Document Processing (PDF/DOC/DOCX/TXT) =====
Original file line number Diff line number Diff line change @@ -81,7 +81,7 @@ async def startup_event():
8181if __name__ == "__main__" :
8282 uvicorn .run (
8383 app ,
84- host = "0.0.0.0" ,
84+ host = "0.0.0.0" , # nosec B104 - Binding to all interfaces is intentional for Docker container
8585 port = config .SERVICE_PORT ,
8686 timeout_keep_alive = 300
8787 )
You can’t perform that action at this time.
0 commit comments