tsk-62: Use QueuedSong and QueuedCoverArt to replace references to path of Song and CoverArt (#64)
Closes #62 Reviewed-on: #64 Co-authored-by: phoenix <kundeng00@pm.me> Co-committed-by: phoenix <kundeng00@pm.me>
This commit is contained in:
2
Cargo.lock
generated
2
Cargo.lock
generated
@@ -1322,7 +1322,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "songparser"
|
||||
version = "0.4.6"
|
||||
version = "0.4.7"
|
||||
dependencies = [
|
||||
"futures",
|
||||
"icarus_envy",
|
||||
|
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "songparser"
|
||||
version = "0.4.6"
|
||||
version = "0.4.7"
|
||||
edition = "2024"
|
||||
rust-version = "1.90"
|
||||
|
||||
|
24
src/api.rs
24
src/api.rs
@@ -193,21 +193,20 @@ pub mod refresh_token {
|
||||
pub mod update_queued_song {
|
||||
pub async fn update_queued_song(
|
||||
app: &crate::config::App,
|
||||
song_path: &String,
|
||||
song_queue_id: &uuid::Uuid,
|
||||
queued_song: &crate::queued_item::QueuedSong,
|
||||
) -> Result<reqwest::Response, reqwest::Error> {
|
||||
let client = reqwest::Client::builder().build()?;
|
||||
|
||||
println!("Song path: {song_path:?}");
|
||||
println!("Queued song path: {:?}", queued_song.path);
|
||||
|
||||
// TODO: Make the filename random
|
||||
let form = reqwest::multipart::Form::new().part(
|
||||
"file",
|
||||
reqwest::multipart::Part::bytes(std::fs::read(song_path).unwrap())
|
||||
reqwest::multipart::Part::bytes(std::fs::read(&queued_song.path).unwrap())
|
||||
.file_name("track01.flac"),
|
||||
);
|
||||
|
||||
let url = format!("{}/api/v2/song/queue/{song_queue_id}", app.uri);
|
||||
let url = format!("{}/api/v2/song/queue/{}", app.uri, queued_song.id);
|
||||
println!("Url: {url:?}");
|
||||
|
||||
let (key, header) = crate::api::auth_header(app).await;
|
||||
@@ -274,15 +273,14 @@ pub mod create_song {
|
||||
}
|
||||
|
||||
pub mod create_coverart {
|
||||
|
||||
pub async fn create(
|
||||
app: &crate::config::App,
|
||||
song_id: &uuid::Uuid,
|
||||
coverart_queue_id: &uuid::Uuid,
|
||||
song: &icarus_models::song::Song,
|
||||
queued_coverart: &crate::queued_item::QueuedCoverArt,
|
||||
) -> Result<reqwest::Response, reqwest::Error> {
|
||||
let client = reqwest::Client::builder().build()?;
|
||||
let url = format!("{}/api/v2/coverart", app.uri);
|
||||
let payload = get_payload(song_id, coverart_queue_id);
|
||||
let payload = get_payload(&song.id, &queued_coverart.id);
|
||||
let (key, header) = crate::api::auth_header(app).await;
|
||||
let request = client.post(url).json(&payload).header(key, header);
|
||||
|
||||
@@ -309,12 +307,12 @@ pub mod wipe_data {
|
||||
pub mod song_queue {
|
||||
pub async fn wipe_data(
|
||||
app: &crate::config::App,
|
||||
song_queue_id: &uuid::Uuid,
|
||||
queued_song: &crate::queued_item::QueuedSong,
|
||||
) -> Result<reqwest::Response, reqwest::Error> {
|
||||
let client = reqwest::Client::builder().build()?;
|
||||
let url = format!("{}/api/v2/song/queue/data/wipe", app.uri);
|
||||
let payload = serde_json::json!({
|
||||
"song_queue_id": song_queue_id
|
||||
"song_queue_id": queued_song.id
|
||||
});
|
||||
let (key, header) = crate::api::auth_header(app).await;
|
||||
let request = client.patch(url).json(&payload).header(key, header);
|
||||
@@ -333,12 +331,12 @@ pub mod wipe_data {
|
||||
pub mod coverart_queue {
|
||||
pub async fn wipe_data(
|
||||
app: &crate::config::App,
|
||||
coverart_queue_id: &uuid::Uuid,
|
||||
queued_coverart: &crate::queued_item::QueuedCoverArt,
|
||||
) -> Result<reqwest::Response, reqwest::Error> {
|
||||
let client = reqwest::Client::builder().build()?;
|
||||
let url = format!("{}/api/v2/coverart/queue/data/wipe", app.uri);
|
||||
let payload = serde_json::json!({
|
||||
"coverart_queue_id": coverart_queue_id
|
||||
"coverart_queue_id": queued_coverart.id
|
||||
});
|
||||
let (key, header) = crate::api::auth_header(app).await;
|
||||
let request = client.patch(url).json(&payload).header(key, header);
|
||||
|
116
src/main.rs
116
src/main.rs
@@ -2,6 +2,7 @@ pub mod api;
|
||||
pub mod auth;
|
||||
pub mod config;
|
||||
pub mod metadata;
|
||||
pub mod queued_item;
|
||||
pub mod util;
|
||||
|
||||
pub const SECONDS_TO_SLEEP: u64 = 5;
|
||||
@@ -59,14 +60,8 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let user_id = song_queue_item.data[0].user_id;
|
||||
|
||||
match some_work(&app, &song_queue_id, &user_id).await {
|
||||
Ok((
|
||||
song,
|
||||
coverart,
|
||||
(song_queue_id, _song_queue_path),
|
||||
(coverart_queue_id, _coverart_queue_path),
|
||||
)) => {
|
||||
match wipe_data_from_queues(&app, &song_queue_id, &coverart_queue_id)
|
||||
.await
|
||||
Ok((song, coverart, _metadata, queued_song, queued_coverart)) => {
|
||||
match wipe_data_from_queues(&app, &queued_song, &queued_coverart).await
|
||||
{
|
||||
Ok(_) => match cleanup(&song, &coverart).await {
|
||||
Ok(_) => {
|
||||
@@ -101,16 +96,16 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
|
||||
async fn wipe_data_from_queues(
|
||||
app: &config::App,
|
||||
song_queue_id: &uuid::Uuid,
|
||||
coverart_queue_id: &uuid::Uuid,
|
||||
queued_song: &crate::queued_item::QueuedSong,
|
||||
queued_coverart: &crate::queued_item::QueuedCoverArt,
|
||||
) -> Result<(), std::io::Error> {
|
||||
match api::wipe_data::song_queue::wipe_data(app, song_queue_id).await {
|
||||
match api::wipe_data::song_queue::wipe_data(app, queued_song).await {
|
||||
Ok(response) => match response
|
||||
.json::<api::wipe_data::song_queue::response::Response>()
|
||||
.await
|
||||
{
|
||||
Ok(_resp) => {
|
||||
match api::wipe_data::coverart_queue::wipe_data(app, coverart_queue_id).await {
|
||||
match api::wipe_data::coverart_queue::wipe_data(app, queued_coverart).await {
|
||||
Ok(inner_response) => match inner_response
|
||||
.json::<api::wipe_data::coverart_queue::response::Response>()
|
||||
.await
|
||||
@@ -179,50 +174,19 @@ async fn some_work(
|
||||
(
|
||||
icarus_models::song::Song,
|
||||
icarus_models::coverart::CoverArt,
|
||||
(uuid::Uuid, String),
|
||||
(uuid::Uuid, String),
|
||||
api::get_metadata_queue::response::Metadata,
|
||||
queued_item::QueuedSong,
|
||||
queued_item::QueuedCoverArt,
|
||||
),
|
||||
std::io::Error,
|
||||
> {
|
||||
match prep_song(app, song_queue_id).await {
|
||||
Ok((
|
||||
(song_directory, song_filename),
|
||||
(coverart_directory, coverart_filename),
|
||||
metadata,
|
||||
coverart_queue_id,
|
||||
)) => {
|
||||
Ok((queued_song, queued_coverart, metadata)) => {
|
||||
println!("Prepping song");
|
||||
|
||||
let mut song_queue_path: String = String::new();
|
||||
let p = std::path::Path::new(&song_directory);
|
||||
let sp = p.join(&song_filename);
|
||||
song_queue_path.push_str(sp.to_str().unwrap_or_default());
|
||||
let coverart_queue = icarus_models::coverart::CoverArt {
|
||||
directory: coverart_directory,
|
||||
filename: coverart_filename,
|
||||
..Default::default()
|
||||
};
|
||||
let coverart_queue_path = match coverart_queue.get_path() {
|
||||
Ok(path) => path,
|
||||
Err(err) => {
|
||||
eprintln!("Could not get CoverArt path");
|
||||
eprintln!("Error: {err:?}");
|
||||
std::process::exit(-1);
|
||||
}
|
||||
};
|
||||
|
||||
println!("CoverArt path: {coverart_queue_path:?}");
|
||||
|
||||
match metadata::apply_metadata(&song_queue_path, &coverart_queue_path, &metadata).await
|
||||
{
|
||||
match metadata::apply_metadata(&queued_song, &queued_coverart, &metadata).await {
|
||||
Ok(_applied) => {
|
||||
match api::update_queued_song::update_queued_song(
|
||||
app,
|
||||
&song_queue_path,
|
||||
song_queue_id,
|
||||
)
|
||||
.await
|
||||
{
|
||||
match api::update_queued_song::update_queued_song(app, &queued_song).await {
|
||||
Ok(response) => {
|
||||
match response
|
||||
.json::<api::update_queued_song::response::Response>()
|
||||
@@ -249,19 +213,19 @@ async fn some_work(
|
||||
println!("Response: {resp:?}");
|
||||
|
||||
let mut song = resp.data[0].clone();
|
||||
song.directory = song_directory;
|
||||
song.filename = song_filename;
|
||||
song.directory = queued_song.song.directory.clone();
|
||||
song.filename = queued_song.song.filename.clone();
|
||||
|
||||
match api::create_coverart::create(app, &song.id, &coverart_queue_id).await {
|
||||
match api::create_coverart::create(app, &song, &queued_coverart).await {
|
||||
Ok(response) => match response.json::<api::create_coverart::response::Response>().await {
|
||||
Ok(resp) => {
|
||||
println!("CoverArt sent and successfully parsed response");
|
||||
println!("json: {resp:?}");
|
||||
let mut coverart = resp.data[0].clone();
|
||||
coverart.directory = coverart_queue.directory;
|
||||
coverart.filename = coverart_queue.filename;
|
||||
coverart.directory = queued_coverart.coverart.directory.clone();
|
||||
coverart.filename = queued_coverart.coverart.filename.clone();
|
||||
|
||||
Ok((song.clone(), coverart.clone(), (metadata.song_queue_id, song_queue_path), (coverart_queue_id, coverart_queue_path)))
|
||||
Ok((song.clone(), coverart.clone(), metadata, queued_song.clone(), queued_coverart.clone()))
|
||||
}
|
||||
Err(err) => {
|
||||
Err(std::io::Error::other(err.to_string()))
|
||||
@@ -295,10 +259,9 @@ async fn prep_song(
|
||||
song_queue_id: &uuid::Uuid,
|
||||
) -> Result<
|
||||
(
|
||||
(String, String),
|
||||
(String, String),
|
||||
queued_item::QueuedSong,
|
||||
queued_item::QueuedCoverArt,
|
||||
api::get_metadata_queue::response::Metadata,
|
||||
uuid::Uuid,
|
||||
),
|
||||
reqwest::Error,
|
||||
> {
|
||||
@@ -317,14 +280,25 @@ async fn prep_song(
|
||||
..Default::default()
|
||||
};
|
||||
let songpath = song.song_path().unwrap_or_default();
|
||||
let song_queue_path = match song.save_to_filesystem() {
|
||||
Ok(_) => std::path::Path::new(&songpath),
|
||||
Err(_err) => std::path::Path::new(""),
|
||||
};
|
||||
|
||||
println!("Saved at: {song_queue_path:?}");
|
||||
let queued_song: crate::queued_item::QueuedSong =
|
||||
match song.save_to_filesystem() {
|
||||
Ok(_) => queued_item::QueuedSong {
|
||||
id: *song_queue_id,
|
||||
song,
|
||||
path: songpath,
|
||||
},
|
||||
Err(err) => {
|
||||
eprintln!("Error: {err:?}");
|
||||
queued_item::QueuedSong {
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
match api::get_metadata_queue::get(app, song_queue_id).await {
|
||||
println!("Saved at: {:?}", queued_song.path);
|
||||
|
||||
match api::get_metadata_queue::get(app, &queued_song.id).await {
|
||||
Ok(response) => {
|
||||
match response
|
||||
.json::<api::get_metadata_queue::response::Response>()
|
||||
@@ -339,7 +313,7 @@ async fn prep_song(
|
||||
println!("Created at: {created_at:?}");
|
||||
|
||||
println!("Getting coverart queue");
|
||||
match api::get_coverart_queue::get(app, song_queue_id).await {
|
||||
match api::get_coverart_queue::get(app, &queued_song.id).await {
|
||||
Ok(response) => {
|
||||
match response.json::<api::get_coverart_queue::response::Response>().await {
|
||||
Ok(response) => {
|
||||
@@ -367,10 +341,16 @@ async fn prep_song(
|
||||
std::process::exit(-1);
|
||||
}
|
||||
};
|
||||
let coverart_queue_path = std::path::Path::new(&coverart_queue_fs_path);
|
||||
println!("Saved coverart queue file at: {coverart_queue_path:?}");
|
||||
|
||||
Ok(((song.directory, song.filename), (coverart.directory, coverart.filename), metadata.clone(), coverart_queue_id))
|
||||
let queued_coverart = queued_item::QueuedCoverArt {
|
||||
id: coverart_queue_id,
|
||||
coverart,
|
||||
path: coverart_queue_fs_path
|
||||
};
|
||||
|
||||
println!("Saved coverart queue file at: {:?}", queued_coverart.path);
|
||||
|
||||
Ok((queued_song, queued_coverart, metadata.clone()))
|
||||
}
|
||||
Err(err) => {
|
||||
Err(err)
|
||||
|
@@ -1,6 +1,7 @@
|
||||
/// Applies metadata to the queued song
|
||||
pub async fn apply_metadata(
|
||||
song_queue_path: &String,
|
||||
coverart_queue_path: &String,
|
||||
queued_song: &crate::queued_item::QueuedSong,
|
||||
queued_coverart: &crate::queued_item::QueuedCoverArt,
|
||||
metadata: &crate::api::get_metadata_queue::response::Metadata,
|
||||
) -> Result<bool, std::io::Error> {
|
||||
// Apply metadata fields
|
||||
@@ -11,7 +12,7 @@ pub async fn apply_metadata(
|
||||
icarus_meta::types::Type::Album => {
|
||||
let meta_type =
|
||||
icarus_meta::types::MetadataType::from_string(metadata.album.clone());
|
||||
match icarus_meta::meta::metadata::set_meta_value(t, song_queue_path, meta_type) {
|
||||
match icarus_meta::meta::metadata::set_meta_value(t, &queued_song.path, meta_type) {
|
||||
Ok(_) => {}
|
||||
Err(_err) => {
|
||||
return Err(_err);
|
||||
@@ -21,7 +22,7 @@ pub async fn apply_metadata(
|
||||
icarus_meta::types::Type::AlbumArtist => {
|
||||
let meta_type =
|
||||
icarus_meta::types::MetadataType::from_string(metadata.album_artist.clone());
|
||||
match icarus_meta::meta::metadata::set_meta_value(t, song_queue_path, meta_type) {
|
||||
match icarus_meta::meta::metadata::set_meta_value(t, &queued_song.path, meta_type) {
|
||||
Ok(_) => {}
|
||||
Err(_err) => {
|
||||
return Err(_err);
|
||||
@@ -31,7 +32,7 @@ pub async fn apply_metadata(
|
||||
icarus_meta::types::Type::Artist => {
|
||||
let meta_type =
|
||||
icarus_meta::types::MetadataType::from_string(metadata.artist.clone());
|
||||
match icarus_meta::meta::metadata::set_meta_value(t, song_queue_path, meta_type) {
|
||||
match icarus_meta::meta::metadata::set_meta_value(t, &queued_song.path, meta_type) {
|
||||
Ok(_) => {}
|
||||
Err(_err) => {
|
||||
return Err(_err);
|
||||
@@ -42,7 +43,7 @@ pub async fn apply_metadata(
|
||||
// TODO: Do something about this discrepancy
|
||||
let meta_type =
|
||||
icarus_meta::types::MetadataType::from_string(metadata.year.to_string());
|
||||
match icarus_meta::meta::metadata::set_meta_value(t, song_queue_path, meta_type) {
|
||||
match icarus_meta::meta::metadata::set_meta_value(t, &queued_song.path, meta_type) {
|
||||
Ok(_) => {}
|
||||
Err(_err) => {
|
||||
return Err(_err);
|
||||
@@ -51,7 +52,7 @@ pub async fn apply_metadata(
|
||||
}
|
||||
icarus_meta::types::Type::Disc => {
|
||||
let meta_type = icarus_meta::types::MetadataType::from_int(metadata.disc);
|
||||
match icarus_meta::meta::metadata::set_meta_value(t, song_queue_path, meta_type) {
|
||||
match icarus_meta::meta::metadata::set_meta_value(t, &queued_song.path, meta_type) {
|
||||
Ok(_) => {}
|
||||
Err(_err) => {
|
||||
return Err(_err);
|
||||
@@ -61,7 +62,7 @@ pub async fn apply_metadata(
|
||||
icarus_meta::types::Type::Genre => {
|
||||
let meta_type =
|
||||
icarus_meta::types::MetadataType::from_string(metadata.genre.clone());
|
||||
match icarus_meta::meta::metadata::set_meta_value(t, song_queue_path, meta_type) {
|
||||
match icarus_meta::meta::metadata::set_meta_value(t, &queued_song.path, meta_type) {
|
||||
Ok(_) => {}
|
||||
Err(_err) => {
|
||||
return Err(_err);
|
||||
@@ -71,7 +72,7 @@ pub async fn apply_metadata(
|
||||
icarus_meta::types::Type::Title => {
|
||||
let meta_type =
|
||||
icarus_meta::types::MetadataType::from_string(metadata.title.clone());
|
||||
match icarus_meta::meta::metadata::set_meta_value(t, song_queue_path, meta_type) {
|
||||
match icarus_meta::meta::metadata::set_meta_value(t, &queued_song.path, meta_type) {
|
||||
Ok(_) => {}
|
||||
Err(_err) => {
|
||||
return Err(_err);
|
||||
@@ -80,7 +81,7 @@ pub async fn apply_metadata(
|
||||
}
|
||||
icarus_meta::types::Type::Track => {
|
||||
let meta_type = icarus_meta::types::MetadataType::from_int(metadata.track);
|
||||
match icarus_meta::meta::metadata::set_meta_value(t, song_queue_path, meta_type) {
|
||||
match icarus_meta::meta::metadata::set_meta_value(t, &queued_song.path, meta_type) {
|
||||
Ok(_) => {}
|
||||
Err(_err) => {
|
||||
return Err(_err);
|
||||
@@ -89,7 +90,7 @@ pub async fn apply_metadata(
|
||||
}
|
||||
icarus_meta::types::Type::TrackCount => {
|
||||
let meta_type = icarus_meta::types::MetadataType::from_int(metadata.track_count);
|
||||
match icarus_meta::meta::metadata::set_meta_value(t, song_queue_path, meta_type) {
|
||||
match icarus_meta::meta::metadata::set_meta_value(t, &queued_song.path, meta_type) {
|
||||
Ok(_) => {}
|
||||
Err(_err) => {
|
||||
return Err(_err);
|
||||
@@ -98,7 +99,7 @@ pub async fn apply_metadata(
|
||||
}
|
||||
icarus_meta::types::Type::DiscCount => {
|
||||
let meta_type = icarus_meta::types::MetadataType::from_int(metadata.disc_count);
|
||||
match icarus_meta::meta::metadata::set_meta_value(t, song_queue_path, meta_type) {
|
||||
match icarus_meta::meta::metadata::set_meta_value(t, &queued_song.path, meta_type) {
|
||||
Ok(_) => {}
|
||||
Err(_err) => {
|
||||
return Err(_err);
|
||||
@@ -109,11 +110,11 @@ pub async fn apply_metadata(
|
||||
}
|
||||
|
||||
// Apply coverart
|
||||
match icarus_meta::meta::coverart::contains_coverart(song_queue_path) {
|
||||
match icarus_meta::meta::coverart::contains_coverart(&queued_song.path) {
|
||||
Ok((exists, size)) => {
|
||||
if exists {
|
||||
println!("Coverart exists: {size:?} size");
|
||||
match icarus_meta::meta::coverart::remove_coverart(song_queue_path) {
|
||||
match icarus_meta::meta::coverart::remove_coverart(&queued_song.path) {
|
||||
Ok(_data) => {}
|
||||
Err(err) => {
|
||||
return Err(err);
|
||||
@@ -121,7 +122,10 @@ pub async fn apply_metadata(
|
||||
}
|
||||
}
|
||||
|
||||
match icarus_meta::meta::coverart::set_coverart(song_queue_path, coverart_queue_path) {
|
||||
match icarus_meta::meta::coverart::set_coverart(
|
||||
&queued_song.path,
|
||||
&queued_coverart.path,
|
||||
) {
|
||||
Ok(_data) => {
|
||||
if _data.is_empty() {
|
||||
println!("There was an issue");
|
||||
|
13
src/queued_item.rs
Normal file
13
src/queued_item.rs
Normal file
@@ -0,0 +1,13 @@
|
||||
#[derive(Clone, Debug, Default)]
|
||||
pub struct QueuedSong {
|
||||
pub id: uuid::Uuid,
|
||||
pub song: icarus_models::song::Song,
|
||||
pub path: String,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Default)]
|
||||
pub struct QueuedCoverArt {
|
||||
pub id: uuid::Uuid,
|
||||
pub coverart: icarus_models::coverart::CoverArt,
|
||||
pub path: String,
|
||||
}
|
Reference in New Issue
Block a user