Cosmetic changes

This commit is contained in:
Antoine Gersant 2017-07-01 13:09:51 -07:00
parent 312be5e631
commit bdc33a29aa
5 changed files with 47 additions and 29 deletions

View file

@ -152,7 +152,8 @@ impl Handler for AuthHandler {
// Auth via Authorization header
if let Some(auth) = req.headers.get::<Authorization<Basic>>() {
if let Some(ref password) = auth.password {
auth_success = user::auth(self.db.deref(), auth.username.as_str(), password.as_str())?;
auth_success =
user::auth(self.db.deref(), auth.username.as_str(), password.as_str())?;
req.extensions
.insert::<SessionKey>(Session { username: auth.username.clone() });
}

View file

@ -11,7 +11,7 @@ use ddns::{DDNSConfigSource, DDNSConfig};
use errors::*;
use index;
use user::*;
use vfs::{MountPoint, Vfs, VFSSource};
use vfs::{MountPoint, VFS, VFSSource};
mod schema;
@ -138,9 +138,9 @@ impl DDNSConfigSource for DB {
}
impl VFSSource for DB {
fn get_vfs(&self) -> Result<Vfs> {
fn get_vfs(&self) -> Result<VFS> {
use self::mount_points::dsl::*;
let mut vfs = Vfs::new();
let mut vfs = VFS::new();
let connection = self.connection.lock().unwrap();
let connection = connection.deref();
let points: Vec<MountPoint> = mount_points
@ -180,4 +180,3 @@ fn test_migrations_down() {
db.migrate_down().unwrap();
db.migrate_up().unwrap();
}

View file

@ -15,7 +15,7 @@ use config::{MiscSettings, UserConfig};
use db::ConnectionSource;
use db::DB;
use db::{directories, misc_settings, songs};
use vfs::{Vfs, VFSSource};
use vfs::{VFS, VFSSource};
use errors::*;
use metadata;
@ -281,7 +281,9 @@ impl<'conn> IndexBuilder<'conn> {
}
}
fn clean<T>(db: &T) -> Result<()> where T: ConnectionSource + VFSSource {
fn clean<T>(db: &T) -> Result<()>
where T: ConnectionSource + VFSSource
{
let vfs = db.get_vfs()?;
{
@ -346,7 +348,9 @@ fn clean<T>(db: &T) -> Result<()> where T: ConnectionSource + VFSSource {
Ok(())
}
fn populate<T>(db: &T) -> Result<()> where T: ConnectionSource + VFSSource {
fn populate<T>(db: &T) -> Result<()>
where T: ConnectionSource + VFSSource
{
let vfs = db.get_vfs()?;
let mount_points = vfs.get_mount_points();
let connection = db.get_connection();
@ -368,7 +372,9 @@ fn populate<T>(db: &T) -> Result<()> where T: ConnectionSource + VFSSource {
Ok(())
}
pub fn update<T>(db: &T) -> Result<()> where T: ConnectionSource + VFSSource {
pub fn update<T>(db: &T) -> Result<()>
where T: ConnectionSource + VFSSource
{
let start = time::Instant::now();
println!("Beginning library index update");
clean(db)?;
@ -378,7 +384,9 @@ pub fn update<T>(db: &T) -> Result<()> where T: ConnectionSource + VFSSource {
Ok(())
}
pub fn update_loop<T>(db: &T) where T: ConnectionSource + VFSSource {
pub fn update_loop<T>(db: &T)
where T: ConnectionSource + VFSSource
{
loop {
if let Err(e) = update(db) {
println!("Error while updating index: {}", e);
@ -404,7 +412,7 @@ pub fn update_loop<T>(db: &T) where T: ConnectionSource + VFSSource {
}
}
fn virtualize_song(vfs: &Vfs, mut song: Song) -> Option<Song> {
fn virtualize_song(vfs: &VFS, mut song: Song) -> Option<Song> {
song.path = match vfs.real_to_virtual(Path::new(&song.path)) {
Ok(p) => p.to_string_lossy().into_owned(),
_ => return None,
@ -418,7 +426,7 @@ fn virtualize_song(vfs: &Vfs, mut song: Song) -> Option<Song> {
Some(song)
}
fn virtualize_directory(vfs: &Vfs, mut directory: Directory) -> Option<Directory> {
fn virtualize_directory(vfs: &VFS, mut directory: Directory) -> Option<Directory> {
directory.path = match vfs.real_to_virtual(Path::new(&directory.path)) {
Ok(p) => p.to_string_lossy().into_owned(),
_ => return None,
@ -432,7 +440,9 @@ fn virtualize_directory(vfs: &Vfs, mut directory: Directory) -> Option<Directory
Some(directory)
}
pub fn browse<T>(db: &T, virtual_path: &Path) -> Result<Vec<CollectionFile>> where T: ConnectionSource + VFSSource {
pub fn browse<T>(db: &T, virtual_path: &Path) -> Result<Vec<CollectionFile>>
where T: ConnectionSource + VFSSource
{
let mut output = Vec::new();
let vfs = db.get_vfs()?;
let connection = db.get_connection();
@ -448,8 +458,8 @@ pub fn browse<T>(db: &T, virtual_path: &Path) -> Result<Vec<CollectionFile>> whe
.into_iter()
.filter_map(|s| virtualize_directory(&vfs, s));
output.extend(virtual_directories
.into_iter()
.map(|d| CollectionFile::Directory(d)));
.into_iter()
.map(|d| CollectionFile::Directory(d)));
} else {
// Browse sub-directory
@ -478,7 +488,9 @@ pub fn browse<T>(db: &T, virtual_path: &Path) -> Result<Vec<CollectionFile>> whe
Ok(output)
}
pub fn flatten<T>(db: &T, virtual_path: &Path) -> Result<Vec<Song>> where T: ConnectionSource + VFSSource {
pub fn flatten<T>(db: &T, virtual_path: &Path) -> Result<Vec<Song>>
where T: ConnectionSource + VFSSource
{
use self::songs::dsl::*;
let vfs = db.get_vfs()?;
let connection = db.get_connection();
@ -493,7 +505,9 @@ pub fn flatten<T>(db: &T, virtual_path: &Path) -> Result<Vec<Song>> where T: Con
Ok(virtual_songs.collect::<Vec<_>>())
}
pub fn get_random_albums<T>(db: &T, count: i64) -> Result<Vec<Directory>> where T: ConnectionSource + VFSSource {
pub fn get_random_albums<T>(db: &T, count: i64) -> Result<Vec<Directory>>
where T: ConnectionSource + VFSSource
{
use self::directories::dsl::*;
let vfs = db.get_vfs()?;
let connection = db.get_connection();
@ -510,7 +524,9 @@ pub fn get_random_albums<T>(db: &T, count: i64) -> Result<Vec<Directory>> where
Ok(virtual_directories.collect::<Vec<_>>())
}
pub fn get_recent_albums<T>(db: &T, count: i64) -> Result<Vec<Directory>> where T: ConnectionSource + VFSSource {
pub fn get_recent_albums<T>(db: &T, count: i64) -> Result<Vec<Directory>>
where T: ConnectionSource + VFSSource
{
use self::directories::dsl::*;
let vfs = db.get_vfs()?;
let connection = db.get_connection();
@ -527,7 +543,7 @@ pub fn get_recent_albums<T>(db: &T, count: i64) -> Result<Vec<Directory>> where
Ok(virtual_directories.collect::<Vec<_>>())
}
fn _get_test_db(name: &str) -> DB {
fn _get_test_db(name: &str) -> DB {
let config_path = Path::new("test/config.toml");
let config = UserConfig::parse(&config_path).unwrap();
@ -661,4 +677,4 @@ fn test_recent() {
let results = get_recent_albums(&db, 2).unwrap();
assert_eq!(results.len(), 2);
assert!(results[0].date_added >= results[1].date_added);
}
}

View file

@ -61,7 +61,9 @@ impl NewUser {
}
}
pub fn auth<T>(db: &T, username: &str, password: &str) -> Result<bool> where T: ConnectionSource {
pub fn auth<T>(db: &T, username: &str, password: &str) -> Result<bool>
where T: ConnectionSource
{
use db::users::dsl::*;
let connection = db.get_connection();
let connection = connection.lock().unwrap();

View file

@ -6,7 +6,7 @@ use db::mount_points;
use errors::*;
pub trait VFSSource {
fn get_vfs(&self) -> Result<Vfs>;
fn get_vfs(&self) -> Result<VFS>;
}
#[derive(Debug, Deserialize, Insertable, Queryable)]
@ -16,13 +16,13 @@ pub struct MountPoint {
pub name: String,
}
pub struct Vfs {
pub struct VFS {
mount_points: HashMap<String, PathBuf>,
}
impl Vfs {
pub fn new() -> Vfs {
Vfs { mount_points: HashMap::new() }
impl VFS {
pub fn new() -> VFS {
VFS { mount_points: HashMap::new() }
}
pub fn mount(&mut self, real_path: &Path, name: &str) -> Result<()> {
@ -72,7 +72,7 @@ impl Vfs {
#[test]
fn test_virtual_to_real() {
let mut vfs = Vfs::new();
let mut vfs = VFS::new();
vfs.mount(Path::new("test_dir"), "root").unwrap();
let mut correct_path = PathBuf::new();
@ -91,7 +91,7 @@ fn test_virtual_to_real() {
#[test]
fn test_virtual_to_real_no_trail() {
let mut vfs = Vfs::new();
let mut vfs = VFS::new();
vfs.mount(Path::new("test_dir"), "root").unwrap();
let correct_path = Path::new("test_dir");
let found_path = vfs.virtual_to_real(Path::new("root")).unwrap();
@ -100,7 +100,7 @@ fn test_virtual_to_real_no_trail() {
#[test]
fn test_real_to_virtual() {
let mut vfs = Vfs::new();
let mut vfs = VFS::new();
vfs.mount(Path::new("test_dir"), "root").unwrap();
let mut correct_path = PathBuf::new();