File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -127,19 +127,27 @@ def _ensure_disk_space(resp, file_path: str) -> None:
127127async def _stream_to_tempfile (resp , file_path : str , hasher ) -> str :
128128 """Stream HTTP body into a temp file."""
129129 tmp_dir = os .path .dirname (file_path )
130- async with aiofiles .tempfile .NamedTemporaryFile ("wb" , delete = False , dir = tmp_dir ) as tmp :
131- tmp_path = tmp .name
130+ tmp_path = None
131+
132+ try :
133+ async with aiofiles .tempfile .NamedTemporaryFile ("wb" , delete = False , dir = tmp_dir ) as tmp :
134+ tmp_path = tmp .name
132135
133- async for chunk in resp .aiter_bytes (chunk_size = 262_144 ):
134- if not chunk :
135- continue
136+ async for chunk in resp .aiter_bytes (chunk_size = 262_144 ):
137+ if not chunk :
138+ continue
136139
137- if hasher :
138- hasher .update (chunk )
140+ if hasher :
141+ hasher .update (chunk )
139142
140- await tmp .write (chunk )
143+ await tmp .write (chunk )
141144
142- return tmp_path
145+ return tmp_path
146+ except Exception :
147+ # Clean up any partially written temp file on error
148+ if tmp_path and os .path .exists (tmp_path ):
149+ os .remove (tmp_path )
150+ raise
143151
144152
145153def _hash_matches (hasher , expected_md5 : str , file_path : str , tmp_path : str | None ) -> bool :
You can’t perform that action at this time.
0 commit comments