Autoformat
This commit is contained in:
parent
d87ec09c38
commit
e41a5273a8
8 changed files with 66 additions and 49 deletions
34
src/api.rs
34
src/api.rs
|
@ -54,12 +54,14 @@ pub fn get_handler(db: Arc<DB>) -> Result<Chain> {
|
||||||
let mut api_chain = Chain::new(api_handler);
|
let mut api_chain = Chain::new(api_handler);
|
||||||
|
|
||||||
let auth_secret = db.deref().get_auth_secret()?;
|
let auth_secret = db.deref().get_auth_secret()?;
|
||||||
let session_manager = ChaCha20Poly1305SessionManager::<Session>::from_password(auth_secret.as_bytes());
|
let session_manager =
|
||||||
|
ChaCha20Poly1305SessionManager::<Session>::from_password(auth_secret.as_bytes());
|
||||||
let session_config = SessionConfig::default();
|
let session_config = SessionConfig::default();
|
||||||
let session_middleware =
|
let session_middleware =
|
||||||
SessionMiddleware::<Session,
|
SessionMiddleware::<Session,
|
||||||
SessionKey,
|
SessionKey,
|
||||||
ChaCha20Poly1305SessionManager<Session>>::new(session_manager, session_config);
|
ChaCha20Poly1305SessionManager<Session>>::new(session_manager,
|
||||||
|
session_config);
|
||||||
api_chain.link_around(session_middleware);
|
api_chain.link_around(session_middleware);
|
||||||
|
|
||||||
Ok(api_chain)
|
Ok(api_chain)
|
||||||
|
@ -79,33 +81,28 @@ fn get_endpoints(db: Arc<DB>) -> Mount {
|
||||||
let mut auth_api_mount = Mount::new();
|
let mut auth_api_mount = Mount::new();
|
||||||
{
|
{
|
||||||
let db = db.clone();
|
let db = db.clone();
|
||||||
auth_api_mount.mount("/browse/", move |request: &mut Request| {
|
auth_api_mount.mount("/browse/",
|
||||||
self::browse(request, db.deref())
|
move |request: &mut Request| self::browse(request, db.deref()));
|
||||||
});
|
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
let db = db.clone();
|
let db = db.clone();
|
||||||
auth_api_mount.mount("/flatten/", move |request: &mut Request| {
|
auth_api_mount.mount("/flatten/",
|
||||||
self::flatten(request, db.deref())
|
move |request: &mut Request| self::flatten(request, db.deref()));
|
||||||
});
|
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
let db = db.clone();
|
let db = db.clone();
|
||||||
auth_api_mount.mount("/random/", move |request: &mut Request| {
|
auth_api_mount.mount("/random/",
|
||||||
self::random(request, db.deref())
|
move |request: &mut Request| self::random(request, db.deref()));
|
||||||
});
|
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
let db = db.clone();
|
let db = db.clone();
|
||||||
auth_api_mount.mount("/recent/", move |request: &mut Request| {
|
auth_api_mount.mount("/recent/",
|
||||||
self::recent(request, db.deref())
|
move |request: &mut Request| self::recent(request, db.deref()));
|
||||||
});
|
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
let db = db.clone();
|
let db = db.clone();
|
||||||
auth_api_mount.mount("/serve/", move |request: &mut Request| {
|
auth_api_mount.mount("/serve/",
|
||||||
self::serve(request, db.deref())
|
move |request: &mut Request| self::serve(request, db.deref()));
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut auth_api_chain = Chain::new(auth_api_mount);
|
let mut auth_api_chain = Chain::new(auth_api_mount);
|
||||||
|
@ -152,8 +149,7 @@ impl Handler for AuthHandler {
|
||||||
// Auth via Authorization header
|
// Auth via Authorization header
|
||||||
if let Some(auth) = req.headers.get::<Authorization<Basic>>() {
|
if let Some(auth) = req.headers.get::<Authorization<Basic>>() {
|
||||||
if let Some(ref password) = auth.password {
|
if let Some(ref password) = auth.password {
|
||||||
auth_success = self.db
|
auth_success = self.db.auth(auth.username.as_str(), password.as_str())?;
|
||||||
.auth(auth.username.as_str(), password.as_str())?;
|
|
||||||
req.extensions
|
req.extensions
|
||||||
.insert::<SessionKey>(Session { username: auth.username.clone() });
|
.insert::<SessionKey>(Session { username: auth.username.clone() });
|
||||||
}
|
}
|
||||||
|
|
|
@ -38,7 +38,7 @@ impl UserConfig {
|
||||||
for mount_dir in mount_dirs {
|
for mount_dir in mount_dirs {
|
||||||
match clean_path_string(&mount_dir.source).to_str() {
|
match clean_path_string(&mount_dir.source).to_str() {
|
||||||
Some(p) => mount_dir.source = p.to_owned(),
|
Some(p) => mount_dir.source = p.to_owned(),
|
||||||
_ => bail!("Bad mount directory path")
|
_ => bail!("Bad mount directory path"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,7 +5,7 @@ use diesel::sqlite::SqliteConnection;
|
||||||
use regex::Regex;
|
use regex::Regex;
|
||||||
use std::fs;
|
use std::fs;
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
use std::sync::{Mutex};
|
use std::sync::Mutex;
|
||||||
use std::thread;
|
use std::thread;
|
||||||
use std::time;
|
use std::time;
|
||||||
|
|
||||||
|
@ -128,10 +128,7 @@ impl<'db> IndexBuilder<'db> {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
|
||||||
fn populate_directory(&mut self,
|
fn populate_directory(&mut self, parent: Option<&Path>, path: &Path) -> Result<()> {
|
||||||
parent: Option<&Path>,
|
|
||||||
path: &Path)
|
|
||||||
-> Result<()> {
|
|
||||||
|
|
||||||
// Find artwork
|
// Find artwork
|
||||||
let artwork = self.get_artwork(path);
|
let artwork = self.get_artwork(path);
|
||||||
|
@ -245,8 +242,7 @@ impl<'db> IndexBuilder<'db> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Index {
|
pub struct Index {}
|
||||||
}
|
|
||||||
|
|
||||||
impl Index {
|
impl Index {
|
||||||
pub fn new() -> Index {
|
pub fn new() -> Index {
|
||||||
|
@ -364,11 +360,15 @@ impl Index {
|
||||||
let connection = db.get_connection();
|
let connection = db.get_connection();
|
||||||
let connection = connection.lock().unwrap();
|
let connection = connection.lock().unwrap();
|
||||||
let connection = connection.deref();
|
let connection = connection.deref();
|
||||||
let settings: Result<MiscSettings> = misc_settings::table.get_result(connection).map_err(|e| e.into());
|
let settings: Result<MiscSettings> = misc_settings::table
|
||||||
|
.get_result(connection)
|
||||||
|
.map_err(|e| e.into());
|
||||||
if let Err(ref e) = settings {
|
if let Err(ref e) = settings {
|
||||||
println!("Could not retrieve index sleep duration: {}", e);
|
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));
|
thread::sleep(time::Duration::from_secs(sleep_duration as u64));
|
||||||
}
|
}
|
||||||
|
|
|
@ -86,7 +86,9 @@ impl DB {
|
||||||
let connection = connection.deref();
|
let connection = connection.deref();
|
||||||
if let Some(ref mount_dirs) = config.mount_dirs {
|
if let Some(ref mount_dirs) = config.mount_dirs {
|
||||||
diesel::delete(mount_points::table).execute(connection)?;
|
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 {
|
if let Some(ref config_users) = config.users {
|
||||||
|
@ -94,20 +96,23 @@ impl DB {
|
||||||
for config_user in config_users {
|
for config_user in config_users {
|
||||||
let new_user = NewUser::new(&config_user.name, &config_user.password);
|
let new_user = NewUser::new(&config_user.name, &config_user.password);
|
||||||
println!("new user: {}", &config_user.name);
|
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 {
|
if let Some(sleep_duration) = config.reindex_every_n_seconds {
|
||||||
diesel::update(misc_settings::table)
|
diesel::update(misc_settings::table)
|
||||||
.set(misc_settings::columns::index_sleep_duration_seconds.eq(sleep_duration as i32))
|
.set(misc_settings::columns::index_sleep_duration_seconds.eq(sleep_duration as
|
||||||
.execute(connection)?;
|
i32))
|
||||||
|
.execute(connection)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(ref album_art_pattern) = config.album_art_pattern {
|
if let Some(ref album_art_pattern) = config.album_art_pattern {
|
||||||
diesel::update(misc_settings::table)
|
diesel::update(misc_settings::table)
|
||||||
.set(misc_settings::columns::index_album_art_pattern.eq(album_art_pattern))
|
.set(misc_settings::columns::index_album_art_pattern.eq(album_art_pattern))
|
||||||
.execute(connection)?;
|
.execute(connection)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
@ -116,7 +121,7 @@ impl DB {
|
||||||
pub fn get_auth_secret(&self) -> Result<String> {
|
pub fn get_auth_secret(&self) -> Result<String> {
|
||||||
let connection = self.connection.lock().unwrap();
|
let connection = self.connection.lock().unwrap();
|
||||||
let connection = connection.deref();
|
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())
|
Ok(misc.auth_secret.to_owned())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -129,7 +134,7 @@ impl DB {
|
||||||
let mut vfs = Vfs::new();
|
let mut vfs = Vfs::new();
|
||||||
let connection = self.connection.lock().unwrap();
|
let connection = self.connection.lock().unwrap();
|
||||||
let connection = connection.deref();
|
let connection = connection.deref();
|
||||||
let mount_points : Vec<MountPoint> = mount_points::table.get_results(connection)?;
|
let mount_points: Vec<MountPoint> = mount_points::table.get_results(connection)?;
|
||||||
for mount_point in mount_points {
|
for mount_point in mount_points {
|
||||||
vfs.mount(&Path::new(&mount_point.real_path), &mount_point.name)?;
|
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<DDNSConfig> {
|
fn get_ddns_config(&self) -> Result<DDNSConfig> {
|
||||||
let connection = self.connection.lock().unwrap();
|
let connection = self.connection.lock().unwrap();
|
||||||
let connection = connection.deref();
|
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)?)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -53,7 +53,12 @@ pub struct User {
|
||||||
|
|
||||||
impl User {
|
impl User {
|
||||||
pub fn verify_password(&self, attempted_password: &str) -> bool {
|
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<u8>, password: &str) -> Vec<u8> {
|
pub fn hash_password(salt: &Vec<u8>, password: &str) -> Vec<u8> {
|
||||||
let mut hash: PasswordHash = [0; CREDENTIAL_LEN];
|
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()
|
hash.to_vec()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -92,7 +101,7 @@ impl NewUser {
|
||||||
// VFS
|
// VFS
|
||||||
#[derive(Debug, Queryable)]
|
#[derive(Debug, Queryable)]
|
||||||
pub struct MountPoint {
|
pub struct MountPoint {
|
||||||
id: i32,
|
id: i32,
|
||||||
pub real_path: String,
|
pub real_path: String,
|
||||||
pub name: String,
|
pub name: String,
|
||||||
}
|
}
|
||||||
|
@ -113,5 +122,3 @@ pub struct MiscSettings {
|
||||||
pub index_sleep_duration_seconds: i32,
|
pub index_sleep_duration_seconds: i32,
|
||||||
pub index_album_art_pattern: String,
|
pub index_album_art_pattern: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -46,7 +46,9 @@ impl From<reqwest::Error> for DDNSError {
|
||||||
const DDNS_UPDATE_URL: &'static str = "https://ydns.io/api/v1/update/";
|
const DDNS_UPDATE_URL: &'static str = "https://ydns.io/api/v1/update/";
|
||||||
|
|
||||||
|
|
||||||
fn update_my_ip<T>(config_source: &T) -> Result<(), DDNSError> where T: DDNSConfigSource {
|
fn update_my_ip<T>(config_source: &T) -> Result<(), DDNSError>
|
||||||
|
where T: DDNSConfigSource
|
||||||
|
{
|
||||||
let config = config_source.get_ddns_config()?;
|
let config = config_source.get_ddns_config()?;
|
||||||
if config.host.len() == 0 || config.username.len() == 0 {
|
if config.host.len() == 0 || config.username.len() == 0 {
|
||||||
println!("Skipping DDNS update because credentials are missing");
|
println!("Skipping DDNS update because credentials are missing");
|
||||||
|
@ -69,7 +71,9 @@ fn update_my_ip<T>(config_source: &T) -> Result<(), DDNSError> where T: DDNSConf
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn run<T>(config_source: &T) where T: DDNSConfigSource {
|
pub fn run<T>(config_source: &T)
|
||||||
|
where T: DDNSConfigSource
|
||||||
|
{
|
||||||
loop {
|
loop {
|
||||||
if let Err(e) = update_my_ip(config_source) {
|
if let Err(e) = update_my_ip(config_source) {
|
||||||
println!("Dynamic DNS update error: {:?}", e);
|
println!("Dynamic DNS update error: {:?}", e);
|
||||||
|
|
|
@ -123,9 +123,9 @@ fn run() -> Result<()> {
|
||||||
// Begin indexing
|
// Begin indexing
|
||||||
let db_ref = db.clone();
|
let db_ref = db.clone();
|
||||||
std::thread::spawn(move || {
|
std::thread::spawn(move || {
|
||||||
let db = db_ref.deref();
|
let db = db_ref.deref();
|
||||||
db.get_index().update_loop(db);
|
db.get_index().update_loop(db);
|
||||||
});
|
});
|
||||||
|
|
||||||
// Mount API
|
// Mount API
|
||||||
println!("Mounting API");
|
println!("Mounting API");
|
||||||
|
|
|
@ -15,7 +15,8 @@ impl Vfs {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn mount(&mut self, real_path: &Path, name: &str) -> Result<()> {
|
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(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue