diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/api/icons.rs | 12 | ||||
-rw-r--r-- | src/db/mod.rs | 22 | ||||
-rw-r--r-- | src/main.rs | 41 |
3 files changed, 30 insertions, 45 deletions
diff --git a/src/api/icons.rs b/src/api/icons.rs index ebcace6d..921d48b9 100644 --- a/src/api/icons.rs +++ b/src/api/icons.rs @@ -19,7 +19,7 @@ use tokio::{ io::{AsyncReadExt, AsyncWriteExt}, }; -use html5gum::{Emitter, HtmlString, InfallibleTokenizer, Readable, StringReader, Tokenizer}; +use html5gum::{Emitter, HtmlString, Readable, StringReader, Tokenizer}; use crate::{ error::Error, @@ -261,11 +261,7 @@ impl Icon { } } -fn get_favicons_node( - dom: InfallibleTokenizer<StringReader<'_>, FaviconEmitter>, - icons: &mut Vec<Icon>, - url: &url::Url, -) { +fn get_favicons_node(dom: Tokenizer<StringReader<'_>, FaviconEmitter>, icons: &mut Vec<Icon>, url: &url::Url) { const TAG_LINK: &[u8] = b"link"; const TAG_BASE: &[u8] = b"base"; const TAG_HEAD: &[u8] = b"head"; @@ -274,7 +270,7 @@ fn get_favicons_node( let mut base_url = url.clone(); let mut icon_tags: Vec<Tag> = Vec::new(); - for token in dom { + for Ok(token) in dom { let tag_name: &[u8] = &token.tag.name; match tag_name { TAG_LINK => { @@ -401,7 +397,7 @@ async fn get_icon_url(domain: &str) -> Result<IconUrlResult, Error> { // 384KB should be more than enough for the HTML, though as we only really need the HTML header. let limited_reader = stream_to_bytes_limit(content, 384 * 1024).await?.to_vec(); - let dom = Tokenizer::new_with_emitter(limited_reader.to_reader(), FaviconEmitter::default()).infallible(); + let dom = Tokenizer::new_with_emitter(limited_reader.to_reader(), FaviconEmitter::default()); get_favicons_node(dom, &mut iconlist, &url); } else { // Add the default favicon.ico to the list with just the given domain diff --git a/src/db/mod.rs b/src/db/mod.rs index fe1ab79b..464be561 100644 --- a/src/db/mod.rs +++ b/src/db/mod.rs @@ -373,24 +373,18 @@ pub async fn backup_database(conn: &mut DbConn) -> Result<String, Error> { err!("PostgreSQL and MySQL/MariaDB do not support this backup feature"); } sqlite { - backup_sqlite_database(conn) + let db_url = CONFIG.database_url(); + let db_path = std::path::Path::new(&db_url).parent().unwrap(); + let backup_file = db_path + .join(format!("db_{}.sqlite3", chrono::Utc::now().format("%Y%m%d_%H%M%S"))) + .to_string_lossy() + .into_owned(); + diesel::sql_query(format!("VACUUM INTO '{backup_file}'")).execute(conn)?; + Ok(backup_file) } } } -#[cfg(sqlite)] -pub fn backup_sqlite_database(conn: &mut diesel::sqlite::SqliteConnection) -> Result<String, Error> { - use diesel::RunQueryDsl; - let db_url = CONFIG.database_url(); - let db_path = std::path::Path::new(&db_url).parent().unwrap(); - let backup_file = db_path - .join(format!("db_{}.sqlite3", chrono::Utc::now().format("%Y%m%d_%H%M%S"))) - .to_string_lossy() - .into_owned(); - diesel::sql_query(format!("VACUUM INTO '{backup_file}'")).execute(conn)?; - Ok(backup_file) -} - /// Get the SQL Server version pub async fn get_sql_server_version(conn: &mut DbConn) -> String { db_run! {@raw conn: diff --git a/src/main.rs b/src/main.rs index 7e180e2e..3a151cdf 100644 --- a/src/main.rs +++ b/src/main.rs @@ -67,7 +67,7 @@ pub use util::is_running_in_container; #[rocket::main] async fn main() -> Result<(), Error> { - parse_args(); + parse_args().await; launch_info(); let level = init_logging()?; @@ -115,7 +115,7 @@ PRESETS: m= t= p= pub const VERSION: Option<&str> = option_env!("VW_VERSION"); -fn parse_args() { +async fn parse_args() { let mut pargs = pico_args::Arguments::from_env(); let version = VERSION.unwrap_or("(Version info from Git not present)"); @@ -186,7 +186,7 @@ fn parse_args() { exit(1); } } else if command == "backup" { - match backup_sqlite() { + match backup_sqlite().await { Ok(f) => { println!("Backup to '{f}' was successful"); exit(0); @@ -201,25 +201,20 @@ fn parse_args() { } } -fn backup_sqlite() -> Result<String, Error> { - #[cfg(sqlite)] - { - use crate::db::{backup_sqlite_database, DbConnType}; - if DbConnType::from_url(&CONFIG.database_url()).map(|t| t == DbConnType::sqlite).unwrap_or(false) { - use diesel::Connection; - let url = CONFIG.database_url(); - - // Establish a connection to the sqlite database - let mut conn = diesel::sqlite::SqliteConnection::establish(&url)?; - let backup_file = backup_sqlite_database(&mut conn)?; - Ok(backup_file) - } else { - err_silent!("The database type is not SQLite. Backups only works for SQLite databases") - } - } - #[cfg(not(sqlite))] - { - err_silent!("The 'sqlite' feature is not enabled. Backups only works for SQLite databases") +async fn backup_sqlite() -> Result<String, Error> { + use crate::db::{backup_database, DbConnType}; + if DbConnType::from_url(&CONFIG.database_url()).map(|t| t == DbConnType::sqlite).unwrap_or(false) { + // Establish a connection to the sqlite database + let mut conn = db::DbPool::from_config() + .expect("SQLite database connection failed") + .get() + .await + .expect("Unable to get SQLite db pool"); + + let backup_file = backup_database(&mut conn).await?; + Ok(backup_file) + } else { + err_silent!("The database type is not SQLite. Backups only works for SQLite databases") } } @@ -610,7 +605,7 @@ async fn launch_rocket(pool: db::DbPool, extra_debug: bool) -> Result<(), Error> // If we need more signals to act upon, we might want to use select! here. // With only one item to listen for this is enough. let _ = signal_user1.recv().await; - match backup_sqlite() { + match backup_sqlite().await { Ok(f) => info!("Backup to '{f}' was successful"), Err(e) => error!("Backup failed. {e:?}"), } |