Optimised the album art display

This commit is contained in:
MrDulfin 2024-12-29 00:57:51 -05:00
parent 280a17788b
commit e1a0ffbd05
5 changed files with 949 additions and 1015 deletions

View file

@ -1,18 +1,17 @@
pub mod music_storage {
pub mod library;
pub mod music_collection;
pub mod playlist;
mod utils;
#[allow(dead_code)]
pub mod db_reader;
}
pub mod music_controller {
pub mod connections;
pub mod controller;
pub mod controller_handle;
pub mod queue;
}
pub mod config;
pub mod music_storage {
pub mod library;
pub mod music_collection;
pub mod playlist;
mod utils;
#[allow(dead_code)]
pub mod db_reader;
}
pub mod music_controller {
pub mod controller;
pub mod controller_handle;
pub mod queue;
}
pub mod config;

View file

@ -1,100 +0,0 @@
// use std::{
// sync::{Arc, RwLock},
// error::Error,
// };
// use discord_rpc_client::Client;
// use listenbrainz::ListenBrainz;
// use uuid::Uuid;
// use crate::{
// config::config::Config, music_controller::controller::{Controller, QueueCmd, QueueResponse}, music_storage::library::{MusicLibrary, Song, Tag}
// };
// use super::controller::DatabaseResponse;
// impl Controller {
// pub fn listenbrainz_authenticate(&mut self) -> Result<ListenBrainz, Box<dyn Error>> {
// let config = &self.config.read().unwrap();
// let mut client = ListenBrainz::new();
// let lbz_token = match &config.connections.listenbrainz_token {
// Some(token) => token,
// None => todo!("No ListenBrainz token in config")
// };
// if !client.is_authenticated() {
// client.authenticate(lbz_token)?;
// }
// Ok(client)
// }
// pub fn lbz_scrobble(&self, client: ListenBrainz, uuid: Uuid) -> Result<(), Box<dyn Error>> {
// let config = &self.config.read().unwrap();
// &self.db_mail.send(super::controller::DatabaseCmd::QueryUuid(uuid));
// let res = &self.db_mail.recv()?;
// let song = match res {
// DatabaseResponse::Song(song) => song,
// _ => todo!()
// };
// let unknown = &"unknown".to_string();
// let artist = song.get_tag(&Tag::Artist).unwrap_or(unknown);
// let track = song.get_tag(&Tag::Title).unwrap_or(unknown);
// let release = song.get_tag(&Tag::Album).map(|rel| rel.as_str());
// client.listen(artist, track, release)?;
// Ok(())
// }
// pub fn lbz_now_playing(&self, client: ListenBrainz, uuid: Uuid) -> Result<(), Box<dyn Error>> {
// let config = &self.config.read().unwrap();
// &self.db_mail.send(super::controller::DatabaseCmd::QueryUuid(uuid));
// let res = &self.db_mail.recv()?;
// let song = match res {
// DatabaseResponse::Song(song) => song,
// _ => todo!()
// };
// let unknown = &"unknown".to_string();
// let artist = song.get_tag(&Tag::Artist).unwrap_or(unknown);
// let track = song.get_tag(&Tag::Title).unwrap_or(unknown);
// let release = song.get_tag(&Tag::Album).map(|rel| rel.as_str());
// client.listen(artist, track, release)?;
// Ok(())
// }
// pub fn discord_song_change(client: &mut Client,song: Song) {
// client.set_activity(|a| {
// a.state(format!("Listening to {}", song.get_tag(&Tag::Title).unwrap()))
// .into()
// });
// }
// }
// #[cfg(test)]
// mod test_super {
// use std::{thread::sleep, time::Duration};
// use super::*;
// use crate::config::config::tests::read_config_lib;
// #[test]
// fn listenbrainz() {
// let mut c = Controller::start(".\\test-config\\config_test.json").unwrap();
// let client = c.listenbrainz_authenticate().unwrap();
// c.q_new().unwrap();
// c.queue_mail[0].send(QueueCmd::SetVolume(0.04)).unwrap();
// let songs = c.lib_get_songs();
// c.q_enqueue(0, songs[1].location.to_owned()).unwrap();
// c.q_play(0).unwrap();
// sleep(Duration::from_secs(100));
// c.lbz_scrobble(client, songs[1].uuid).unwrap();
// }
// }

File diff suppressed because it is too large Load diff

View file

@ -1,57 +1,59 @@
use std::{fs::OpenOptions, io::{Read, Write}};
use dmp_core::music_controller::{controller::{ControllerHandle, LibraryResponse, PlayerCommand, PlayerLocation, PlayerResponse, QueueResponse}, queue::QueueSong};
use kushi::QueueItem;
use tauri::{AppHandle, Emitter, State, Wry};
use tempfile::TempDir;
use uuid::Uuid;
use crate::wrappers::_Song;
#[tauri::command]
pub async fn add_song_to_queue(app: AppHandle<Wry>, ctrl_handle: State<'_, ControllerHandle>, uuid: Uuid, location: PlayerLocation) -> Result<(), String> {
dbg!(&location);
let (song, _) = ctrl_handle.lib_get_song(uuid).await;
match ctrl_handle.queue_append(QueueItem::from_item_type(kushi::QueueItemType::Single(QueueSong { song, location }))).await {
Ok(()) => (),
Err(e) => return Err(e.to_string())
}
app.emit("queue_updated", ()).unwrap();
Ok(())
}
#[tauri::command]
pub async fn play_now(app: AppHandle<Wry>, ctrl_handle: State<'_, ControllerHandle>, uuid: Uuid, location: PlayerLocation) -> Result<(), String> {
let song = match ctrl_handle.play_now(uuid, location).await {
Ok(song) => song,
Err(e) => return Err(e.to_string())
};
app.emit("queue_updated", ()).unwrap();
app.emit("now_playing_change", _Song::from(&song)).unwrap();
app.emit("playing", ()).unwrap();
Ok(())
}
#[tauri::command]
pub async fn display_album_art(ctrl_handle: State<'_, ControllerHandle>, temp_dir: State<'_, TempDir>, uuid: Uuid) -> Result<(), String> {
match ctrl_handle.lib_get_song(uuid.clone()).await.0.album_art(0) {
Ok(art) => {
let mut art = art.unwrap();
let path = temp_dir.path().join(format!("CoverArt_{uuid}.{}", file_format::FileFormat::from_bytes(&art).extension()));
// TODO: This can be optimised later
let mut file = OpenOptions::new()
.create(true)
.truncate(true)
.write(true)
.read(true)
.open(path.clone())
.unwrap();
file.write_all(&mut art).unwrap();
opener::open(path).unwrap();
}
Err(e) => return Err(e.to_string())
};
Ok(())
use std::{fs::OpenOptions, io::{Read, Write}};
use dmp_core::music_controller::{controller::{ControllerHandle, LibraryResponse, PlayerCommand, PlayerLocation, PlayerResponse, QueueResponse}, queue::QueueSong};
use kushi::QueueItem;
use tauri::{AppHandle, Emitter, State, Wry};
use tempfile::TempDir;
use uuid::Uuid;
use crate::wrappers::_Song;
#[tauri::command]
pub async fn add_song_to_queue(app: AppHandle<Wry>, ctrl_handle: State<'_, ControllerHandle>, uuid: Uuid, location: PlayerLocation) -> Result<(), String> {
dbg!(&location);
let (song, _) = ctrl_handle.lib_get_song(uuid).await;
match ctrl_handle.queue_append(QueueItem::from_item_type(kushi::QueueItemType::Single(QueueSong { song, location }))).await {
Ok(()) => (),
Err(e) => return Err(e.to_string())
}
app.emit("queue_updated", ()).unwrap();
Ok(())
}
#[tauri::command]
pub async fn play_now(app: AppHandle<Wry>, ctrl_handle: State<'_, ControllerHandle>, uuid: Uuid, location: PlayerLocation) -> Result<(), String> {
let song = match ctrl_handle.play_now(uuid, location).await {
Ok(song) => song,
Err(e) => return Err(e.to_string())
};
app.emit("queue_updated", ()).unwrap();
app.emit("now_playing_change", _Song::from(&song)).unwrap();
app.emit("playing", ()).unwrap();
Ok(())
}
#[tauri::command]
pub async fn display_album_art(ctrl_handle: State<'_, ControllerHandle>, temp_dir: State<'_, TempDir>, uuid: Uuid) -> Result<(), String> {
match ctrl_handle.lib_get_song(uuid.clone()).await.0.album_art(0) {
Ok(art) => {
let mut art = art.unwrap();
let path = temp_dir.path().join(format!("CoverArt_{uuid}.{}", file_format::FileFormat::from_bytes(&art).extension()));
if !path.exists() {
// TODO: This can be optimised later
let mut file = OpenOptions::new()
.create(true)
.truncate(true)
.write(true)
.read(true)
.open(path.clone())
.unwrap();
file.write_all(&mut art).unwrap();
}
opener::open(path).unwrap();
}
Err(e) => return Err(e.to_string())
};
Ok(())
}

View file

@ -1,211 +1,221 @@
use std::{fs, path::PathBuf, str::FromStr, thread::spawn};
use crossbeam::channel::{unbounded, Receiver, Sender};
use dmp_core::{config::{Config, ConfigLibrary}, music_controller::controller::{Controller, ControllerHandle, LibraryResponse}, music_storage::library::MusicLibrary};
use rfd::FileHandle;
use tauri::{http::Response, Emitter, Manager, State, WebviewWindowBuilder, Wry};
use uuid::Uuid;
use crate::wrappers::{get_library, play, pause, prev, set_volume, get_song, next, get_queue, import_playlist, get_playlist, get_playlists, remove_from_queue};
use commands::{add_song_to_queue, play_now, display_album_art};
pub mod wrappers;
pub mod commands;
const DEFAULT_IMAGE: &[u8] = include_bytes!("../icons/icon.png");
#[cfg_attr(mobile, tauri::mobile_entry_point)]
pub fn run() {
let (rx, tx) = unbounded::<Config>();
let (lib_rx, lib_tx) = unbounded::<Option<PathBuf>>();
let (handle_rx, handle_tx) = unbounded::<ControllerHandle>();
let controller_thread = spawn(move || {
let mut config = { tx.recv().unwrap() } ;
let scan_path = { lib_tx.recv().unwrap() };
let _temp_config = ConfigLibrary::default();
let _lib = config.libraries.get_default().unwrap_or(&_temp_config);
let save_path = if _lib.path == PathBuf::default() {
scan_path.as_ref().unwrap().clone().canonicalize().unwrap().join("library.dlib")
} else {
_lib.path.clone()
};
println!("save_path: {}\nscan_path:{scan_path:?}", save_path.display());
let mut library = MusicLibrary::init(
save_path.clone(),
_lib.uuid
).unwrap();
let scan_path = scan_path.unwrap_or_else(|| config.libraries.get_default().unwrap().scan_folders.as_ref().unwrap()[0].clone());
if config.libraries.get_default().is_err() {
library.scan_folder(&scan_path).unwrap();
config.push_library( ConfigLibrary::new(save_path.clone(), String::from("Library"), Some(vec![scan_path.clone()]), Some(library.uuid)));
}
if library.library.is_empty() {
println!("library is empty");
} else {
config.write_file().unwrap();
}
println!("scan_path: {}", scan_path.display());
library.save(save_path).unwrap();
let (handle, input) = ControllerHandle::new(
library,
std::sync::Arc::new(std::sync::RwLock::new(config))
);
handle_rx.send(handle).unwrap();
let _controller = futures::executor::block_on(Controller::start(input)).unwrap();
});
let app = tauri::Builder::default()
.plugin(tauri_plugin_shell::init())
.invoke_handler(tauri::generate_handler![
get_config,
create_new_library,
get_library,
play,
pause,
set_volume,
next,
prev,
get_song,
lib_already_created,
get_queue,
add_song_to_queue,
play_now,
import_playlist,
get_playlist,
get_playlists,
remove_from_queue,
display_album_art,
]).manage(ConfigRx(rx))
.manage(LibRx(lib_rx))
.manage(HandleTx(handle_tx))
.manage(tempfile::TempDir::new().unwrap())
.register_asynchronous_uri_scheme_protocol("asset", move |ctx, req, res| {
let query = req
.clone()
.uri()
.clone()
.into_parts()
.path_and_query
.unwrap()
.query()
.unwrap()
.to_string();
let bytes = if query.as_str() == "default" {
Some(DEFAULT_IMAGE.to_vec())
} else {futures::executor::block_on(async move {
let controller = ctx.app_handle().state::<ControllerHandle>();
controller.lib_mail.send(dmp_core::music_controller::controller::LibraryCommand::Song(Uuid::parse_str(query.as_str()).unwrap())).await.unwrap();
let LibraryResponse::Song(song, _) = controller.lib_mail.recv().await.unwrap() else {
return None
};
Some(song.album_art(0).unwrap_or_else(|_| None).unwrap_or(DEFAULT_IMAGE.to_vec()))
})};
res.respond(
Response::builder()
.header("Origin", "*")
.header("Content-Length", bytes.as_ref().unwrap_or(&vec![]).len())
.status(200)
.body(bytes.unwrap_or_default())
.unwrap()
);
})
.build(tauri::generate_context!())
.expect("error while building tauri application");
app
.run(|_app_handle, event| match event {
tauri::RunEvent::ExitRequested { api, .. } => {
// api.prevent_exit();
panic!("does this kill the player?")
}
_ => {}
});
}
struct ConfigRx(Sender<Config>);
struct LibRx(Sender<Option<PathBuf>>);
struct HandleTx(Receiver<ControllerHandle>);
#[tauri::command]
async fn get_config(state: State<'_, ConfigRx>) -> Result<Config, String> {
if let Some(dir) = directories::ProjectDirs::from("", "Dangoware", "dmp") {
let path = dir.config_dir();
fs::create_dir_all(path).or_else(|err| {
if err.kind() == std::io::ErrorKind::AlreadyExists {
Ok(())
} else {
Err(err)
}
}).unwrap();
let config = if let Ok(mut c) = Config::read_file(PathBuf::from(path).join("config")) {
if c.state_path == PathBuf::default() {
c.state_path = PathBuf::from(path).join("state");
}
c
} else {
let c = Config {
path: PathBuf::from(path).join("config"),
state_path: PathBuf::from(path).join("state"),
..Default::default()
};
c.write_file().unwrap();
c
};
state.inner().0.send(config.clone()).unwrap();
Ok(config)
} else {
panic!("No config dir for DMP")
}
}
#[tauri::command]
async fn create_new_library(
app: tauri::AppHandle<Wry>,
lib_rx: State<'_, LibRx>,
handle_tx: State<'_, HandleTx>,
) -> Result<(), String> {
let dir = rfd::AsyncFileDialog::new()
.set_title("Pick a library path")
.pick_folder()
.await
.unwrap();
let path = dir.path().canonicalize().unwrap();
println!("{}", path.display());
if !path.exists() {
panic!("Path {} does not exist!", path.display())
} else if !path.is_dir() {
panic!("Path {} is not a directory!", path.display())
}
lib_rx.inner().0.send(Some(path)).unwrap();
app.manage(handle_tx.inner().0.recv().unwrap());
app.emit("library_loaded", ()).unwrap();
Ok(())
}
#[tauri::command]
async fn lib_already_created(app: tauri::AppHandle<Wry>, lib_rx: State<'_, LibRx>, handle_tx: State<'_, HandleTx>) -> Result<(), String> {
println!("lib already created");
lib_rx.inner().0.send(None).unwrap();
app.manage(handle_tx.inner().0.recv().unwrap());
app.emit("library_loaded", ()).unwrap();
Ok(())
}
use std::{fs, path::PathBuf, str::FromStr, thread::{scope, spawn}};
use crossbeam::channel::{unbounded, Receiver, Sender};
use dmp_core::{config::{Config, ConfigLibrary}, music_controller::controller::{Controller, ControllerHandle, LibraryResponse, PlaybackInfo}, music_storage::library::MusicLibrary};
use rfd::FileHandle;
use tauri::{http::Response, Emitter, Manager, State, WebviewWindowBuilder, Wry};
use uuid::Uuid;
use crate::wrappers::{get_library, play, pause, prev, set_volume, get_song, next, get_queue, import_playlist, get_playlist, get_playlists, remove_from_queue};
use commands::{add_song_to_queue, play_now, display_album_art};
pub mod wrappers;
pub mod commands;
const DEFAULT_IMAGE: &[u8] = include_bytes!("../icons/icon.png");
#[cfg_attr(mobile, tauri::mobile_entry_point)]
pub fn run() {
let (rx, tx) = unbounded::<Config>();
let (lib_rx, lib_tx) = unbounded::<Option<PathBuf>>();
let (handle_rx, handle_tx) = unbounded::<ControllerHandle>();
let (playback_handle_rx, playback_handle_tx) = unbounded::<crossbeam::channel::Receiver<PlaybackInfo>>();
let controller_thread = spawn(move || {
let mut config = { tx.recv().unwrap() } ;
let scan_path = { lib_tx.recv().unwrap() };
let _temp_config = ConfigLibrary::default();
let _lib = config.libraries.get_default().unwrap_or(&_temp_config);
let save_path = if _lib.path == PathBuf::default() {
scan_path.as_ref().unwrap().clone().canonicalize().unwrap().join("library.dlib")
} else {
_lib.path.clone()
};
println!("save_path: {}\nscan_path:{scan_path:?}", save_path.display());
let mut library = MusicLibrary::init(
save_path.clone(),
_lib.uuid
).unwrap();
let scan_path = scan_path.unwrap_or_else(|| config.libraries.get_default().unwrap().scan_folders.as_ref().unwrap()[0].clone());
if config.libraries.get_default().is_err() {
library.scan_folder(&scan_path).unwrap();
config.push_library( ConfigLibrary::new(save_path.clone(), String::from("Library"), Some(vec![scan_path.clone()]), Some(library.uuid)));
}
if library.library.is_empty() {
println!("library is empty");
} else {
config.write_file().unwrap();
}
println!("scan_path: {}", scan_path.display());
library.save(save_path).unwrap();
let (handle, input, playback_tx) = ControllerHandle::new(
library,
std::sync::Arc::new(std::sync::RwLock::new(config))
);
handle_rx.send(handle).unwrap();
scope(|s| {
s.spawn(|| {
let _controller = futures::executor::block_on(Controller::start(input)).unwrap();
});
s.spawn(|| {
});
})
});
let app = tauri::Builder::default()
.plugin(tauri_plugin_shell::init())
.invoke_handler(tauri::generate_handler![
get_config,
create_new_library,
get_library,
play,
pause,
set_volume,
next,
prev,
get_song,
lib_already_created,
get_queue,
add_song_to_queue,
play_now,
import_playlist,
get_playlist,
get_playlists,
remove_from_queue,
display_album_art,
]).manage(ConfigRx(rx))
.manage(LibRx(lib_rx))
.manage(HandleTx(handle_tx))
.manage(tempfile::TempDir::new().unwrap())
.register_asynchronous_uri_scheme_protocol("asset", move |ctx, req, res| {
let query = req
.clone()
.uri()
.clone()
.into_parts()
.path_and_query
.unwrap()
.query()
.unwrap()
.to_string();
let bytes = if query.as_str() == "default" {
Some(DEFAULT_IMAGE.to_vec())
} else {futures::executor::block_on(async move {
let controller = ctx.app_handle().state::<ControllerHandle>();
controller.lib_mail.send(dmp_core::music_controller::controller::LibraryCommand::Song(Uuid::parse_str(query.as_str()).unwrap())).await.unwrap();
let LibraryResponse::Song(song, _) = controller.lib_mail.recv().await.unwrap() else {
return None
};
Some(song.album_art(0).unwrap_or_else(|_| None).unwrap_or(DEFAULT_IMAGE.to_vec()))
})};
res.respond(
Response::builder()
.header("Origin", "*")
.header("Content-Length", bytes.as_ref().unwrap_or(&vec![]).len())
.status(200)
.body(bytes.unwrap_or_default())
.unwrap()
);
})
.build(tauri::generate_context!())
.expect("error while building tauri application");
app
.run(|_app_handle, event| match event {
tauri::RunEvent::ExitRequested { api, .. } => {
// api.prevent_exit();
panic!("does this kill the player?")
}
_ => {}
});
}
struct ConfigRx(Sender<Config>);
struct LibRx(Sender<Option<PathBuf>>);
struct HandleTx(Receiver<ControllerHandle>);
#[tauri::command]
async fn get_config(state: State<'_, ConfigRx>) -> Result<Config, String> {
if let Some(dir) = directories::ProjectDirs::from("", "Dangoware", "dmp") {
let path = dir.config_dir();
fs::create_dir_all(path).or_else(|err| {
if err.kind() == std::io::ErrorKind::AlreadyExists {
Ok(())
} else {
Err(err)
}
}).unwrap();
let config = if let Ok(mut c) = Config::read_file(PathBuf::from(path).join("config")) {
if c.state_path == PathBuf::default() {
c.state_path = PathBuf::from(path).join("state");
}
c
} else {
let c = Config {
path: PathBuf::from(path).join("config"),
state_path: PathBuf::from(path).join("state"),
..Default::default()
};
c.write_file().unwrap();
c
};
state.inner().0.send(config.clone()).unwrap();
Ok(config)
} else {
panic!("No config dir for DMP")
}
}
#[tauri::command]
async fn create_new_library(
app: tauri::AppHandle<Wry>,
lib_rx: State<'_, LibRx>,
handle_tx: State<'_, HandleTx>,
) -> Result<(), String> {
let dir = rfd::AsyncFileDialog::new()
.set_title("Pick a library path")
.pick_folder()
.await
.unwrap();
let path = dir.path().canonicalize().unwrap();
println!("{}", path.display());
if !path.exists() {
panic!("Path {} does not exist!", path.display())
} else if !path.is_dir() {
panic!("Path {} is not a directory!", path.display())
}
lib_rx.inner().0.send(Some(path)).unwrap();
app.manage(handle_tx.inner().0.recv().unwrap());
app.emit("library_loaded", ()).unwrap();
Ok(())
}
#[tauri::command]
async fn lib_already_created(app: tauri::AppHandle<Wry>, lib_rx: State<'_, LibRx>, handle_tx: State<'_, HandleTx>) -> Result<(), String> {
println!("lib already created");
lib_rx.inner().0.send(None).unwrap();
app.manage(handle_tx.inner().0.recv().unwrap());
app.emit("library_loaded", ()).unwrap();
Ok(())
}