Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 9aefeace23 | |||
| a0c6ae65a3 | |||
| c7230d3b32 | |||
| 703fc49f32 |
@@ -68,7 +68,7 @@ jobs:
|
|||||||
env:
|
env:
|
||||||
ROOT_DIRECTORY: "/tmp"
|
ROOT_DIRECTORY: "/tmp"
|
||||||
DATABASE_URL: "postgres://${{ secrets.POSTGRES_USER }}:${{ secrets.POSTGRES_PASSWORD }}@localhost:5432/${{ secrets.POSTGRES_DB }}"
|
DATABASE_URL: "postgres://${{ secrets.POSTGRES_USER }}:${{ secrets.POSTGRES_PASSWORD }}@localhost:5432/${{ secrets.POSTGRES_DB }}"
|
||||||
run: cargo test --verbose
|
run: cargo test --verbose --
|
||||||
|
|
||||||
- name: Run clippy
|
- name: Run clippy
|
||||||
run: cargo clippy -- -D warnings
|
run: cargo clippy -- -D warnings
|
||||||
|
|||||||
Generated
+1
-1
@@ -762,7 +762,7 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "icarus"
|
name = "icarus"
|
||||||
version = "0.1.96"
|
version = "0.1.100"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"axum",
|
"axum",
|
||||||
"common-multipart-rfc7578",
|
"common-multipart-rfc7578",
|
||||||
|
|||||||
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "icarus"
|
name = "icarus"
|
||||||
version = "0.1.96"
|
version = "0.1.100"
|
||||||
edition = "2024"
|
edition = "2024"
|
||||||
rust-version = "1.88"
|
rust-version = "1.88"
|
||||||
|
|
||||||
|
|||||||
@@ -376,6 +376,88 @@ pub mod cov_db {
|
|||||||
Err(_) => Err(sqlx::Error::RowNotFound),
|
Err(_) => Err(sqlx::Error::RowNotFound),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn get_coverart_with_song_id(
|
||||||
|
pool: &sqlx::PgPool,
|
||||||
|
song_id: &uuid::Uuid,
|
||||||
|
) -> Result<icarus_models::coverart::CoverArt, sqlx::Error> {
|
||||||
|
let result = sqlx::query(
|
||||||
|
r#"
|
||||||
|
SELECT id, title, path, song_id FROM "coverart" WHERE song_id = $1;
|
||||||
|
"#,
|
||||||
|
)
|
||||||
|
.bind(song_id)
|
||||||
|
.fetch_one(pool)
|
||||||
|
.await
|
||||||
|
.map_err(|e| {
|
||||||
|
eprintln!("Error querying data: {e:?}");
|
||||||
|
});
|
||||||
|
|
||||||
|
match result {
|
||||||
|
Ok(row) => Ok(icarus_models::coverart::CoverArt {
|
||||||
|
id: row
|
||||||
|
.try_get("id")
|
||||||
|
.map_err(|_e| sqlx::Error::RowNotFound)
|
||||||
|
.unwrap(),
|
||||||
|
title: row
|
||||||
|
.try_get("title")
|
||||||
|
.map_err(|_e| sqlx::Error::RowNotFound)
|
||||||
|
.unwrap(),
|
||||||
|
path: row
|
||||||
|
.try_get("path")
|
||||||
|
.map_err(|_e| sqlx::Error::RowNotFound)
|
||||||
|
.unwrap(),
|
||||||
|
data: Vec::new(),
|
||||||
|
song_id: row
|
||||||
|
.try_get("song_id")
|
||||||
|
.map_err(|_e| sqlx::Error::RowNotFound)
|
||||||
|
.unwrap(),
|
||||||
|
}),
|
||||||
|
Err(_) => Err(sqlx::Error::RowNotFound),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn delete_coverart(
|
||||||
|
pool: &sqlx::PgPool,
|
||||||
|
id: &uuid::Uuid,
|
||||||
|
) -> Result<icarus_models::coverart::CoverArt, sqlx::Error> {
|
||||||
|
let result = sqlx::query(
|
||||||
|
r#"
|
||||||
|
DELETE FROM "coverart"
|
||||||
|
WHERE id = $1
|
||||||
|
RETURNING id, title, path, song_id
|
||||||
|
"#,
|
||||||
|
)
|
||||||
|
.bind(id)
|
||||||
|
.fetch_one(pool)
|
||||||
|
.await
|
||||||
|
.map_err(|e| {
|
||||||
|
eprintln!("Error deleting data: {e:?}");
|
||||||
|
});
|
||||||
|
|
||||||
|
match result {
|
||||||
|
Ok(row) => Ok(icarus_models::coverart::CoverArt {
|
||||||
|
id: row
|
||||||
|
.try_get("id")
|
||||||
|
.map_err(|_e| sqlx::Error::RowNotFound)
|
||||||
|
.unwrap(),
|
||||||
|
title: row
|
||||||
|
.try_get("title")
|
||||||
|
.map_err(|_e| sqlx::Error::RowNotFound)
|
||||||
|
.unwrap(),
|
||||||
|
path: row
|
||||||
|
.try_get("path")
|
||||||
|
.map_err(|_e| sqlx::Error::RowNotFound)
|
||||||
|
.unwrap(),
|
||||||
|
song_id: row
|
||||||
|
.try_get("song_id")
|
||||||
|
.map_err(|_e| sqlx::Error::RowNotFound)
|
||||||
|
.unwrap(),
|
||||||
|
data: Vec::new(),
|
||||||
|
}),
|
||||||
|
Err(_err) => Err(sqlx::Error::RowNotFound),
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub mod endpoint {
|
pub mod endpoint {
|
||||||
@@ -646,4 +728,40 @@ pub mod endpoint {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn download_coverart(
|
||||||
|
axum::Extension(pool): axum::Extension<sqlx::PgPool>,
|
||||||
|
axum::extract::Path(id): axum::extract::Path<uuid::Uuid>,
|
||||||
|
) -> (axum::http::StatusCode, axum::response::Response) {
|
||||||
|
match super::cov_db::get_coverart(&pool, &id).await {
|
||||||
|
Ok(coverart) => match coverart.to_data() {
|
||||||
|
Ok(data) => {
|
||||||
|
let bytes = axum::body::Bytes::from(data);
|
||||||
|
let mut response = bytes.into_response();
|
||||||
|
let headers = response.headers_mut();
|
||||||
|
// TODO: Address hard coding
|
||||||
|
headers.insert(
|
||||||
|
axum::http::header::CONTENT_TYPE,
|
||||||
|
"audio/jpg".parse().unwrap(),
|
||||||
|
);
|
||||||
|
headers.insert(
|
||||||
|
axum::http::header::CONTENT_DISPOSITION,
|
||||||
|
format!("attachment; filename=\"{id}.jpg\"")
|
||||||
|
.parse()
|
||||||
|
.unwrap(),
|
||||||
|
);
|
||||||
|
|
||||||
|
(axum::http::StatusCode::OK, response)
|
||||||
|
}
|
||||||
|
Err(_err) => (
|
||||||
|
axum::http::StatusCode::NOT_FOUND,
|
||||||
|
axum::response::Response::default(),
|
||||||
|
),
|
||||||
|
},
|
||||||
|
Err(_err) => (
|
||||||
|
axum::http::StatusCode::NOT_FOUND,
|
||||||
|
axum::response::Response::default(),
|
||||||
|
),
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,8 +18,11 @@ pub mod endpoints {
|
|||||||
pub const CREATESONG: &str = "/api/v2/song";
|
pub const CREATESONG: &str = "/api/v2/song";
|
||||||
pub const GETSONGS: &str = "/api/v2/song";
|
pub const GETSONGS: &str = "/api/v2/song";
|
||||||
pub const STREAMSONG: &str = "/api/v2/song/stream/{id}";
|
pub const STREAMSONG: &str = "/api/v2/song/stream/{id}";
|
||||||
|
pub const DOWNLOADSONG: &str = "/api/v2/song/download/{id}";
|
||||||
|
pub const DELETESONG: &str = "/api/v2/song/{id}";
|
||||||
pub const CREATECOVERART: &str = "/api/v2/coverart";
|
pub const CREATECOVERART: &str = "/api/v2/coverart";
|
||||||
pub const GETCOVERART: &str = "/api/v2/coverart";
|
pub const GETCOVERART: &str = "/api/v2/coverart";
|
||||||
|
pub const DOWNLOADCOVERART: &str = "/api/v2/coverart/download/{id}";
|
||||||
}
|
}
|
||||||
|
|
||||||
pub mod response {
|
pub mod response {
|
||||||
|
|||||||
@@ -173,6 +173,20 @@ pub mod response {
|
|||||||
pub data: Vec<icarus_models::song::Song>,
|
pub data: Vec<icarus_models::song::Song>,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub mod delete_song {
|
||||||
|
#[derive(Debug, Default, serde::Deserialize, serde::Serialize)]
|
||||||
|
pub struct SongAndCoverArt {
|
||||||
|
pub song: icarus_models::song::Song,
|
||||||
|
pub coverart: icarus_models::coverart::CoverArt,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Default, serde::Deserialize, serde::Serialize)]
|
||||||
|
pub struct Response {
|
||||||
|
pub message: String,
|
||||||
|
pub data: Vec<SongAndCoverArt>,
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Might make a distinction between year and date in a song's tag at some point
|
// TODO: Might make a distinction between year and date in a song's tag at some point
|
||||||
@@ -336,6 +350,105 @@ pub mod song_db {
|
|||||||
Err(_) => Err(sqlx::Error::RowNotFound),
|
Err(_) => Err(sqlx::Error::RowNotFound),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn delete_song(
|
||||||
|
pool: &sqlx::PgPool,
|
||||||
|
id: &uuid::Uuid,
|
||||||
|
) -> Result<icarus_models::song::Song, sqlx::Error> {
|
||||||
|
let result = sqlx::query(
|
||||||
|
// icarus_models::song::Song,
|
||||||
|
r#"
|
||||||
|
DELETE FROM "song"
|
||||||
|
WHERE id = $1
|
||||||
|
RETURNING id, title, artist, album, album_artist, genre, year, disc, track, track_count, disc_count, duration, audio_type, date_created, filename, directory, user_id
|
||||||
|
"#,
|
||||||
|
)
|
||||||
|
.bind(id)
|
||||||
|
.fetch_one(pool)
|
||||||
|
.await
|
||||||
|
.map_err(|e| {
|
||||||
|
eprintln!("Error deleting data: {e:?}")
|
||||||
|
});
|
||||||
|
|
||||||
|
match result {
|
||||||
|
Ok(row) => {
|
||||||
|
let date_created_time: time::OffsetDateTime = row
|
||||||
|
.try_get("date_created")
|
||||||
|
.map_err(|_e| sqlx::Error::RowNotFound)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
Ok(icarus_models::song::Song {
|
||||||
|
id: row
|
||||||
|
.try_get("id")
|
||||||
|
.map_err(|_e| sqlx::Error::RowNotFound)
|
||||||
|
.unwrap(),
|
||||||
|
title: row
|
||||||
|
.try_get("title")
|
||||||
|
.map_err(|_e| sqlx::Error::RowNotFound)
|
||||||
|
.unwrap(),
|
||||||
|
artist: row
|
||||||
|
.try_get("artist")
|
||||||
|
.map_err(|_e| sqlx::Error::RowNotFound)
|
||||||
|
.unwrap(),
|
||||||
|
album_artist: row
|
||||||
|
.try_get("album_artist")
|
||||||
|
.map_err(|_e| sqlx::Error::RowNotFound)
|
||||||
|
.unwrap(),
|
||||||
|
album: row
|
||||||
|
.try_get("album")
|
||||||
|
.map_err(|_e| sqlx::Error::RowNotFound)
|
||||||
|
.unwrap(),
|
||||||
|
genre: row
|
||||||
|
.try_get("genre")
|
||||||
|
.map_err(|_e| sqlx::Error::RowNotFound)
|
||||||
|
.unwrap(),
|
||||||
|
year: row
|
||||||
|
.try_get("year")
|
||||||
|
.map_err(|_e| sqlx::Error::RowNotFound)
|
||||||
|
.unwrap(),
|
||||||
|
track: row
|
||||||
|
.try_get("track")
|
||||||
|
.map_err(|_e| sqlx::Error::RowNotFound)
|
||||||
|
.unwrap(),
|
||||||
|
disc: row
|
||||||
|
.try_get("disc")
|
||||||
|
.map_err(|_e| sqlx::Error::RowNotFound)
|
||||||
|
.unwrap(),
|
||||||
|
track_count: row
|
||||||
|
.try_get("track_count")
|
||||||
|
.map_err(|_e| sqlx::Error::RowNotFound)
|
||||||
|
.unwrap(),
|
||||||
|
disc_count: row
|
||||||
|
.try_get("disc_count")
|
||||||
|
.map_err(|_e| sqlx::Error::RowNotFound)
|
||||||
|
.unwrap(),
|
||||||
|
duration: row
|
||||||
|
.try_get("duration")
|
||||||
|
.map_err(|_e| sqlx::Error::RowNotFound)
|
||||||
|
.unwrap(),
|
||||||
|
audio_type: row
|
||||||
|
.try_get("audio_type")
|
||||||
|
.map_err(|_e| sqlx::Error::RowNotFound)
|
||||||
|
.unwrap(),
|
||||||
|
filename: row
|
||||||
|
.try_get("filename")
|
||||||
|
.map_err(|_e| sqlx::Error::RowNotFound)
|
||||||
|
.unwrap(),
|
||||||
|
directory: row
|
||||||
|
.try_get("directory")
|
||||||
|
.map_err(|_e| sqlx::Error::RowNotFound)
|
||||||
|
.unwrap(),
|
||||||
|
date_created: date_created_time.to_string(),
|
||||||
|
user_id: row
|
||||||
|
.try_get("user_id")
|
||||||
|
.map_err(|_e| sqlx::Error::RowNotFound)
|
||||||
|
.unwrap(),
|
||||||
|
data: Vec::new(),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
Err(_) => Err(sqlx::Error::RowNotFound),
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
mod song_queue {
|
mod song_queue {
|
||||||
@@ -1059,4 +1172,136 @@ pub mod endpoint {
|
|||||||
)),
|
)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn download_song(
|
||||||
|
axum::Extension(pool): axum::Extension<sqlx::PgPool>,
|
||||||
|
axum::extract::Path(id): axum::extract::Path<uuid::Uuid>,
|
||||||
|
) -> (axum::http::StatusCode, axum::response::Response) {
|
||||||
|
match super::song_db::get_song(&pool, &id).await {
|
||||||
|
Ok(song) => match song.to_data() {
|
||||||
|
Ok(data) => {
|
||||||
|
let bytes = axum::body::Bytes::from(data);
|
||||||
|
let mut response = bytes.into_response();
|
||||||
|
let headers = response.headers_mut();
|
||||||
|
headers.insert(
|
||||||
|
axum::http::header::CONTENT_TYPE,
|
||||||
|
"audio/flac".parse().unwrap(),
|
||||||
|
);
|
||||||
|
headers.insert(
|
||||||
|
axum::http::header::CONTENT_DISPOSITION,
|
||||||
|
format!("attachment; filename=\"{id}.flac\"")
|
||||||
|
.parse()
|
||||||
|
.unwrap(),
|
||||||
|
);
|
||||||
|
|
||||||
|
(axum::http::StatusCode::OK, response)
|
||||||
|
}
|
||||||
|
Err(_err) => (
|
||||||
|
axum::http::StatusCode::INTERNAL_SERVER_ERROR,
|
||||||
|
axum::response::Response::default(),
|
||||||
|
),
|
||||||
|
},
|
||||||
|
Err(_err) => (
|
||||||
|
axum::http::StatusCode::NOT_FOUND,
|
||||||
|
axum::response::Response::default(),
|
||||||
|
),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn delete_song(
|
||||||
|
axum::Extension(pool): axum::Extension<sqlx::PgPool>,
|
||||||
|
axum::extract::Path(id): axum::extract::Path<uuid::Uuid>,
|
||||||
|
) -> (
|
||||||
|
axum::http::StatusCode,
|
||||||
|
axum::Json<super::response::delete_song::Response>,
|
||||||
|
) {
|
||||||
|
let mut response = super::response::delete_song::Response::default();
|
||||||
|
|
||||||
|
match super::song_db::get_song(&pool, &id).await {
|
||||||
|
Ok(song) => {
|
||||||
|
match super::super::coverart::cov_db::get_coverart_with_song_id(&pool, &song.id)
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
Ok(coverart) => {
|
||||||
|
let coverart_path = std::path::Path::new(&coverart.path);
|
||||||
|
if coverart_path.exists() {
|
||||||
|
match song.song_path() {
|
||||||
|
Ok(song_path) => {
|
||||||
|
match super::song_db::delete_song(&pool, &song.id).await {
|
||||||
|
Ok(deleted_song) => {
|
||||||
|
match super::super::coverart::cov_db::delete_coverart(
|
||||||
|
&pool,
|
||||||
|
&coverart.id,
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
Ok(deleted_coverart) => {
|
||||||
|
match std::fs::remove_file(song_path) {
|
||||||
|
Ok(_) => match std::fs::remove_file(
|
||||||
|
&coverart.path,
|
||||||
|
) {
|
||||||
|
Ok(_) => {
|
||||||
|
response.message = String::from(super::super::response::SUCCESSFUL);
|
||||||
|
response.data.push(super::response::delete_song::SongAndCoverArt{ song: deleted_song, coverart: deleted_coverart });
|
||||||
|
(
|
||||||
|
axum::http::StatusCode::OK,
|
||||||
|
axum::Json(response),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
Err(err) => {
|
||||||
|
response.message = err.to_string();
|
||||||
|
(axum::http::StatusCode::INTERNAL_SERVER_ERROR, axum::Json(response))
|
||||||
|
}
|
||||||
|
},
|
||||||
|
Err(err) => {
|
||||||
|
response.message = err.to_string();
|
||||||
|
(axum::http::StatusCode::INTERNAL_SERVER_ERROR, axum::Json(response))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Err(err) => {
|
||||||
|
response.message = err.to_string();
|
||||||
|
(axum::http::StatusCode::INTERNAL_SERVER_ERROR, axum::Json(response))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(err) => {
|
||||||
|
response.message = err.to_string();
|
||||||
|
(
|
||||||
|
axum::http::StatusCode::INTERNAL_SERVER_ERROR,
|
||||||
|
axum::Json(response),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(err) => {
|
||||||
|
response.message = err.to_string();
|
||||||
|
(
|
||||||
|
axum::http::StatusCode::INTERNAL_SERVER_ERROR,
|
||||||
|
axum::Json(response),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
response.message =
|
||||||
|
String::from("Could not locate coverart on the filesystem");
|
||||||
|
(
|
||||||
|
axum::http::StatusCode::INTERNAL_SERVER_ERROR,
|
||||||
|
axum::Json(response),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(err) => {
|
||||||
|
response.message = err.to_string();
|
||||||
|
(axum::http::StatusCode::NOT_FOUND, axum::Json(response))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(err) => {
|
||||||
|
response.message = err.to_string();
|
||||||
|
(axum::http::StatusCode::NOT_FOUND, axum::Json(response))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+308
-7
@@ -44,7 +44,7 @@ async fn main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub mod init {
|
pub mod init {
|
||||||
use axum::routing::{get, patch, post};
|
use axum::routing::{delete, get, patch, post};
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
use tower_http::timeout::TimeoutLayer;
|
use tower_http::timeout::TimeoutLayer;
|
||||||
|
|
||||||
@@ -123,10 +123,22 @@ pub mod init {
|
|||||||
crate::callers::endpoints::GETCOVERART,
|
crate::callers::endpoints::GETCOVERART,
|
||||||
get(crate::callers::coverart::endpoint::get_coverart),
|
get(crate::callers::coverart::endpoint::get_coverart),
|
||||||
)
|
)
|
||||||
|
.route(
|
||||||
|
crate::callers::endpoints::DOWNLOADCOVERART,
|
||||||
|
get(crate::callers::coverart::endpoint::download_coverart),
|
||||||
|
)
|
||||||
.route(
|
.route(
|
||||||
crate::callers::endpoints::STREAMSONG,
|
crate::callers::endpoints::STREAMSONG,
|
||||||
get(crate::callers::song::endpoint::stream_song),
|
get(crate::callers::song::endpoint::stream_song),
|
||||||
)
|
)
|
||||||
|
.route(
|
||||||
|
crate::callers::endpoints::DOWNLOADSONG,
|
||||||
|
get(crate::callers::song::endpoint::download_song),
|
||||||
|
)
|
||||||
|
.route(
|
||||||
|
crate::callers::endpoints::DELETESONG,
|
||||||
|
delete(crate::callers::song::endpoint::delete_song),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn app() -> axum::Router {
|
pub async fn app() -> axum::Router {
|
||||||
@@ -353,7 +365,7 @@ mod tests {
|
|||||||
app: &axum::Router,
|
app: &axum::Router,
|
||||||
) -> Result<axum::response::Response, std::convert::Infallible> {
|
) -> Result<axum::response::Response, std::convert::Infallible> {
|
||||||
let mut form = MultipartForm::default();
|
let mut form = MultipartForm::default();
|
||||||
let _ = form.add_file("jpg", "tests/I/Coverart.jpg");
|
let _ = form.add_file("jpg", "tests/I/Coverart-1.jpg");
|
||||||
|
|
||||||
// Create request
|
// Create request
|
||||||
let content_type = form.content_type();
|
let content_type = form.content_type();
|
||||||
@@ -654,6 +666,11 @@ mod tests {
|
|||||||
serde_json::from_slice(&body).unwrap()
|
serde_json::from_slice(&body).unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn format_url_with_value(endpoint: &str, value: &uuid::Uuid) -> String {
|
||||||
|
let last = endpoint.len() - 5;
|
||||||
|
format!("{}/{value}", &endpoint[0..last])
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: Change the name of the function to be more expressive and put into it's own module
|
// TODO: Change the name of the function to be more expressive and put into it's own module
|
||||||
pub mod payload_data {
|
pub mod payload_data {
|
||||||
pub async fn queue_metadata_payload_data(song_queue_id: &uuid::Uuid) -> serde_json::Value {
|
pub async fn queue_metadata_payload_data(song_queue_id: &uuid::Uuid) -> serde_json::Value {
|
||||||
@@ -1822,7 +1839,7 @@ mod tests {
|
|||||||
let _ = db_mgr::drop_database(&tm_pool, &db_name).await;
|
let _ = db_mgr::drop_database(&tm_pool, &db_name).await;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub mod after_song_queue {
|
pub mod zzz_after_song_queue {
|
||||||
use futures::StreamExt;
|
use futures::StreamExt;
|
||||||
use tower::ServiceExt;
|
use tower::ServiceExt;
|
||||||
|
|
||||||
@@ -1845,7 +1862,7 @@ mod tests {
|
|||||||
|
|
||||||
let app = super::init::app(pool).await;
|
let app = super::init::app(pool).await;
|
||||||
|
|
||||||
let id = test_data::song_id().await.unwrap();
|
let (id, _, _, _) = test_data::song_id().await.unwrap();
|
||||||
|
|
||||||
let uri = format!("{}?id={id}", crate::callers::endpoints::GETSONGS);
|
let uri = format!("{}?id={id}", crate::callers::endpoints::GETSONGS);
|
||||||
|
|
||||||
@@ -1933,8 +1950,29 @@ mod tests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub mod test_data {
|
pub mod test_data {
|
||||||
pub async fn song_id() -> Result<uuid::Uuid, uuid::Error> {
|
pub async fn song_id() -> Result<(uuid::Uuid, String, String, String), uuid::Error> {
|
||||||
uuid::Uuid::parse_str("44cf7940-34ff-489f-9124-d0ec90a55af9")
|
match uuid::Uuid::parse_str("44cf7940-34ff-489f-9124-d0ec90a55af9") {
|
||||||
|
Ok(id) => Ok((
|
||||||
|
id,
|
||||||
|
String::from("tests/I/"),
|
||||||
|
String::from("track01.flac"),
|
||||||
|
String::from("tests/I/Coverart-1.jpg"),
|
||||||
|
)),
|
||||||
|
Err(err) => Err(err),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn other_song_id() -> Result<(uuid::Uuid, String, String, String), uuid::Error>
|
||||||
|
{
|
||||||
|
match uuid::Uuid::parse_str("94cf7940-34ff-489f-9124-d0ec90a55af4") {
|
||||||
|
Ok(id) => Ok((
|
||||||
|
id,
|
||||||
|
String::from("tests/I/"),
|
||||||
|
String::from("track02.flac"),
|
||||||
|
String::from("tests/I/Coverart-2.jpg"),
|
||||||
|
)),
|
||||||
|
Err(err) => Err(err),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn coverart_id() -> Result<uuid::Uuid, uuid::Error> {
|
pub async fn coverart_id() -> Result<uuid::Uuid, uuid::Error> {
|
||||||
@@ -1961,7 +1999,7 @@ mod tests {
|
|||||||
|
|
||||||
let app = super::init::app(pool).await;
|
let app = super::init::app(pool).await;
|
||||||
|
|
||||||
let id = test_data::song_id().await.unwrap();
|
let (id, _, _, _) = test_data::song_id().await.unwrap();
|
||||||
|
|
||||||
let my_url = crate::callers::endpoints::STREAMSONG;
|
let my_url = crate::callers::endpoints::STREAMSONG;
|
||||||
let last = my_url.len() - 5;
|
let last = my_url.len() - 5;
|
||||||
@@ -1997,5 +2035,268 @@ mod tests {
|
|||||||
|
|
||||||
let _ = super::db_mgr::drop_database(&tm_pool, &db_name).await;
|
let _ = super::db_mgr::drop_database(&tm_pool, &db_name).await;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn test_download_song() {
|
||||||
|
let tm_pool = super::db_mgr::get_pool().await.unwrap();
|
||||||
|
let db_name = super::db_mgr::generate_db_name().await;
|
||||||
|
|
||||||
|
match super::db_mgr::create_database(&tm_pool, &db_name).await {
|
||||||
|
Ok(_) => {
|
||||||
|
println!("Success");
|
||||||
|
}
|
||||||
|
Err(err) => {
|
||||||
|
assert!(false, "Error: {:?}", err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let pool = super::db_mgr::connect_to_db(&db_name).await.unwrap();
|
||||||
|
super::db_mgr::migrations(&pool).await;
|
||||||
|
|
||||||
|
let app = super::init::app(pool).await;
|
||||||
|
|
||||||
|
let (id, _, _, _) = test_data::song_id().await.unwrap();
|
||||||
|
|
||||||
|
let uri =
|
||||||
|
super::format_url_with_value(crate::callers::endpoints::DOWNLOADSONG, &id).await;
|
||||||
|
|
||||||
|
match app
|
||||||
|
.clone()
|
||||||
|
.oneshot(
|
||||||
|
axum::http::Request::builder()
|
||||||
|
.method(axum::http::Method::GET)
|
||||||
|
.uri(&uri)
|
||||||
|
.body(axum::body::Body::empty())
|
||||||
|
.unwrap(),
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
Ok(response) => {
|
||||||
|
let e = response.into_body();
|
||||||
|
let mut data = e.into_data_stream();
|
||||||
|
while let Some(chunk) = data.next().await {
|
||||||
|
match chunk {
|
||||||
|
Ok(_data) => {}
|
||||||
|
Err(err) => {
|
||||||
|
assert!(false, "Error: {err:?}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(err) => {
|
||||||
|
assert!(false, "Error: {err:?}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let _ = super::db_mgr::drop_database(&tm_pool, &db_name).await;
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn test_download_coverart() {
|
||||||
|
let tm_pool = super::db_mgr::get_pool().await.unwrap();
|
||||||
|
let db_name = super::db_mgr::generate_db_name().await;
|
||||||
|
|
||||||
|
match super::db_mgr::create_database(&tm_pool, &db_name).await {
|
||||||
|
Ok(_) => {
|
||||||
|
println!("Success");
|
||||||
|
}
|
||||||
|
Err(err) => {
|
||||||
|
assert!(false, "Error: {:?}", err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let pool = super::db_mgr::connect_to_db(&db_name).await.unwrap();
|
||||||
|
super::db_mgr::migrations(&pool).await;
|
||||||
|
|
||||||
|
let app = super::init::app(pool).await;
|
||||||
|
|
||||||
|
let id = test_data::coverart_id().await.unwrap();
|
||||||
|
|
||||||
|
let uri =
|
||||||
|
super::format_url_with_value(crate::callers::endpoints::DOWNLOADCOVERART, &id)
|
||||||
|
.await;
|
||||||
|
|
||||||
|
match app
|
||||||
|
.clone()
|
||||||
|
.oneshot(
|
||||||
|
axum::http::Request::builder()
|
||||||
|
.method(axum::http::Method::GET)
|
||||||
|
.uri(&uri)
|
||||||
|
.body(axum::body::Body::empty())
|
||||||
|
.unwrap(),
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
Ok(response) => {
|
||||||
|
let e = response.into_body();
|
||||||
|
let mut data = e.into_data_stream();
|
||||||
|
while let Some(chunk) = data.next().await {
|
||||||
|
match chunk {
|
||||||
|
Ok(_data) => {}
|
||||||
|
Err(err) => {
|
||||||
|
assert!(false, "Error: {err:?}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(err) => {
|
||||||
|
assert!(false, "Error: {err:?}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let _ = super::db_mgr::drop_database(&tm_pool, &db_name).await;
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn get_test_data(
|
||||||
|
song_directory: &String,
|
||||||
|
song_filename: &String,
|
||||||
|
coverart_path: &String,
|
||||||
|
) -> Result<(Vec<u8>, Vec<u8>), std::io::Error> {
|
||||||
|
let song = icarus_models::song::Song {
|
||||||
|
directory: song_directory.clone(),
|
||||||
|
filename: song_filename.clone(),
|
||||||
|
..Default::default()
|
||||||
|
};
|
||||||
|
|
||||||
|
let coverart = icarus_models::coverart::CoverArt {
|
||||||
|
path: coverart_path.clone(),
|
||||||
|
..Default::default()
|
||||||
|
};
|
||||||
|
|
||||||
|
match song.to_data() {
|
||||||
|
Ok(song_data) => match coverart.to_data() {
|
||||||
|
Ok(coverart_data) => Ok((song_data, coverart_data)),
|
||||||
|
Err(err) => Err(err),
|
||||||
|
},
|
||||||
|
Err(err) => Err(err),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn save_test_again(
|
||||||
|
song_directory: &String,
|
||||||
|
song_filename: &String,
|
||||||
|
song_data: Vec<u8>,
|
||||||
|
coverart_path: &String,
|
||||||
|
coverart_data: Vec<u8>,
|
||||||
|
) -> Result<(), std::io::Error> {
|
||||||
|
let song = icarus_models::song::Song {
|
||||||
|
directory: song_directory.clone(),
|
||||||
|
filename: song_filename.clone(),
|
||||||
|
..Default::default()
|
||||||
|
};
|
||||||
|
|
||||||
|
let coverart = icarus_models::coverart::CoverArt {
|
||||||
|
path: coverart_path.clone(),
|
||||||
|
..Default::default()
|
||||||
|
};
|
||||||
|
|
||||||
|
use std::io::Write;
|
||||||
|
|
||||||
|
match song.song_path() {
|
||||||
|
Ok(song_path) => {
|
||||||
|
let song_p = std::path::Path::new(&song_path);
|
||||||
|
match std::fs::File::create(song_p) {
|
||||||
|
Ok(mut song_file) => match song_file.write_all(&song_data) {
|
||||||
|
Ok(_) => {}
|
||||||
|
Err(err) => {
|
||||||
|
return Err(err);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
Err(err) => {
|
||||||
|
return Err(err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(err) => {
|
||||||
|
return Err(err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let coverart_p = std::path::Path::new(&coverart.path);
|
||||||
|
match std::fs::File::create(coverart_p) {
|
||||||
|
Ok(mut coverart_file) => match coverart_file.write_all(&coverart_data) {
|
||||||
|
Ok(_) => Ok(()),
|
||||||
|
Err(err) => Err(err),
|
||||||
|
},
|
||||||
|
Err(err) => Err(err),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn test_last_delete_song() {
|
||||||
|
let tm_pool = super::db_mgr::get_pool().await.unwrap();
|
||||||
|
let db_name = super::db_mgr::generate_db_name().await;
|
||||||
|
|
||||||
|
match super::db_mgr::create_database(&tm_pool, &db_name).await {
|
||||||
|
Ok(_) => {
|
||||||
|
println!("Success");
|
||||||
|
}
|
||||||
|
Err(err) => {
|
||||||
|
assert!(false, "Error: {:?}", err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let pool = super::db_mgr::connect_to_db(&db_name).await.unwrap();
|
||||||
|
super::db_mgr::migrations(&pool).await;
|
||||||
|
|
||||||
|
let app = super::init::app(pool).await;
|
||||||
|
|
||||||
|
let (id, song_directory, song_filename, coverart_path) =
|
||||||
|
test_data::other_song_id().await.unwrap();
|
||||||
|
let (song_data, coverart_data) =
|
||||||
|
get_test_data(&song_directory, &song_filename, &coverart_path)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let uri =
|
||||||
|
super::format_url_with_value(crate::callers::endpoints::DELETESONG, &id).await;
|
||||||
|
|
||||||
|
match app
|
||||||
|
.clone()
|
||||||
|
.oneshot(
|
||||||
|
axum::http::Request::builder()
|
||||||
|
.method(axum::http::Method::DELETE)
|
||||||
|
.uri(&uri)
|
||||||
|
.body(axum::body::Body::empty())
|
||||||
|
.unwrap(),
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
Ok(response) => {
|
||||||
|
let resp = super::get_resp_data::<
|
||||||
|
crate::callers::song::response::delete_song::Response,
|
||||||
|
>(response)
|
||||||
|
.await;
|
||||||
|
assert_eq!(false, resp.data.is_empty(), "Response has no data");
|
||||||
|
|
||||||
|
let song_and_coverart = &resp.data[0];
|
||||||
|
assert_eq!(
|
||||||
|
id, song_and_coverart.song.id,
|
||||||
|
"Song Ids do not match {id:?} {:?}",
|
||||||
|
song_and_coverart.song.id
|
||||||
|
);
|
||||||
|
|
||||||
|
match save_test_again(
|
||||||
|
&song_directory,
|
||||||
|
&song_filename,
|
||||||
|
song_data,
|
||||||
|
&coverart_path,
|
||||||
|
coverart_data,
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
Ok(_) => {}
|
||||||
|
Err(err) => {
|
||||||
|
assert!(false, "Error: {err:?}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(err) => {
|
||||||
|
assert!(false, "Error: {err:?}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let _ = super::db_mgr::drop_database(&tm_pool, &db_name).await;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,3 +1,6 @@
|
|||||||
-- Add migration script here
|
-- Add migration script here
|
||||||
INSERT INTO "song" (id, title, artist, album_artist, album, genre, year, track, disc, track_count, disc_count, duration, audio_type, date_created, filename, directory, user_id) VALUES('44cf7940-34ff-489f-9124-d0ec90a55af9', 'Hypocrite Like The Rest', 'Kuoth', 'Kuoth', 'I', 'Alternative Hip-Hop', 2020, 1, 1, 9, 1, 139, 'flac', '2020-01-01 13:00:00-05', 'track01.flac', 'tests/I', '47491f9b-725a-4ba4-b9a5-711e1be46670');
|
INSERT INTO "song" (id, title, artist, album_artist, album, genre, year, track, disc, track_count, disc_count, duration, audio_type, date_created, filename, directory, user_id) VALUES('44cf7940-34ff-489f-9124-d0ec90a55af9', 'Hypocrite Like The Rest', 'Kuoth', 'Kuoth', 'I', 'Alternative Hip-Hop', 2020, 1, 1, 9, 1, 139, 'flac', '2020-01-01 13:00:00-05', 'track01.flac', 'tests/I', '47491f9b-725a-4ba4-b9a5-711e1be46670');
|
||||||
INSERT INTO "coverart" VALUES('996122cd-5ae9-4013-9934-60768d3006ed', 'I', 'tests/I/Coverart.jpg', '44cf7940-34ff-489f-9124-d0ec90a55af9');
|
INSERT INTO "coverart" VALUES('996122cd-5ae9-4013-9934-60768d3006ed', 'I', 'tests/I/Coverart-1.jpg', '44cf7940-34ff-489f-9124-d0ec90a55af9');
|
||||||
|
|
||||||
|
INSERT INTO "song" (id, title, artist, album_artist, album, genre, year, track, disc, track_count, disc_count, duration, audio_type, date_created, filename, directory, user_id) VALUES('94cf7940-34ff-489f-9124-d0ec90a55af4', 'It''s Too Late', 'Kuoth', 'Kuoth', 'I', 'Alternative Hip-Hop', 2020, 2, 1, 9, 1, 116, 'flac', '2020-01-01 13:01:00-05', 'track02.flac', 'tests/I', '47491f9b-725a-4ba4-b9a5-711e1be46670');
|
||||||
|
INSERT INTO "coverart" VALUES('d96122cd-5ae9-4013-9934-60768d3006e9', 'I', 'tests/I/Coverart-2.jpg', '94cf7940-34ff-489f-9124-d0ec90a55af4');
|
||||||
|
|||||||
|
Before Width: | Height: | Size: 6.7 MiB After Width: | Height: | Size: 6.7 MiB |
Binary file not shown.
|
After Width: | Height: | Size: 6.7 MiB |
Reference in New Issue
Block a user