Compare commits

..

No commits in common. "dae598e15cc4864a905b1b75618e0ec1364f9081" and "26a3381a25d17dd51b4e617c0444e2cd36dd2da6" have entirely different histories.

2 changed files with 34 additions and 41 deletions

View file

@ -53,14 +53,8 @@ async fn get_player(_req: tide::Request<()>) -> tide::Result {
let song = mpd.command("currentsong").await?.into_hashmap();
let status = mpd.command("status").await?.into_hashmap();
let elapsed = status
.get("elapsed")
.and_then(|e| e.parse().ok())
.unwrap_or(0.0);
let duration = status
.get("duration")
.and_then(|e| e.parse().ok())
.unwrap_or(1.0);
let elapsed = status["elapsed"].parse().unwrap_or(0.0);
let duration = status["duration"].parse().unwrap_or(1.0);
let mut template = PlayerTemplate {
song: if song.is_empty() { None } else { Some(&song) },

View file

@ -49,20 +49,6 @@ pub struct CommandResult {
}
impl CommandResult {
pub fn new(properties: Vec<(String, String)>) -> Self {
Self {
properties,
binary: None,
}
}
pub fn new_binary(properties: Vec<(String, String)>, binary: Vec<u8>) -> Self {
Self {
properties,
binary: Some(binary),
}
}
pub fn into_hashmap(self) -> HashMap<String, String> {
self.properties.into_iter().collect()
}
@ -98,24 +84,28 @@ impl Mpd {
}
pub async fn connect() -> anyhow::Result<Self> {
let stream = TcpStream::connect(host()).await?;
let reader = BufReader::new(stream.clone());
let mut this = Self { stream, reader };
let mut stream = TcpStream::connect(host()).await?;
let mut reader = BufReader::new(stream.clone());
// skip OK MPD line
// TODO check if it is indeed OK
let mut buffer = String::new();
this.reader.read_line(&mut buffer).await?;
reader.read_line(&mut buffer).await?;
let password = std::env::var("MPD_PASSWORD").unwrap_or(String::new());
if !password.is_empty() {
let password = Self::escape_str(&password);
this.command(&format!(r#"password "{password}""#)).await?;
let command = format!("password \"{password}\"\n");
stream.write_all(command.as_bytes()).await?;
reader.read_line(&mut buffer).await?;
}
this.command("binarylimit 1048576").await?;
stream.write_all(b"binarylimit 1048576\n").await?;
buffer.clear();
reader.read_line(&mut buffer).await?;
Ok(this)
Ok(Self { stream, reader })
}
async fn read_binary_data(&mut self, size: usize) -> anyhow::Result<Vec<u8>> {
@ -152,15 +142,21 @@ impl Mpd {
if key == "binary" {
let binary = self.read_binary_data(value.parse()?).await?;
break Ok(CommandResult::new_binary(properties, binary));
break Ok(CommandResult {
properties,
binary: Some(binary),
});
}
} else if buffer.starts_with("OK") {
break Ok(CommandResult::new(properties));
break Ok(CommandResult {
properties,
binary: None,
});
} else if buffer.starts_with("ACK") {
break Err(anyhow!(buffer));
} else {
println!("Unexpected MPD response {buffer}");
break Err(anyhow!(buffer));
}
}
}
@ -176,10 +172,16 @@ impl Mpd {
if !binary.is_empty() {
buffer.append(&mut binary);
} else {
return Ok(CommandResult::new_binary(result.properties, buffer));
return Ok(CommandResult {
properties: result.properties,
binary: Some(buffer),
});
}
} else {
return Ok(CommandResult::new(result.properties));
return Ok(CommandResult {
properties: result.properties,
binary: None,
});
}
}
}
@ -198,7 +200,7 @@ impl Mpd {
pub async fn add_pos(&mut self, path: &str, pos: &str) -> anyhow::Result<()> {
let path = Self::escape_str(path);
let pos = Self::escape_str(pos);
self.command(&format!(r#"add "{path}" "{pos}""#)).await?;
self.command(&format!("add \"{path}\" \"{pos}\"")).await?;
Ok(())
}
@ -221,9 +223,7 @@ impl Mpd {
pub async fn albumart(&mut self, path: &str) -> anyhow::Result<Vec<u8>> {
let path = Self::escape_str(path);
let result = self
.command_binary(&format!(r#"albumart "{path}""#))
.await?;
let result = self.command_binary(&format!("albumart \"{path}\"")).await?;
match result.binary {
Some(binary) => Ok(binary),
@ -234,7 +234,7 @@ impl Mpd {
pub async fn readpicture(&mut self, path: &str) -> anyhow::Result<Vec<u8>> {
let path = Self::escape_str(path);
let result = self
.command_binary(&format!(r#"readpicture "{path}""#))
.command_binary(&format!("readpicture \"{path}\""))
.await?;
match result.binary {
@ -252,9 +252,8 @@ impl Mpd {
.unwrap_or("n/a".to_string())
}
let path = Self::escape_str(path);
let result = self
.command(&format!(r#"lsinfo "{path}""#))
.command(&format!("lsinfo \"{}\"", Self::escape_str(path)))
.await?
.into_hashmaps(&["file", "directory", "playlist"]);