Skip to content

Commit

Permalink
added CLEANUP_TEMPORARY_FILES setting, defaults to true (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
9-FS authored Sep 19, 2024
1 parent e57b589 commit e821490
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 12 deletions.
2 changes: 2 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
pub struct Config
{
pub CF_CLEARANCE: String, // bypass bot protection
pub CLEANUP_TEMPORARY_FILES: bool, // clean up temporary files after downloading? some prefer off for deduplication or compatibility with other tools
pub CSRFTOKEN: String, // bypass bot protection
pub DATABASE_URL: String, // url to database file
pub DEBUG: Option<bool>, // debug mode?
Expand All @@ -26,6 +27,7 @@ impl Default for Config
Self
{
CF_CLEARANCE: "".to_owned(),
CLEANUP_TEMPORARY_FILES: true,
CSRFTOKEN: "".to_owned(),
DATABASE_URL: "./db/db.sqlite".to_owned(),
DEBUG: None, // no entry in default config, defaults to false
Expand Down
12 changes: 6 additions & 6 deletions src/get_hentai_id_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,11 @@ pub async fn get_hentai_id_list(downloadme_filepath: &str, http_client: &reqwest
{
match file.write_all(hentai_id_list.iter().map(|id| id.to_string()).collect::<Vec<String>>().join("\n").as_bytes()).await
{
Ok(_) => log::info!("Saved hentai ID list from tag search in {downloadme_filepath}."),
Err(e) => log::warn!("Writing hentai ID list to {downloadme_filepath} failed with: {e}"),
Ok(_) => log::info!("Saved hentai ID list from tag search at \"{downloadme_filepath}\"."),
Err(e) => log::warn!("Writing hentai ID list to \"{downloadme_filepath}\" failed with: {e}"),
}
},
Err(e) => log::warn!("Saving hentai ID list at {downloadme_filepath} failed with: {e}"),
Err(e) => log::warn!("Saving hentai ID list at \"{downloadme_filepath}\" failed with: {e}"),
}
#[cfg(not(target_family = "unix"))]
match tokio::fs::OpenOptions::new().create_new(true).write(true).open(downloadme_filepath).await
Expand All @@ -86,11 +86,11 @@ pub async fn get_hentai_id_list(downloadme_filepath: &str, http_client: &reqwest
{
match file.write_all(hentai_id_list.iter().map(|id| id.to_string()).collect::<Vec<String>>().join("\n").as_bytes()).await
{
Ok(_) => log::info!("Saved hentai ID list from tag search in {downloadme_filepath}."),
Err(e) => log::warn!("Writing hentai ID list to {downloadme_filepath} failed with: {e}"),
Ok(_) => log::info!("Saved hentai ID list from tag search at \"{downloadme_filepath}\"."),
Err(e) => log::warn!("Writing hentai ID list to \"{downloadme_filepath}\" failed with: {e}"),
}
},
Err(e) => log::warn!("Saving hentai ID list at {downloadme_filepath} failed with: {e}"),
Err(e) => log::warn!("Saving hentai ID list at \"{downloadme_filepath}\" failed with: {e}"),
}
log::debug!("{hentai_id_list:?}");
return hentai_id_list;
Expand Down
44 changes: 39 additions & 5 deletions src/hentai.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,12 @@ impl Hentai
///
/// # Returns
/// - nothing or error
pub async fn download(&self, http_client: &reqwest::Client) -> Result<(), HentaiDownloadError>
pub async fn download(&self, http_client: &reqwest::Client, cleanup_temporary_files: bool) -> Result<(), HentaiDownloadError>
{
const WORKERS: usize = 5; // number of parallel workers
let cbz_final_filepath: String; //filepath to final cbz in library
let cbz_temp_filepath: String = format!("{}{}/{}", self.library_path, self.id, self.cbz_filename); //filepath to temporary cbz, cbz is created here and when finished moved to final location, roundabout way over temporary cbz filepath in case program gets stopped while creating cbz, so no half finished cbz remains in library
let comicinfoxml_filepath: String = format!("{}{}/ComicInfo.xml", self.library_path, self.id); // filepath to metadata file if cleanup_temporary_files is false
let f = scaler::Formatter::new()
.set_scaling(scaler::Scaling::None)
.set_rounding(scaler::Rounding::Magnitude(0)); // formatter
Expand Down Expand Up @@ -261,13 +262,46 @@ impl Hentai
{
if let Some(parent) = std::path::Path::new(cbz_final_filepath.as_str()).parent() {tokio::fs::DirBuilder::new().recursive(true).create(parent).await?;} // create all parent directories
}
tokio::fs::rename(cbz_temp_filepath, cbz_final_filepath).await?; // move finished cbz to final location in library
log::info!("Saved hentai cbz.");
tokio::fs::rename(cbz_temp_filepath, &cbz_final_filepath).await?; // move finished cbz to final location in library
log::info!("Saved hentai cbz at \"{cbz_final_filepath}\".");


if let Err(e) = tokio::fs::remove_dir_all(format!("{}{}", self.library_path, self.id)).await // cleanup, delete image directory
if cleanup_temporary_files // if temporary files should be cleaned up
{
log::warn!("Deleting \"{}/\" failed with: {e}", format!("{}{}", self.library_path, self.id));
match tokio::fs::remove_dir_all(format!("{}{}", self.library_path, self.id)).await // cleanup, delete image directory
{
Ok(_) => log::debug!("Deleted \"{}{}/\".", self.library_path, self.id),
Err(e) => log::warn!("Deleting \"{}{}/\" failed with: {e}", self.library_path, self.id),
}
}
else // if temporary files should not be cleaned up
{
#[cfg(target_family = "unix")]
match tokio::fs::OpenOptions::new().create_new(true).mode(0o666).write(true).open(&comicinfoxml_filepath).await
{
Ok(mut file) =>
{
match file.write_all(serde_xml_rs::to_string(&ComicInfoXml::from(self.clone()))?.as_bytes()).await // even add the ComicInfo.xml to the directory
{
Ok(_) => log::info!("Saved hentai metadata file at \"{comicinfoxml_filepath}\"."),
Err(e) => log::warn!("Writing hentai metadata to \"{comicinfoxml_filepath}\" failed with: {e}"),
}
},
Err(e) => log::warn!("Saving hentai metadata at \"{comicinfoxml_filepath}\" failed with: {e}"),
}
#[cfg(not(target_family = "unix"))]
match tokio::fs::OpenOptions::new().create_new(true).write(true).open(&comicinfoxml_filepath).await
{
Ok(mut file) =>
{
match file.write_all(serde_xml_rs::to_string(&ComicInfoXml::from(self.clone()))?.as_bytes()).await // even add the ComicInfo.xml to the directory
{
Ok(_) => log::info!("Saved hentai metadata file."),
Err(e) => log::warn!("Writing hentai metadata to \"{comicinfoxml_filepath}\" failed with: {e}"),
}
},
Err(e) => log::warn!("Saving hentai metadata at \"{comicinfoxml_filepath}\" failed with: {e}"),
}
}

return Ok(());
Expand Down
2 changes: 1 addition & 1 deletion src/main_inner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ pub async fn main_inner(config: Config) -> Result<(), Error>
}
}

if let Err(e) = hentai.download(&http_client).await
if let Err(e) = hentai.download(&http_client, config.CLEANUP_TEMPORARY_FILES).await
{
log::error!{"{e}"};
}
Expand Down

0 comments on commit e821490

Please sign in to comment.