Skip to content

Not all epubs use cover-image #39

@tadghh

Description

@tadghh

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"));
        }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions