mirror of
https://github.com/Raine-gay/royal_road_archiver.git
synced 2024-11-14 19:38:42 -06:00
Seperated out the tempdir functions.
This commit is contained in:
parent
9c715ef588
commit
3df3d05c34
1
.vscode/settings.json
vendored
1
.vscode/settings.json
vendored
|
@ -20,6 +20,7 @@
|
|||
"reqwest",
|
||||
"royalroad",
|
||||
"tempdir",
|
||||
"tempfile",
|
||||
"thiserror",
|
||||
"ureq"
|
||||
]
|
||||
|
|
|
@ -34,10 +34,7 @@ pub fn setup_html2xhtml() -> Result<TempDir, GenerationError> {
|
|||
#[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.
|
||||
// Compile it on linux for windows like a sane person.
|
||||
let html2xhtml_temp_dir = match TempDir::new() {
|
||||
Ok(temp_dir) => temp_dir,
|
||||
Err(error) => return Err(GenerationError::TempDirCreationError {error}),
|
||||
};
|
||||
let html2xhtml_temp_dir = create_temp_dir()?;
|
||||
|
||||
match zip_extract::extract(Cursor::new(HTML2XHTML), html2xhtml_temp_dir.path(), true) {
|
||||
Ok(_) => (),
|
||||
|
@ -49,10 +46,7 @@ pub fn setup_html2xhtml() -> Result<TempDir, GenerationError> {
|
|||
|
||||
#[cfg(target_os = "linux")] {
|
||||
const HTML2XHTML: &[u8; 186938] = include_bytes!("../html2xhtml-linux.zip");
|
||||
let html2xhtml_temp_dir = match TempDir::new() {
|
||||
Ok(temp_dir) => temp_dir,
|
||||
Err(error) => return Err(GenerationError::TempDirCreationError {error}),
|
||||
};
|
||||
let html2xhtml_temp_dir = create_temp_dir()?;
|
||||
|
||||
match zip_extract::extract(Cursor::new(HTML2XHTML), html2xhtml_temp_dir.path(), true) {
|
||||
Ok(_) => (),
|
||||
|
@ -71,11 +65,19 @@ pub fn setup_html2xhtml() -> Result<TempDir, GenerationError> {
|
|||
Err(GenerationError::OsUnsupportedError {os: misc::Oses::OtherUnknownOs})
|
||||
}
|
||||
|
||||
/// Delete html2xhtml from the operating system's temp directory.
|
||||
pub fn delete_html2xhtml(html2xhtml_dir: TempDir) {
|
||||
let temp_dir_path = html2xhtml_dir.path().to_path_buf();
|
||||
/// Function to create a temporary directory.
|
||||
fn create_temp_dir() -> Result<TempDir, GenerationError> {
|
||||
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(_) => (),
|
||||
Err(warning) => {
|
||||
let warning = Warning::TempDirDeletionError {
|
||||
|
|
|
@ -124,7 +124,7 @@ pub fn generate_epub(epub_args: EpubArgs, book_url: Url, output_directory: PathB
|
|||
epub_builder.inline_toc();
|
||||
|
||||
// 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();
|
||||
|
||||
|
@ -165,7 +165,7 @@ pub fn generate_epub(epub_args: EpubArgs, book_url: Url, output_directory: PathB
|
|||
|
||||
let xhtml: String;
|
||||
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 {
|
||||
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]);
|
||||
}
|
||||
|
||||
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())
|
||||
|
@ -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());
|
||||
|
||||
// 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())
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue