diff options
| -rw-r--r-- | Cargo.lock | 39 | ||||
| -rw-r--r-- | Cargo.toml | 4 | ||||
| -rw-r--r-- | src/epub.rs | 20 |
3 files changed, 52 insertions, 11 deletions
@@ -409,6 +409,23 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7e3450815272ef58cec6d564423f6e755e25379b217b0bc688e295ba24df6b1d" [[package]] +name = "futures-io" +version = "0.3.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cecba35d7ad927e23624b22ad55235f2239cfa44fd10428eecbeba6d6a717718" + +[[package]] +name = "futures-macro" +version = "0.3.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e835b70203e41293343137df5c0664546da5745f82ec9b84d40be8336958447b" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] name = "futures-sink" version = "0.3.32" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -427,7 +444,11 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "389ca41296e6190b48053de0321d02a77f32f8a5d2461dd38762c0593805c6d6" dependencies = [ "futures-core", + "futures-io", + "futures-macro", + "futures-sink", "futures-task", + "memchr", "pin-project-lite", "slab", ] @@ -921,6 +942,7 @@ dependencies = [ "anyhow", "clap", "directories", + "futures-util", "ogrim", "quick-xml", "relative-path", @@ -928,6 +950,7 @@ dependencies = [ "serde", "serde_json", "tokio", + "tokio-util", "url", "zip", ] @@ -1172,6 +1195,7 @@ dependencies = [ "cookie_store", "encoding_rs", "futures-core", + "futures-util", "h2", "http", "http-body", @@ -1193,12 +1217,14 @@ dependencies = [ "sync_wrapper", "tokio", "tokio-rustls", + "tokio-util", "tower", "tower-http", "tower-service", "url", "wasm-bindgen", "wasm-bindgen-futures", + "wasm-streams", "web-sys", ] @@ -1867,6 +1893,19 @@ dependencies = [ ] [[package]] +name = "wasm-streams" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9d1ec4f6517c9e11ae630e200b2b65d193279042e28edd4a2cda233e46670bbb" +dependencies = [ + "futures-util", + "js-sys", + "wasm-bindgen", + "wasm-bindgen-futures", + "web-sys", +] + +[[package]] name = "web-sys" version = "0.3.91" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -9,10 +9,12 @@ clap = { version = "4.5.60", features = ["derive"] } quick-xml = "0.39.2" ogrim = "0.1.1" relative-path = { version = "2.0.1", features = ["serde"] } -reqwest = { version = "0.13.2", features = ["cookies", "json"] } +reqwest = { version = "0.13.2", features = ["cookies", "json", "stream"] } serde = { version = "1.0.228", features = ["derive"] } serde_json = "1.0.149" tokio = { version = "1.49.0", features = ["full"] } url = { version = "2.5.8", features = ["serde"] } zip = { version = "8.1.0", default-features = false, features = ["deflate"] } directories = "6.0.0" +tokio-util = { version = "0.7.18", features = ["io"] } +futures-util = "0.3.32" 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<W: Write>(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)?; } } |
