Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not all epubs use cover-image #39

Open
tadghh opened this issue Aug 9, 2023 · 0 comments
Open

Not all epubs use cover-image #39

tadghh opened this issue Aug 9, 2023 · 0 comments

Comments

@tadghh
Copy link

tadghh commented Aug 9, 2023

Some epubs may use custom string when assigning their cover images. My current workaround is below, some context is missing but i'm sure you can figure it out

        //Look for keys in the hashmap containing the word cover
        let pattern = r"(?i)cover";
        let regex = Regex::new(pattern).unwrap();

        let epub_resources = doc.resources.clone();
        println!("Resources {:?}", epub_resources);
        let cover_id = epub_resources.keys().find(|key| regex.is_match(key));

        if cover_id.is_some() {
            let cover = doc.get_resource(cover_id.unwrap());
            let cover_data = cover.unwrap().0;
            let mut f = fs::File
                ::create(&cover_path)
                .map_err(|err| format!("Error creating cover file: {}", err))?;
            f.write_all(&cover_data).map_err(|err| format!("Error writing cover data: {}", err))?;
        } else {
            //Return our error thumbnail placeholder
            return Ok(format!("{}/{}", get_home_dir(), "error.jpg"));
        }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant