Skip to content

Commit 8e7635a

Browse files
committed
use temp file while downloading
1 parent b5aef51 commit 8e7635a

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

src/download_manager.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,7 @@ def run(self):
277277
filename = self.file_data['file']
278278
url = self.file_data['url']
279279
save_path = os.path.join(self.model_root_dir, filename)
280+
temp_path = save_path + ".download"
280281

281282
try:
282283
# Use stream=True to handle large files
@@ -291,7 +292,9 @@ def run(self):
291292
block_size = 8192
292293
bytes_downloaded = 0
293294

294-
with open(save_path, 'wb') as f:
295+
os.makedirs(os.path.dirname(temp_path), exist_ok=True)
296+
297+
with open(temp_path, 'wb') as f:
295298
for data in response.iter_content(block_size):
296299
if not self._is_running:
297300
f.close()
@@ -304,11 +307,15 @@ def run(self):
304307
progress = int(bytes_downloaded * 100 / total_size)
305308
self.progress_updated.emit(filename, progress)
306309

310+
os.replace(temp_path, save_path)
307311
self.download_finished.emit(filename, "Success")
308312

309313
except requests.exceptions.RequestException as e:
310314
self.error_occurred.emit(filename, f"Network Error: {e}")
311315
except Exception as e:
316+
if os.path.exists(temp_path):
317+
try: os.remove(temp_path)
318+
except: pass
312319
self.error_occurred.emit(filename, f"File System Error: {e}")
313320

314321
def stop(self):
@@ -360,7 +367,11 @@ def _add_group_header(self, name):
360367
self.list_layout.addWidget(header_lbl)
361368

362369
def _check_if_model_downloaded(self, model_data):
363-
return all(os.path.exists(os.path.join(self.model_root_dir, f['file'])) for f in model_data['files'])
370+
for f in model_data['files']:
371+
path = os.path.join(self.model_root_dir, f['file'])
372+
if not os.path.exists(path) or os.path.getsize(path) == 0:
373+
return False
374+
return True
364375

365376
def _add_model_row(self, model_data, model_type):
366377
model_name = model_data['name']

0 commit comments

Comments
 (0)