Get queued coverart data (#26)
All checks were successful
Release Tagging / release (push) Successful in 28s
Rust Build / Check (push) Successful in 31s
Rust Build / Test Suite (push) Successful in 33s
Rust Build / Rustfmt (push) Successful in 24s
Rust Build / Clippy (push) Successful in 31s
Rust Build / build (push) Successful in 37s
Rust Build / Check (pull_request) Successful in 32s
Rust Build / Test Suite (pull_request) Successful in 32s
Rust Build / Rustfmt (pull_request) Successful in 23s
Rust Build / Clippy (pull_request) Successful in 31s
Rust Build / build (pull_request) Successful in 37s

Reviewed-on: #26
Co-authored-by: kdeng00 <kundeng00@pm.me>
Co-committed-by: kdeng00 <kundeng00@pm.me>
This commit is contained in:
2025-06-19 00:54:13 +00:00
committed by phoenix
parent 557264482f
commit 29b806fd02
3 changed files with 90 additions and 32 deletions

1
Cargo.lock generated
View File

@@ -1172,6 +1172,7 @@ dependencies = [
"futures",
"icarus_envy",
"icarus_models",
"rand",
"reqwest",
"serde",
"serde_json",

View File

@@ -12,5 +12,6 @@ serde = { version = "1.0.218", features = ["derive"] }
serde_json = { version = "1.0.139" }
time = { version = "0.3.41", features = ["macros", "serde"] }
uuid = { version = "1.16.0", features = ["v4", "serde"] }
rand = { version = "0.9" }
icarus_models = { git = "ssh://git@git.kundeng.us/phoenix/icarus_models.git", tag = "v0.4.3" }
icarus_envy = { git = "ssh://git@git.kundeng.us/phoenix/icarus_envy.git", tag = "v0.2.2-devel-84ea6e4c22-006" }

View File

@@ -59,10 +59,10 @@ async fn process_song(api_url: &String, song_queue_id: &uuid::Uuid) -> Result<()
match api::fetch_song_queue_data::get_data(api_url, song_queue_id).await {
Ok(response) => {
// Process data here...
match api::fetch_song_queue_data::response::parse_response(response).await {
Ok(all_bytes) => {
match api::parsing::parse_response_into_bytes(response).await {
Ok(song_bytes) => {
let (directory, filename) = generate_song_queue_dir_and_filename().await;
let save_path = save_song_to_fs(&directory, &filename, &all_bytes).await;
let save_path = save_file_to_fs(&directory, &filename, &song_bytes).await;
println!("Saved at: {:?}", save_path);
@@ -88,13 +88,30 @@ async fn process_song(api_url: &String, song_queue_id: &uuid::Uuid) -> Result<()
Ok(response) => {
let coverart_queue_id = &response.data[0].id;
println!("Coverart queue Id: {:?}", coverart_queue_id);
// TODO: Get queued coverart's data
// TODO: Apply metadata to the queued song (modifying file)
// TODO: Update the queued song with the updated queued song
// TODO: Create song
// TODO: Create coverart
// TODO: Wipe data from queued song
// TODO: Wipe data from queued coverart
match api::get_coverart_queue::get_data(api_url, coverart_queue_id).await {
Ok(response) => match api::parsing::parse_response_into_bytes(response).await {
Ok(coverart_queue_bytes) => {
let (directory, filename) = generate_coverart_queue_dir_and_filename().await;
let save_path = save_file_to_fs(&directory, &filename, &coverart_queue_bytes).await;
println!("Saved coverart queue file at: {:?}", save_path);
// TODO: Apply metadata to the queued song (modifying file)
// TODO: Update the queued song with the updated queued song
// TODO: Create song
// TODO: Create coverart
// TODO: Wipe data from queued song
// TODO: Wipe data from queued coverart
}
Err(err) => {
eprintln!("Error: {:?}", err);
}
}
Err(err) => {
eprintln!("Error: {:?}", err);
}
}
}
Err(err) => {
eprintln!("Error: {:?}", err);
@@ -136,8 +153,37 @@ pub async fn generate_song_queue_dir_and_filename() -> (String, String) {
(song.directory, song.filename)
}
// TODO: Consider having something like this in icarus_models
pub async fn generate_coverart_queue_dir_and_filename() -> (String, String) {
use rand::Rng;
let mut filename: String = String::new();
let filename_len = 10;
let some_chars: String = String::from("abcdefghij0123456789");
let mut rng = rand::rng();
for _i in 0..filename_len {
let random_number: i32 = rng.random_range(0..=19);
let index = random_number as usize;
let rando_char = some_chars.chars().nth(index);
if let Some(c) = rando_char {
filename.push(c);
}
}
// TODO: Do not hard code the file extension
filename += ".jpeg";
// TODO: Consider separating song and coverart when saving to the filesystem
let directory = icarus_envy::environment::get_root_directory().await;
(directory, filename)
}
// TODO: Check to see if this is available in icarus_models
pub async fn save_song_to_fs(
pub async fn save_file_to_fs(
directory: &String,
filename: &String,
data: &[u8],
@@ -182,6 +228,26 @@ mod api {
client.get(api_url).send().await
}
pub mod parsing {
use futures::StreamExt;
pub async fn parse_response_into_bytes(
response: reqwest::Response,
) -> Result<Vec<u8>, reqwest::Error> {
// TODO: At some point, handle the flow if the size is small or
// large
let mut byte_stream = response.bytes_stream();
let mut all_bytes = Vec::new();
while let Some(chunk) = byte_stream.next().await {
let chunk = chunk?;
all_bytes.extend_from_slice(&chunk);
}
Ok(all_bytes)
}
}
pub mod fetch_song_queue_data {
pub async fn get_data(
base_url: &String,
@@ -192,26 +258,6 @@ mod api {
let api_url = format!("{}/{}/{}", base_url, endpoint, id);
client.get(api_url).send().await
}
pub mod response {
use futures::StreamExt;
pub async fn parse_response(
response: reqwest::Response,
) -> Result<Vec<u8>, reqwest::Error> {
// TODO: At some point, handle the flow if the size is small or
// large
let mut byte_stream = response.bytes_stream();
let mut all_bytes = Vec::new();
while let Some(chunk) = byte_stream.next().await {
let chunk = chunk?;
all_bytes.extend_from_slice(&chunk);
}
Ok(all_bytes)
}
}
}
pub mod get_metadata_queue {
@@ -234,7 +280,7 @@ mod api {
#[derive(Debug, Deserialize, Serialize)]
pub struct Metadata {
pub id: uuid::Uuid,
pub song_queue_id: uuid::Uuid,
pub album: String,
pub album_artist: String,
pub artist: String,
@@ -280,6 +326,16 @@ mod api {
.await
}
pub async fn get_data(
base_url: &String,
coverart_queue_id: &uuid::Uuid,
) -> Result<reqwest::Response, reqwest::Error> {
let client = reqwest::Client::new();
let endpoint = String::from("api/v2/coverart/queue/data");
let api_url = format!("{}/{}/{}", base_url, endpoint, coverart_queue_id);
client.get(api_url).send().await
}
pub mod response {
use serde::{Deserialize, Serialize};