Compare commits
11 Commits
60069d9e14
...
v0.4.8
Author | SHA1 | Date | |
---|---|---|---|
113416432b | |||
bd351b1157 | |||
106867e5ee | |||
a2128f99e6 | |||
5636f82b29 | |||
3bb7fb1010 | |||
98f1c8bdf6 | |||
da86c3e054 | |||
acbeb31792 | |||
6041d7d6c6 | |||
6d54664a81 |
@@ -10,14 +10,14 @@ jobs:
|
|||||||
runs-on: ubuntu-24.04
|
runs-on: ubuntu-24.04
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout code
|
- name: Checkout code
|
||||||
uses: actions/checkout@v3
|
uses: actions/checkout@v5
|
||||||
with:
|
with:
|
||||||
fetch-depth: 0 # Important for git describe --tags
|
fetch-depth: 0 # Important for git describe --tags
|
||||||
|
|
||||||
- name: Install Rust
|
- name: Install Rust
|
||||||
uses: actions-rs/toolchain@v1
|
uses: actions-rs/toolchain@v1
|
||||||
with:
|
with:
|
||||||
toolchain: 1.86.0
|
toolchain: 1.90.0
|
||||||
components: cargo
|
components: cargo
|
||||||
|
|
||||||
- name: Extract Version from Cargo.toml
|
- name: Extract Version from Cargo.toml
|
||||||
|
@@ -15,10 +15,10 @@ jobs:
|
|||||||
name: Check
|
name: Check
|
||||||
runs-on: ubuntu-24.04
|
runs-on: ubuntu-24.04
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v4
|
- uses: actions/checkout@v5
|
||||||
- uses: actions-rust-lang/setup-rust-toolchain@v1
|
- uses: actions-rust-lang/setup-rust-toolchain@v1
|
||||||
with:
|
with:
|
||||||
toolchain: 1.88.0
|
toolchain: 1.90.0
|
||||||
- run: |
|
- run: |
|
||||||
mkdir -p ~/.ssh
|
mkdir -p ~/.ssh
|
||||||
echo "${{ secrets.MYREPO_TOKEN }}" > ~/.ssh/gitlab_deploy_key
|
echo "${{ secrets.MYREPO_TOKEN }}" > ~/.ssh/gitlab_deploy_key
|
||||||
@@ -33,10 +33,10 @@ jobs:
|
|||||||
runs-on: ubuntu-24.04
|
runs-on: ubuntu-24.04
|
||||||
needs: setup_ssh
|
needs: setup_ssh
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v4
|
- uses: actions/checkout@v5
|
||||||
- uses: actions-rust-lang/setup-rust-toolchain@v1
|
- uses: actions-rust-lang/setup-rust-toolchain@v1
|
||||||
with:
|
with:
|
||||||
toolchain: 1.88.0
|
toolchain: 1.90.0
|
||||||
- run: |
|
- run: |
|
||||||
mkdir -p ~/.ssh
|
mkdir -p ~/.ssh
|
||||||
echo "${{ secrets.MYREPO_TOKEN }}" > ~/.ssh/gitlab_deploy_key
|
echo "${{ secrets.MYREPO_TOKEN }}" > ~/.ssh/gitlab_deploy_key
|
||||||
@@ -51,10 +51,10 @@ jobs:
|
|||||||
runs-on: ubuntu-24.04
|
runs-on: ubuntu-24.04
|
||||||
needs: setup_ssh
|
needs: setup_ssh
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v4
|
- uses: actions/checkout@v5
|
||||||
- uses: actions-rust-lang/setup-rust-toolchain@v1
|
- uses: actions-rust-lang/setup-rust-toolchain@v1
|
||||||
with:
|
with:
|
||||||
toolchain: 1.88.0
|
toolchain: 1.90.0
|
||||||
- run: rustup component add rustfmt
|
- run: rustup component add rustfmt
|
||||||
- run: |
|
- run: |
|
||||||
mkdir -p ~/.ssh
|
mkdir -p ~/.ssh
|
||||||
@@ -70,10 +70,10 @@ jobs:
|
|||||||
runs-on: ubuntu-24.04
|
runs-on: ubuntu-24.04
|
||||||
needs: setup_ssh
|
needs: setup_ssh
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v4
|
- uses: actions/checkout@v5
|
||||||
- uses: actions-rust-lang/setup-rust-toolchain@v1
|
- uses: actions-rust-lang/setup-rust-toolchain@v1
|
||||||
with:
|
with:
|
||||||
toolchain: 1.88.0
|
toolchain: 1.90.0
|
||||||
- run: rustup component add clippy
|
- run: rustup component add clippy
|
||||||
- run: |
|
- run: |
|
||||||
mkdir -p ~/.ssh
|
mkdir -p ~/.ssh
|
||||||
@@ -89,10 +89,10 @@ jobs:
|
|||||||
runs-on: ubuntu-24.04
|
runs-on: ubuntu-24.04
|
||||||
needs: setup_ssh
|
needs: setup_ssh
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v4
|
- uses: actions/checkout@v5
|
||||||
- uses: actions-rust-lang/setup-rust-toolchain@v1
|
- uses: actions-rust-lang/setup-rust-toolchain@v1
|
||||||
with:
|
with:
|
||||||
toolchain: 1.88.0
|
toolchain: 1.90.0
|
||||||
- run: |
|
- run: |
|
||||||
mkdir -p ~/.ssh
|
mkdir -p ~/.ssh
|
||||||
echo "${{ secrets.MYREPO_TOKEN }}" > ~/.ssh/gitlab_deploy_key
|
echo "${{ secrets.MYREPO_TOKEN }}" > ~/.ssh/gitlab_deploy_key
|
||||||
|
612
Cargo.lock
generated
612
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
24
Cargo.toml
24
Cargo.toml
@@ -1,18 +1,18 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "songparser"
|
name = "songparser"
|
||||||
version = "0.3.1"
|
version = "0.4.8"
|
||||||
edition = "2024"
|
edition = "2024"
|
||||||
rust-version = "1.88"
|
rust-version = "1.90"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
tokio = { version = "1.45.1", features = ["full"] }
|
tokio = { version = "1.47.1", features = ["full"] }
|
||||||
futures = { version = "0.3.31" }
|
futures = { version = "0.3.31" }
|
||||||
reqwest = { version = "0.12.20", features = ["json", "stream", "multipart"] }
|
reqwest = { version = "0.12.23", features = ["json", "stream", "multipart"] }
|
||||||
serde = { version = "1.0.219", features = ["derive"] }
|
serde = { version = "1.0.228", features = ["derive"] }
|
||||||
serde_json = { version = "1.0.140" }
|
serde_json = { version = "1.0.145" }
|
||||||
time = { version = "0.3.41", features = ["macros", "serde"] }
|
time = { version = "0.3.44", features = ["macros", "serde"] }
|
||||||
uuid = { version = "1.17.0", features = ["v4", "serde"] }
|
uuid = { version = "1.18.1", features = ["v4", "serde"] }
|
||||||
rand = { version = "0.9.1" }
|
rand = { version = "0.9.2" }
|
||||||
icarus_meta = { git = "ssh://git@git.kundeng.us/phoenix/icarus_meta.git", tag = "v0.3.0" }
|
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.5.6" }
|
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.4.1-main-8f0d123db5-006" }
|
icarus_envy = { git = "ssh://git@git.kundeng.us/phoenix/icarus_envy.git", tag = "v0.5.0" }
|
||||||
|
@@ -1,7 +1,7 @@
|
|||||||
# Stage 1: Build the application
|
# Stage 1: Build the application
|
||||||
# Use a specific Rust version for reproducibility. Choose one that matches your development environment.
|
# Use a specific Rust version for reproducibility. Choose one that matches your development environment.
|
||||||
# Using slim variant for smaller base image
|
# Using slim variant for smaller base image
|
||||||
FROM rust:1.88 as builder
|
FROM rust:1.90 as builder
|
||||||
|
|
||||||
# Set the working directory inside the container
|
# Set the working directory inside the container
|
||||||
WORKDIR /usr/src/app
|
WORKDIR /usr/src/app
|
||||||
|
201
src/api.rs
201
src/api.rs
@@ -1,12 +1,33 @@
|
|||||||
pub async fn fetch_next_queue_item(
|
pub mod fetch_next_queue_item {
|
||||||
app: &crate::config::App,
|
|
||||||
) -> Result<reqwest::Response, reqwest::Error> {
|
|
||||||
let client = reqwest::Client::new();
|
|
||||||
let fetch_endpoint = String::from("api/v2/song/queue/next");
|
|
||||||
let api_url = format!("{}/{fetch_endpoint}", app.uri);
|
|
||||||
let (key, header) = auth_header(app).await;
|
|
||||||
|
|
||||||
client.get(api_url).header(key, header).send().await
|
pub async fn fetch_next_queue_item(
|
||||||
|
app: &crate::config::App,
|
||||||
|
) -> Result<reqwest::Response, reqwest::Error> {
|
||||||
|
let client = reqwest::Client::new();
|
||||||
|
let fetch_endpoint = String::from("api/v2/song/queue/next");
|
||||||
|
let api_url = format!("{}/{fetch_endpoint}", app.uri);
|
||||||
|
let (key, header) = super::auth_header(app).await;
|
||||||
|
|
||||||
|
client.get(api_url).header(key, header).send().await
|
||||||
|
}
|
||||||
|
|
||||||
|
pub mod response {
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
#[derive(Debug, Deserialize, Serialize)]
|
||||||
|
pub struct QueueItem {
|
||||||
|
pub id: uuid::Uuid,
|
||||||
|
pub filename: String,
|
||||||
|
pub status: String,
|
||||||
|
pub user_id: uuid::Uuid,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Deserialize, Serialize)]
|
||||||
|
pub struct SongQueueItem {
|
||||||
|
pub message: String,
|
||||||
|
pub data: Vec<QueueItem>,
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn auth_header(
|
pub async fn auth_header(
|
||||||
@@ -137,6 +158,7 @@ pub mod get_coverart_queue {
|
|||||||
#[derive(Debug, Deserialize, Serialize)]
|
#[derive(Debug, Deserialize, Serialize)]
|
||||||
pub struct CoverArtQueue {
|
pub struct CoverArtQueue {
|
||||||
pub id: uuid::Uuid,
|
pub id: uuid::Uuid,
|
||||||
|
pub file_type: String,
|
||||||
pub song_queue_id: uuid::Uuid,
|
pub song_queue_id: uuid::Uuid,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -167,3 +189,166 @@ pub mod refresh_token {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub mod update_queued_song {
|
||||||
|
pub async fn update_queued_song(
|
||||||
|
app: &crate::config::App,
|
||||||
|
queued_song: &crate::queued_item::QueuedSong,
|
||||||
|
) -> Result<reqwest::Response, reqwest::Error> {
|
||||||
|
let client = reqwest::Client::builder().build()?;
|
||||||
|
|
||||||
|
println!("Queued song path: {:?}", queued_song.path);
|
||||||
|
|
||||||
|
let form = reqwest::multipart::Form::new().part(
|
||||||
|
"file",
|
||||||
|
reqwest::multipart::Part::bytes(std::fs::read(&queued_song.path).unwrap())
|
||||||
|
.file_name(queued_song.song.filename.clone()),
|
||||||
|
);
|
||||||
|
|
||||||
|
let url = format!("{}/api/v2/song/queue/{}", app.uri, queued_song.id);
|
||||||
|
println!("Url: {url:?}");
|
||||||
|
|
||||||
|
let (key, header) = crate::api::auth_header(app).await;
|
||||||
|
let request = client.patch(url).multipart(form).header(key, header);
|
||||||
|
|
||||||
|
let response = request.send().await?;
|
||||||
|
|
||||||
|
Ok(response)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub mod response {
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
#[derive(Debug, Deserialize, Serialize)]
|
||||||
|
pub struct Response {
|
||||||
|
pub message: String,
|
||||||
|
pub data: Vec<uuid::Uuid>,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub mod create_song {
|
||||||
|
pub async fn create(
|
||||||
|
app: &crate::config::App,
|
||||||
|
metadata_queue: &crate::api::get_metadata_queue::response::Metadata,
|
||||||
|
user_id: &uuid::Uuid,
|
||||||
|
song_type: &String,
|
||||||
|
) -> Result<reqwest::Response, reqwest::Error> {
|
||||||
|
let payload = serde_json::json!(
|
||||||
|
{
|
||||||
|
"album": &metadata_queue.album,
|
||||||
|
"album_artist": &metadata_queue.album_artist,
|
||||||
|
"artist": &metadata_queue.artist,
|
||||||
|
"disc": metadata_queue.disc,
|
||||||
|
"disc_count": metadata_queue.disc_count,
|
||||||
|
"duration": metadata_queue.duration,
|
||||||
|
"genre": &metadata_queue.genre,
|
||||||
|
"title": &metadata_queue.title,
|
||||||
|
"track": metadata_queue.track,
|
||||||
|
"track_count": metadata_queue.track_count,
|
||||||
|
"date": metadata_queue.year.to_string(),
|
||||||
|
"audio_type": &song_type,
|
||||||
|
"user_id": &user_id,
|
||||||
|
"song_queue_id": &metadata_queue.song_queue_id,
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
let client = reqwest::Client::builder().build()?;
|
||||||
|
|
||||||
|
let url = format!("{}/api/v2/song", app.uri);
|
||||||
|
let (key, header) = crate::api::auth_header(app).await;
|
||||||
|
|
||||||
|
let request = client.post(url).json(&payload).header(key, header);
|
||||||
|
request.send().await
|
||||||
|
}
|
||||||
|
|
||||||
|
pub mod response {
|
||||||
|
#[derive(Debug, serde::Deserialize, serde::Serialize)]
|
||||||
|
pub struct Response {
|
||||||
|
pub message: String,
|
||||||
|
pub data: Vec<icarus_models::song::Song>,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub mod create_coverart {
|
||||||
|
pub async fn create(
|
||||||
|
app: &crate::config::App,
|
||||||
|
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, &queued_coverart.id);
|
||||||
|
let (key, header) = crate::api::auth_header(app).await;
|
||||||
|
let request = client.post(url).json(&payload).header(key, header);
|
||||||
|
|
||||||
|
request.send().await
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_payload(song_id: &uuid::Uuid, coverart_queue_id: &uuid::Uuid) -> serde_json::Value {
|
||||||
|
serde_json::json!({
|
||||||
|
"song_id": &song_id,
|
||||||
|
"coverart_queue_id": &coverart_queue_id,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
pub mod response {
|
||||||
|
#[derive(Debug, serde::Deserialize, serde::Serialize)]
|
||||||
|
pub struct Response {
|
||||||
|
pub message: String,
|
||||||
|
pub data: Vec<icarus_models::coverart::CoverArt>,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub mod wipe_data {
|
||||||
|
pub mod song_queue {
|
||||||
|
pub async fn wipe_data(
|
||||||
|
app: &crate::config::App,
|
||||||
|
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": queued_song.id
|
||||||
|
});
|
||||||
|
let (key, header) = crate::api::auth_header(app).await;
|
||||||
|
let request = client.patch(url).json(&payload).header(key, header);
|
||||||
|
|
||||||
|
request.send().await
|
||||||
|
}
|
||||||
|
|
||||||
|
pub mod response {
|
||||||
|
#[derive(Debug, serde::Deserialize, serde::Serialize)]
|
||||||
|
pub struct Response {
|
||||||
|
pub message: String,
|
||||||
|
pub data: Vec<uuid::Uuid>,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub mod coverart_queue {
|
||||||
|
pub async fn wipe_data(
|
||||||
|
app: &crate::config::App,
|
||||||
|
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": queued_coverart.id
|
||||||
|
});
|
||||||
|
let (key, header) = crate::api::auth_header(app).await;
|
||||||
|
let request = client.patch(url).json(&payload).header(key, header);
|
||||||
|
|
||||||
|
request.send().await
|
||||||
|
}
|
||||||
|
|
||||||
|
pub mod response {
|
||||||
|
#[derive(Debug, serde::Deserialize, serde::Serialize)]
|
||||||
|
pub struct Response {
|
||||||
|
pub message: String,
|
||||||
|
pub data: Vec<uuid::Uuid>,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
57
src/auth/mod.rs
Normal file
57
src/auth/mod.rs
Normal file
@@ -0,0 +1,57 @@
|
|||||||
|
pub async fn get_token(
|
||||||
|
app: &crate::config::App,
|
||||||
|
) -> Result<icarus_models::login_result::LoginResult, std::io::Error> {
|
||||||
|
let client = reqwest::Client::new();
|
||||||
|
let endpoint = String::from("api/v2/service/login");
|
||||||
|
let api_url = format!("{}/{endpoint}", app.auth_uri);
|
||||||
|
|
||||||
|
let payload = serde_json::json!({
|
||||||
|
"passphrase": icarus_envy::environment::get_service_passphrase().await.value,
|
||||||
|
});
|
||||||
|
|
||||||
|
match client.post(api_url).json(&payload).send().await {
|
||||||
|
Ok(response) => match response
|
||||||
|
.json::<crate::api::service_token::response::Response>()
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
Ok(resp) => {
|
||||||
|
if resp.data.is_empty() {
|
||||||
|
Err(std::io::Error::other(String::from("No token returned")))
|
||||||
|
} else {
|
||||||
|
Ok(resp.data[0].clone())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(err) => Err(std::io::Error::other(err.to_string())),
|
||||||
|
},
|
||||||
|
Err(err) => Err(std::io::Error::other(err.to_string())),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn get_refresh_token(
|
||||||
|
app: &crate::config::App,
|
||||||
|
) -> Result<icarus_models::login_result::LoginResult, std::io::Error> {
|
||||||
|
let client = reqwest::Client::new();
|
||||||
|
let endpoint = String::from("api/v2/token/refresh");
|
||||||
|
let api_url = format!("{}/{endpoint}", app.auth_uri);
|
||||||
|
|
||||||
|
let payload = serde_json::json!({
|
||||||
|
"access_token": app.token.token
|
||||||
|
});
|
||||||
|
|
||||||
|
match client.post(api_url).json(&payload).send().await {
|
||||||
|
Ok(response) => match response
|
||||||
|
.json::<crate::api::refresh_token::response::Response>()
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
Ok(resp) => {
|
||||||
|
if resp.data.is_empty() {
|
||||||
|
Err(std::io::Error::other(String::from("No token returned")))
|
||||||
|
} else {
|
||||||
|
Ok(resp.data[0].clone())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(err) => Err(std::io::Error::other(err.to_string())),
|
||||||
|
},
|
||||||
|
Err(err) => Err(std::io::Error::other(err.to_string())),
|
||||||
|
}
|
||||||
|
}
|
547
src/main.rs
547
src/main.rs
@@ -1,12 +1,12 @@
|
|||||||
pub mod api;
|
pub mod api;
|
||||||
|
pub mod auth;
|
||||||
pub mod config;
|
pub mod config;
|
||||||
pub mod responses;
|
pub mod metadata;
|
||||||
pub mod the_rest;
|
pub mod parser;
|
||||||
pub mod update_queued_song;
|
pub mod queue;
|
||||||
|
pub mod queued_item;
|
||||||
pub mod util;
|
pub mod util;
|
||||||
|
|
||||||
use std::io::Write;
|
|
||||||
|
|
||||||
pub const SECONDS_TO_SLEEP: u64 = 5;
|
pub const SECONDS_TO_SLEEP: u64 = 5;
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
@@ -36,7 +36,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
loop {
|
loop {
|
||||||
println!("Token: {:?}", app.token);
|
println!("Token: {:?}", app.token);
|
||||||
|
|
||||||
if auth::did_token_expire(&app.token).await {
|
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 {
|
||||||
Ok(login_result) => login_result,
|
Ok(login_result) => login_result,
|
||||||
@@ -52,7 +52,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
println!("Token did not expire");
|
println!("Token did not expire");
|
||||||
}
|
}
|
||||||
|
|
||||||
match is_queue_empty(&app).await {
|
match queue::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");
|
||||||
@@ -61,27 +61,19 @@ 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;
|
||||||
|
|
||||||
// TODO: Do something with the result later
|
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,
|
|
||||||
(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
|
.await
|
||||||
{
|
{
|
||||||
Ok(_) => {
|
Ok(_) => match parser::cleanup(&song, &coverart).await {
|
||||||
match cleanup(&song_queue_path, &coverart_queue_path).await {
|
Ok(_) => {
|
||||||
Ok(_) => {
|
println!("Successful cleanup");
|
||||||
println!("Successful cleanup");
|
|
||||||
}
|
|
||||||
Err(err) => {
|
|
||||||
eprintln!("Error: {err:?}");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
Err(err) => {
|
||||||
|
eprintln!("Error: {err:?}");
|
||||||
|
}
|
||||||
|
},
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
eprintln!("Error: {err:?}");
|
eprintln!("Error: {err:?}");
|
||||||
}
|
}
|
||||||
@@ -104,508 +96,3 @@ 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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
mod auth {
|
|
||||||
pub async fn get_token(
|
|
||||||
app: &crate::config::App,
|
|
||||||
) -> Result<icarus_models::login_result::LoginResult, std::io::Error> {
|
|
||||||
let client = reqwest::Client::new();
|
|
||||||
let endpoint = String::from("api/v2/service/login");
|
|
||||||
let api_url = format!("{}/{endpoint}", app.auth_uri);
|
|
||||||
|
|
||||||
let payload = serde_json::json!({
|
|
||||||
"passphrase": icarus_envy::environment::get_service_passphrase().await.value,
|
|
||||||
});
|
|
||||||
|
|
||||||
match client.post(api_url).json(&payload).send().await {
|
|
||||||
Ok(response) => match response
|
|
||||||
.json::<crate::api::service_token::response::Response>()
|
|
||||||
.await
|
|
||||||
{
|
|
||||||
Ok(resp) => {
|
|
||||||
if resp.data.is_empty() {
|
|
||||||
Err(std::io::Error::other(String::from("No token returned")))
|
|
||||||
} else {
|
|
||||||
Ok(resp.data[0].clone())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Err(err) => Err(std::io::Error::other(err.to_string())),
|
|
||||||
},
|
|
||||||
Err(err) => Err(std::io::Error::other(err.to_string())),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: Might want to put the functionality within icarus_models at some point
|
|
||||||
pub async fn did_token_expire(login_result: &icarus_models::login_result::LoginResult) -> bool {
|
|
||||||
let current_time = time::OffsetDateTime::now_utc();
|
|
||||||
let expire_time =
|
|
||||||
time::OffsetDateTime::from_unix_timestamp(login_result.expiration).unwrap();
|
|
||||||
current_time > expire_time
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn get_refresh_token(
|
|
||||||
app: &crate::config::App,
|
|
||||||
) -> Result<icarus_models::login_result::LoginResult, std::io::Error> {
|
|
||||||
let client = reqwest::Client::new();
|
|
||||||
let endpoint = String::from("api/v2/token/refresh");
|
|
||||||
let api_url = format!("{}/{endpoint}", app.auth_uri);
|
|
||||||
|
|
||||||
let payload = serde_json::json!({
|
|
||||||
"access_token": app.token.token
|
|
||||||
});
|
|
||||||
|
|
||||||
match client.post(api_url).json(&payload).send().await {
|
|
||||||
Ok(response) => match response
|
|
||||||
.json::<crate::api::refresh_token::response::Response>()
|
|
||||||
.await
|
|
||||||
{
|
|
||||||
Ok(resp) => {
|
|
||||||
if resp.data.is_empty() {
|
|
||||||
Err(std::io::Error::other(String::from("No token returned")))
|
|
||||||
} else {
|
|
||||||
Ok(resp.data[0].clone())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Err(err) => Err(std::io::Error::other(err.to_string())),
|
|
||||||
},
|
|
||||||
Err(err) => Err(std::io::Error::other(err.to_string())),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn wipe_data_from_queues(
|
|
||||||
app: &config::App,
|
|
||||||
song_queue_id: &uuid::Uuid,
|
|
||||||
coverart_queue_id: &uuid::Uuid,
|
|
||||||
) -> Result<(), std::io::Error> {
|
|
||||||
match the_rest::wipe_data::song_queue::wipe_data(app, song_queue_id).await {
|
|
||||||
Ok(response) => match response
|
|
||||||
.json::<the_rest::wipe_data::song_queue::response::Response>()
|
|
||||||
.await
|
|
||||||
{
|
|
||||||
Ok(_resp) => {
|
|
||||||
match the_rest::wipe_data::coverart_queue::wipe_data(app, coverart_queue_id).await {
|
|
||||||
Ok(inner_response) => match inner_response
|
|
||||||
.json::<the_rest::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_queue_path: &String,
|
|
||||||
coverart_queue_path: &String,
|
|
||||||
) -> Result<(), std::io::Error> {
|
|
||||||
match the_rest::cleanup::clean_song_queue(song_queue_path) {
|
|
||||||
Ok(_) => {}
|
|
||||||
Err(err) => {
|
|
||||||
eprintln!("Error: Problem cleaning up SongQueue files {err:?}");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
match the_rest::cleanup::clean_coverart_queue(coverart_queue_path) {
|
|
||||||
Ok(_) => Ok(()),
|
|
||||||
Err(err) => Err(err),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn is_queue_empty(
|
|
||||||
app: &config::App,
|
|
||||||
) -> Result<(bool, responses::fetch_next_queue_item::SongQueueItem), reqwest::Error> {
|
|
||||||
match api::fetch_next_queue_item(app).await {
|
|
||||||
Ok(response) => {
|
|
||||||
match response
|
|
||||||
.json::<responses::fetch_next_queue_item::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,
|
|
||||||
(uuid::Uuid, String),
|
|
||||||
(uuid::Uuid, String),
|
|
||||||
),
|
|
||||||
std::io::Error,
|
|
||||||
> {
|
|
||||||
match prep_song(app, song_queue_id).await {
|
|
||||||
Ok((song_queue_path, coverart_queue_path, metadata, coverart_queue_id)) => {
|
|
||||||
match apply_metadata(&song_queue_path, &coverart_queue_path, &metadata).await {
|
|
||||||
Ok(_applied) => {
|
|
||||||
match update_queued_song::update_queued_song(
|
|
||||||
app,
|
|
||||||
&song_queue_path,
|
|
||||||
song_queue_id,
|
|
||||||
)
|
|
||||||
.await
|
|
||||||
{
|
|
||||||
Ok(response) => {
|
|
||||||
match response
|
|
||||||
.json::<update_queued_song::response::Response>()
|
|
||||||
.await
|
|
||||||
{
|
|
||||||
Ok(_inner_response) => {
|
|
||||||
println!("Response: {_inner_response:?}");
|
|
||||||
|
|
||||||
// TODO: Place this somewhere else
|
|
||||||
let song_type = String::from("flac");
|
|
||||||
|
|
||||||
match the_rest::create_song::create(
|
|
||||||
app, &metadata, user_id, &song_type,
|
|
||||||
)
|
|
||||||
.await
|
|
||||||
{
|
|
||||||
Ok(response) => match response
|
|
||||||
.json::<the_rest::create_song::response::Response>()
|
|
||||||
.await
|
|
||||||
{
|
|
||||||
Ok(resp) => {
|
|
||||||
println!("Response: {resp:?}");
|
|
||||||
|
|
||||||
let song = &resp.data[0];
|
|
||||||
match the_rest::create_coverart::create(app, &song.id, &coverart_queue_id).await {
|
|
||||||
Ok(response) => match response.json::<the_rest::create_coverart::response::Response>().await {
|
|
||||||
Ok(resp) => {
|
|
||||||
println!("CoverArt sent and successfully parsed response");
|
|
||||||
println!("json: {resp:?}");
|
|
||||||
let coverart = &resp.data[0];
|
|
||||||
Ok((song.clone(), coverart.clone(), (metadata.song_queue_id, song_queue_path), (coverart_queue_id, coverart_queue_path)))
|
|
||||||
}
|
|
||||||
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,
|
|
||||||
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 (directory, filename) = generate_song_queue_dir_and_filename().await;
|
|
||||||
let song_queue_path = save_file_to_fs(&directory, &filename, &song_bytes).await;
|
|
||||||
|
|
||||||
println!("Saved at: {song_queue_path:?}");
|
|
||||||
|
|
||||||
match api::get_metadata_queue::get(app, song_queue_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, song_queue_id).await {
|
|
||||||
Ok(response) => {
|
|
||||||
match response.json::<api::get_coverart_queue::response::Response>().await {
|
|
||||||
Ok(response) => {
|
|
||||||
let coverart_queue_id = &response.data[0].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().await;
|
|
||||||
let coverart_queue_path = save_file_to_fs(&directory, &filename, &coverart_queue_bytes).await;
|
|
||||||
|
|
||||||
println!("Saved coverart queue file at: {coverart_queue_path:?}");
|
|
||||||
|
|
||||||
let c_path = util::path_buf_to_string(&coverart_queue_path);
|
|
||||||
let s_path = util::path_buf_to_string(&song_queue_path);
|
|
||||||
Ok((s_path, c_path, metadata.clone(), *coverart_queue_id))
|
|
||||||
}
|
|
||||||
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_song_queue_dir_and_filename() -> (String, String) {
|
|
||||||
let mut song = icarus_models::song::Song::default();
|
|
||||||
song.filename = song.generate_filename(icarus_models::types::MusicTypes::FlacExtension, true);
|
|
||||||
|
|
||||||
song.directory = icarus_envy::environment::get_root_directory().await.value;
|
|
||||||
|
|
||||||
(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.value;
|
|
||||||
|
|
||||||
(directory, filename)
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: Check to see if this is available in icarus_models
|
|
||||||
pub async fn save_file_to_fs(
|
|
||||||
directory: &String,
|
|
||||||
filename: &String,
|
|
||||||
data: &[u8],
|
|
||||||
) -> std::path::PathBuf {
|
|
||||||
// TODO: Add function to save bytes to a file in icarus_models
|
|
||||||
// repo
|
|
||||||
let dir = std::path::Path::new(directory);
|
|
||||||
let save_path = dir.join(filename);
|
|
||||||
|
|
||||||
let mut file = std::fs::File::create(&save_path).unwrap();
|
|
||||||
file.write_all(data).unwrap();
|
|
||||||
|
|
||||||
save_path
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn apply_metadata(
|
|
||||||
song_queue_path: &String,
|
|
||||||
coverart_queue_path: &String,
|
|
||||||
metadata: &api::get_metadata_queue::response::Metadata,
|
|
||||||
) -> Result<bool, std::io::Error> {
|
|
||||||
// Apply metadata fields
|
|
||||||
let types = icarus_meta::types::all_metadata_types();
|
|
||||||
|
|
||||||
for t in types {
|
|
||||||
match t {
|
|
||||||
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) {
|
|
||||||
Ok(_) => {}
|
|
||||||
Err(_err) => {
|
|
||||||
return Err(_err);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
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) {
|
|
||||||
Ok(_) => {}
|
|
||||||
Err(_err) => {
|
|
||||||
return Err(_err);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
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) {
|
|
||||||
Ok(_) => {}
|
|
||||||
Err(_err) => {
|
|
||||||
return Err(_err);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
icarus_meta::types::Type::Date => {
|
|
||||||
// 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) {
|
|
||||||
Ok(_) => {}
|
|
||||||
Err(_err) => {
|
|
||||||
return Err(_err);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
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) {
|
|
||||||
Ok(_) => {}
|
|
||||||
Err(_err) => {
|
|
||||||
return Err(_err);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
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) {
|
|
||||||
Ok(_) => {}
|
|
||||||
Err(_err) => {
|
|
||||||
return Err(_err);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
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) {
|
|
||||||
Ok(_) => {}
|
|
||||||
Err(_err) => {
|
|
||||||
return Err(_err);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
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) {
|
|
||||||
Ok(_) => {}
|
|
||||||
Err(_err) => {
|
|
||||||
return Err(_err);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
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) {
|
|
||||||
Ok(_) => {}
|
|
||||||
Err(_err) => {
|
|
||||||
return Err(_err);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
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) {
|
|
||||||
Ok(_) => {}
|
|
||||||
Err(_err) => {
|
|
||||||
return Err(_err);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Apply coverart
|
|
||||||
match icarus_meta::meta::coverart::contains_coverart(song_queue_path) {
|
|
||||||
Ok((exists, size)) => {
|
|
||||||
if exists {
|
|
||||||
println!("Coverart exists: {size:?} size");
|
|
||||||
match icarus_meta::meta::coverart::remove_coverart(song_queue_path) {
|
|
||||||
Ok(_data) => {}
|
|
||||||
Err(err) => {
|
|
||||||
return Err(err);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
match icarus_meta::meta::coverart::set_coverart(song_queue_path, coverart_queue_path) {
|
|
||||||
Ok(_data) => {
|
|
||||||
if _data.is_empty() {
|
|
||||||
println!("There was an issue");
|
|
||||||
Ok(false)
|
|
||||||
} else {
|
|
||||||
println!("Success in applying coverart to song");
|
|
||||||
Ok(true)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Err(err) => Err(err),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Err(err) => Err(err),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
143
src/metadata/mod.rs
Normal file
143
src/metadata/mod.rs
Normal file
@@ -0,0 +1,143 @@
|
|||||||
|
/// Applies metadata to the queued song
|
||||||
|
pub async fn apply_metadata(
|
||||||
|
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
|
||||||
|
let types = icarus_meta::types::all_metadata_types();
|
||||||
|
|
||||||
|
for t in types {
|
||||||
|
match t {
|
||||||
|
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, &queued_song.path, meta_type) {
|
||||||
|
Ok(_) => {}
|
||||||
|
Err(_err) => {
|
||||||
|
return Err(_err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
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, &queued_song.path, meta_type) {
|
||||||
|
Ok(_) => {}
|
||||||
|
Err(_err) => {
|
||||||
|
return Err(_err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
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, &queued_song.path, meta_type) {
|
||||||
|
Ok(_) => {}
|
||||||
|
Err(_err) => {
|
||||||
|
return Err(_err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
icarus_meta::types::Type::Date => {
|
||||||
|
// 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, &queued_song.path, meta_type) {
|
||||||
|
Ok(_) => {}
|
||||||
|
Err(_err) => {
|
||||||
|
return Err(_err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
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, &queued_song.path, meta_type) {
|
||||||
|
Ok(_) => {}
|
||||||
|
Err(_err) => {
|
||||||
|
return Err(_err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
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, &queued_song.path, meta_type) {
|
||||||
|
Ok(_) => {}
|
||||||
|
Err(_err) => {
|
||||||
|
return Err(_err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
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, &queued_song.path, meta_type) {
|
||||||
|
Ok(_) => {}
|
||||||
|
Err(_err) => {
|
||||||
|
return Err(_err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
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, &queued_song.path, meta_type) {
|
||||||
|
Ok(_) => {}
|
||||||
|
Err(_err) => {
|
||||||
|
return Err(_err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
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, &queued_song.path, meta_type) {
|
||||||
|
Ok(_) => {}
|
||||||
|
Err(_err) => {
|
||||||
|
return Err(_err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
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, &queued_song.path, meta_type) {
|
||||||
|
Ok(_) => {}
|
||||||
|
Err(_err) => {
|
||||||
|
return Err(_err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Apply coverart
|
||||||
|
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(&queued_song.path) {
|
||||||
|
Ok(_data) => {}
|
||||||
|
Err(err) => {
|
||||||
|
return Err(err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
match icarus_meta::meta::coverart::set_coverart(
|
||||||
|
&queued_song.path,
|
||||||
|
&queued_coverart.path,
|
||||||
|
) {
|
||||||
|
Ok(_data) => {
|
||||||
|
if _data.is_empty() {
|
||||||
|
println!("There was an issue");
|
||||||
|
Ok(false)
|
||||||
|
} else {
|
||||||
|
println!("Success in applying coverart to song");
|
||||||
|
Ok(true)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(err) => Err(err),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(err) => Err(err),
|
||||||
|
}
|
||||||
|
}
|
234
src/parser/mod.rs
Normal file
234
src/parser/mod.rs
Normal file
@@ -0,0 +1,234 @@
|
|||||||
|
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::MusicTypes::FlacExtension,
|
||||||
|
true,
|
||||||
|
),
|
||||||
|
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 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 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 (directory, filename) = crate::util::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 = crate::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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
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),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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),
|
||||||
|
}
|
||||||
|
}
|
60
src/queue/mod.rs
Normal file
60
src/queue/mod.rs
Normal file
@@ -0,0 +1,60 @@
|
|||||||
|
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),
|
||||||
|
}
|
||||||
|
}
|
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,
|
||||||
|
}
|
@@ -1,17 +0,0 @@
|
|||||||
pub mod fetch_next_queue_item {
|
|
||||||
use serde::{Deserialize, Serialize};
|
|
||||||
|
|
||||||
#[derive(Debug, Deserialize, Serialize)]
|
|
||||||
pub struct QueueItem {
|
|
||||||
pub id: uuid::Uuid,
|
|
||||||
pub filename: String,
|
|
||||||
pub status: String,
|
|
||||||
pub user_id: uuid::Uuid,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Deserialize, Serialize)]
|
|
||||||
pub struct SongQueueItem {
|
|
||||||
pub message: String,
|
|
||||||
pub data: Vec<QueueItem>,
|
|
||||||
}
|
|
||||||
}
|
|
179
src/the_rest.rs
179
src/the_rest.rs
@@ -1,179 +0,0 @@
|
|||||||
// TODO: Refactor this file when this app is functional
|
|
||||||
|
|
||||||
pub mod create_song {
|
|
||||||
pub async fn create(
|
|
||||||
app: &crate::config::App,
|
|
||||||
metadata_queue: &crate::api::get_metadata_queue::response::Metadata,
|
|
||||||
user_id: &uuid::Uuid,
|
|
||||||
song_type: &String,
|
|
||||||
) -> Result<reqwest::Response, reqwest::Error> {
|
|
||||||
let payload = serde_json::json!(
|
|
||||||
{
|
|
||||||
"album": &metadata_queue.album,
|
|
||||||
"album_artist": &metadata_queue.album_artist,
|
|
||||||
"artist": &metadata_queue.artist,
|
|
||||||
"disc": metadata_queue.disc,
|
|
||||||
"disc_count": metadata_queue.disc_count,
|
|
||||||
"duration": metadata_queue.duration,
|
|
||||||
"genre": &metadata_queue.genre,
|
|
||||||
"title": &metadata_queue.title,
|
|
||||||
"track": metadata_queue.track,
|
|
||||||
"track_count": metadata_queue.track_count,
|
|
||||||
"date": metadata_queue.year.to_string(),
|
|
||||||
"audio_type": &song_type,
|
|
||||||
"user_id": &user_id,
|
|
||||||
"song_queue_id": &metadata_queue.song_queue_id,
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
let client = reqwest::Client::builder().build()?;
|
|
||||||
|
|
||||||
let url = format!("{}/api/v2/song", app.uri);
|
|
||||||
let (key, header) = crate::api::auth_header(app).await;
|
|
||||||
|
|
||||||
let request = client.post(url).json(&payload).header(key, header);
|
|
||||||
request.send().await
|
|
||||||
}
|
|
||||||
|
|
||||||
pub mod response {
|
|
||||||
#[derive(Debug, serde::Deserialize, serde::Serialize)]
|
|
||||||
pub struct Response {
|
|
||||||
pub message: String,
|
|
||||||
pub data: Vec<icarus_models::song::Song>,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub mod create_coverart {
|
|
||||||
|
|
||||||
pub async fn create(
|
|
||||||
app: &crate::config::App,
|
|
||||||
song_id: &uuid::Uuid,
|
|
||||||
coverart_queue_id: &uuid::Uuid,
|
|
||||||
) -> 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 (key, header) = crate::api::auth_header(app).await;
|
|
||||||
let request = client.post(url).json(&payload).header(key, header);
|
|
||||||
|
|
||||||
request.send().await
|
|
||||||
}
|
|
||||||
|
|
||||||
fn get_payload(song_id: &uuid::Uuid, coverart_queue_id: &uuid::Uuid) -> serde_json::Value {
|
|
||||||
serde_json::json!({
|
|
||||||
"song_id": &song_id,
|
|
||||||
"coverart_queue_id": &coverart_queue_id,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
pub mod response {
|
|
||||||
#[derive(Debug, serde::Deserialize, serde::Serialize)]
|
|
||||||
pub struct Response {
|
|
||||||
pub message: String,
|
|
||||||
pub data: Vec<icarus_models::coverart::CoverArt>,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub mod wipe_data {
|
|
||||||
pub mod song_queue {
|
|
||||||
pub async fn wipe_data(
|
|
||||||
app: &crate::config::App,
|
|
||||||
song_queue_id: &uuid::Uuid,
|
|
||||||
) -> 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
|
|
||||||
});
|
|
||||||
let (key, header) = crate::api::auth_header(app).await;
|
|
||||||
let request = client.patch(url).json(&payload).header(key, header);
|
|
||||||
|
|
||||||
request.send().await
|
|
||||||
}
|
|
||||||
|
|
||||||
pub mod response {
|
|
||||||
#[derive(Debug, serde::Deserialize, serde::Serialize)]
|
|
||||||
pub struct Response {
|
|
||||||
pub message: String,
|
|
||||||
pub data: Vec<uuid::Uuid>,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
pub mod coverart_queue {
|
|
||||||
pub async fn wipe_data(
|
|
||||||
app: &crate::config::App,
|
|
||||||
coverart_queue_id: &uuid::Uuid,
|
|
||||||
) -> 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
|
|
||||||
});
|
|
||||||
let (key, header) = crate::api::auth_header(app).await;
|
|
||||||
let request = client.patch(url).json(&payload).header(key, header);
|
|
||||||
|
|
||||||
request.send().await
|
|
||||||
}
|
|
||||||
|
|
||||||
pub mod response {
|
|
||||||
#[derive(Debug, serde::Deserialize, serde::Serialize)]
|
|
||||||
pub struct Response {
|
|
||||||
pub message: String,
|
|
||||||
pub data: Vec<uuid::Uuid>,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub mod cleanup {
|
|
||||||
pub fn clean_song_queue(song_queue_path: &String) -> Result<(), std::io::Error> {
|
|
||||||
let file_path = std::path::Path::new(song_queue_path);
|
|
||||||
if file_path.exists() {
|
|
||||||
match std::fs::remove_file(file_path) {
|
|
||||||
Ok(_) => {
|
|
||||||
if check_file_existence(song_queue_path) {
|
|
||||||
Err(std::io::Error::other(String::from(
|
|
||||||
"SongQueue file exists after a deletion",
|
|
||||||
)))
|
|
||||||
} else {
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Err(err) => Err(std::io::Error::other(err.to_string())),
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
Err(std::io::Error::other(String::from(
|
|
||||||
"SongQueue file path does not exists",
|
|
||||||
)))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn clean_coverart_queue(coverart_queue_path: &String) -> Result<(), std::io::Error> {
|
|
||||||
let coverart_file_path = std::path::Path::new(coverart_queue_path);
|
|
||||||
if coverart_file_path.exists() {
|
|
||||||
match std::fs::remove_file(coverart_file_path) {
|
|
||||||
Ok(_) => {
|
|
||||||
if !check_file_existence(coverart_queue_path) {
|
|
||||||
Ok(())
|
|
||||||
} else {
|
|
||||||
Err(std::io::Error::other(String::from(
|
|
||||||
"CoverArt file stil exists",
|
|
||||||
)))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Err(err) => Err(std::io::Error::other(err.to_string())),
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
Err(std::io::Error::other(String::from(
|
|
||||||
"CoverArt file does not exists",
|
|
||||||
)))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn check_file_existence(file_path: &String) -> bool {
|
|
||||||
let path = std::path::Path::new(file_path);
|
|
||||||
path.exists()
|
|
||||||
}
|
|
||||||
}
|
|
@@ -1,36 +0,0 @@
|
|||||||
pub async fn update_queued_song(
|
|
||||||
app: &crate::config::App,
|
|
||||||
song_path: &String,
|
|
||||||
song_queue_id: &uuid::Uuid,
|
|
||||||
) -> Result<reqwest::Response, reqwest::Error> {
|
|
||||||
let client = reqwest::Client::builder().build()?;
|
|
||||||
|
|
||||||
println!("Song path: {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())
|
|
||||||
.file_name("track01.flac"),
|
|
||||||
);
|
|
||||||
|
|
||||||
let url = format!("{}/api/v2/song/queue/{song_queue_id}", app.uri);
|
|
||||||
println!("Url: {url:?}");
|
|
||||||
|
|
||||||
let (key, header) = crate::api::auth_header(app).await;
|
|
||||||
let request = client.patch(url).multipart(form).header(key, header);
|
|
||||||
|
|
||||||
let response = request.send().await?;
|
|
||||||
|
|
||||||
Ok(response)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub mod response {
|
|
||||||
use serde::{Deserialize, Serialize};
|
|
||||||
|
|
||||||
#[derive(Debug, Deserialize, Serialize)]
|
|
||||||
pub struct Response {
|
|
||||||
pub message: String,
|
|
||||||
pub data: Vec<uuid::Uuid>,
|
|
||||||
}
|
|
||||||
}
|
|
36
src/util.rs
36
src/util.rs
@@ -4,3 +4,39 @@ pub fn path_buf_to_string(path: &std::path::Path) -> String {
|
|||||||
None => String::new(),
|
None => String::new(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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)
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user