royal_road_archiver/src/file_system_crap.rs

101 lines
3.2 KiB
Rust
Raw Normal View History

use std::{io::Cursor, path::PathBuf};
use path_slash::PathBufExt as _;
use tempfile::TempDir;
use crate::{misc, GenerationError, Warning, WARNINGS};
/// Converts a given path to windows style if needed.
pub fn convert_path_to_os_specific(path: PathBuf) -> PathBuf {
// If target os is windows.
2024-02-10 16:32:27 -06:00
#[cfg(target_os = "windows")]
{
return PathBuf::from_slash_lossy(path.into_os_string());
}
// If target os is not windows.
2024-02-10 16:32:27 -06:00
#[cfg(not(target_os = "windows"))]
{
return PathBuf::from_backslash_lossy(path.into_os_string());
}
}
/// Remove chars that are illegal to be used in filenames on both unix & windows.
pub fn remove_illegal_chars(mut string: String) -> String {
const ILLEGAL_CHARS: [char; 9] = ['/', '\u{005C}', '<', '>', ':', '\u{0022}', '|', '?', '*'];
for char in ILLEGAL_CHARS {
string = string.replace(char, " ");
}
return string;
}
/// Setup html2xhtml in the operating system's temp directory.
pub fn setup_html2xhtml() -> Result<TempDir, GenerationError> {
2024-02-10 16:32:27 -06:00
#[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.
2024-01-30 09:44:47 -06:00
let html2xhtml_temp_dir = create_temp_dir()?;
match zip_extract::extract(Cursor::new(HTML2XHTML), html2xhtml_temp_dir.path(), true) {
Ok(_) => (),
2024-02-10 16:32:27 -06:00
Err(error) => return Err(GenerationError::Html2XhtmlExtractionError { error }),
}
return Ok(html2xhtml_temp_dir);
}
2024-02-10 16:32:27 -06:00
#[cfg(target_os = "linux")]
{
const HTML2XHTML: &[u8; 186938] = include_bytes!("../html2xhtml-linux.zip");
2024-01-30 09:44:47 -06:00
let html2xhtml_temp_dir = create_temp_dir()?;
match zip_extract::extract(Cursor::new(HTML2XHTML), html2xhtml_temp_dir.path(), true) {
Ok(_) => (),
2024-02-10 16:32:27 -06:00
Err(error) => return Err(GenerationError::Html2XhtmlExtractionError { error }),
}
return Ok(html2xhtml_temp_dir);
}
2024-02-10 16:32:27 -06:00
#[cfg(target_os = "macos")]
{
Err(GenerationError::OsUnsupportedError {
os: misc::Oses::MacOs,
})
}
2024-02-10 16:32:27 -06:00
// In the event the OS is unknown.
#[allow(unreachable_code)]
2024-02-10 16:32:27 -06:00
Err(GenerationError::OsUnsupportedError {
os: misc::Oses::OtherUnknownOs,
})
}
2024-01-30 09:44:47 -06:00
/// Function to create a temporary directory.
fn create_temp_dir() -> Result<TempDir, GenerationError> {
match TempDir::new() {
Ok(temp_dir) => return Ok(temp_dir),
2024-02-10 16:32:27 -06:00
Err(error) => return Err(GenerationError::TempDirCreationError { error }),
2024-01-30 09:44:47 -06:00
}
}
/// Delete a temporary directory.
pub fn delete_temp_dir(temp_dir: TempDir) {
let temp_dir_path = temp_dir.path().to_path_buf();
2024-01-30 09:44:47 -06:00
match temp_dir.close() {
Ok(_) => (),
Err(warning) => {
2024-02-10 16:32:27 -06:00
let warning = Warning::TempDirDeletionError {
warning_msg: "Unable to close and delete temp directory".to_string(),
temp_directory_path: temp_dir_path,
error: warning,
};
WARNINGS.lock().unwrap().add_warning(warning);
2024-02-10 16:32:27 -06:00
},
}
2024-02-10 16:32:27 -06:00
}