From ee4e024d31e500c8c60c514650544f79e3def9ba Mon Sep 17 00:00:00 2001 From: Muaz Ahmad Date: Thu, 28 Nov 2024 17:02:28 +0500 Subject: [PATCH] Parse image from API, trigger event --- src/player/mod.rs | 2 ++ src/ssonic/client.rs | 17 ++++++++++++++--- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/player/mod.rs b/src/player/mod.rs index f1e1cf9..c2246e6 100644 --- a/src/player/mod.rs +++ b/src/player/mod.rs @@ -6,6 +6,7 @@ use std::{ thread, }; +use image::DynamicImage; use ratatui::DefaultTerminal; use crate::{ @@ -17,6 +18,7 @@ use crate::{ pub enum PlayerEvent { AddSongList(Vec), + UpdateCover(DynamicImage), UserQuit, PlayNext, } diff --git a/src/ssonic/client.rs b/src/ssonic/client.rs index 1377edd..301ca52 100644 --- a/src/ssonic/client.rs +++ b/src/ssonic/client.rs @@ -1,3 +1,5 @@ +use std::io::{BufReader, Read}; + use reqwest::{ blocking::Response, header::{ACCEPT, CONTENT_TYPE}, @@ -29,7 +31,7 @@ impl APIClient { } fn get_cover_art(&mut self, cover_id: String) -> Result { - let response = self.request("/getCoverArt", Some(&[("id", cover_id)]))?; + let mut response = self.request("/getCoverArt", Some(&[("id", cover_id)]))?; if !response.status().is_success() { return Err(Box::new(APIError::StatusError( response.status(), @@ -37,8 +39,17 @@ impl APIClient { ))); }; if response.headers()[CONTENT_TYPE].to_str()? == "application/json" { - self.validate(response, "/getCoverArt")?; // will never error - }; + self.validate(response, "/getCoverArt")?; // will never error if not image + } else { + let mut image = Vec::new(); + response.read_to_end(&mut image)?; + let cover = image::load_from_memory_with_format( + &mut image, + image::ImageFormat::from_mime_type(response.headers()[CONTENT_TYPE].to_str()?) + .unwrap(), + )?; + return Ok(PlayerEvent::UpdateCover(cover)); + } unimplemented!() }