Skip to content

Commit

Permalink
Fix for unknown file size, fix for nested output files
Browse files Browse the repository at this point in the history
  • Loading branch information
multimeric committed Sep 4, 2024
1 parent add5f27 commit e47ccfc
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
8 changes: 5 additions & 3 deletions filesender/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ async def download_file(
token: str,
file_id: int,
out_dir: Path,
file_size: Union[int, float] = float("inf"),
file_size: Union[int, float, None] = None,
file_name: Optional[str] = None
) -> None:
"""
Expand All @@ -376,7 +376,7 @@ async def download_file(
token: Obtained from the transfer email. The same as [`GuestAuth`][filesender.GuestAuth]'s `guest_token`.
file_id: A single file ID indicating the file to be downloaded.
out_dir: The path to write the downloaded file.
file_size: The file size in bytes, optionally
file_size: The file size in bytes, optionally.
file_name: The file name of the file being downloaded. This will impact the name by which it's saved.
"""
download_endpoint = urlunparse(
Expand All @@ -394,9 +394,11 @@ async def download_file(
else:
raise Exception("No filename found")

file_path = out_dir / file_name
file_path.parent.mkdir(parents=True, exist_ok=True)
chunk_size = 8192
chunk_size_mb = chunk_size / 1024 / 1024
with tqdm(desc=file_name, unit="MB", total=int(file_size / 1024 / 1024)) as progress:
with tqdm(desc=file_name, unit="MB", total=None if file_size is None else int(file_size / 1024 / 1024)) as progress:
async with aiofiles.open(out_dir / file_name, "wb") as fp:
# We can't add the total here, because we don't know it:
# https://github.com/filesender/filesender/issues/1555
Expand Down
11 changes: 8 additions & 3 deletions test/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
from filesender.request_types import GuestOptions
from filesender.benchmark import make_tempfile, make_tempfiles, benchmark

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

@pytest.mark.asyncio
async def test_round_trip(base_url: str, username: str, apikey: str, recipient: str):
Expand Down Expand Up @@ -34,7 +39,7 @@ async def test_round_trip(base_url: str, username: str, apikey: str, recipient:
file_id=transfer["files"][0]["id"],
out_dir=Path(download_dir),
)
assert len(list(Path(download_dir).iterdir())) == 1
assert count_files_recursively(Path(download_dir)) == 1


@pytest.mark.asyncio
Expand Down Expand Up @@ -62,7 +67,7 @@ async def test_round_trip_dir(base_url: str, username: str, apikey: str, recipie
token=transfer["recipients"][0]["token"],
out_dir=Path(download_dir),
)
assert len(list(Path(download_dir).iterdir())) == 2
assert count_files_recursively(Path(download_dir)) == 2


@pytest.mark.asyncio
Expand Down Expand Up @@ -113,7 +118,7 @@ async def test_voucher_round_trip(
file_id=transfer["files"][0]["id"],
out_dir=Path(download_dir),
)
assert len(list(Path(download_dir).iterdir())) == 1
assert count_files_recursively(Path(download_dir)) == 1


@pytest.mark.asyncio
Expand Down

0 comments on commit e47ccfc

Please sign in to comment.