Added, but disabled icon dl impl. Player improvements
This commit is contained in:
@@ -26,3 +26,6 @@ camino.workspace = true
|
||||
lazy_static.workspace = true
|
||||
log.workspace = true
|
||||
uuid.workspace = true
|
||||
reqwest.workspace = true
|
||||
url.workspace = true
|
||||
image.workspace = true
|
||||
|
||||
@@ -0,0 +1,133 @@
|
||||
use std::{collections::HashMap, ffi::OsStr, io::{BufReader, Cursor}, path::PathBuf, process::{Command, Stdio}, str::FromStr, sync::{Arc, Mutex, MutexGuard}};
|
||||
|
||||
use anyhow::anyhow;
|
||||
use image::ImageReader;
|
||||
use xmpd_manifest::song::{IconType, Song, SourceType};
|
||||
|
||||
use crate::{downloader::song::SongStatus, DlStatus};
|
||||
|
||||
lazy_static::lazy_static!(
|
||||
static ref ICON_CACHE_DL: Arc<Mutex<IconCacheDl>> = Arc::default();
|
||||
);
|
||||
|
||||
#[derive(Debug, Default, Clone)]
|
||||
pub struct IconCacheDl {
|
||||
pub jobs: HashMap<uuid::Uuid, DlStatus>,
|
||||
pub current_jobs: usize,
|
||||
}
|
||||
|
||||
impl IconCacheDl {
|
||||
pub fn get() -> crate::Result<MutexGuard<'static, Self>> {
|
||||
match ICON_CACHE_DL.lock() {
|
||||
Ok(v) => Ok(v),
|
||||
Err(e) => anyhow::bail!(format!("{e:?}"))
|
||||
}
|
||||
}
|
||||
pub fn is_job_list_full(&self) -> bool {
|
||||
self.current_jobs >= 5
|
||||
}
|
||||
|
||||
pub fn download(&mut self, sid: uuid::Uuid, song: Song) -> crate::Result<()> {
|
||||
match song.icon_type().clone() {
|
||||
IconType::FromSource => {
|
||||
let tooling = xmpd_settings::Settings::get()?.tooling.clone();
|
||||
match song.source_type() {
|
||||
SourceType::Youtube => {
|
||||
self.jobs.insert(sid.clone(), DlStatus::Downloading);
|
||||
let mut path = xmpd_cliargs::CLIARGS.cache_path();
|
||||
path.push("icons");
|
||||
path.push(sid.to_string());
|
||||
|
||||
let mut cmd = Command::new(tooling.ytdlp_path);
|
||||
cmd.arg(song.url().to_string());
|
||||
cmd.arg("-o");
|
||||
cmd.arg(&path);
|
||||
cmd.args(["--write-thumbnail", "--skip-download"]);
|
||||
if xmpd_cliargs::CLIARGS.debug {
|
||||
cmd.stdout(Stdio::piped());
|
||||
cmd.stderr(Stdio::piped());
|
||||
} else {
|
||||
cmd.stdout(Stdio::null());
|
||||
cmd.stderr(Stdio::null());
|
||||
}
|
||||
let child = cmd.spawn()?;
|
||||
std::thread::spawn(move || {
|
||||
if let Ok(output) = child.wait_with_output() {
|
||||
for line in String::from_utf8(output.stdout).unwrap().lines() {
|
||||
log::info!("CMD: {}", line);
|
||||
}
|
||||
for line in String::from_utf8(output.stderr).unwrap().lines() {
|
||||
log::error!("CMD: {}", line);
|
||||
}
|
||||
}
|
||||
let old_p = path.with_extension("webp"); // Default for yt-dlp
|
||||
let new_p = path.with_extension("png"); // Default for all
|
||||
let old_img = ImageReader::open(&old_p).unwrap().decode().unwrap();
|
||||
old_img.save(&new_p).unwrap();
|
||||
std::fs::remove_file(old_p).unwrap();
|
||||
let mut cache = IconCacheDl::get().unwrap();
|
||||
cache.jobs.insert(sid, DlStatus::Done(Some(new_p.into())));
|
||||
});
|
||||
}
|
||||
SourceType::Spotify => {
|
||||
todo!()
|
||||
}
|
||||
SourceType::Soundcloud => {
|
||||
todo!()
|
||||
}
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
IconType::CustomUrl(url) => self.download_custom_url_icon(&sid, &url)?,
|
||||
IconType::None => ()
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn download_custom_url_icon(&mut self, sid: &uuid::Uuid, url: &url::Url) -> crate::Result<()> {
|
||||
self.jobs.insert(sid.clone(), DlStatus::Downloading);
|
||||
let url_p = PathBuf::from_str(url.path())?;
|
||||
let Some(ext) = url_p.extension() else {
|
||||
anyhow::bail!("Url without extension, cant continue");
|
||||
};
|
||||
let ext = ext.to_string_lossy().to_string();
|
||||
let mut path = xmpd_cliargs::CLIARGS.cache_path();
|
||||
path.push("icons");
|
||||
path.push(sid.to_string());
|
||||
path.set_extension(ext);
|
||||
let sid = sid.clone();
|
||||
let url = url.clone();
|
||||
std::thread::spawn(move || {
|
||||
match reqwest::blocking::get(url.clone()) {
|
||||
Ok(v) => {
|
||||
match v.bytes() {
|
||||
Ok(bytes) => {
|
||||
if let Err(e) = std::fs::write(path, bytes) {
|
||||
if let Ok(mut cache) = IconCacheDl::get() {
|
||||
if let Some(job) = cache.jobs.get_mut(&sid) {
|
||||
*job = DlStatus::Error(file!(), line!() as usize, format!("{e:?}"));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
if let Ok(mut cache) = IconCacheDl::get() {
|
||||
if let Some(job) = cache.jobs.get_mut(&sid) {
|
||||
*job = DlStatus::Error(file!(), line!() as usize, format!("{e:?}"));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
if let Ok(mut cache) = IconCacheDl::get() {
|
||||
if let Some(job) = cache.jobs.get_mut(&sid) {
|
||||
*job = DlStatus::Error(file!(), line!() as usize, format!("{e:?}"));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use std::{collections::HashMap, str::FromStr, sync::{mpsc::{self, Receiver, Sender}, Arc, Mutex, MutexGuard}, time::Duration};
|
||||
use std::{collections::HashMap, path::PathBuf, str::FromStr, sync::{mpsc::{self, Receiver, Sender}, Arc, Mutex, MutexGuard}, time::Duration};
|
||||
use downloader::song::SongStatus;
|
||||
use xmpd_manifest::song::Song;
|
||||
|
||||
@@ -14,20 +14,24 @@ lazy_static::lazy_static!(
|
||||
pub struct Cache {
|
||||
cache_dir: camino::Utf8PathBuf,
|
||||
song_cache: HashMap<uuid::Uuid, DlStatus>,
|
||||
queue: Vec<(uuid::Uuid, Song)>
|
||||
icon_cache: HashMap<uuid::Uuid, DlStatus>,
|
||||
song_queue: Vec<(uuid::Uuid, Song)>,
|
||||
icon_queue: Vec<(uuid::Uuid, Song)>,
|
||||
//meta_queue: Vec<(uuid::Uuid, Song)>
|
||||
// TODO: Add Icon, metadata cache
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum DlStatus {
|
||||
Done(camino::Utf8PathBuf),
|
||||
Done(Option<PathBuf>),
|
||||
Downloading,
|
||||
Error(String),
|
||||
Error(&'static str, usize, String),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum Message {
|
||||
DownloadDone(uuid::Uuid),
|
||||
Error(&'static str, usize, String)
|
||||
}
|
||||
|
||||
impl Cache {
|
||||
@@ -60,7 +64,7 @@ impl Cache {
|
||||
let id = uuid::Uuid::from_str(file_name)?;
|
||||
log::debug!("Found song {id}");
|
||||
// TODO: Check if id is in manifest
|
||||
self.song_cache.insert(id, DlStatus::Done(file_path.to_path_buf()));
|
||||
self.song_cache.insert(id, DlStatus::Done(Some(file_path.into())));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -73,21 +77,35 @@ impl Cache {
|
||||
Ok(cache_rx)
|
||||
}
|
||||
|
||||
pub fn download_to_cache(&mut self, sid: uuid::Uuid, song: Song) {
|
||||
self.queue.push((sid, song));
|
||||
pub fn download_song_to_cache(&mut self, sid: uuid::Uuid, song: Song) {
|
||||
self.song_queue.push((sid, song));
|
||||
self.song_cache.insert(sid, DlStatus::Downloading);
|
||||
}
|
||||
|
||||
pub fn download_to_cache_multiple(&mut self, mut songs: Vec<(uuid::Uuid, Song)>) {
|
||||
while let Some((sid, song)) = songs.pop() {
|
||||
self.download_to_cache(sid, song);
|
||||
}
|
||||
}
|
||||
pub fn download_icon_to_cache(&mut self, sid: uuid::Uuid, song: Song) {
|
||||
self.icon_queue.push((sid, song));
|
||||
self.icon_cache.insert(sid, DlStatus::Downloading);
|
||||
}
|
||||
|
||||
pub fn get_cached_song_status(&mut self, sid: &uuid::Uuid) -> Option<DlStatus> {
|
||||
let original = self.song_cache.get(sid)?.clone();
|
||||
Some(original)
|
||||
}
|
||||
pub fn get_cached_icon_status(&mut self, sid: &uuid::Uuid) -> Option<DlStatus> {
|
||||
let original = self.icon_cache.get(sid)?.clone();
|
||||
Some(original)
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! he {
|
||||
($tx:expr, $val:expr) => {
|
||||
match $val {
|
||||
Ok(v) => v,
|
||||
Err(e) => {
|
||||
let _ = $tx.send(Message::Error(std::file!(), std::line!() as usize, format!("{e:?}")));
|
||||
continue;
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
fn start_cache_mv_thread(tx: Sender<Message>) {
|
||||
@@ -95,19 +113,19 @@ fn start_cache_mv_thread(tx: Sender<Message>) {
|
||||
loop {
|
||||
{
|
||||
std::thread::sleep(Duration::from_millis(500));
|
||||
let song_format = xmpd_settings::Settings::get().unwrap().tooling.song_format.clone();
|
||||
let song_format = he!(tx, xmpd_settings::Settings::get()).tooling.song_format.clone();
|
||||
let mut done_jobs = Vec::new();
|
||||
let mut dlc = downloader::song::SongCacheDl::get().unwrap();
|
||||
let mut dlc = he!(tx, downloader::song::SongCacheDl::get());
|
||||
for (sid, status) in &dlc.jobs {
|
||||
if *status == SongStatus::Done {
|
||||
let mut cache = CACHE.lock().unwrap();
|
||||
let mut cache = he!(tx, CACHE.lock());
|
||||
let mut song_p = xmpd_cliargs::CLIARGS.cache_path().clone();
|
||||
song_p.push("songs");
|
||||
song_p.push(sid.clone().to_string());
|
||||
let song_p = song_p.with_extension(&song_format);
|
||||
if song_p.exists() {
|
||||
let _ = tx.send(Message::DownloadDone(sid.clone()));
|
||||
cache.song_cache.insert(sid.clone(), DlStatus::Done(song_p));
|
||||
cache.song_cache.insert(sid.clone(), DlStatus::Done(Some(song_p.into())));
|
||||
done_jobs.push(sid.clone());
|
||||
}
|
||||
}
|
||||
@@ -115,17 +133,44 @@ fn start_cache_mv_thread(tx: Sender<Message>) {
|
||||
for sid in done_jobs {
|
||||
dlc.jobs.remove(&sid);
|
||||
}
|
||||
{
|
||||
let mut done_jobs = Vec::new();
|
||||
let mut dlc = he!(tx, downloader::icon::IconCacheDl::get());
|
||||
for (sid, status) in &dlc.jobs {
|
||||
if let DlStatus::Done(path) = status {
|
||||
let mut cache = he!(tx, CACHE.lock());
|
||||
cache.icon_cache.insert(sid.clone(), DlStatus::Done(path.clone()));
|
||||
done_jobs.push(sid.clone());
|
||||
}
|
||||
}
|
||||
for sid in done_jobs {
|
||||
dlc.jobs.remove(&sid);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
let mut cache = Cache::get().unwrap();
|
||||
let mut dlc = downloader::song::SongCacheDl::get().unwrap();
|
||||
if !dlc.is_job_list_full() {
|
||||
if let Some((sid, song)) = cache.queue.pop() {
|
||||
dlc.download(sid, song).unwrap();
|
||||
let mut cache = he!(tx, Cache::get());
|
||||
{
|
||||
let mut dlc = he!(tx, downloader::song::SongCacheDl::get());
|
||||
if !dlc.is_job_list_full() {
|
||||
if let Some((sid, song)) = cache.song_queue.pop() {
|
||||
he!(tx, dlc.download(sid, song));
|
||||
}
|
||||
}
|
||||
}
|
||||
{
|
||||
let mut icnc = he!(tx, downloader::icon::IconCacheDl::get());
|
||||
if !icnc.is_job_list_full() {
|
||||
if let Some((sid, song)) = cache.icon_queue.pop() {
|
||||
log::debug!("Downloading {sid:?}");
|
||||
he!(tx, icnc.download(sid, song));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user