From 6bcc81d4fc2c3f4e8330398859bee75166387bab Mon Sep 17 00:00:00 2001 From: A Farzat Date: Tue, 10 Mar 2026 12:28:21 +0300 Subject: Stream downloads to files directly --- src/epub.rs | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) (limited to 'src/epub.rs') diff --git a/src/epub.rs b/src/epub.rs index 9a59a7c..735bb1d 100644 --- a/src/epub.rs +++ b/src/epub.rs @@ -3,18 +3,17 @@ use crate::{ xml::{build_epub_chapter, write_modified_opf}, }; use anyhow::{Context, Result}; +use futures_util::TryStreamExt; use ogrim::xml; use relative_path::{RelativePath, RelativePathBuf}; use reqwest::Client; use std::{ collections::HashMap, - io::{BufReader, Write, copy}, + io::{BufReader, Write}, path::Path, }; -use tokio::{ - fs::{self, File}, - io::AsyncWriteExt, -}; +use tokio::fs::{self, File}; +use tokio_util::io::StreamReader; use zip::{CompressionMethod, ZipWriter, write::FileOptions}; /// Creates and writes container.xml. @@ -34,6 +33,7 @@ fn write_container_xml(out: &mut W, opf_full_path: &RelativePathBuf) - Ok(()) } +/// Downloads files to the relative location specified in full_path. pub async fn download_all_files( client: &Client, file_entries: &[FileEntry], @@ -47,15 +47,15 @@ pub async fn download_all_files( } let mut file = File::create(dest_path).await?; - let bytes = client + let bytes_stream = client .get(entry.url.clone()) .send() .await? .error_for_status()? - .bytes() - .await?; + .bytes_stream(); + let mut reader = StreamReader::new(bytes_stream.map_err(std::io::Error::other)); - file.write_all(&bytes).await?; + tokio::io::copy(&mut reader, &mut file).await?; } Ok(()) } @@ -122,7 +122,7 @@ pub fn create_epub_archive( } else if entry.ourn == opf_entry.ourn { write_modified_opf(buf_reader, &mut zip, &epub_data.descriptions.plain)?; } else { - copy(&mut buf_reader, &mut zip)?; + std::io::copy(&mut buf_reader, &mut zip)?; } } -- cgit v1.2.3-70-g09d2