Compare commits
2 Commits
main
...
12921c26c3
| Author | SHA1 | Date | |
|---|---|---|---|
|
12921c26c3
|
|||
|
c2e2e1b03a
|
6
Cargo.lock
generated
6
Cargo.lock
generated
@@ -520,8 +520,8 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "icarus_models"
|
name = "icarus_models"
|
||||||
version = "0.9.2"
|
version = "0.8.0"
|
||||||
source = "git+ssh://git@git.kundeng.us/phoenix/icarus_models.git?tag=v0.9.2#05525618514409101c1d6474dafb201386d14a30"
|
source = "git+ssh://git@git.kundeng.us/phoenix/icarus_models.git?tag=v0.8.0#860d684e7b340b05d43d1fc3e6bd36b7cc45d81b"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"josekit",
|
"josekit",
|
||||||
"rand",
|
"rand",
|
||||||
@@ -1322,7 +1322,7 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "songparser"
|
name = "songparser"
|
||||||
version = "0.4.12"
|
version = "0.4.6"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"futures",
|
"futures",
|
||||||
"icarus_envy",
|
"icarus_envy",
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "songparser"
|
name = "songparser"
|
||||||
version = "0.4.12"
|
version = "0.4.6"
|
||||||
edition = "2024"
|
edition = "2024"
|
||||||
rust-version = "1.90"
|
rust-version = "1.90"
|
||||||
|
|
||||||
@@ -14,5 +14,5 @@ time = { version = "0.3.44", features = ["macros", "serde"] }
|
|||||||
uuid = { version = "1.18.1", features = ["v4", "serde"] }
|
uuid = { version = "1.18.1", features = ["v4", "serde"] }
|
||||||
rand = { version = "0.9.2" }
|
rand = { version = "0.9.2" }
|
||||||
icarus_meta = { git = "ssh://git@git.kundeng.us/phoenix/icarus_meta.git", tag = "v0.4.3" }
|
icarus_meta = { git = "ssh://git@git.kundeng.us/phoenix/icarus_meta.git", tag = "v0.4.3" }
|
||||||
icarus_models = { git = "ssh://git@git.kundeng.us/phoenix/icarus_models.git", tag = "v0.9.2" }
|
icarus_models = { git = "ssh://git@git.kundeng.us/phoenix/icarus_models.git", tag = "v0.8.0" }
|
||||||
icarus_envy = { git = "ssh://git@git.kundeng.us/phoenix/icarus_envy.git", tag = "v0.5.0" }
|
icarus_envy = { git = "ssh://git@git.kundeng.us/phoenix/icarus_envy.git", tag = "v0.5.0" }
|
||||||
|
|||||||
14
src/api.rs
14
src/api.rs
@@ -1,4 +1,5 @@
|
|||||||
pub mod fetch_next_queue_item {
|
pub mod fetch_next_queue_item {
|
||||||
|
|
||||||
pub async fn fetch_next_queue_item(
|
pub async fn fetch_next_queue_item(
|
||||||
app: &crate::config::App,
|
app: &crate::config::App,
|
||||||
) -> Result<reqwest::Response, reqwest::Error> {
|
) -> Result<reqwest::Response, reqwest::Error> {
|
||||||
@@ -11,7 +12,9 @@ pub mod fetch_next_queue_item {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub mod response {
|
pub mod response {
|
||||||
#[derive(Debug, serde::Deserialize, serde::Serialize)]
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
#[derive(Debug, Deserialize, Serialize)]
|
||||||
pub struct QueueItem {
|
pub struct QueueItem {
|
||||||
pub id: uuid::Uuid,
|
pub id: uuid::Uuid,
|
||||||
pub filename: String,
|
pub filename: String,
|
||||||
@@ -19,7 +22,7 @@ pub mod fetch_next_queue_item {
|
|||||||
pub user_id: uuid::Uuid,
|
pub user_id: uuid::Uuid,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, serde::Deserialize, serde::Serialize)]
|
#[derive(Debug, Deserialize, Serialize)]
|
||||||
pub struct SongQueueItem {
|
pub struct SongQueueItem {
|
||||||
pub message: String,
|
pub message: String,
|
||||||
pub data: Vec<QueueItem>,
|
pub data: Vec<QueueItem>,
|
||||||
@@ -196,10 +199,11 @@ pub mod update_queued_song {
|
|||||||
|
|
||||||
println!("Queued song path: {:?}", queued_song.path);
|
println!("Queued song path: {:?}", queued_song.path);
|
||||||
|
|
||||||
|
// TODO: Make the filename random
|
||||||
let form = reqwest::multipart::Form::new().part(
|
let form = reqwest::multipart::Form::new().part(
|
||||||
"file",
|
"file",
|
||||||
reqwest::multipart::Part::bytes(std::fs::read(&queued_song.path).unwrap())
|
reqwest::multipart::Part::bytes(std::fs::read(&queued_song.path).unwrap())
|
||||||
.file_name(queued_song.song.filename.clone()),
|
.file_name("track01.flac"),
|
||||||
);
|
);
|
||||||
|
|
||||||
let url = format!("{}/api/v2/song/queue/{}", app.uri, queued_song.id);
|
let url = format!("{}/api/v2/song/queue/{}", app.uri, queued_song.id);
|
||||||
@@ -303,7 +307,7 @@ pub mod wipe_data {
|
|||||||
pub mod song_queue {
|
pub mod song_queue {
|
||||||
pub async fn wipe_data(
|
pub async fn wipe_data(
|
||||||
app: &crate::config::App,
|
app: &crate::config::App,
|
||||||
queued_song: &crate::queued_item::QueuedSong,
|
queued_song: &crate::queued_item::QueuedSong
|
||||||
) -> Result<reqwest::Response, reqwest::Error> {
|
) -> Result<reqwest::Response, reqwest::Error> {
|
||||||
let client = reqwest::Client::builder().build()?;
|
let client = reqwest::Client::builder().build()?;
|
||||||
let url = format!("{}/api/v2/song/queue/data/wipe", app.uri);
|
let url = format!("{}/api/v2/song/queue/data/wipe", app.uri);
|
||||||
@@ -327,7 +331,7 @@ pub mod wipe_data {
|
|||||||
pub mod coverart_queue {
|
pub mod coverart_queue {
|
||||||
pub async fn wipe_data(
|
pub async fn wipe_data(
|
||||||
app: &crate::config::App,
|
app: &crate::config::App,
|
||||||
queued_coverart: &crate::queued_item::QueuedCoverArt,
|
queued_coverart: &crate::queued_item::QueuedCoverArt
|
||||||
) -> Result<reqwest::Response, reqwest::Error> {
|
) -> Result<reqwest::Response, reqwest::Error> {
|
||||||
let client = reqwest::Client::builder().build()?;
|
let client = reqwest::Client::builder().build()?;
|
||||||
let url = format!("{}/api/v2/coverart/queue/data/wipe", app.uri);
|
let url = format!("{}/api/v2/coverart/queue/data/wipe", app.uri);
|
||||||
|
|||||||
@@ -3,25 +3,4 @@ pub struct App {
|
|||||||
pub uri: String,
|
pub uri: String,
|
||||||
pub auth_uri: String,
|
pub auth_uri: String,
|
||||||
pub token: icarus_models::login_result::LoginResult,
|
pub token: icarus_models::login_result::LoginResult,
|
||||||
pub root_directory: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl App {
|
|
||||||
pub fn does_root_directory_exists(&self) -> bool {
|
|
||||||
let path = std::path::Path::new(&self.root_directory);
|
|
||||||
if path.exists() { path.is_dir() } else { false }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn initialize_app_config() -> App {
|
|
||||||
App {
|
|
||||||
uri: icarus_envy::environment::get_icarus_base_api_url()
|
|
||||||
.await
|
|
||||||
.value,
|
|
||||||
auth_uri: icarus_envy::environment::get_icarus_auth_base_api_url()
|
|
||||||
.await
|
|
||||||
.value,
|
|
||||||
root_directory: icarus_envy::environment::get_root_directory().await.value,
|
|
||||||
..Default::default()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
408
src/main.rs
408
src/main.rs
@@ -2,8 +2,6 @@ pub mod api;
|
|||||||
pub mod auth;
|
pub mod auth;
|
||||||
pub mod config;
|
pub mod config;
|
||||||
pub mod metadata;
|
pub mod metadata;
|
||||||
pub mod parser;
|
|
||||||
pub mod queue;
|
|
||||||
pub mod queued_item;
|
pub mod queued_item;
|
||||||
pub mod util;
|
pub mod util;
|
||||||
|
|
||||||
@@ -11,27 +9,22 @@ pub const SECONDS_TO_SLEEP: u64 = 5;
|
|||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
let mut app = config::initialize_app_config().await;
|
let mut app = config::App {
|
||||||
|
uri: icarus_envy::environment::get_icarus_base_api_url()
|
||||||
|
.await
|
||||||
|
.value,
|
||||||
|
auth_uri: icarus_envy::environment::get_icarus_auth_base_api_url()
|
||||||
|
.await
|
||||||
|
.value,
|
||||||
|
..Default::default()
|
||||||
|
};
|
||||||
println!("Base URL: {:?}", app.uri);
|
println!("Base URL: {:?}", app.uri);
|
||||||
println!("Auth URL: {:?}", app.auth_uri);
|
println!("Auth URL: {:?}", app.auth_uri);
|
||||||
|
|
||||||
if !app.does_root_directory_exists() {
|
match auth::get_token(&app).await {
|
||||||
eprintln!("Root directory does not exist");
|
Ok(login_result) => {
|
||||||
println!("Attempting to create directory");
|
app.token = login_result;
|
||||||
let path = std::path::Path::new(&app.root_directory);
|
|
||||||
match std::fs::create_dir(path) {
|
|
||||||
Ok(_) => {
|
|
||||||
println!("Directory created");
|
|
||||||
}
|
|
||||||
Err(err) => {
|
|
||||||
eprintln!("Error creating directory: {err:?}");
|
|
||||||
std::process::exit(-1);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
app.token = match auth::get_token(&app).await {
|
|
||||||
Ok(login_result) => login_result,
|
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
eprintln!("Error: {err:?}");
|
eprintln!("Error: {err:?}");
|
||||||
std::process::exit(-1);
|
std::process::exit(-1);
|
||||||
@@ -39,6 +32,8 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
};
|
};
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
|
println!("Token: {:?}", app.token);
|
||||||
|
|
||||||
if app.token.token_expired() {
|
if app.token.token_expired() {
|
||||||
println!("Token expired");
|
println!("Token expired");
|
||||||
app.token = match auth::get_refresh_token(&app).await {
|
app.token = match auth::get_refresh_token(&app).await {
|
||||||
@@ -55,7 +50,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
println!("Token did not expire");
|
println!("Token did not expire");
|
||||||
}
|
}
|
||||||
|
|
||||||
match queue::is_queue_empty(&app).await {
|
match is_queue_empty(&app).await {
|
||||||
Ok((empty, song_queue_item)) => {
|
Ok((empty, song_queue_item)) => {
|
||||||
if !empty {
|
if !empty {
|
||||||
println!("Queue is not empty");
|
println!("Queue is not empty");
|
||||||
@@ -64,12 +59,20 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
let song_queue_id = song_queue_item.data[0].id;
|
let song_queue_id = song_queue_item.data[0].id;
|
||||||
let user_id = song_queue_item.data[0].user_id;
|
let user_id = song_queue_item.data[0].user_id;
|
||||||
|
|
||||||
match parser::some_work(&app, &song_queue_id, &user_id).await {
|
match some_work(&app, &song_queue_id, &user_id).await {
|
||||||
Ok((song, coverart, _metadata, queued_song, queued_coverart)) => {
|
Ok((
|
||||||
match queue::wipe_data_from_queues(&app, &queued_song, &queued_coverart)
|
song,
|
||||||
|
coverart,
|
||||||
|
_metadata,
|
||||||
|
queued_song,
|
||||||
|
queued_coverart
|
||||||
|
// (song_queue_id, _song_queue_path),
|
||||||
|
// (coverart_queue_id, _coverart_queue_path),
|
||||||
|
)) => {
|
||||||
|
match wipe_data_from_queues(&app, &queued_song, &queued_coverart)
|
||||||
.await
|
.await
|
||||||
{
|
{
|
||||||
Ok(_) => match parser::cleanup(&song, &coverart).await {
|
Ok(_) => match cleanup(&song, &coverart).await {
|
||||||
Ok(_) => {
|
Ok(_) => {
|
||||||
println!("Successful cleanup");
|
println!("Successful cleanup");
|
||||||
}
|
}
|
||||||
@@ -99,3 +102,360 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
tokio::time::sleep(tokio::time::Duration::from_secs(SECONDS_TO_SLEEP)).await;
|
tokio::time::sleep(tokio::time::Duration::from_secs(SECONDS_TO_SLEEP)).await;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fn wipe_data_from_queues(
|
||||||
|
app: &config::App,
|
||||||
|
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, 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, queued_coverart).await {
|
||||||
|
Ok(inner_response) => match inner_response
|
||||||
|
.json::<api::wipe_data::coverart_queue::response::Response>()
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
Ok(_inner_resp) => {
|
||||||
|
println!("Wiped data from CoverArt queue");
|
||||||
|
println!("Resp: {_inner_resp:?}");
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
Err(err) => Err(std::io::Error::other(err.to_string())),
|
||||||
|
},
|
||||||
|
Err(err) => Err(std::io::Error::other(err.to_string())),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(err) => Err(std::io::Error::other(err.to_string())),
|
||||||
|
},
|
||||||
|
Err(err) => Err(std::io::Error::other(err.to_string())),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn cleanup(
|
||||||
|
song: &icarus_models::song::Song,
|
||||||
|
coverart: &icarus_models::coverart::CoverArt,
|
||||||
|
) -> Result<(), std::io::Error> {
|
||||||
|
match song.remove_from_filesystem() {
|
||||||
|
Ok(_) => {}
|
||||||
|
Err(err) => {
|
||||||
|
eprintln!("Error: Problem cleaning up SongQueue files {err:?}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
match coverart.remove_from_filesystem() {
|
||||||
|
Ok(_) => Ok(()),
|
||||||
|
Err(err) => Err(err),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn is_queue_empty(
|
||||||
|
app: &config::App,
|
||||||
|
) -> Result<(bool, api::fetch_next_queue_item::response::SongQueueItem), reqwest::Error> {
|
||||||
|
match api::fetch_next_queue_item::fetch_next_queue_item(app).await {
|
||||||
|
Ok(response) => {
|
||||||
|
match response
|
||||||
|
.json::<api::fetch_next_queue_item::response::SongQueueItem>()
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
Ok(response) => {
|
||||||
|
if response.data.is_empty() {
|
||||||
|
Ok((true, response))
|
||||||
|
} else {
|
||||||
|
Ok((false, response))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(err) => Err(err),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(err) => Err(err),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn some_work(
|
||||||
|
app: &crate::config::App,
|
||||||
|
song_queue_id: &uuid::Uuid,
|
||||||
|
user_id: &uuid::Uuid,
|
||||||
|
) -> Result<
|
||||||
|
(
|
||||||
|
icarus_models::song::Song,
|
||||||
|
icarus_models::coverart::CoverArt,
|
||||||
|
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),
|
||||||
|
queued_song,
|
||||||
|
queued_coverart,
|
||||||
|
// (coverart_directory, coverart_filename),
|
||||||
|
metadata,
|
||||||
|
// coverart_queue_id,
|
||||||
|
)) => {
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
*/
|
||||||
|
|
||||||
|
match metadata::apply_metadata(&queued_song, &queued_coverart, &metadata).await
|
||||||
|
{
|
||||||
|
Ok(_applied) => {
|
||||||
|
match api::update_queued_song::update_queued_song(
|
||||||
|
app,
|
||||||
|
&queued_song,
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
Ok(response) => {
|
||||||
|
match response
|
||||||
|
.json::<api::update_queued_song::response::Response>()
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
Ok(_inner_response) => {
|
||||||
|
println!("Updated queued song");
|
||||||
|
println!("Response: {_inner_response:?}");
|
||||||
|
|
||||||
|
let song_type = String::from(
|
||||||
|
icarus_meta::detection::song::constants::FLAC_TYPE,
|
||||||
|
);
|
||||||
|
|
||||||
|
match api::create_song::create(
|
||||||
|
app, &metadata, user_id, &song_type,
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
Ok(response) => match response
|
||||||
|
.json::<api::create_song::response::Response>()
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
Ok(resp) => {
|
||||||
|
println!("Response: {resp:?}");
|
||||||
|
|
||||||
|
let mut song = resp.data[0].clone();
|
||||||
|
song.directory = queued_song.song.directory.clone();
|
||||||
|
song.filename = queued_song.song.filename.clone();
|
||||||
|
|
||||||
|
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 = queued_coverart.coverart.directory.clone();
|
||||||
|
coverart.filename = queued_coverart.coverart.filename.clone();
|
||||||
|
|
||||||
|
Ok((song.clone(), coverart.clone(), metadata, queued_song.clone(), queued_coverart.clone()))
|
||||||
|
}
|
||||||
|
Err(err) => {
|
||||||
|
Err(std::io::Error::other(err.to_string()))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(err) => {
|
||||||
|
Err(std::io::Error::other(err.to_string()))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(err) => Err(std::io::Error::other(err.to_string())),
|
||||||
|
},
|
||||||
|
Err(err) => Err(std::io::Error::other(err.to_string())),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(err) => Err(std::io::Error::other(err.to_string())),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(err) => Err(std::io::Error::other(err.to_string())),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(err) => Err(err),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(err) => Err(std::io::Error::other(err.to_string())),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn prep_song(
|
||||||
|
app: &crate::config::App,
|
||||||
|
song_queue_id: &uuid::Uuid,
|
||||||
|
) -> Result<
|
||||||
|
(
|
||||||
|
// (String, String),
|
||||||
|
queued_item::QueuedSong,
|
||||||
|
queued_item::QueuedCoverArt,
|
||||||
|
// (String, String),
|
||||||
|
api::get_metadata_queue::response::Metadata,
|
||||||
|
// uuid::Uuid,
|
||||||
|
),
|
||||||
|
reqwest::Error,
|
||||||
|
> {
|
||||||
|
match api::fetch_song_queue_data::get_data(app, song_queue_id).await {
|
||||||
|
Ok(response) => {
|
||||||
|
// Process data here...
|
||||||
|
match api::parsing::parse_response_into_bytes(response).await {
|
||||||
|
Ok(song_bytes) => {
|
||||||
|
let song = icarus_models::song::Song {
|
||||||
|
directory: icarus_envy::environment::get_root_directory().await.value,
|
||||||
|
filename: icarus_models::song::generate_filename(
|
||||||
|
icarus_models::types::MusicTypes::FlacExtension,
|
||||||
|
true,
|
||||||
|
),
|
||||||
|
data: song_bytes,
|
||||||
|
..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(""),
|
||||||
|
};
|
||||||
|
*/
|
||||||
|
|
||||||
|
let queued_song = queued_item::QueuedSong {
|
||||||
|
id: *song_queue_id,
|
||||||
|
song,
|
||||||
|
path: songpath
|
||||||
|
};
|
||||||
|
|
||||||
|
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>()
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
Ok(response) => {
|
||||||
|
let id = &response.data[0].id;
|
||||||
|
let created_at = &response.data[0].created_at;
|
||||||
|
let metadata = &response.data[0].metadata;
|
||||||
|
println!("Id: {id:?}");
|
||||||
|
println!("Metadata: {metadata:?}");
|
||||||
|
println!("Created at: {created_at:?}");
|
||||||
|
|
||||||
|
println!("Getting coverart queue");
|
||||||
|
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) => {
|
||||||
|
let coverart_queue = &response.data[0];
|
||||||
|
let coverart_queue_id = coverart_queue.id;
|
||||||
|
println!("Coverart queue Id: {coverart_queue_id:?}");
|
||||||
|
|
||||||
|
match api::get_coverart_queue::get_data(app, &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(&coverart_queue.file_type).await;
|
||||||
|
let coverart = icarus_models::coverart::CoverArt {
|
||||||
|
directory,
|
||||||
|
filename,
|
||||||
|
data: coverart_queue_bytes,
|
||||||
|
..Default::default()
|
||||||
|
};
|
||||||
|
coverart.save_to_filesystem().unwrap();
|
||||||
|
let coverart_queue_fs_path = match coverart.get_path() {
|
||||||
|
Ok(path) => {
|
||||||
|
path
|
||||||
|
}
|
||||||
|
Err(err) => {
|
||||||
|
eprintln!("Error: {err:?}");
|
||||||
|
std::process::exit(-1);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let queued_coverart = queued_item::QueuedCoverArt {
|
||||||
|
id: coverart_queue_id,
|
||||||
|
coverart: coverart,
|
||||||
|
path: coverart_queue_fs_path
|
||||||
|
};
|
||||||
|
// let coverart_queue_path = std::path::Path::new(&coverart_queue_fs_path);
|
||||||
|
println!("Saved coverart queue file at: {:?}", queued_coverart.path);
|
||||||
|
|
||||||
|
Ok((queued_song, queued_coverart, metadata.clone()))
|
||||||
|
}
|
||||||
|
Err(err) => {
|
||||||
|
Err(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(err) => {
|
||||||
|
Err(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(err) => {
|
||||||
|
Err(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(err) => Err(err),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(err) => Err(err),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(err) => Err(err),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(err) => Err(err),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(err) => Err(err),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: Consider having something like this in icarus_models
|
||||||
|
pub async fn generate_coverart_queue_dir_and_filename(file_type: &str) -> (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 _ 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
filename += if file_type == icarus_meta::detection::coverart::constants::JPEG_TYPE
|
||||||
|
|| file_type == icarus_meta::detection::coverart::constants::JPG_TYPE
|
||||||
|
{
|
||||||
|
icarus_models::constants::file_extensions::image::JPEGEXTENSION
|
||||||
|
} else if file_type == icarus_meta::detection::coverart::constants::PNG_TYPE {
|
||||||
|
icarus_models::constants::file_extensions::image::PNGEXTENSION
|
||||||
|
} else {
|
||||||
|
""
|
||||||
|
};
|
||||||
|
|
||||||
|
// TODO: Consider separating song and coverart when saving to the filesystem
|
||||||
|
let directory = icarus_envy::environment::get_root_directory().await.value;
|
||||||
|
|
||||||
|
(directory, filename)
|
||||||
|
}
|
||||||
|
|||||||
@@ -122,10 +122,7 @@ pub async fn apply_metadata(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
match icarus_meta::meta::coverart::set_coverart(
|
match icarus_meta::meta::coverart::set_coverart(&queued_song.path, &queued_coverart.path) {
|
||||||
&queued_song.path,
|
|
||||||
&queued_coverart.path,
|
|
||||||
) {
|
|
||||||
Ok(_data) => {
|
Ok(_data) => {
|
||||||
if _data.is_empty() {
|
if _data.is_empty() {
|
||||||
println!("There was an issue");
|
println!("There was an issue");
|
||||||
|
|||||||
@@ -1,272 +0,0 @@
|
|||||||
pub async fn some_work(
|
|
||||||
app: &crate::config::App,
|
|
||||||
song_queue_id: &uuid::Uuid,
|
|
||||||
user_id: &uuid::Uuid,
|
|
||||||
) -> Result<
|
|
||||||
(
|
|
||||||
icarus_models::song::Song,
|
|
||||||
icarus_models::coverart::CoverArt,
|
|
||||||
crate::api::get_metadata_queue::response::Metadata,
|
|
||||||
crate::queued_item::QueuedSong,
|
|
||||||
crate::queued_item::QueuedCoverArt,
|
|
||||||
),
|
|
||||||
std::io::Error,
|
|
||||||
> {
|
|
||||||
match prep_song(app, song_queue_id).await {
|
|
||||||
Ok((queued_song, queued_coverart, metadata)) => {
|
|
||||||
println!("Prepping song");
|
|
||||||
|
|
||||||
match crate::metadata::apply_metadata(&queued_song, &queued_coverart, &metadata).await {
|
|
||||||
Ok(_applied) => {
|
|
||||||
match crate::api::update_queued_song::update_queued_song(app, &queued_song)
|
|
||||||
.await
|
|
||||||
{
|
|
||||||
Ok(response) => {
|
|
||||||
match response
|
|
||||||
.json::<crate::api::update_queued_song::response::Response>()
|
|
||||||
.await
|
|
||||||
{
|
|
||||||
Ok(_inner_response) => {
|
|
||||||
println!("Updated queued song");
|
|
||||||
println!("Response: {_inner_response:?}");
|
|
||||||
|
|
||||||
let song_type = String::from(
|
|
||||||
icarus_meta::detection::song::constants::FLAC_TYPE,
|
|
||||||
);
|
|
||||||
|
|
||||||
match crate::api::create_song::create(
|
|
||||||
app, &metadata, user_id, &song_type,
|
|
||||||
)
|
|
||||||
.await
|
|
||||||
{
|
|
||||||
Ok(response) => match response
|
|
||||||
.json::<crate::api::create_song::response::Response>()
|
|
||||||
.await
|
|
||||||
{
|
|
||||||
Ok(resp) => {
|
|
||||||
println!("Response: {resp:?}");
|
|
||||||
|
|
||||||
let mut song = resp.data[0].clone();
|
|
||||||
song.directory = queued_song.song.directory.clone();
|
|
||||||
song.filename = queued_song.song.filename.clone();
|
|
||||||
|
|
||||||
match crate::api::create_coverart::create(app, &song, &queued_coverart).await {
|
|
||||||
Ok(response) => match response.json::<crate::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 = queued_coverart.coverart.directory.clone();
|
|
||||||
coverart.filename = queued_coverart.coverart.filename.clone();
|
|
||||||
|
|
||||||
Ok((song.clone(), coverart.clone(), metadata, queued_song.clone(), queued_coverart.clone()))
|
|
||||||
}
|
|
||||||
Err(err) => {
|
|
||||||
Err(std::io::Error::other(err.to_string()))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Err(err) => {
|
|
||||||
Err(std::io::Error::other(err.to_string()))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Err(err) => Err(std::io::Error::other(err.to_string())),
|
|
||||||
},
|
|
||||||
Err(err) => Err(std::io::Error::other(err.to_string())),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Err(err) => Err(std::io::Error::other(err.to_string())),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Err(err) => Err(std::io::Error::other(err.to_string())),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Err(err) => Err(err),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Err(err) => Err(std::io::Error::other(err.to_string())),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn prep_song(
|
|
||||||
app: &crate::config::App,
|
|
||||||
song_queue_id: &uuid::Uuid,
|
|
||||||
) -> Result<
|
|
||||||
(
|
|
||||||
crate::queued_item::QueuedSong,
|
|
||||||
crate::queued_item::QueuedCoverArt,
|
|
||||||
crate::api::get_metadata_queue::response::Metadata,
|
|
||||||
),
|
|
||||||
reqwest::Error,
|
|
||||||
> {
|
|
||||||
match crate::api::fetch_song_queue_data::get_data(app, song_queue_id).await {
|
|
||||||
Ok(response) => {
|
|
||||||
// Process data here...
|
|
||||||
match crate::api::parsing::parse_response_into_bytes(response).await {
|
|
||||||
Ok(song_bytes) => {
|
|
||||||
let song = icarus_models::song::Song {
|
|
||||||
directory: icarus_envy::environment::get_root_directory().await.value,
|
|
||||||
filename: icarus_models::song::generate_filename(
|
|
||||||
icarus_models::types::MusicType::FlacExtension,
|
|
||||||
true,
|
|
||||||
)
|
|
||||||
.unwrap(),
|
|
||||||
data: song_bytes,
|
|
||||||
..Default::default()
|
|
||||||
};
|
|
||||||
let songpath = song.song_path().unwrap_or_default();
|
|
||||||
|
|
||||||
let queued_song: crate::queued_item::QueuedSong =
|
|
||||||
match song.save_to_filesystem() {
|
|
||||||
Ok(_) => crate::queued_item::QueuedSong {
|
|
||||||
id: *song_queue_id,
|
|
||||||
song,
|
|
||||||
path: songpath,
|
|
||||||
},
|
|
||||||
Err(err) => {
|
|
||||||
eprintln!("Error: {err:?}");
|
|
||||||
crate::queued_item::QueuedSong {
|
|
||||||
..Default::default()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
println!("Saved at: {:?}", queued_song.path);
|
|
||||||
|
|
||||||
match crate::api::get_metadata_queue::get(app, &queued_song.id).await {
|
|
||||||
Ok(response) => {
|
|
||||||
match response
|
|
||||||
.json::<crate::api::get_metadata_queue::response::Response>()
|
|
||||||
.await
|
|
||||||
{
|
|
||||||
Ok(response) => {
|
|
||||||
let bod = &response.data[0];
|
|
||||||
match process_coverart(app, &queued_song.id, bod).await {
|
|
||||||
Ok(qc) => Ok((queued_song, qc, bod.metadata.clone())),
|
|
||||||
Err(err) => Err(err),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Err(err) => Err(err),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Err(err) => Err(err),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Err(err) => Err(err),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Err(err) => Err(err),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn process_coverart(
|
|
||||||
app: &crate::config::App,
|
|
||||||
queued_song_id: &uuid::Uuid,
|
|
||||||
queued_item: &crate::api::get_metadata_queue::response::QueueItem,
|
|
||||||
) -> Result<crate::queued_item::QueuedCoverArt, reqwest::Error> {
|
|
||||||
let id = queued_item.id;
|
|
||||||
let created_at = queued_item.created_at;
|
|
||||||
let metadata = &queued_item.metadata;
|
|
||||||
println!("Id: {id:?}");
|
|
||||||
println!("Metadata: {metadata:?}");
|
|
||||||
println!("Created at: {created_at:?}");
|
|
||||||
|
|
||||||
println!("Getting coverart queue");
|
|
||||||
match crate::api::get_coverart_queue::get(app, queued_song_id).await {
|
|
||||||
Ok(response) => {
|
|
||||||
match response
|
|
||||||
.json::<crate::api::get_coverart_queue::response::Response>()
|
|
||||||
.await
|
|
||||||
{
|
|
||||||
Ok(response) => {
|
|
||||||
let coverart_queue = &response.data[0];
|
|
||||||
let coverart_queue_id = coverart_queue.id;
|
|
||||||
println!("Coverart queue Id: {coverart_queue_id:?}");
|
|
||||||
|
|
||||||
match crate::api::get_coverart_queue::get_data(app, &coverart_queue_id).await {
|
|
||||||
Ok(response) => {
|
|
||||||
match crate::api::parsing::parse_response_into_bytes(response).await {
|
|
||||||
Ok(coverart_queue_bytes) => {
|
|
||||||
let queued_coverart = init_queued_coverart(
|
|
||||||
&coverart_queue_id,
|
|
||||||
&coverart_queue.file_type,
|
|
||||||
coverart_queue_bytes,
|
|
||||||
)
|
|
||||||
.await;
|
|
||||||
println!(
|
|
||||||
"Saved coverart queue file at: {:?}",
|
|
||||||
queued_coverart.path
|
|
||||||
);
|
|
||||||
println!(
|
|
||||||
"Queued CoverArt file type: {:?}",
|
|
||||||
queued_coverart.coverart.file_type
|
|
||||||
);
|
|
||||||
|
|
||||||
Ok(queued_coverart)
|
|
||||||
}
|
|
||||||
Err(err) => Err(err),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Err(err) => Err(err),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Err(err) => Err(err),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Err(err) => Err(err),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn init_queued_coverart(
|
|
||||||
coverart_queue_id: &uuid::Uuid,
|
|
||||||
file_type: &str,
|
|
||||||
bytes: Vec<u8>,
|
|
||||||
) -> crate::queued_item::QueuedCoverArt {
|
|
||||||
// TODO: Consider separating song and coverart when saving to the filesystem
|
|
||||||
let covart_type = if file_type == icarus_meta::detection::coverart::constants::PNG_TYPE {
|
|
||||||
icarus_models::types::CoverArtType::PngExtension
|
|
||||||
} else if file_type == icarus_meta::detection::coverart::constants::JPEG_TYPE {
|
|
||||||
icarus_models::types::CoverArtType::JpegExtension
|
|
||||||
} else if file_type == icarus_meta::detection::coverart::constants::JPG_TYPE {
|
|
||||||
icarus_models::types::CoverArtType::JpgExtension
|
|
||||||
} else {
|
|
||||||
icarus_models::types::CoverArtType::None
|
|
||||||
};
|
|
||||||
let coverart = icarus_models::coverart::CoverArt {
|
|
||||||
directory: icarus_envy::environment::get_root_directory().await.value,
|
|
||||||
filename: match icarus_models::coverart::generate_filename(covart_type, true) {
|
|
||||||
Ok(filename) => filename,
|
|
||||||
Err(err) => {
|
|
||||||
eprintln!("Error generating CoverArt filename: {err:?}");
|
|
||||||
panic!("Error initializing queued CoverArt");
|
|
||||||
}
|
|
||||||
},
|
|
||||||
file_type: String::from(file_type),
|
|
||||||
data: bytes,
|
|
||||||
..Default::default()
|
|
||||||
};
|
|
||||||
coverart.save_to_filesystem().unwrap();
|
|
||||||
let coverart_queue_fs_path = coverart.get_path().unwrap();
|
|
||||||
crate::queued_item::QueuedCoverArt {
|
|
||||||
id: *coverart_queue_id,
|
|
||||||
coverart,
|
|
||||||
path: coverart_queue_fs_path,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn cleanup(
|
|
||||||
song: &icarus_models::song::Song,
|
|
||||||
coverart: &icarus_models::coverart::CoverArt,
|
|
||||||
) -> Result<(), std::io::Error> {
|
|
||||||
match song.remove_from_filesystem() {
|
|
||||||
Ok(_) => {}
|
|
||||||
Err(err) => {
|
|
||||||
eprintln!("Error: Problem cleaning up SongQueue files {err:?}");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
match coverart.remove_from_filesystem() {
|
|
||||||
Ok(_) => Ok(()),
|
|
||||||
Err(err) => Err(err),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,60 +0,0 @@
|
|||||||
pub async fn wipe_data_from_queues(
|
|
||||||
app: &crate::config::App,
|
|
||||||
queued_song: &crate::queued_item::QueuedSong,
|
|
||||||
queued_coverart: &crate::queued_item::QueuedCoverArt,
|
|
||||||
) -> Result<(), std::io::Error> {
|
|
||||||
match crate::api::wipe_data::song_queue::wipe_data(app, queued_song).await {
|
|
||||||
Ok(response) => match response
|
|
||||||
.json::<crate::api::wipe_data::song_queue::response::Response>()
|
|
||||||
.await
|
|
||||||
{
|
|
||||||
Ok(_resp) => {
|
|
||||||
match crate::api::wipe_data::coverart_queue::wipe_data(app, queued_coverart).await {
|
|
||||||
Ok(inner_response) => match inner_response
|
|
||||||
.json::<crate::api::wipe_data::coverart_queue::response::Response>()
|
|
||||||
.await
|
|
||||||
{
|
|
||||||
Ok(_inner_resp) => {
|
|
||||||
println!("Wiped data from CoverArt queue");
|
|
||||||
println!("Resp: {_inner_resp:?}");
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
Err(err) => Err(std::io::Error::other(err.to_string())),
|
|
||||||
},
|
|
||||||
Err(err) => Err(std::io::Error::other(err.to_string())),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Err(err) => Err(std::io::Error::other(err.to_string())),
|
|
||||||
},
|
|
||||||
Err(err) => Err(std::io::Error::other(err.to_string())),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn is_queue_empty(
|
|
||||||
app: &crate::config::App,
|
|
||||||
) -> Result<
|
|
||||||
(
|
|
||||||
bool,
|
|
||||||
crate::api::fetch_next_queue_item::response::SongQueueItem,
|
|
||||||
),
|
|
||||||
reqwest::Error,
|
|
||||||
> {
|
|
||||||
match crate::api::fetch_next_queue_item::fetch_next_queue_item(app).await {
|
|
||||||
Ok(response) => {
|
|
||||||
match response
|
|
||||||
.json::<crate::api::fetch_next_queue_item::response::SongQueueItem>()
|
|
||||||
.await
|
|
||||||
{
|
|
||||||
Ok(response) => {
|
|
||||||
if response.data.is_empty() {
|
|
||||||
Ok((true, response))
|
|
||||||
} else {
|
|
||||||
Ok((false, response))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Err(err) => Err(err),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Err(err) => Err(err),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,11 +1,11 @@
|
|||||||
#[derive(Clone, Debug, Default)]
|
#[derive(Clone, Debug)]
|
||||||
pub struct QueuedSong {
|
pub struct QueuedSong {
|
||||||
pub id: uuid::Uuid,
|
pub id: uuid::Uuid,
|
||||||
pub song: icarus_models::song::Song,
|
pub song: icarus_models::song::Song,
|
||||||
pub path: String,
|
pub path: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug, Default)]
|
#[derive(Clone, Debug)]
|
||||||
pub struct QueuedCoverArt {
|
pub struct QueuedCoverArt {
|
||||||
pub id: uuid::Uuid,
|
pub id: uuid::Uuid,
|
||||||
pub coverart: icarus_models::coverart::CoverArt,
|
pub coverart: icarus_models::coverart::CoverArt,
|
||||||
|
|||||||
Reference in New Issue
Block a user