aboutsummaryrefslogtreecommitdiff
path: root/src/epub.rs
diff options
context:
space:
mode:
authorA Farzat <a@farzat.xyz>2026-03-07 21:50:56 +0300
committerA Farzat <a@farzat.xyz>2026-03-07 21:50:56 +0300
commitc181afaf936ba0d5cd19c38869422fc9351af2d1 (patch)
treea8361f6c40bfa0f4d4883a89dc418ece5b1ce8fc /src/epub.rs
parenta1ffe295ade3026f8712c2608eacc285c595f08c (diff)
downloadoreilly-epub-c181afaf936ba0d5cd19c38869422fc9351af2d1.tar.gz
oreilly-epub-c181afaf936ba0d5cd19c38869422fc9351af2d1.zip
Avoid using string buffers when modifying chapters
This saves on memory. Bytes are read from the file as needed, and written to zip as soon as they are ready.
Diffstat (limited to 'src/epub.rs')
-rw-r--r--src/epub.rs13
1 files changed, 6 insertions, 7 deletions
diff --git a/src/epub.rs b/src/epub.rs
index f9a5aac..c455671 100644
--- a/src/epub.rs
+++ b/src/epub.rs
@@ -8,7 +8,7 @@ use relative_path::{RelativePath, RelativePathBuf};
use reqwest::Client;
use std::{
collections::HashMap,
- io::{Read, Write},
+ io::{BufReader, Read, Write},
path::Path,
};
use tokio::{
@@ -107,21 +107,20 @@ pub fn create_epub_archive(
for entry in file_entries {
zip.start_file(&entry.full_path, options)?;
let mut src_file = std::fs::File::open(entry.full_path.to_path(epub_root))?;
- let mut buffer = Vec::new();
- src_file.read_to_end(&mut buffer)?;
if let Some(chapter) = chapters.get(&entry.ourn) {
let chapter_dir = entry.full_path.parent().unwrap_or(RelativePath::new(""));
- let html = String::from_utf8(buffer)?;
- let html = build_epub_chapter(
+ build_epub_chapter(
epub_data,
chapter,
chapter_dir,
- &html,
+ BufReader::new(src_file),
&url_to_file,
&url_path_to_local,
+ &mut zip,
)?;
- zip.write_all(html.as_bytes())?;
} else {
+ let mut buffer = Vec::new();
+ src_file.read_to_end(&mut buffer)?;
zip.write_all(&buffer)?;
}
}