From ab800d0174a6c0317c8c7b0d31a07fabfefae879 Mon Sep 17 00:00:00 2001 From: Kenneth Gitere Date: Mon, 23 Nov 2020 09:01:05 +0300 Subject: [PATCH] Bug fix and add printing of the name of the extracted EPUB The fix prevents creating the res directory if it already exists --- src/main.rs | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/main.rs b/src/main.rs index 78ba0e2..3009ff4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,6 +2,7 @@ extern crate lazy_static; use std::fs::File; +use std::path::Path; use async_std::{fs::create_dir, fs::remove_dir_all, task}; use epub_builder::{EpubBuilder, EpubContent, ZipLibrary}; @@ -49,15 +50,17 @@ fn download(urls: Vec) { let mut extractor = Extractor::from_html(&html); extractor.extract_content(&url); if extractor.article().is_some() { - create_dir("res/") - .await - .expect("Unable to create res/ output folder"); + if !Path::new("res/").exists() { + create_dir("res/") + .await + .expect("Unable to create res/ output folder"); + } extractor .download_images(&Url::parse(&url).unwrap()) .await .expect("Unable to download images"); - let mut out_file = - File::create(format!("{}.epub", extractor.metadata().title())).unwrap(); + let file_name = format!("{}.epub", extractor.metadata().title()); + let mut out_file = File::create(&file_name).unwrap(); let mut html_buf = Vec::new(); extractor .article() @@ -84,6 +87,7 @@ fn download(urls: Vec) { epub.generate(&mut out_file).unwrap(); println!("Cleaning up"); remove_dir_all("res/").await.unwrap(); + println!("Created {:?}", file_name); } } })