diff options
| author | A Farzat <a@farzat.xyz> | 2026-03-03 11:27:17 +0300 |
|---|---|---|
| committer | A Farzat <a@farzat.xyz> | 2026-03-03 11:27:17 +0300 |
| commit | 09b76f2bd136bfa41a0d13e698cc8c651ede0892 (patch) | |
| tree | 36fe026514cacae621d71dc73751788ba69a4bbb /src | |
| parent | 6aad662ec59a3974cbca6c423c1e08e8bc5e90f6 (diff) | |
| download | oreilly-epub-09b76f2bd136bfa41a0d13e698cc8c651ede0892.tar.gz oreilly-epub-09b76f2bd136bfa41a0d13e698cc8c651ede0892.zip | |
Add a function to write container.xml
Diffstat (limited to 'src')
| -rw-r--r-- | src/epub.rs | 26 | ||||
| -rw-r--r-- | src/main.rs | 3 |
2 files changed, 28 insertions, 1 deletions
diff --git a/src/epub.rs b/src/epub.rs index f216567..6f736de 100644 --- a/src/epub.rs +++ b/src/epub.rs @@ -7,6 +7,32 @@ use tokio::{ io::AsyncWriteExt, }; +/// Creates and writes container.xml. +pub async fn write_container_xml(dest_root: &Path, opf_full_path: &str) -> Result<()> { + // Create destination directory. + let dest_dir = dest_root.join("META-INF"); + fs::create_dir_all(&dest_dir).await?; + + // Create distination file. + let dest_path = dest_dir.join("container.xml"); + let mut file = File::create(dest_path).await?; + + // Prepare file contents. + let contents = format!( + r#"<?xml version="1.0" encoding="UTF-8"?> +<container version="1.0" xmlns="urn:oasis:names:tc:opendocument:xmlns:container"> + <rootfiles> + <rootfile full-path="{opf_full_path}" media-type="application/oebps-package+xml"/> + </rootfiles> +</container> +"# + ); + + // Write down the file. + file.write_all(contents.as_bytes()).await?; + Ok(()) +} + /// Creates and writes the mimetype. Assumes dest_root already exists. pub async fn write_mimetype(dest_root: &Path) -> Result<()> { let dest_path = dest_root.join("mimetype"); diff --git a/src/main.rs b/src/main.rs index 6e9e6dd..6376315 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,7 +5,7 @@ mod models; use std::collections::HashMap; use std::path::Path; -use crate::epub::{download_all_files, write_mimetype}; +use crate::epub::{download_all_files, write_container_xml, write_mimetype}; use crate::http_client::build_authenticated_client; use crate::models::{Chapter, EpubResponse, FileEntry, Paginated, SpineItem, TocNode}; use anyhow::{Context, Result, ensure}; @@ -122,6 +122,7 @@ async fn main() -> Result<()> { let dest_root = Path::new(&dest_root); download_all_files(&client, &file_entries, dest_root).await?; write_mimetype(dest_root).await?; + write_container_xml(dest_root, &opf_entry.full_path).await?; // Sanity check: Every entry in spine exists in chapters. let chapters: HashMap<String, Chapter> = |
