This commit is contained in:
Marc Plano-Lesay 2024-10-15 17:30:02 +11:00
parent 789f55ebac
commit ebd2f3443e
3 changed files with 16 additions and 16 deletions

1
Cargo.lock generated
View file

@ -244,6 +244,7 @@ dependencies = [
name = "cbz2pdf"
version = "0.1.0"
dependencies = [
"anyhow",
"clap",
"dialoguer",
"image",

View file

@ -4,6 +4,7 @@ version = "0.1.0"
edition = "2021"
[dependencies]
anyhow = "1.0.89"
clap = { version = "4.5.20", features = ["derive"] }
dialoguer = "0.11.0"
image = "0.25.2"

View file

@ -1,3 +1,4 @@
use anyhow::Result;
use clap::{Parser, ValueHint};
use dialoguer::theme::ColorfulTheme;
use dialoguer::Confirm;
@ -86,7 +87,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
};
if proceed {
process_jobs(jobs);
process_jobs(jobs)?;
}
Ok(())
@ -145,7 +146,7 @@ impl Job {
}
}
fn convert_cbz(cbz_path: &Path, output_path: &Path) -> Result<(), Box<dyn std::error::Error>> {
fn convert_cbz(cbz_path: &Path, output_path: &Path) -> Result<()> {
let a4 = Rect::new(0.0, 0.0, 595.0, 842.0);
let mut zip = ZipArchive::new(File::open(cbz_path)?)?;
@ -180,13 +181,12 @@ fn convert_cbz(cbz_path: &Path, output_path: &Path) -> Result<(), Box<dyn std::e
pdf.catalog(catalog_id).pages(page_tree_id);
let mut pages = Vec::new();
let image_count: i32 = images.len().try_into().unwrap();
let image_count = images.len();
for (pos, image) in images.iter().enumerate() {
let npos: i32 = pos.try_into().unwrap();
let page_id = Ref::new(npos + 10);
let image_id = Ref::new(image_count + 10 + npos);
let content_id = Ref::new(image_count * 3 + 10 + npos);
let page_id = Ref::new(pos as i32 + 10);
let image_id = Ref::new(image_count as i32 + 10 + pos as i32);
let content_id = Ref::new(image_count as i32 * 3 + 10 + pos as i32);
pages.push(page_id);
let mut page = pdf.page(page_id);
let image_name = Name(b"Im1");
@ -214,22 +214,19 @@ fn convert_cbz(cbz_path: &Path, output_path: &Path) -> Result<(), Box<dyn std::e
}
let page_count = pages.len();
pdf.pages(page_tree_id)
.kids(pages)
.count(page_count.try_into().unwrap());
pdf.pages(page_tree_id).kids(pages).count(page_count as i32);
std::fs::write(output_path, pdf.finish())?;
Ok(())
}
fn process_jobs(jobs: Vec<Job>) {
let pb = ProgressBar::new(jobs.len().try_into().unwrap());
fn process_jobs(jobs: Vec<Job>) -> Result<()> {
let pb = ProgressBar::new(jobs.len() as u64);
pb.enable_steady_tick(Duration::from_millis(300));
pb.set_style(
ProgressStyle::with_template("[{elapsed_precise}] {wide_bar} {pos:>7}/{len:7} {msg}")
.unwrap(),
);
pb.set_style(ProgressStyle::with_template(
"[{elapsed_precise}] {wide_bar} {pos:>7}/{len:7} {msg}",
)?);
jobs.par_iter().for_each(|entry| {
convert_cbz(&entry.cbz_path, &entry.pdf_path).unwrap();
@ -237,4 +234,5 @@ fn process_jobs(jobs: Vec<Job>) {
});
pb.finish();
Ok(())
}