Skip to content

Commit e47ccfc

Browse files
committed
Fix for unknown file size, fix for nested output files
1 parent add5f27 commit e47ccfc

2 files changed

Lines changed: 13 additions & 6 deletions

File tree

filesender/api.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ async def download_file(
366366
token: str,
367367
file_id: int,
368368
out_dir: Path,
369-
file_size: Union[int, float] = float("inf"),
369+
file_size: Union[int, float, None] = None,
370370
file_name: Optional[str] = None
371371
) -> None:
372372
"""
@@ -376,7 +376,7 @@ async def download_file(
376376
token: Obtained from the transfer email. The same as [`GuestAuth`][filesender.GuestAuth]'s `guest_token`.
377377
file_id: A single file ID indicating the file to be downloaded.
378378
out_dir: The path to write the downloaded file.
379-
file_size: The file size in bytes, optionally
379+
file_size: The file size in bytes, optionally.
380380
file_name: The file name of the file being downloaded. This will impact the name by which it's saved.
381381
"""
382382
download_endpoint = urlunparse(
@@ -394,9 +394,11 @@ async def download_file(
394394
else:
395395
raise Exception("No filename found")
396396

397+
file_path = out_dir / file_name
398+
file_path.parent.mkdir(parents=True, exist_ok=True)
397399
chunk_size = 8192
398400
chunk_size_mb = chunk_size / 1024 / 1024
399-
with tqdm(desc=file_name, unit="MB", total=int(file_size / 1024 / 1024)) as progress:
401+
with tqdm(desc=file_name, unit="MB", total=None if file_size is None else int(file_size / 1024 / 1024)) as progress:
400402
async with aiofiles.open(out_dir / file_name, "wb") as fp:
401403
# We can't add the total here, because we don't know it:
402404
# https://github.com/filesender/filesender/issues/1555

test/test_client.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66
from filesender.request_types import GuestOptions
77
from filesender.benchmark import make_tempfile, make_tempfiles, benchmark
88

9+
def count_files_recursively(path: Path) -> int:
10+
"""
11+
Returns a recursive count of the number of files within a directory. Subdirectories are not counted.
12+
"""
13+
return sum([1 if child.is_file() else 0 for child in path.rglob("*")])
914

1015
@pytest.mark.asyncio
1116
async def test_round_trip(base_url: str, username: str, apikey: str, recipient: str):
@@ -34,7 +39,7 @@ async def test_round_trip(base_url: str, username: str, apikey: str, recipient:
3439
file_id=transfer["files"][0]["id"],
3540
out_dir=Path(download_dir),
3641
)
37-
assert len(list(Path(download_dir).iterdir())) == 1
42+
assert count_files_recursively(Path(download_dir)) == 1
3843

3944

4045
@pytest.mark.asyncio
@@ -62,7 +67,7 @@ async def test_round_trip_dir(base_url: str, username: str, apikey: str, recipie
6267
token=transfer["recipients"][0]["token"],
6368
out_dir=Path(download_dir),
6469
)
65-
assert len(list(Path(download_dir).iterdir())) == 2
70+
assert count_files_recursively(Path(download_dir)) == 2
6671

6772

6873
@pytest.mark.asyncio
@@ -113,7 +118,7 @@ async def test_voucher_round_trip(
113118
file_id=transfer["files"][0]["id"],
114119
out_dir=Path(download_dir),
115120
)
116-
assert len(list(Path(download_dir).iterdir())) == 1
121+
assert count_files_recursively(Path(download_dir)) == 1
117122

118123

119124
@pytest.mark.asyncio

0 commit comments

Comments
 (0)