Applied rustfmt
This commit is contained in:
parent
56bb1d4e23
commit
d073e02549
8 changed files with 155 additions and 112 deletions
12
src/api.rs
12
src/api.rs
|
@ -74,7 +74,7 @@ pub fn get_api_handler(collection: Arc<Collection>) -> Mount {
|
|||
}
|
||||
|
||||
let mut auth_api_chain = Chain::new(auth_api_mount);
|
||||
let auth = AuthRequirement { collection:collection.clone() };
|
||||
let auth = AuthRequirement { collection: collection.clone() };
|
||||
auth_api_chain.link_around(auth);
|
||||
|
||||
api_handler.mount("/", auth_api_chain);
|
||||
|
@ -83,7 +83,10 @@ pub fn get_api_handler(collection: Arc<Collection>) -> Mount {
|
|||
}
|
||||
|
||||
fn path_from_request(request: &Request) -> Result<PathBuf> {
|
||||
let path_string = request.url.path().join(&::std::path::MAIN_SEPARATOR.to_string());
|
||||
let path_string = request
|
||||
.url
|
||||
.path()
|
||||
.join(&::std::path::MAIN_SEPARATOR.to_string());
|
||||
let decoded_path = percent_decode(path_string.as_bytes()).decode_utf8()?;
|
||||
Ok(PathBuf::from(decoded_path.deref()))
|
||||
}
|
||||
|
@ -96,7 +99,7 @@ impl AroundMiddleware for AuthRequirement {
|
|||
fn around(self, handler: Box<Handler>) -> Box<Handler> {
|
||||
Box::new(AuthHandler {
|
||||
collection: self.collection,
|
||||
handler: handler
|
||||
handler: handler,
|
||||
}) as Box<Handler>
|
||||
}
|
||||
}
|
||||
|
@ -121,7 +124,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 = self.collection.auth(auth.username.as_str(), password.as_str());
|
||||
auth_success = self.collection
|
||||
.auth(auth.username.as_str(), password.as_str());
|
||||
username = Some(auth.username.clone());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -45,7 +45,9 @@ impl Collection {
|
|||
}
|
||||
|
||||
pub fn auth(&self, username: &str, password: &str) -> bool {
|
||||
self.users.iter().any(|u| u.name == username && u.password == password)
|
||||
self.users
|
||||
.iter()
|
||||
.any(|u| u.name == username && u.password == password)
|
||||
}
|
||||
|
||||
pub fn browse(&self, virtual_path: &Path) -> Result<Vec<CollectionFile>> {
|
||||
|
|
|
@ -52,7 +52,8 @@ impl Config {
|
|||
let mut config_file_content = String::new();
|
||||
config_file.read_to_string(&mut config_file_content)?;
|
||||
let parsed_config = toml::Parser::new(config_file_content.as_str()).parse();
|
||||
let parsed_config = parsed_config.ok_or("Could not parse config as valid TOML")?;
|
||||
let parsed_config = parsed_config
|
||||
.ok_or("Could not parse config as valid TOML")?;
|
||||
|
||||
let mut config = Config {
|
||||
secret: String::new(),
|
||||
|
@ -77,7 +78,8 @@ impl Config {
|
|||
}
|
||||
|
||||
fn parse_secret(&mut self, source: &toml::Table) -> Result<()> {
|
||||
self.secret = source.get(CONFIG_SECRET)
|
||||
self.secret = source
|
||||
.get(CONFIG_SECRET)
|
||||
.and_then(|s| s.as_str())
|
||||
.map(|s| s.to_owned())
|
||||
.ok_or("Could not parse config secret")?;
|
||||
|
@ -173,8 +175,9 @@ impl Config {
|
|||
_ => bail!("Could not parse DDNS settings table"),
|
||||
};
|
||||
|
||||
let host =
|
||||
ddns.get(CONFIG_DDNS_HOST).and_then(|n| n.as_str()).ok_or("Could not parse DDNS host")?;
|
||||
let host = ddns.get(CONFIG_DDNS_HOST)
|
||||
.and_then(|n| n.as_str())
|
||||
.ok_or("Could not parse DDNS host")?;
|
||||
let username = ddns.get(CONFIG_DDNS_USERNAME)
|
||||
.and_then(|n| n.as_str())
|
||||
.ok_or("Could not parse DDNS username")?;
|
||||
|
|
|
@ -53,7 +53,10 @@ fn update_my_ip(ip: &String, config: &DDNSConfig) -> Result<(), DDNSError> {
|
|||
password: Some(config.password.to_owned()),
|
||||
});
|
||||
|
||||
let res = client.get(full_url.as_str()).header(auth_header).send()?;
|
||||
let res = client
|
||||
.get(full_url.as_str())
|
||||
.header(auth_header)
|
||||
.send()?;
|
||||
match res.status {
|
||||
hyper::status::StatusCode::Ok => Ok(()),
|
||||
s => Err(DDNSError::UpdateError(s)),
|
||||
|
|
91
src/index.rs
91
src/index.rs
|
@ -101,11 +101,9 @@ impl<'db> IndexBuilder<'db> {
|
|||
Ok(IndexBuilder {
|
||||
queue: queue,
|
||||
db: db,
|
||||
insert_directory:
|
||||
db.prepare("INSERT OR REPLACE INTO directories (path, parent, artwork, year, \
|
||||
insert_directory: db.prepare("INSERT OR REPLACE INTO directories (path, parent, artwork, year, \
|
||||
artist, album) VALUES (?, ?, ?, ?, ?, ?)")?,
|
||||
insert_song:
|
||||
db.prepare("INSERT OR REPLACE INTO songs (path, parent, disc_number, track_number, \
|
||||
insert_song: db.prepare("INSERT OR REPLACE INTO songs (path, parent, disc_number, track_number, \
|
||||
title, year, album_artist, artist, album, artwork) VALUES (?, ?, ?, ?, \
|
||||
?, ?, ?, ?, ?, ?)")?,
|
||||
})
|
||||
|
@ -130,11 +128,14 @@ impl<'db> IndexBuilder<'db> {
|
|||
CollectionFile::Directory(directory) => {
|
||||
let parent = IndexBuilder::get_parent(directory.path.as_str());
|
||||
self.insert_directory.reset()?;
|
||||
self.insert_directory.bind(1, &Value::String(directory.path))?;
|
||||
self.insert_directory.bind(2, &string_option_to_value(parent))?;
|
||||
self.insert_directory
|
||||
.bind(1, &Value::String(directory.path))?;
|
||||
self.insert_directory
|
||||
.bind(2, &string_option_to_value(parent))?;
|
||||
self.insert_directory
|
||||
.bind(3, &string_option_to_value(directory.artwork))?;
|
||||
self.insert_directory.bind(4, &i32_option_to_value(directory.year))?;
|
||||
self.insert_directory
|
||||
.bind(4, &i32_option_to_value(directory.year))?;
|
||||
self.insert_directory
|
||||
.bind(5, &string_option_to_value(directory.artist))?;
|
||||
self.insert_directory
|
||||
|
@ -147,15 +148,24 @@ impl<'db> IndexBuilder<'db> {
|
|||
let parent = IndexBuilder::get_parent(song.path.as_str());
|
||||
self.insert_song.reset()?;
|
||||
self.insert_song.bind(1, &Value::String(song.path))?;
|
||||
self.insert_song.bind(2, &string_option_to_value(parent))?;
|
||||
self.insert_song.bind(3, &u32_option_to_value(song.disc_number))?;
|
||||
self.insert_song.bind(4, &u32_option_to_value(song.track_number))?;
|
||||
self.insert_song.bind(5, &string_option_to_value(song.title))?;
|
||||
self.insert_song.bind(6, &i32_option_to_value(song.year))?;
|
||||
self.insert_song.bind(7, &string_option_to_value(song.album_artist))?;
|
||||
self.insert_song.bind(8, &string_option_to_value(song.artist))?;
|
||||
self.insert_song.bind(9, &string_option_to_value(song.album))?;
|
||||
self.insert_song.bind(10, &string_option_to_value(song.artwork))?;
|
||||
self.insert_song
|
||||
.bind(2, &string_option_to_value(parent))?;
|
||||
self.insert_song
|
||||
.bind(3, &u32_option_to_value(song.disc_number))?;
|
||||
self.insert_song
|
||||
.bind(4, &u32_option_to_value(song.track_number))?;
|
||||
self.insert_song
|
||||
.bind(5, &string_option_to_value(song.title))?;
|
||||
self.insert_song
|
||||
.bind(6, &i32_option_to_value(song.year))?;
|
||||
self.insert_song
|
||||
.bind(7, &string_option_to_value(song.album_artist))?;
|
||||
self.insert_song
|
||||
.bind(8, &string_option_to_value(song.artist))?;
|
||||
self.insert_song
|
||||
.bind(9, &string_option_to_value(song.album))?;
|
||||
self.insert_song
|
||||
.bind(10, &string_option_to_value(song.artwork))?;
|
||||
self.insert_song.next()?;
|
||||
}
|
||||
|
||||
|
@ -458,7 +468,11 @@ impl Index {
|
|||
let mut artwork = None;
|
||||
if let Some(artwork_path) = artwork_path {
|
||||
artwork = match self.vfs.real_to_virtual(artwork_path) {
|
||||
Ok(p) => Some(p.to_str().ok_or("Invalid song artwork path")?.to_owned()),
|
||||
Ok(p) => {
|
||||
Some(p.to_str()
|
||||
.ok_or("Invalid song artwork path")?
|
||||
.to_owned())
|
||||
}
|
||||
_ => None,
|
||||
};
|
||||
}
|
||||
|
@ -500,7 +514,11 @@ impl Index {
|
|||
let mut artwork = None;
|
||||
if let Some(artwork_path) = artwork_path {
|
||||
artwork = match self.vfs.real_to_virtual(artwork_path) {
|
||||
Ok(p) => Some(p.to_str().ok_or("Invalid directory artwork path")?.to_owned()),
|
||||
Ok(p) => {
|
||||
Some(p.to_str()
|
||||
.ok_or("Invalid directory artwork path")?
|
||||
.to_owned())
|
||||
}
|
||||
_ => None,
|
||||
};
|
||||
}
|
||||
|
@ -523,13 +541,16 @@ impl Index {
|
|||
let db = self.connect()?;
|
||||
|
||||
let path_string = real_path.to_string_lossy();
|
||||
let mut select =
|
||||
db.prepare("SELECT path, artwork, year, artist, album FROM directories WHERE \
|
||||
let mut select = db.prepare("SELECT path, artwork, year, artist, album FROM directories WHERE \
|
||||
parent = ? ORDER BY path COLLATE NOCASE ASC")?;
|
||||
select.bind(1, &Value::String(path_string.deref().to_owned()))?;
|
||||
select
|
||||
.bind(1, &Value::String(path_string.deref().to_owned()))?;
|
||||
|
||||
let output = self.select_directories(&mut select)?;
|
||||
let output = output.into_iter().map(|d| CollectionFile::Directory(d)).collect();
|
||||
let output = output
|
||||
.into_iter()
|
||||
.map(|d| CollectionFile::Directory(d))
|
||||
.collect();
|
||||
Ok(output)
|
||||
}
|
||||
|
||||
|
@ -537,12 +558,15 @@ impl Index {
|
|||
fn browse_songs(&self, real_path: &Path) -> Result<Vec<CollectionFile>> {
|
||||
let db = self.connect()?;
|
||||
let path_string = real_path.to_string_lossy();
|
||||
let mut select =
|
||||
db.prepare("SELECT path, disc_number, track_number, title, year, album_artist, \
|
||||
let mut select = db.prepare("SELECT path, disc_number, track_number, title, year, album_artist, \
|
||||
artist, album, artwork FROM songs WHERE parent = ? ORDER BY track_number, path \
|
||||
COLLATE NOCASE ASC")?;
|
||||
select.bind(1, &Value::String(path_string.deref().to_owned()))?;
|
||||
Ok(self.select_songs(&mut select)?.into_iter().map(|s| CollectionFile::Song(s)).collect())
|
||||
select
|
||||
.bind(1, &Value::String(path_string.deref().to_owned()))?;
|
||||
Ok(self.select_songs(&mut select)?
|
||||
.into_iter()
|
||||
.map(|s| CollectionFile::Song(s))
|
||||
.collect())
|
||||
}
|
||||
|
||||
pub fn browse(&self, virtual_path: &Path) -> Result<Vec<CollectionFile>> {
|
||||
|
@ -578,18 +602,17 @@ impl Index {
|
|||
let db = self.connect()?;
|
||||
let real_path = self.vfs.virtual_to_real(virtual_path)?;
|
||||
let path_string = real_path.to_string_lossy().into_owned() + "%";
|
||||
let mut select =
|
||||
db.prepare("SELECT path, disc_number, track_number, title, year, album_artist, \
|
||||
let mut select = db.prepare("SELECT path, disc_number, track_number, title, year, album_artist, \
|
||||
artist, album, artwork FROM songs WHERE path LIKE ? ORDER BY path \
|
||||
COLLATE NOCASE ASC")?;
|
||||
select.bind(1, &Value::String(path_string.deref().to_owned()))?;
|
||||
select
|
||||
.bind(1, &Value::String(path_string.deref().to_owned()))?;
|
||||
self.select_songs(&mut select)
|
||||
}
|
||||
|
||||
pub fn get_random_albums(&self, count: u32) -> Result<Vec<Directory>> {
|
||||
let db = self.connect()?;
|
||||
let mut select =
|
||||
db.prepare("SELECT path, artwork, year, artist, album FROM directories WHERE album \
|
||||
let mut select = db.prepare("SELECT path, artwork, year, artist, album FROM directories WHERE album \
|
||||
IS NOT NULL ORDER BY RANDOM() LIMIT ?")?;
|
||||
select.bind(1, &Value::Integer(count as i64))?;
|
||||
self.select_directories(&mut select)
|
||||
|
@ -665,7 +688,8 @@ fn test_browse() {
|
|||
khemmis_path.push("root");
|
||||
khemmis_path.push("Khemmis");
|
||||
|
||||
let khemmis = CollectionFile::Directory(Directory {
|
||||
let khemmis =
|
||||
CollectionFile::Directory(Directory {
|
||||
path: khemmis_path.to_string_lossy().deref().to_string(),
|
||||
artist: None,
|
||||
album: None,
|
||||
|
@ -677,7 +701,8 @@ fn test_browse() {
|
|||
tobokegao_path.push("root");
|
||||
tobokegao_path.push("Tobokegao");
|
||||
|
||||
let tobokegao = CollectionFile::Directory(Directory {
|
||||
let tobokegao =
|
||||
CollectionFile::Directory(Directory {
|
||||
path: tobokegao_path.to_string_lossy().deref().to_string(),
|
||||
artist: None,
|
||||
album: None,
|
||||
|
|
|
@ -120,9 +120,7 @@ fn run() -> Result<()> {
|
|||
match config.ddns {
|
||||
Some(ref ddns_config) => {
|
||||
let ddns_config = ddns_config.clone();
|
||||
std::thread::spawn(|| {
|
||||
ddns::run(ddns_config);
|
||||
});
|
||||
std::thread::spawn(|| { ddns::run(ddns_config); });
|
||||
}
|
||||
None => (),
|
||||
};
|
||||
|
|
|
@ -139,7 +139,9 @@ fn read_vorbis(path: &Path) -> Result<SongTags> {
|
|||
fn read_flac(path: &Path) -> Result<SongTags> {
|
||||
let tag = metaflac::Tag::read_from_path(path)?;
|
||||
let vorbis = tag.vorbis_comments().ok_or("Missing Vorbis comments")?;
|
||||
let disc_number = vorbis.get("DISCNUMBER").and_then(|d| d[0].parse::<u32>().ok());
|
||||
let disc_number = vorbis
|
||||
.get("DISCNUMBER")
|
||||
.and_then(|d| d[0].parse::<u32>().ok());
|
||||
let year = vorbis.get("DATE").and_then(|d| d[0].parse::<i32>().ok());
|
||||
Ok(SongTags {
|
||||
artist: vorbis.artist().map(|v| v[0].clone()),
|
||||
|
|
12
src/vfs.rs
12
src/vfs.rs
|
@ -62,7 +62,9 @@ impl Vfs {
|
|||
#[test]
|
||||
fn test_virtual_to_real() {
|
||||
let mut config = VfsConfig::new();
|
||||
config.mount_points.insert("root".to_owned(), Path::new("test_dir").to_path_buf());
|
||||
config
|
||||
.mount_points
|
||||
.insert("root".to_owned(), Path::new("test_dir").to_path_buf());
|
||||
let vfs = Vfs::new(config);
|
||||
|
||||
let mut correct_path = PathBuf::new();
|
||||
|
@ -82,7 +84,9 @@ fn test_virtual_to_real() {
|
|||
#[test]
|
||||
fn test_virtual_to_real_no_trail() {
|
||||
let mut config = VfsConfig::new();
|
||||
config.mount_points.insert("root".to_owned(), Path::new("test_dir").to_path_buf());
|
||||
config
|
||||
.mount_points
|
||||
.insert("root".to_owned(), Path::new("test_dir").to_path_buf());
|
||||
let vfs = Vfs::new(config);
|
||||
let correct_path = Path::new("test_dir");
|
||||
let found_path = vfs.virtual_to_real(Path::new("root")).unwrap();
|
||||
|
@ -92,7 +96,9 @@ fn test_virtual_to_real_no_trail() {
|
|||
#[test]
|
||||
fn test_real_to_virtual() {
|
||||
let mut config = VfsConfig::new();
|
||||
config.mount_points.insert("root".to_owned(), Path::new("test_dir").to_path_buf());
|
||||
config
|
||||
.mount_points
|
||||
.insert("root".to_owned(), Path::new("test_dir").to_path_buf());
|
||||
let vfs = Vfs::new(config);
|
||||
|
||||
let mut correct_path = PathBuf::new();
|
||||
|
|
Loading…
Add table
Reference in a new issue