fix: Resume unfinished imageset downloads
This commit is contained in:
1 parent
be47aede02
commit
50ea658fc7
2 files changed
+113
-12
No files matched your search
Regular → Executable
+80
@@ -297,5 +297,85 @@ def test_resume_resizing(
|
||||
trash.assert_called_once_with(file_paths=[image_path])
|
||||
|
||||
|
||||
@pytest.mark.parametrize("extension", [".zip", ".mp4"])
|
||||
@pytest.mark.parametrize("unfinished", [False, True])
|
||||
def test_filehost_resumes_unfinished_downloads(
|
||||
tmp_path: Path,
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
caplog: pytest.LogCaptureFixture,
|
||||
extension: str,
|
||||
unfinished: bool,
|
||||
) -> None:
|
||||
directory = tmp_path / "album"
|
||||
directory.mkdir()
|
||||
destination = (
|
||||
directory / "download.zip" if extension == ".zip" else tmp_path / "album.mp4"
|
||||
)
|
||||
with destination.open(mode="wb") as output:
|
||||
output.truncate(6 * 1024 * 1024)
|
||||
control_file = destination.with_name(f"{destination.name}.aria2")
|
||||
if unfinished:
|
||||
control_file.touch()
|
||||
url = "https://example.com/download"
|
||||
monkeypatch.setattr(
|
||||
download_imageset, "resolve_download_url_with_alldebrid", Mock(return_value=url)
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
download_imageset,
|
||||
"_resolve_url",
|
||||
Mock(return_value=download_imageset.UrlMeta(
|
||||
suggested_filename=f"download{extension}",
|
||||
suggested_extension=extension,
|
||||
size_bytes=destination.stat().st_size,
|
||||
)),
|
||||
)
|
||||
run_command = Mock()
|
||||
monkeypatch.setattr(download_imageset, "run_command", run_command)
|
||||
monkeypatch.setattr(download_imageset, "process_imageset", Mock())
|
||||
|
||||
result = CliRunner().invoke(
|
||||
cli=download_imageset.cli,
|
||||
args=["filehost", url, "--name", "album", "--cwd", str(tmp_path), "--token", "test"],
|
||||
)
|
||||
|
||||
assert result.exit_code == 0, result.output
|
||||
if unfinished:
|
||||
assert f"unfinished download; resuming {destination}" in caplog.text
|
||||
run_command.assert_called_once_with(args=[
|
||||
"aria2c", "--continue=true", f"--dir={destination.parent}",
|
||||
f"--out={destination.name}", url,
|
||||
])
|
||||
else:
|
||||
assert "unfinished download; resuming" not in caplog.text
|
||||
run_command.assert_not_called()
|
||||
|
||||
|
||||
@pytest.mark.parametrize("process_directory", [False, True])
|
||||
def test_process_skips_unfinished_archive(
|
||||
tmp_path: Path,
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
caplog: pytest.LogCaptureFixture,
|
||||
process_directory: bool,
|
||||
) -> None:
|
||||
archive_path = tmp_path / "gallery.zip"
|
||||
with zipfile.ZipFile(file=archive_path, mode="w") as archive:
|
||||
archive.writestr(zinfo_or_arcname="photo.png", data=b"image data")
|
||||
archive_path.with_name(f"{archive_path.name}.aria2").touch()
|
||||
trash = Mock()
|
||||
monkeypatch.setattr(download_imageset, "move_to_trash", trash)
|
||||
|
||||
result = CliRunner().invoke(
|
||||
cli=download_imageset.cli,
|
||||
args=["process", str(tmp_path if process_directory else archive_path)],
|
||||
)
|
||||
|
||||
assert result.exit_code == 0, result.output
|
||||
assert "download is unfinished" in caplog.text
|
||||
assert not (tmp_path / "photo.png").exists()
|
||||
assert not (tmp_path / "gallery").exists()
|
||||
assert archive_path.is_file()
|
||||
trash.assert_not_called()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(pytest.main(args=[str(Path(__file__).resolve()), *sys.argv[1:]]))
|
||||
Reference in new issue
Block a user