diff options
| author | A Farzat <a@farzat.xyz> | 2026-03-03 10:28:20 +0300 |
|---|---|---|
| committer | A Farzat <a@farzat.xyz> | 2026-03-03 11:05:07 +0300 |
| commit | b0035a511b13b03c4c73b0cfad488dbab541e01e (patch) | |
| tree | 1f57a0bc5403b59259bb92c65493663bb5b59382 /src/epub.rs | |
| parent | 859700794abece7d5a90896a87fe012164d9fc0e (diff) | |
| download | oreilly-epub-b0035a511b13b03c4c73b0cfad488dbab541e01e.tar.gz oreilly-epub-b0035a511b13b03c4c73b0cfad488dbab541e01e.zip | |
Add file download logic
All files in file_entries are downloaded according to their full_path.
Diffstat (limited to 'src/epub.rs')
| -rw-r--r-- | src/epub.rs | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/src/epub.rs b/src/epub.rs new file mode 100644 index 0000000..0c0bc74 --- /dev/null +++ b/src/epub.rs @@ -0,0 +1,34 @@ +use crate::models::FileEntry; +use anyhow::Result; +use reqwest::Client; +use std::path::Path; +use tokio::{ + fs::{self, File}, + io::AsyncWriteExt, +}; + +pub async fn download_all_files( + client: &Client, + file_entries: &[FileEntry], + dest_root: &Path, +) -> Result<()> { + for entry in file_entries { + let dest_path = dest_root.join(&entry.full_path); + + if let Some(parent_dir) = dest_path.parent() { + fs::create_dir_all(parent_dir).await?; + } + + let mut file = File::create(dest_path).await?; + let bytes = client + .get(&entry.url) + .send() + .await? + .error_for_status()? + .bytes() + .await?; + + file.write_all(&bytes).await?; + } + Ok(()) +} |
