mirror of
https://github.com/agersant/polaris
synced 2024-12-03 18:19:09 +00:00
Applied rustfmt
This commit is contained in:
parent
56bb1d4e23
commit
d073e02549
8 changed files with 155 additions and 112 deletions
16
src/api.rs
16
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()))
|
||||
}
|
||||
|
@ -95,9 +98,9 @@ struct AuthRequirement {
|
|||
impl AroundMiddleware for AuthRequirement {
|
||||
fn around(self, handler: Box<Handler>) -> Box<Handler> {
|
||||
Box::new(AuthHandler {
|
||||
collection: self.collection,
|
||||
handler: handler
|
||||
}) as Box<Handler>
|
||||
collection: self.collection,
|
||||
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")?;
|
||||
|
@ -183,10 +186,10 @@ impl Config {
|
|||
.ok_or("Could not parse DDNS password")?;
|
||||
|
||||
self.ddns = Some(DDNSConfig {
|
||||
host: host.to_owned(),
|
||||
username: username.to_owned(),
|
||||
password: password.to_owned(),
|
||||
});
|
||||
host: host.to_owned(),
|
||||
username: username.to_owned(),
|
||||
password: password.to_owned(),
|
||||
});
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
|
11
src/ddns.rs
11
src/ddns.rs
|
@ -49,11 +49,14 @@ fn update_my_ip(ip: &String, config: &DDNSConfig) -> Result<(), DDNSError> {
|
|||
let host = &config.host;
|
||||
let full_url = format!("{}?host={}&ip={}", url, host, ip);
|
||||
let auth_header = Authorization(Basic {
|
||||
username: config.username.clone(),
|
||||
password: Some(config.password.to_owned()),
|
||||
});
|
||||
username: config.username.clone(),
|
||||
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)),
|
||||
|
|
141
src/index.rs
141
src/index.rs
|
@ -99,16 +99,14 @@ impl<'db> IndexBuilder<'db> {
|
|||
let mut queue = Vec::new();
|
||||
queue.reserve_exact(INDEX_BUILDING_INSERT_BUFFER_SIZE);
|
||||
Ok(IndexBuilder {
|
||||
queue: queue,
|
||||
db: db,
|
||||
insert_directory:
|
||||
db.prepare("INSERT OR REPLACE INTO directories (path, parent, artwork, year, \
|
||||
queue: queue,
|
||||
db: db,
|
||||
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 (?, ?, ?, ?, \
|
||||
?, ?, ?, ?, ?, ?)")?,
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
fn get_parent(path: &str) -> Option<String> {
|
||||
|
@ -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)
|
||||
|
@ -647,16 +670,16 @@ fn test_metadata() {
|
|||
assert_eq!(results.len(), 7);
|
||||
assert_eq!(results[4],
|
||||
CollectionFile::Song(Song {
|
||||
path: song_path.to_str().unwrap().to_string(),
|
||||
track_number: Some(5),
|
||||
disc_number: None,
|
||||
title: Some("シャーベット (Sherbet)".to_owned()),
|
||||
artist: Some("Tobokegao".to_owned()),
|
||||
album_artist: None,
|
||||
album: Some("Picnic".to_owned()),
|
||||
year: Some(2016),
|
||||
artwork: Some(artwork_path.to_str().unwrap().to_string()),
|
||||
}));
|
||||
path: song_path.to_str().unwrap().to_string(),
|
||||
track_number: Some(5),
|
||||
disc_number: None,
|
||||
title: Some("シャーベット (Sherbet)".to_owned()),
|
||||
artist: Some("Tobokegao".to_owned()),
|
||||
album_artist: None,
|
||||
album: Some("Picnic".to_owned()),
|
||||
year: Some(2016),
|
||||
artwork: Some(artwork_path.to_str().unwrap().to_string()),
|
||||
}));
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -665,25 +688,27 @@ fn test_browse() {
|
|||
khemmis_path.push("root");
|
||||
khemmis_path.push("Khemmis");
|
||||
|
||||
let khemmis = CollectionFile::Directory(Directory {
|
||||
path: khemmis_path.to_string_lossy().deref().to_string(),
|
||||
artist: None,
|
||||
album: None,
|
||||
year: None,
|
||||
artwork: None,
|
||||
});
|
||||
let khemmis =
|
||||
CollectionFile::Directory(Directory {
|
||||
path: khemmis_path.to_string_lossy().deref().to_string(),
|
||||
artist: None,
|
||||
album: None,
|
||||
year: None,
|
||||
artwork: None,
|
||||
});
|
||||
|
||||
let mut tobokegao_path = PathBuf::new();
|
||||
tobokegao_path.push("root");
|
||||
tobokegao_path.push("Tobokegao");
|
||||
|
||||
let tobokegao = CollectionFile::Directory(Directory {
|
||||
path: tobokegao_path.to_string_lossy().deref().to_string(),
|
||||
artist: None,
|
||||
album: None,
|
||||
year: None,
|
||||
artwork: None,
|
||||
});
|
||||
let tobokegao =
|
||||
CollectionFile::Directory(Directory {
|
||||
path: tobokegao_path.to_string_lossy().deref().to_string(),
|
||||
artist: None,
|
||||
album: None,
|
||||
year: None,
|
||||
artwork: None,
|
||||
});
|
||||
|
||||
let index = _get_test_index("browse.sqlite");
|
||||
index.update_index().unwrap();
|
||||
|
|
|
@ -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 => (),
|
||||
};
|
||||
|
|
|
@ -47,14 +47,14 @@ fn read_id3(path: &Path) -> Result<SongTags> {
|
|||
.or(tag.date_recorded().and_then(|d| d.year));
|
||||
|
||||
Ok(SongTags {
|
||||
artist: artist,
|
||||
album_artist: album_artist,
|
||||
album: album,
|
||||
title: title,
|
||||
disc_number: disc_number,
|
||||
track_number: track_number,
|
||||
year: year,
|
||||
})
|
||||
artist: artist,
|
||||
album_artist: album_artist,
|
||||
album: album,
|
||||
title: title,
|
||||
disc_number: disc_number,
|
||||
track_number: track_number,
|
||||
year: year,
|
||||
})
|
||||
}
|
||||
|
||||
fn read_ape_string(item: &ape::Item) -> Option<String> {
|
||||
|
@ -95,14 +95,14 @@ fn read_ape(path: &Path) -> Result<SongTags> {
|
|||
let disc_number = tag.item("Disc").and_then(read_ape_x_of_y);
|
||||
let track_number = tag.item("Track").and_then(read_ape_x_of_y);
|
||||
Ok(SongTags {
|
||||
artist: artist,
|
||||
album_artist: album_artist,
|
||||
album: album,
|
||||
title: title,
|
||||
disc_number: disc_number,
|
||||
track_number: track_number,
|
||||
year: year,
|
||||
})
|
||||
artist: artist,
|
||||
album_artist: album_artist,
|
||||
album: album,
|
||||
title: title,
|
||||
disc_number: disc_number,
|
||||
track_number: track_number,
|
||||
year: year,
|
||||
})
|
||||
}
|
||||
|
||||
fn read_vorbis(path: &Path) -> Result<SongTags> {
|
||||
|
@ -139,17 +139,19 @@ 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()),
|
||||
album_artist: vorbis.album_artist().map(|v| v[0].clone()),
|
||||
album: vorbis.album().map(|v| v[0].clone()),
|
||||
title: vorbis.title().map(|v| v[0].clone()),
|
||||
disc_number: disc_number,
|
||||
track_number: vorbis.track(),
|
||||
year: year,
|
||||
})
|
||||
artist: vorbis.artist().map(|v| v[0].clone()),
|
||||
album_artist: vorbis.album_artist().map(|v| v[0].clone()),
|
||||
album: vorbis.album().map(|v| v[0].clone()),
|
||||
title: vorbis.title().map(|v| v[0].clone()),
|
||||
disc_number: disc_number,
|
||||
track_number: vorbis.track(),
|
||||
year: year,
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
20
src/vfs.rs
20
src/vfs.rs
|
@ -43,10 +43,10 @@ impl Vfs {
|
|||
match virtual_path.strip_prefix(mount_path) {
|
||||
Ok(p) => {
|
||||
return if p.components().count() == 0 {
|
||||
Ok(target.clone())
|
||||
} else {
|
||||
Ok(target.join(p))
|
||||
}
|
||||
Ok(target.clone())
|
||||
} else {
|
||||
Ok(target.join(p))
|
||||
}
|
||||
}
|
||||
Err(_) => (),
|
||||
}
|
||||
|
@ -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…
Reference in a new issue