Skip to content

Commit

Permalink
positions gated by feature shared-positions
Browse files Browse the repository at this point in the history
  • Loading branch information
izderadicka committed May 26, 2019
1 parent aaa0e05 commit 1811a7a
Show file tree
Hide file tree
Showing 11 changed files with 156 additions and 133 deletions.
4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,19 +55,21 @@ optional=true

[dependencies.websock]
path="crates/websock"
optional = true

[target.'cfg(unix)'.dependencies]
nix = "0.13"

[features]
default = ["tls", "symlinks", "search-cache", "folder-download"]
default = ["tls", "symlinks", "search-cache", "folder-download", "shared-positions"]
tls=["tokio-tls", "native-tls"]
symlinks=["cachedirtree/symlinks"]
search-cache=["cachedirtree"]
transcoding-cache=["simple-file-cache", "bytes"]
folder-download = ["async-tar"]
static = ["media_info/static"]
partially-static = ["media_info/partially-static"]
shared-positions = ["websock"]

[profile.release]
lto = true
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ Feature | Description | Default | Program options
| symlinks | Enables to use symbolic links in media folders | Yes | Use --allow-symlinks to follow symbolic links
| search-cache | Caches structure of media directories for fast search | Yes | Use --search-cache to enable this cache
| folder-download | Enables API endpoint to download content of a folder in tar archive | Yes | Can be disabled with argument --disable-folder-download
| shared-positions | Clients can share recent playback positions via simple websocket API | Yes |
| transcoding-cache | Cache to save transcoded files for fast next use | No | Can be disabled by --t-cache-disable and modified by --t-cache-dir --t-cache-size --t-cache-max-files
| static | Enables fully static build of audioserve. Check above notes for static build | No |
| partially-static | Statically links libavformat (and related).Enables to run audioserve on systems, which do not have required version of libavformat| No |
Expand Down
129 changes: 75 additions & 54 deletions src/config/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,10 +246,10 @@ pub fn parse_args() -> Result<Config> {
parse_args_from(env::args_os())
}

pub fn parse_args_from<I,T>(args: I) -> Result<Config>
where
I: IntoIterator<Item = T>,
T: Into<std::ffi::OsString> + Clone
pub fn parse_args_from<I, T>(args: I) -> Result<Config>
where
I: IntoIterator<Item = T>,
T: Into<std::ffi::OsString> + Clone,
{
let p = create_parser();
let args = p.get_matches_from(args);
Expand All @@ -265,8 +265,14 @@ pub fn parse_args_from<I,T>(args: I) -> Result<Config>
{
let d = base_data_dir();
if !d.is_dir() {
std::fs::create_dir(d).or_else(|e| arg_error!("data-dir",
"Audioserve data directory {:?} cannot be created due to error {}", d, e))?
std::fs::create_dir(d).or_else(|e| {
arg_error!(
"data-dir",
"Audioserve data directory {:?} cannot be created due to error {}",
d,
e
)
})?
}
}

Expand Down Expand Up @@ -295,7 +301,7 @@ pub fn parse_args_from<I,T>(args: I) -> Result<Config>
};

let is_present_or_env = |name: &str, env_name: &str| {
args.is_present(name) || env::var(env_name).map(|s| s.len() > 0).unwrap_or(false)
args.is_present(name) || env::var(env_name).map(|s| !s.is_empty()).unwrap_or(false)
};

if args.is_present("debug") {
Expand All @@ -317,10 +323,8 @@ pub fn parse_args_from<I,T>(args: I) -> Result<Config>
.parse()
.or_else(|_| arg_error!("listen", "Invalid value in $PORT"))?;
config.listen = SocketAddr::from(([0, 0, 0, 0], port));
} else {
if let Some(addr) = args.value_of("listen") {
config.listen = addr.parse().unwrap();
}
} else if let Some(addr) = args.value_of("listen") {
config.listen = addr.parse().unwrap();
}

if is_present_or_env("thread-pool-large", "AUDIOSERVE_THREAD_POOL_LARGE") {
Expand Down Expand Up @@ -449,7 +453,6 @@ pub fn parse_args_from<I,T>(args: I) -> Result<Config>
Ok(config)
}


#[cfg(test)]
mod test {
use super::*;
Expand All @@ -460,50 +463,68 @@ mod test {
let c = parse_args_from(&["audioserve", "--no-authentication", "test_data"]).unwrap();
assert_eq!(1, c.base_dirs.len());


let c = parse_args_from(&["audioserve",
"--listen", "127.0.0.1:4444",
"--thread-pool-large",
"--thread-pool-keep-alive-secs", "60",
"--shared-secret", "usak",
"--transcoding-max-parallel-processes", "99",
"--transcoding-max-runtime", "99",
"--token-validity-days", "99",
"--client-dir", "test_data",
"--secret-file", "test_data/some_secret",
"--chapters-from-duration", "99",
"--chapters-duration", "99",
"--cors",
"test_data", "client"]).unwrap();
let c = parse_args_from(&[
"audioserve",
"--listen",
"127.0.0.1:4444",
"--thread-pool-large",
"--thread-pool-keep-alive-secs",
"60",
"--shared-secret",
"usak",
"--transcoding-max-parallel-processes",
"99",
"--transcoding-max-runtime",
"99",
"--token-validity-days",
"99",
"--client-dir",
"test_data",
"--secret-file",
"test_data/some_secret",
"--chapters-from-duration",
"99",
"--chapters-duration",
"99",
"--cors",
"test_data",
"client",
])
.unwrap();
assert_eq!(2, c.base_dirs.len());
assert_eq!("127.0.0.1:4444".parse::<SocketAddr>().unwrap(), c.listen);
assert_eq!(16,c.thread_pool.num_threads);
assert_eq!(16, c.thread_pool.num_threads);
assert_eq!(1000, c.thread_pool.queue_size);
assert_eq!(Some(Duration::from_secs(60)), c.thread_pool.keep_alive);
assert_eq!(Some("usak".into()), c.shared_secret);
assert_eq!(99, c.transcoding.max_parallel_processes);
assert_eq!(99, c.transcoding.max_runtime_hours);
assert_eq!(99*24, c.token_validity_hours);
assert_eq!(99 * 24, c.token_validity_hours);
assert_eq!(PathBuf::from("test_data"), c.client_dir);
assert_eq!(PathBuf::from("test_data/some_secret"), c.secret_file);
assert_eq!(99, c.chapters.from_duration);
assert_eq!(99, c.chapters.duration);
assert!(c.cors);

}

#[test]
#[cfg(feature="transcoding-cache")]
#[cfg(feature = "transcoding-cache")]
fn test_t_cache() {
init_default_config();
let c = parse_args_from(&["audioserve",
"--no-authentication",
"--t-cache-dir", "test_data",
"--t-cache-size", "999",
"--t-cache-max-files", "999",
"--t-cache-disable",
"--t-cache-save-often",
"test_data"]).unwrap();
let c = parse_args_from(&[
"audioserve",
"--no-authentication",
"--t-cache-dir",
"test_data",
"--t-cache-size",
"999",
"--t-cache-max-files",
"999",
"--t-cache-disable",
"--t-cache-save-often",
"test_data",
])
.unwrap();

assert_eq!(PathBuf::from("test_data"), c.transcoding.cache.root_dir);
assert_eq!(999, c.transcoding.cache.max_size);
Expand All @@ -513,30 +534,32 @@ mod test {
}

#[test]
#[cfg(feature="tls")]
#[cfg(feature = "tls")]
fn test_tls() {
init_default_config();
let c = parse_args_from(&["audioserve",
"--no-authentication",
"--ssl-key", "test_data/desc.txt",
"--ssl-key-password", "neco",
"test_data"]).unwrap();
let c = parse_args_from(&[
"audioserve",
"--no-authentication",
"--ssl-key",
"test_data/desc.txt",
"--ssl-key-password",
"neco",
"test_data",
])
.unwrap();

assert!(c.ssl.is_some());
let ssl = c.ssl.unwrap();
assert_eq!(PathBuf::from("test_data/desc.txt"), ssl.key_file);
assert_eq!("neco", ssl.key_password);

}

#[test]
#[cfg(feature="symlinks")]
#[cfg(feature = "symlinks")]
fn test_symlinks_in_env() {
init_default_config();
env::set_var("AUDIOSERVE_ALLOW_SYMLINKS", "1");
let c = parse_args_from(&["audioserve",
"--no-authentication",
"test_data"]).unwrap();
let c = parse_args_from(&["audioserve", "--no-authentication", "test_data"]).unwrap();

assert!(c.allow_symlinks);
env::remove_var("AUDIOSERVE_ALLOW_SYMLINKS");
Expand All @@ -545,11 +568,9 @@ mod test {
#[test]
fn test_from_config() {
init_default_config();
let c = parse_args_from(&["audioserve",
"--config",
"test_data/sample-config.yaml"]).unwrap();
let c =
parse_args_from(&["audioserve", "--config", "test_data/sample-config.yaml"]).unwrap();

assert_eq!("neco", c.ssl.unwrap().key_password);

}
}
14 changes: 10 additions & 4 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use super::services::transcode::{QualityLevel, Transcoder, TranscodingFormat};

use crate::util;
use num_cpus;
use serde_yaml;
use std::env;
Expand All @@ -8,7 +9,6 @@ use std::io::Read;
use std::net::SocketAddr;
use std::path::{Path, PathBuf};
use std::time::Duration;
use crate::util;

pub use self::error::{Error, Result};

Expand Down Expand Up @@ -63,7 +63,7 @@ impl Default for TranscodingCacheConfig {
#[cfg(feature = "transcoding-cache")]
impl TranscodingCacheConfig {
pub fn check(&self) -> Result<()> {
if ! util::parent_dir_exists(&self.root_dir) {
if !util::parent_dir_exists(&self.root_dir) {
return value_error!(
"root_dir",
"Parent directory does not exists for {:?}",
Expand Down Expand Up @@ -306,7 +306,13 @@ impl Config {
}

pub fn check(&self) -> Result<()> {
if self.shared_secret.as_ref().map(String::len).unwrap_or(std::usize::MAX) < 3 {
if self
.shared_secret
.as_ref()
.map(String::len)
.unwrap_or(std::usize::MAX)
< 3
{
return value_error!("shared_secret", "Shared secret must be at least 3 bytes");
}

Expand Down Expand Up @@ -349,7 +355,7 @@ impl Config {
self.thread_pool.check()?;
self.chapters.check()?;

if self.base_dirs.len() == 0 {
if self.base_dirs.is_empty() {
return value_error!(
"base_dirs",
"At least one directory with audio files must be provided"
Expand Down
16 changes: 8 additions & 8 deletions src/config/validators.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
use crate::util;
use std::ffi::{OsStr, OsString};
use std::path::Path;
use crate::util;

type ValidatorResult = Result<(), String>;

pub fn is_socket_addr(v: String) -> ValidatorResult {
let v = v.as_ref();
if let Err(_) = str::parse::<std::net::SocketAddr>(v) {
if str::parse::<std::net::SocketAddr>(v).is_err() {
return Err(format!("{} is not socket address", v));
};
Ok(())
}

pub fn is_number(v: String) -> ValidatorResult {
let v = v.as_ref();
if let Err(_) = str::parse::<u32>(v) {
if str::parse::<u32>(v).is_err() {
return Err(format!("{} is not a number", v));
}
Ok(())
Expand All @@ -39,9 +39,9 @@ pub fn is_existing_file(p: &OsStr) -> Result<(), OsString> {
}

pub fn parent_dir_exists(p: &OsStr) -> Result<(), OsString> {
if !util::parent_dir_exists(&p) {
Err(format!("parent dir for {:?} does not exists", p).into())
} else {
Ok(())
}
if !util::parent_dir_exists(&p) {
Err(format!("parent dir for {:?} does not exists", p).into())
} else {
Ok(())
}
}
Loading

0 comments on commit 1811a7a

Please sign in to comment.