chore: refactor ahead of supporting more conversion types
All checks were successful
Checking Renovate configuration / validate (pull_request) Successful in 1m5s
Build and test / Clippy (pull_request) Successful in 3m25s
Checking yaml / Run yamllint (pull_request) Successful in 11s
Build and test / Tests (pull_request) Successful in 4m12s
Build and test / Build AMD64 (pull_request) Successful in 4m12s
Build and test / Generate Documentation (pull_request) Successful in 3m52s

This commit is contained in:
Marc Plano-Lesay 2025-10-10 16:34:52 +11:00
parent 48b560d85e
commit 034f0b142c
Signed by: kernald
GPG key ID: 66A41B08CC62A6CF
12 changed files with 468 additions and 150 deletions

53
src/formats/cbz.rs Normal file
View file

@ -0,0 +1,53 @@
use std::ffi::OsStr;
use std::fs::File;
use std::io::Read;
use std::path::Path;
use anyhow::Result;
use rayon::prelude::*;
use zip::ZipArchive;
use crate::model::{Document, ImagePage};
use super::FormatReader;
pub struct CbzReader;
impl FormatReader for CbzReader {
fn read(&self, input: &Path) -> Result<Document> {
let mut zip = ZipArchive::new(File::open(input)?)?;
let mut files: Vec<(String, Vec<u8>)> = Vec::new();
for i in 0..zip.len() {
let mut file = zip.by_index(i)?;
let mut image_data = Vec::new();
let name = file
.enclosed_name()
.expect("Failed to read file name")
.to_owned();
if name.extension() == Some(OsStr::new("jpg")) {
file.read_to_end(&mut image_data)?;
files.push((
name.file_name()
.expect("Failed to read file name")
.to_string_lossy()
.to_string(),
image_data,
));
}
}
let mut pages: Vec<ImagePage> = Vec::new();
files
.par_iter()
.map(|(name, data)| ImagePage {
name: name.clone(),
image: image::load_from_memory(data).expect("Failed to decode image"),
jpeg_dct: Some(data.clone()),
})
.collect_into_vec(&mut pages);
pages.par_sort_by_key(|p| p.name.clone());
Ok(Document::new(pages))
}
}