Slice 5: Extract duplicated streaming loop into _stream_to_file() #41

Closed
opened 2026-06-05 18:11:22 +02:00 by AlexKucera · 1 comment
AlexKucera commented 2026-06-05 18:11:22 +02:00 (Migrated from github.com)

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)

## 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)
AlexKucera commented 2026-06-05 19:05:45 +02:00 (Migrated from github.com)

Closed via fcdba33 — extracted _stream_to_file() helper, 5 TDD tests, 224/224 pass

Closed via fcdba33 — extracted _stream_to_file() helper, 5 TDD tests, 224/224 pass
Sign in to join this conversation.