mirror of
https://github.com/Dummi26/musicdb.git
synced 2025-12-18 21:57:51 +01:00
.
This commit is contained in:
@@ -4,7 +4,7 @@ use std::{
|
||||
sync::{Arc, Mutex},
|
||||
};
|
||||
|
||||
use crate::data::{database::Database, CoverId};
|
||||
use crate::data::{database::Database, CoverId, SongId};
|
||||
|
||||
pub struct Client<T: Write + Read>(BufReader<T>);
|
||||
impl<T: Write + Read> Client<T> {
|
||||
@@ -33,6 +33,34 @@ impl<T: Write + Read> Client<T> {
|
||||
Ok(Err(response))
|
||||
}
|
||||
}
|
||||
pub fn song_file(
|
||||
&mut self,
|
||||
id: SongId,
|
||||
blocking: bool,
|
||||
) -> Result<Result<Vec<u8>, String>, std::io::Error> {
|
||||
writeln!(
|
||||
self.0.get_mut(),
|
||||
"{}",
|
||||
con_get_encode_string(&format!(
|
||||
"song-file{}\n{id}",
|
||||
if blocking { "-blocking" } else { "" }
|
||||
))
|
||||
)?;
|
||||
let mut response = String::new();
|
||||
self.0.read_line(&mut response)?;
|
||||
let response = con_get_decode_line(&response);
|
||||
if response.starts_with("len: ") {
|
||||
if let Ok(len) = response[4..].trim().parse() {
|
||||
let mut bytes = vec![0; len];
|
||||
self.0.read_exact(&mut bytes)?;
|
||||
Ok(Ok(bytes))
|
||||
} else {
|
||||
Ok(Err(response))
|
||||
}
|
||||
} else {
|
||||
Ok(Err(response))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn handle_one_connection_as_get(
|
||||
@@ -72,6 +100,40 @@ pub fn handle_one_connection_as_get(
|
||||
writeln!(connection.get_mut(), "no cover")?;
|
||||
}
|
||||
}
|
||||
"song-file" => {
|
||||
if let Some(bytes) =
|
||||
request
|
||||
.next()
|
||||
.and_then(|id| id.parse().ok())
|
||||
.and_then(|id| {
|
||||
db.lock()
|
||||
.unwrap()
|
||||
.get_song(&id)
|
||||
.and_then(|song| song.cached_data())
|
||||
})
|
||||
{
|
||||
writeln!(connection.get_mut(), "len: {}", bytes.len())?;
|
||||
connection.get_mut().write_all(&bytes)?;
|
||||
} else {
|
||||
writeln!(connection.get_mut(), "no data")?;
|
||||
}
|
||||
}
|
||||
"song-file-blocking" => {
|
||||
if let Some(bytes) =
|
||||
request
|
||||
.next()
|
||||
.and_then(|id| id.parse().ok())
|
||||
.and_then(|id| {
|
||||
let db = db.lock().unwrap();
|
||||
db.get_song(&id).and_then(|song| song.cached_data_now(&db))
|
||||
})
|
||||
{
|
||||
writeln!(connection.get_mut(), "len: {}", bytes.len())?;
|
||||
connection.get_mut().write_all(&bytes)?;
|
||||
} else {
|
||||
writeln!(connection.get_mut(), "no data")?;
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@ use std::{
|
||||
net::{SocketAddr, TcpListener},
|
||||
path::PathBuf,
|
||||
sync::{mpsc, Arc, Mutex},
|
||||
thread::{self, JoinHandle},
|
||||
thread,
|
||||
time::Duration,
|
||||
};
|
||||
|
||||
@@ -17,6 +17,7 @@ use crate::{
|
||||
database::{Cover, Database, UpdateEndpoint},
|
||||
queue::Queue,
|
||||
song::Song,
|
||||
AlbumId, ArtistId, SongId,
|
||||
},
|
||||
load::ToFromBytes,
|
||||
player::Player,
|
||||
@@ -46,6 +47,9 @@ pub enum Command {
|
||||
AddCover(Cover),
|
||||
ModifySong(Song),
|
||||
ModifyAlbum(Album),
|
||||
RemoveSong(SongId),
|
||||
RemoveAlbum(AlbumId),
|
||||
RemoveArtist(ArtistId),
|
||||
ModifyArtist(Artist),
|
||||
SetLibraryDirectory(PathBuf),
|
||||
}
|
||||
@@ -262,6 +266,18 @@ impl ToFromBytes for Command {
|
||||
s.write_all(&[0b10011100])?;
|
||||
artist.to_bytes(s)?;
|
||||
}
|
||||
Self::RemoveSong(song) => {
|
||||
s.write_all(&[0b11010000])?;
|
||||
song.to_bytes(s)?;
|
||||
}
|
||||
Self::RemoveAlbum(album) => {
|
||||
s.write_all(&[0b11010011])?;
|
||||
album.to_bytes(s)?;
|
||||
}
|
||||
Self::RemoveArtist(artist) => {
|
||||
s.write_all(&[0b11011100])?;
|
||||
artist.to_bytes(s)?;
|
||||
}
|
||||
Self::SetLibraryDirectory(path) => {
|
||||
s.write_all(&[0b00110001])?;
|
||||
path.to_bytes(s)?;
|
||||
@@ -308,6 +324,9 @@ impl ToFromBytes for Command {
|
||||
0b10010000 => Self::ModifySong(ToFromBytes::from_bytes(s)?),
|
||||
0b10010011 => Self::ModifyAlbum(ToFromBytes::from_bytes(s)?),
|
||||
0b10011100 => Self::ModifyArtist(ToFromBytes::from_bytes(s)?),
|
||||
0b11010000 => Self::RemoveSong(ToFromBytes::from_bytes(s)?),
|
||||
0b11010011 => Self::RemoveAlbum(ToFromBytes::from_bytes(s)?),
|
||||
0b11011100 => Self::RemoveArtist(ToFromBytes::from_bytes(s)?),
|
||||
0b01011101 => Self::AddCover(ToFromBytes::from_bytes(s)?),
|
||||
0b00110001 => Self::SetLibraryDirectory(ToFromBytes::from_bytes(s)?),
|
||||
_ => {
|
||||
|
||||
Reference in New Issue
Block a user