Skip to content

Commit

Permalink
added try to automatically create database directory
Browse files Browse the repository at this point in the history
  • Loading branch information
9FS committed Sep 14, 2024
1 parent dae7d4e commit 6d3be89
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 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.1.2"
version = "3.1.3"

[dependencies]
chrono = { version = "^0.4.0", features = ["serde"] }
Expand Down
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.1.2"
image: "ghcr.io/9-fs/nhentai_archivist:3.1.3"
environment:
HOST_OS: "Unraid"
PGID: 100
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ I'm happy about anyone who finds my software useful and feedback is also always
## Installation

1. Execute the program once to create a default `./config/.env`. This means that in the directory of the executable, there should now be a directory called "config" containing a file called ".env". You might need to enable seeing hidden files in the file explorer.
1. Confirm the database directory in `DATABASE_URL` exists already, which is `./db/` by default. It is not created automatically because the URL could point to a remote directory, though I am open to suggestions here. The database file will and should be created automatically.
1. Confirm the database directory at `DATABASE_URL` exists, which is `./db/` by default. It is possible that it is not created automatically because the URL could point to a remote directory. The database file will and should be created automatically.
1. If you have problems with nhentai's bot protection (error 403), set `CF_CLEARANCE`, `CSRFTOKEN`, and `USER_AGENT`. Though I recommend setting the latter 2 in any case.

### Mozilla Firefox
Expand Down
2 changes: 1 addition & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ impl Default for Config
{
CF_CLEARANCE: "".to_string(),
CSRFTOKEN: "".to_string(),
DATABASE_URL: "sqlite://./db/db.sqlite".to_owned(),
DATABASE_URL: "./db/db.sqlite".to_owned(),
DEBUG: None, // no entry in default config, defaults to false
DOWNLOADME_FILEPATH: "./config/downloadme.txt".to_owned(),
LIBRARY_PATH: "./hentai/".to_string(),
Expand Down
17 changes: 17 additions & 0 deletions src/connect_to_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,23 @@ pub async fn connect_to_db(database_url: &str) -> Result<sqlx::sqlite::SqlitePoo

if !sqlx::sqlite::Sqlite::database_exists(database_url).await? // if database does not exist
{
match std::path::Path::new(database_url).parent()
{
Some(parent) =>
{
#[cfg(target_family = "unix")]
if let Err(e) = tokio::fs::DirBuilder::new().recursive(true).mode(0o777).create(parent).await // create all parent directories with permissions "drwxrwxrwx"
{
log::warn!("Creating parent directories for new database at \"{database_url}\" failed with {e}.\nThis could be expected behaviour, usually if this is a remote pointing URL and not a local filepath. In that case create the parent directories manually.");
}
#[cfg(not(target_family = "unix"))]
if let Err(e) = tokio::fs::DirBuilder::new().recursive(true).create(parent).await // create all parent directories
{
log::warn!("Creating parent directories for new database at \"{database_url}\" failed with {e}.\nThis could be expected behaviour, usually if this is a remote pointing URL and not a local filepath. In that case create the parent directories manually.");
}
}
None => log::warn!("Creating parent directories for new database at \"{database_url}\", because the directory part could not be parsed.\nThis could be expected behaviour, usually if this is a remote pointing URL and not a local filepath. In that case create the parent directories manually."),
}
sqlx::sqlite::Sqlite::create_database(database_url).await?; // create new database
log::info!("Created new database at \"{}\".", database_url);

Expand Down

0 comments on commit 6d3be89

Please sign in to comment.