aboutsummaryrefslogtreecommitdiff
path: root/src/epub.rs
blob: 0c0bc74c8c631a5c692ad40cdda5ef1e1d4e0140 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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(())
}