diff --git a/src/api.rs b/src/api.rs index 95924cc..9cc43b7 100644 --- a/src/api.rs +++ b/src/api.rs @@ -54,12 +54,14 @@ pub fn get_handler(db: Arc) -> Result { let mut api_chain = Chain::new(api_handler); let auth_secret = db.deref().get_auth_secret()?; - let session_manager = ChaCha20Poly1305SessionManager::::from_password(auth_secret.as_bytes()); + let session_manager = + ChaCha20Poly1305SessionManager::::from_password(auth_secret.as_bytes()); let session_config = SessionConfig::default(); let session_middleware = SessionMiddleware::>::new(session_manager, session_config); + ChaCha20Poly1305SessionManager>::new(session_manager, + session_config); api_chain.link_around(session_middleware); Ok(api_chain) @@ -79,33 +81,28 @@ fn get_endpoints(db: Arc) -> Mount { let mut auth_api_mount = Mount::new(); { let db = db.clone(); - auth_api_mount.mount("/browse/", move |request: &mut Request| { - self::browse(request, db.deref()) - }); + auth_api_mount.mount("/browse/", + move |request: &mut Request| self::browse(request, db.deref())); } { let db = db.clone(); - auth_api_mount.mount("/flatten/", move |request: &mut Request| { - self::flatten(request, db.deref()) - }); + auth_api_mount.mount("/flatten/", + move |request: &mut Request| self::flatten(request, db.deref())); } { let db = db.clone(); - auth_api_mount.mount("/random/", move |request: &mut Request| { - self::random(request, db.deref()) - }); + auth_api_mount.mount("/random/", + move |request: &mut Request| self::random(request, db.deref())); } { let db = db.clone(); - auth_api_mount.mount("/recent/", move |request: &mut Request| { - self::recent(request, db.deref()) - }); + auth_api_mount.mount("/recent/", + move |request: &mut Request| self::recent(request, db.deref())); } { let db = db.clone(); - auth_api_mount.mount("/serve/", move |request: &mut Request| { - self::serve(request, db.deref()) - }); + auth_api_mount.mount("/serve/", + move |request: &mut Request| self::serve(request, db.deref())); } let mut auth_api_chain = Chain::new(auth_api_mount); @@ -152,8 +149,7 @@ impl Handler for AuthHandler { // Auth via Authorization header if let Some(auth) = req.headers.get::>() { if let Some(ref password) = auth.password { - auth_success = self.db - .auth(auth.username.as_str(), password.as_str())?; + auth_success = self.db.auth(auth.username.as_str(), password.as_str())?; req.extensions .insert::(Session { username: auth.username.clone() }); } diff --git a/src/config.rs b/src/config.rs index 82441d2..6092185 100644 --- a/src/config.rs +++ b/src/config.rs @@ -38,7 +38,7 @@ impl UserConfig { for mount_dir in mount_dirs { match clean_path_string(&mount_dir.source).to_str() { Some(p) => mount_dir.source = p.to_owned(), - _ => bail!("Bad mount directory path") + _ => bail!("Bad mount directory path"), } } } diff --git a/src/db/index.rs b/src/db/index.rs index 486dcf2..98053a6 100644 --- a/src/db/index.rs +++ b/src/db/index.rs @@ -5,7 +5,7 @@ use diesel::sqlite::SqliteConnection; use regex::Regex; use std::fs; use std::path::{Path, PathBuf}; -use std::sync::{Mutex}; +use std::sync::Mutex; use std::thread; use std::time; @@ -128,10 +128,7 @@ impl<'db> IndexBuilder<'db> { None } - fn populate_directory(&mut self, - parent: Option<&Path>, - path: &Path) - -> Result<()> { + fn populate_directory(&mut self, parent: Option<&Path>, path: &Path) -> Result<()> { // Find artwork let artwork = self.get_artwork(path); @@ -245,8 +242,7 @@ impl<'db> IndexBuilder<'db> { } } -pub struct Index { -} +pub struct Index {} impl Index { pub fn new() -> Index { @@ -364,11 +360,15 @@ impl Index { let connection = db.get_connection(); let connection = connection.lock().unwrap(); let connection = connection.deref(); - let settings: Result = misc_settings::table.get_result(connection).map_err(|e| e.into()); + let settings: Result = misc_settings::table + .get_result(connection) + .map_err(|e| e.into()); if let Err(ref e) = settings { println!("Could not retrieve index sleep duration: {}", e); } - sleep_duration = settings.map(|s| s.index_sleep_duration_seconds).unwrap_or(1800); + sleep_duration = settings + .map(|s| s.index_sleep_duration_seconds) + .unwrap_or(1800); } thread::sleep(time::Duration::from_secs(sleep_duration as u64)); } diff --git a/src/db/mod.rs b/src/db/mod.rs index 4c8047b..bbf2032 100644 --- a/src/db/mod.rs +++ b/src/db/mod.rs @@ -86,7 +86,9 @@ impl DB { let connection = connection.deref(); if let Some(ref mount_dirs) = config.mount_dirs { diesel::delete(mount_points::table).execute(connection)?; - diesel::insert(mount_dirs).into(mount_points::table).execute(connection)?; + diesel::insert(mount_dirs) + .into(mount_points::table) + .execute(connection)?; } if let Some(ref config_users) = config.users { @@ -94,20 +96,23 @@ impl DB { for config_user in config_users { let new_user = NewUser::new(&config_user.name, &config_user.password); println!("new user: {}", &config_user.name); - diesel::insert(&new_user).into(users::table).execute(connection)?; + diesel::insert(&new_user) + .into(users::table) + .execute(connection)?; } } if let Some(sleep_duration) = config.reindex_every_n_seconds { diesel::update(misc_settings::table) - .set(misc_settings::columns::index_sleep_duration_seconds.eq(sleep_duration as i32)) - .execute(connection)?; + .set(misc_settings::columns::index_sleep_duration_seconds.eq(sleep_duration as + i32)) + .execute(connection)?; } if let Some(ref album_art_pattern) = config.album_art_pattern { diesel::update(misc_settings::table) - .set(misc_settings::columns::index_album_art_pattern.eq(album_art_pattern)) - .execute(connection)?; + .set(misc_settings::columns::index_album_art_pattern.eq(album_art_pattern)) + .execute(connection)?; } Ok(()) @@ -116,7 +121,7 @@ impl DB { pub fn get_auth_secret(&self) -> Result { let connection = self.connection.lock().unwrap(); let connection = connection.deref(); - let misc : MiscSettings = misc_settings::table.get_result(connection)?; + let misc: MiscSettings = misc_settings::table.get_result(connection)?; Ok(misc.auth_secret.to_owned()) } @@ -129,7 +134,7 @@ impl DB { let mut vfs = Vfs::new(); let connection = self.connection.lock().unwrap(); let connection = connection.deref(); - let mount_points : Vec = mount_points::table.get_results(connection)?; + let mount_points: Vec = mount_points::table.get_results(connection)?; for mount_point in mount_points { vfs.mount(&Path::new(&mount_point.real_path), &mount_point.name)?; } @@ -268,7 +273,11 @@ impl DDNSConfigSource for DB { fn get_ddns_config(&self) -> Result { let connection = self.connection.lock().unwrap(); let connection = connection.deref(); - Ok(ddns_config::table.select((ddns_config::columns::host, ddns_config::columns::username, ddns_config::columns::password)).get_result(connection)?) + Ok(ddns_config::table + .select((ddns_config::columns::host, + ddns_config::columns::username, + ddns_config::columns::password)) + .get_result(connection)?) } } diff --git a/src/db/models.rs b/src/db/models.rs index 0b689bb..e7de97d 100644 --- a/src/db/models.rs +++ b/src/db/models.rs @@ -53,7 +53,12 @@ pub struct User { impl User { pub fn verify_password(&self, attempted_password: &str) -> bool { - pbkdf2::verify(DIGEST_ALG, HASH_ITERATIONS, &self.password_salt, attempted_password.as_bytes(), &self.password_hash).is_ok() + pbkdf2::verify(DIGEST_ALG, + HASH_ITERATIONS, + &self.password_salt, + attempted_password.as_bytes(), + &self.password_hash) + .is_ok() } } @@ -83,7 +88,11 @@ impl NewUser { pub fn hash_password(salt: &Vec, password: &str) -> Vec { let mut hash: PasswordHash = [0; CREDENTIAL_LEN]; - pbkdf2::derive(DIGEST_ALG, HASH_ITERATIONS, salt, password.as_bytes(), &mut hash); + pbkdf2::derive(DIGEST_ALG, + HASH_ITERATIONS, + salt, + password.as_bytes(), + &mut hash); hash.to_vec() } } @@ -92,7 +101,7 @@ impl NewUser { // VFS #[derive(Debug, Queryable)] pub struct MountPoint { - id: i32, + id: i32, pub real_path: String, pub name: String, } @@ -113,5 +122,3 @@ pub struct MiscSettings { pub index_sleep_duration_seconds: i32, pub index_album_art_pattern: String, } - - diff --git a/src/ddns.rs b/src/ddns.rs index 61922d1..6ad6f34 100644 --- a/src/ddns.rs +++ b/src/ddns.rs @@ -46,7 +46,9 @@ impl From for DDNSError { const DDNS_UPDATE_URL: &'static str = "https://ydns.io/api/v1/update/"; -fn update_my_ip(config_source: &T) -> Result<(), DDNSError> where T: DDNSConfigSource { +fn update_my_ip(config_source: &T) -> Result<(), DDNSError> + where T: DDNSConfigSource +{ let config = config_source.get_ddns_config()?; if config.host.len() == 0 || config.username.len() == 0 { println!("Skipping DDNS update because credentials are missing"); @@ -69,7 +71,9 @@ fn update_my_ip(config_source: &T) -> Result<(), DDNSError> where T: DDNSConf Ok(()) } -pub fn run(config_source: &T) where T: DDNSConfigSource { +pub fn run(config_source: &T) + where T: DDNSConfigSource +{ loop { if let Err(e) = update_my_ip(config_source) { println!("Dynamic DNS update error: {:?}", e); diff --git a/src/main.rs b/src/main.rs index 19f63c9..fbbed9b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -123,9 +123,9 @@ fn run() -> Result<()> { // Begin indexing let db_ref = db.clone(); std::thread::spawn(move || { - let db = db_ref.deref(); - db.get_index().update_loop(db); - }); + let db = db_ref.deref(); + db.get_index().update_loop(db); + }); // Mount API println!("Mounting API"); diff --git a/src/vfs.rs b/src/vfs.rs index d318a51..70b7dc4 100644 --- a/src/vfs.rs +++ b/src/vfs.rs @@ -15,7 +15,8 @@ impl Vfs { } pub fn mount(&mut self, real_path: &Path, name: &str) -> Result<()> { - self.mount_points.insert(name.to_owned(), real_path.to_path_buf()); + self.mount_points + .insert(name.to_owned(), real_path.to_path_buf()); Ok(()) }