Extract a private _stream_to_file(url, output_path, transport, progress, cancel_event) -> Path helper from the ~40-line chunked-download loop that is duplicated between download_m4b() and download_zip_part(). Both become thin wrappers that call the helper then do their post-processing step (return path vs extract ZIP).
The helper handles: open .partial file → seek to existing size for resume → stream chunks in CHUNK_SIZE blocks → check cancel_event on each chunk → atomic rename to final path.
Acceptance criteria
_stream_to_file() exists as a private function in downloader.py
download_m4b() is a thin wrapper around _stream_to_file() (calls it, returns the Path)
download_zip_part() is a thin wrapper around _stream_to_file() (calls it, then extracts ZIP)
3-5 new tests for _stream_to_file(): fresh download, resume, cancel-mid-stream, atomic rename, error propagation
All existing download tests pass (M4B resume, ZIP extract, cancellation)
Net line reduction in downloader.py of ~30-40 lines
Blocked by
None - can start immediately (prefer Slices 3+4 done first to clean surrounding area)
## Parent
#36 — PRD-003: Deepen Codebase Architecture
## What to build
Extract a private `_stream_to_file(url, output_path, transport, progress, cancel_event) -> Path` helper from the ~40-line chunked-download loop that is duplicated between `download_m4b()` and `download_zip_part()`. Both become thin wrappers that call the helper then do their post-processing step (return path vs extract ZIP).
The helper handles: open .partial file → seek to existing size for resume → stream chunks in CHUNK_SIZE blocks → check cancel_event on each chunk → atomic rename to final path.
## Acceptance criteria
- [ ] `_stream_to_file()` exists as a private function in `downloader.py`
- [ ] `download_m4b()` is a thin wrapper around `_stream_to_file()` (calls it, returns the Path)
- [ ] `download_zip_part()` is a thin wrapper around `_stream_to_file()` (calls it, then extracts ZIP)
- [ ] 3-5 new tests for `_stream_to_file()`: fresh download, resume, cancel-mid-stream, atomic rename, error propagation
- [ ] All existing download tests pass (M4B resume, ZIP extract, cancellation)
- [ ] Net line reduction in `downloader.py` of ~30-40 lines
## Blocked by
None - can start immediately (prefer Slices 3+4 done first to clean surrounding area)
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.
Parent
#36 — PRD-003: Deepen Codebase Architecture
What to build
Extract a private
_stream_to_file(url, output_path, transport, progress, cancel_event) -> Pathhelper from the ~40-line chunked-download loop that is duplicated betweendownload_m4b()anddownload_zip_part(). Both become thin wrappers that call the helper then do their post-processing step (return path vs extract ZIP).The helper handles: open .partial file → seek to existing size for resume → stream chunks in CHUNK_SIZE blocks → check cancel_event on each chunk → atomic rename to final path.
Acceptance criteria
_stream_to_file()exists as a private function indownloader.pydownload_m4b()is a thin wrapper around_stream_to_file()(calls it, returns the Path)download_zip_part()is a thin wrapper around_stream_to_file()(calls it, then extracts ZIP)_stream_to_file(): fresh download, resume, cancel-mid-stream, atomic rename, error propagationdownloader.pyof ~30-40 linesBlocked by
None - can start immediately (prefer Slices 3+4 done first to clean surrounding area)
Closed via
fcdba33— extracted _stream_to_file() helper, 5 TDD tests, 224/224 pass