Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions comfy_cli/file_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,18 @@ def download_file(url: str, local_filepath: pathlib.Path, headers: dict | None =

with httpx.stream("GET", url, follow_redirects=True, headers=headers) as response:
if response.status_code == 200:
total = int(response.headers["Content-Length"])
content_length = response.headers.get("Content-Length")
total = int(content_length) if content_length is not None else None
if total is not None:
description = f"Downloading {total // 1024 // 1024} MB"
else:
description = "Downloading..."
try:
with open(local_filepath, "wb") as f:
for data in ui.show_progress(
response.iter_bytes(),
total,
description=f"Downloading {total // 1024 // 1024} MB",
description=description,
):
f.write(data)
except KeyboardInterrupt:
Expand Down
18 changes: 18 additions & 0 deletions tests/test_file_utils_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,24 @@ def test_download_file_success(mock_stream, tmp_path):
assert test_file.read_bytes() == b"test data"


@patch("httpx.stream")
def test_download_file_success_without_content_length(mock_stream, tmp_path):
"""Download should succeed when Content-Length header is missing (e.g. chunked/gzip responses)."""
mock_response = Mock()
mock_response.status_code = 200
mock_response.headers = {}
mock_response.iter_bytes.return_value = [b"chunk1", b"chunk2"]
mock_response.__enter__ = Mock(return_value=mock_response)
mock_response.__exit__ = Mock(return_value=None)
mock_stream.return_value = mock_response

test_file = tmp_path / "test.txt"
download_file("http://example.com", test_file)

assert test_file.exists()
assert test_file.read_bytes() == b"chunk1chunk2"


@patch("httpx.stream")
def test_download_file_failure(mock_stream):
mock_response = Mock()
Expand Down
Loading