Skip to content

Commit

Permalink
added database reconnection every 1000 hentai to free up ressources; …
Browse files Browse the repository at this point in the history
…fixed cbz not being able to be saved because of too long filename; fixed temporary image's filenames
  • Loading branch information
9FS committed Sep 10, 2024
1 parent 321bd8e commit bf1c82f
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 12 deletions.
7 changes: 4 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ license = "MIT"
name = "nhentai_archivist"
readme = "readme.md"
repository = "https://github.com/9-FS/nhentai_archivist"
version = "3.0.1"
version = "3.0.2"

[dependencies]
chrono = { version = "^0.4.0", features = ["serde"] }
Expand All @@ -33,4 +33,5 @@ sqlx = { version = "^0.8.0", features = [
] }
thiserror = "^1.0.0"
tokio = { version = "^1.0.0", features = ["rt-multi-thread"] }
unicode-segmentation = "^1.0.0"
zip = "^2.0.0"
2 changes: 1 addition & 1 deletion docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ version: "3"
services:
nhentai_archivist:
container_name: "nhentai_archivist"
image: "ghcr.io/9-fs/nhentai_archivist:3.0.1"
image: "ghcr.io/9-fs/nhentai_archivist:3.0.2"
environment:
HOST_OS: "Unraid"
PGID: 100
Expand Down
12 changes: 6 additions & 6 deletions src/hentai.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use std::os::unix::fs::OpenOptionsExt;
use std::os::unix::fs::PermissionsExt;
use std::str::FromStr;
use tokio::io::AsyncWriteExt;
use unicode_segmentation::UnicodeSegmentation;


#[derive(Clone, Debug, Eq, PartialEq)]
Expand Down Expand Up @@ -89,20 +90,19 @@ impl Hentai

cbz_filepath = hentai_table_row.title_english.clone().unwrap_or_default();
cbz_filepath.retain(|c| !TITLE_CHARACTERS_FORBIDDEN.contains(c)); // remove forbidden characters
if 240 < cbz_filepath.len() {cbz_filepath = cbz_filepath.graphemes(true).take(240).collect();} // limit title to 240 characters so filename does not exceed 255 characters
if library_split == 0 // no library split
{
cbz_filepath = format!("{}{id} {}.cbz", library_path.to_owned(), cbz_filepath);
cbz_filepath = format!("{}{id} {cbz_filepath}.cbz", library_path.to_owned());
}
if 0 < library_split // with library split
{
cbz_filepath = format!
(
"{}{}~{}/{} {}.cbz",
"{}{}~{}/{id} {cbz_filepath}.cbz",
library_path.to_owned(),
id.div_euclid(library_split) * library_split,
(id.div_euclid(library_split) + 1) * library_split - 1,
id,
cbz_filepath
);
}

Expand Down Expand Up @@ -169,7 +169,7 @@ impl Hentai
let f_clone: scaler::Formatter = f.clone();
let http_client_clone: reqwest::Client = http_client.clone();
let id_clone: u32 = self.id;
let image_filepath: String = format!("{}{}/{}.", self.library_path, self.id, self.images_filename.get(i).expect("Index out of bounds even though should have same size as images_url."));
let image_filepath: String = format!("{}{}/{}", self.library_path, self.id, self.images_filename.get(i).expect("Index out of bounds even though should have same size as images_url."));
let image_url_clone: String = self.images_url.get(i).expect("Index out of bounds even though checked before that it fits.").clone();
let num_pages_clone: u16 = self.num_pages;

Expand Down Expand Up @@ -252,7 +252,7 @@ impl Hentai
for (i, image_filename) in self.images_filename.iter().enumerate() // load images into zip
{
let mut image: Vec<u8> = Vec::new();
std::fs::File::open(format!("{}{}/{image_filename}.", self.library_path, self.id))?.read_to_end(&mut image)?; // open image file, read image into memory
std::fs::File::open(format!("{}{}/{image_filename}", self.library_path, self.id))?.read_to_end(&mut image)?; // open image file, read image into memory
zip_writer.start_file(image_filename, zip::write::SimpleFileOptions::default().unix_permissions(0o666))?; // create image file in zip with permissions "rw-rw-rw-"
zip_writer.write_all(&image)?; // write image into zip
log::debug!("Saved hentai {} image {} / {} in cbz.", self.id, f.format((i+1) as f64), f.format(self.num_pages));
Expand Down
11 changes: 10 additions & 1 deletion src/main_inner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,15 @@ pub async fn main_inner(config: Config) -> Result<()>
let hentai: Hentai; // hentai to download


if (i + 1).rem_euclid(1000) == 0 // reconnect to database every 1000 downloads to free up resources
{
db.close().await; // close database connection
log::info!("Disconnected from database at \"{}\".", config.DATABASE_URL);
tokio::time::sleep(std::time::Duration::from_secs(1)).await; // sleep for 1 s to give database time to free up resources, might be copium idk
db = connect_to_db(&config.DATABASE_URL).await?; // reconnect to database
}


match Hentai::new(*hentai_id, &db, &http_client, NHENTAI_HENTAI_SEARCH_URL, &config.LIBRARY_PATH, config.LIBRARY_SPLIT).await
{
Ok(o) => hentai = o, // hentai created successfully
Expand All @@ -93,7 +102,6 @@ pub async fn main_inner(config: Config) -> Result<()>
}
}


if let Err(e) = hentai.download(&http_client).await
{
match e
Expand All @@ -117,6 +125,7 @@ pub async fn main_inner(config: Config) -> Result<()>
log::error!("Deleting \"{}\" failed with: {e}", config.DOWNLOADME_FILEPATH);
}
db.close().await; // close database connection
log::info!("Disconnected from database at \"{}\".", config.DATABASE_URL);

log::info!("Sleeping for {}s...", f4.format(config.SLEEP_INTERVAL.unwrap_or_default() as f64));
tokio::time::sleep(std::time::Duration::from_secs(config.SLEEP_INTERVAL.unwrap_or_default())).await; // if in server mode: sleep for interval until next check
Expand Down

0 comments on commit bf1c82f

Please sign in to comment.