Seperated out the tempdir functions.

This commit is contained in:
NA 2024-01-30 15:44:47 +00:00
parent 9c715ef588
commit 3df3d05c34
3 changed files with 19 additions and 16 deletions

View file

@ -20,6 +20,7 @@
"reqwest", "reqwest",
"royalroad", "royalroad",
"tempdir", "tempdir",
"tempfile",
"thiserror", "thiserror",
"ureq" "ureq"
] ]

View file

@ -34,10 +34,7 @@ pub fn setup_html2xhtml() -> Result<TempDir, GenerationError> {
#[cfg(target_os = "windows")] { #[cfg(target_os = "windows")] {
const HTML2XHTML: &[u8; 245025] = include_bytes!("../html2xhtml-windows.zip"); // This will not compile on windows due to this and no I don't give a shit. const HTML2XHTML: &[u8; 245025] = include_bytes!("../html2xhtml-windows.zip"); // This will not compile on windows due to this and no I don't give a shit.
// Compile it on linux for windows like a sane person. // Compile it on linux for windows like a sane person.
let html2xhtml_temp_dir = match TempDir::new() { let html2xhtml_temp_dir = create_temp_dir()?;
Ok(temp_dir) => temp_dir,
Err(error) => return Err(GenerationError::TempDirCreationError {error}),
};
match zip_extract::extract(Cursor::new(HTML2XHTML), html2xhtml_temp_dir.path(), true) { match zip_extract::extract(Cursor::new(HTML2XHTML), html2xhtml_temp_dir.path(), true) {
Ok(_) => (), Ok(_) => (),
@ -49,10 +46,7 @@ pub fn setup_html2xhtml() -> Result<TempDir, GenerationError> {
#[cfg(target_os = "linux")] { #[cfg(target_os = "linux")] {
const HTML2XHTML: &[u8; 186938] = include_bytes!("../html2xhtml-linux.zip"); const HTML2XHTML: &[u8; 186938] = include_bytes!("../html2xhtml-linux.zip");
let html2xhtml_temp_dir = match TempDir::new() { let html2xhtml_temp_dir = create_temp_dir()?;
Ok(temp_dir) => temp_dir,
Err(error) => return Err(GenerationError::TempDirCreationError {error}),
};
match zip_extract::extract(Cursor::new(HTML2XHTML), html2xhtml_temp_dir.path(), true) { match zip_extract::extract(Cursor::new(HTML2XHTML), html2xhtml_temp_dir.path(), true) {
Ok(_) => (), Ok(_) => (),
@ -71,11 +65,19 @@ pub fn setup_html2xhtml() -> Result<TempDir, GenerationError> {
Err(GenerationError::OsUnsupportedError {os: misc::Oses::OtherUnknownOs}) Err(GenerationError::OsUnsupportedError {os: misc::Oses::OtherUnknownOs})
} }
/// Delete html2xhtml from the operating system's temp directory. /// Function to create a temporary directory.
pub fn delete_html2xhtml(html2xhtml_dir: TempDir) { fn create_temp_dir() -> Result<TempDir, GenerationError> {
let temp_dir_path = html2xhtml_dir.path().to_path_buf(); match TempDir::new() {
Ok(temp_dir) => return Ok(temp_dir),
Err(error) => return Err(GenerationError::TempDirCreationError {error}),
}
}
match html2xhtml_dir.close() { /// Delete a temporary directory.
pub fn delete_temp_dir(temp_dir: TempDir) {
let temp_dir_path = temp_dir.path().to_path_buf();
match temp_dir.close() {
Ok(_) => (), Ok(_) => (),
Err(warning) => { Err(warning) => {
let warning = Warning::TempDirDeletionError { let warning = Warning::TempDirDeletionError {

View file

@ -124,7 +124,7 @@ pub fn generate_epub(epub_args: EpubArgs, book_url: Url, output_directory: PathB
epub_builder.inline_toc(); epub_builder.inline_toc();
// Setup html2xhtml on the operating system. // Setup html2xhtml on the operating system.
let html2xhtml_dir = file_system_crap::setup_html2xhtml()?; let html2xhtml_temp_dir = file_system_crap::setup_html2xhtml()?;
let mut old_tags_new_tags: HashMap<String, String> = HashMap::new(); let mut old_tags_new_tags: HashMap<String, String> = HashMap::new();
@ -165,7 +165,7 @@ pub fn generate_epub(epub_args: EpubArgs, book_url: Url, output_directory: PathB
let xhtml: String; let xhtml: String;
if epub_args.no_images { if epub_args.no_images {
xhtml = html_to_xhtml(string_to_html_fragment(&remove_image_tags(&chapter.isolated_chapter_html)), &html2xhtml_dir)? xhtml = html_to_xhtml(string_to_html_fragment(&remove_image_tags(&chapter.isolated_chapter_html)), &html2xhtml_temp_dir)?
} }
else { else {
let mut replaced_html = chapter.isolated_chapter_html.html(); let mut replaced_html = chapter.isolated_chapter_html.html();
@ -173,7 +173,7 @@ pub fn generate_epub(epub_args: EpubArgs, book_url: Url, output_directory: PathB
replaced_html = replaced_html.replace(&old_img_tag.clone(), &old_tags_new_tags[old_img_tag]); replaced_html = replaced_html.replace(&old_img_tag.clone(), &old_tags_new_tags[old_img_tag]);
} }
xhtml = html_to_xhtml(string_to_html_fragment(&replaced_html), &html2xhtml_dir)?; xhtml = html_to_xhtml(string_to_html_fragment(&replaced_html), &html2xhtml_temp_dir)?;
} }
epub_builder.add_content(EpubContent::new(format!("chapter_{}.xhtml", i+1), xhtml.as_bytes()) epub_builder.add_content(EpubContent::new(format!("chapter_{}.xhtml", i+1), xhtml.as_bytes())
@ -199,7 +199,7 @@ pub fn generate_epub(epub_args: EpubArgs, book_url: Url, output_directory: PathB
.expect(format!("Unable to write finished epub data to {0}", output_path.to_string_lossy()).as_str()); .expect(format!("Unable to write finished epub data to {0}", output_path.to_string_lossy()).as_str());
// Delete the html2xhtml temp directory. It's good to clean up after yourself. // Delete the html2xhtml temp directory. It's good to clean up after yourself.
file_system_crap::delete_html2xhtml(html2xhtml_dir); file_system_crap::delete_temp_dir(html2xhtml_temp_dir);
Ok(WARNINGS.lock().unwrap()) Ok(WARNINGS.lock().unwrap())
} }