Compare commits
21 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 2023fc81f8 | |||
| 48a40a4821 | |||
| 8bf99be22c | |||
| 8c12c8c761 | |||
| caaec248db | |||
| 309d76785a | |||
| c83a37c20a | |||
| 032801b903 | |||
|
b844f06e82
|
|||
|
517a01d60c
|
|||
| 1ae2f88beb | |||
| 805df87f5b | |||
| 598996cb8d | |||
| 21a97c150c | |||
| 679a4ad322 | |||
| 9aefeace23 | |||
| a0c6ae65a3 | |||
| c7230d3b32 | |||
| 703fc49f32 | |||
| e98e055de0 | |||
| a8603361c0 |
@@ -1,3 +1,8 @@
|
|||||||
|
APP_ENV=development
|
||||||
|
BACKEND_PORT=8000
|
||||||
|
FRONTEND_URL=http://localhost:4200
|
||||||
|
RUST_LOG=debug
|
||||||
|
ALLOWED_ORIGINS=https://soaricarus.com,https://www.soaricarus.com
|
||||||
SECRET_MAIN_KEY=refero34o8rfhfjn983thf39fhc943rf923n3h
|
SECRET_MAIN_KEY=refero34o8rfhfjn983thf39fhc943rf923n3h
|
||||||
ROOT_DIRECTORY=/home/icarus/mydata
|
ROOT_DIRECTORY=/home/icarus/mydata
|
||||||
POSTGRES_MAIN_USER=icarus
|
POSTGRES_MAIN_USER=icarus
|
||||||
|
|||||||
@@ -1,3 +1,8 @@
|
|||||||
|
APP_ENV=production
|
||||||
|
BACKEND_PORT=8000
|
||||||
|
FRONTEND_URL=http://localhost:4200
|
||||||
|
RUST_LOG=debug
|
||||||
|
ALLOWED_ORIGINS=https://soaricarus.com,https://www.soaricarus.com,http://localhost:8000,http://127.0.0.1:8000
|
||||||
SECRET_MAIN_KEY=refero34o8rfhfjn983thf39fhc943rf923n3h
|
SECRET_MAIN_KEY=refero34o8rfhfjn983thf39fhc943rf923n3h
|
||||||
ROOT_DIRECTORY=/home/icarus/mydata
|
ROOT_DIRECTORY=/home/icarus/mydata
|
||||||
POSTGRES_MAIN_USER=icarus
|
POSTGRES_MAIN_USER=icarus
|
||||||
|
|||||||
@@ -0,0 +1,9 @@
|
|||||||
|
# Override specific file types
|
||||||
|
*.cs linguist-language=Rust
|
||||||
|
*.sln linguist-documentation
|
||||||
|
*.csproj linguist-documentation
|
||||||
|
|
||||||
|
# Or vendor all non-Rust files
|
||||||
|
*.cs linguist-vendored
|
||||||
|
*.js linguist-vendored
|
||||||
|
*.py linguist-vendored
|
||||||
@@ -16,7 +16,7 @@ jobs:
|
|||||||
- name: Install Rust
|
- name: Install Rust
|
||||||
uses: actions-rs/toolchain@v1
|
uses: actions-rs/toolchain@v1
|
||||||
with:
|
with:
|
||||||
toolchain: 1.88.0
|
toolchain: 1.90.0
|
||||||
components: cargo
|
components: cargo
|
||||||
|
|
||||||
- name: Extract Version from Cargo.toml
|
- name: Extract Version from Cargo.toml
|
||||||
|
|||||||
@@ -2,9 +2,9 @@ name: Rust CI
|
|||||||
|
|
||||||
on:
|
on:
|
||||||
push:
|
push:
|
||||||
branches: [ "v0.2" ]
|
branches: [ "main" ]
|
||||||
pull_request:
|
pull_request:
|
||||||
branches: [ "v0.2" ]
|
branches: [ "main" ]
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
build:
|
build:
|
||||||
@@ -25,12 +25,12 @@ jobs:
|
|||||||
--health-retries 5
|
--health-retries 5
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v4
|
- uses: actions/checkout@v5
|
||||||
|
|
||||||
- name: Install Rust 1.88.0
|
- name: Install Rust 1.90.0
|
||||||
uses: actions-rs/toolchain@v1
|
uses: actions-rs/toolchain@v1
|
||||||
with:
|
with:
|
||||||
toolchain: 1.88.0
|
toolchain: 1.90.0
|
||||||
components: clippy, rustfmt
|
components: clippy, rustfmt
|
||||||
override: true
|
override: true
|
||||||
|
|
||||||
@@ -68,7 +68,9 @@ 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: |
|
||||||
|
cat .env.sample | head -6 > .env
|
||||||
|
cargo test --verbose --
|
||||||
|
|
||||||
- name: Run clippy
|
- name: Run clippy
|
||||||
run: cargo clippy -- -D warnings
|
run: cargo clippy -- -D warnings
|
||||||
|
|||||||
@@ -7,3 +7,7 @@
|
|||||||
|
|
||||||
.DS_STORE
|
.DS_STORE
|
||||||
Storage/
|
Storage/
|
||||||
|
|
||||||
|
*.sln
|
||||||
|
*.cs
|
||||||
|
*.csproj
|
||||||
|
|||||||
Generated
+630
-290
File diff suppressed because it is too large
Load Diff
+25
-17
@@ -1,27 +1,35 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "icarus"
|
name = "icarus"
|
||||||
version = "0.1.94"
|
version = "0.3.0"
|
||||||
edition = "2024"
|
edition = "2024"
|
||||||
rust-version = "1.88"
|
rust-version = "1.90"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
axum = { version = "0.8.4", features = ["multipart"] }
|
axum = { version = "0.8.6", features = ["multipart"] }
|
||||||
serde = { version = "1.0.219", features = ["derive"] }
|
axum-extra = { version = "0.10.3", features = ["cookie"] }
|
||||||
serde_json = { version = "1.0.140" }
|
serde = { version = "1.0.228", features = ["derive"] }
|
||||||
tower = { version = "0.5.2" }
|
serde_json = { version = "1.0.145" }
|
||||||
tokio = { version = "1.45.1", features = ["full"] }
|
tower = { version = "0.5.2", features = ["full"] }
|
||||||
tokio-util = { version = "0.7.15" }
|
tokio = { version = "1.47.1", features = ["full"] }
|
||||||
tower-http = { version = "0.6.6", features = ["timeout"] }
|
tokio-util = { version = "0.7.16", features = ["io"] }
|
||||||
tracing-subscriber = "0.3.19"
|
tower-http = { version = "0.6.6", features = ["cors", "timeout"] }
|
||||||
|
tracing-subscriber = "0.3.20"
|
||||||
futures = { version = "0.3.31" }
|
futures = { version = "0.3.31" }
|
||||||
uuid = { version = "1.17.0", features = ["v4", "serde"] }
|
mime_guess = { version = "2.0.5" }
|
||||||
|
uuid = { version = "1.18.1", features = ["v4", "serde"] }
|
||||||
sqlx = { version = "0.8.6", features = ["postgres", "runtime-tokio-native-tls", "time", "uuid"] }
|
sqlx = { version = "0.8.6", features = ["postgres", "runtime-tokio-native-tls", "time", "uuid"] }
|
||||||
time = { version = "0.3.41", features = ["formatting", "macros", "parsing", "serde"] }
|
time = { version = "0.3.44", features = ["formatting", "macros", "parsing", "serde"] }
|
||||||
icarus_meta = { git = "ssh://git@git.kundeng.us/phoenix/icarus_meta.git", tag = "v0.3.0-devel-f4b71de969-680" }
|
thiserror = "2.0.17"
|
||||||
icarus_models = { git = "ssh://git@git.kundeng.us/phoenix/icarus_models.git", tag = "v0.4.5-devel-655d05dabb-111" }
|
base64 = "0.22.1"
|
||||||
icarus_envy = { git = "ssh://git@git.kundeng.us/phoenix/icarus_envy.git", tag = "v0.3.0-devel-d73fba9899-006" }
|
jsonwebtoken = { version = "9.3.1" }
|
||||||
|
josekit = { version = "0.10.3" }
|
||||||
|
utoipa = { version = "5.4.0", features = ["axum_extras"] }
|
||||||
|
utoipa-swagger-ui = { version = "9.0.2", features = ["axum"] }
|
||||||
|
icarus_meta = { git = "ssh://git@git.kundeng.us/phoenix/icarus_meta.git", tag = "v0.4.0" }
|
||||||
|
icarus_models = { git = "ssh://git@git.kundeng.us/phoenix/icarus_models.git", tag = "v0.7.0" }
|
||||||
|
icarus_envy = { git = "ssh://git@git.kundeng.us/phoenix/icarus_envy.git", tag = "v0.5.0" }
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
common-multipart-rfc7578 = { version = "0.7.0" }
|
common-multipart-rfc7578 = { version = "0.7.0" }
|
||||||
url = { version = "2.5.4" }
|
url = { version = "2.5.7" }
|
||||||
tempfile = { version = "3.20.0" }
|
tempfile = { version = "3.23.0" }
|
||||||
|
|||||||
+1
-1
@@ -1,5 +1,5 @@
|
|||||||
# Stage 1: Build the application
|
# Stage 1: Build the application
|
||||||
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
|
||||||
|
|||||||
@@ -0,0 +1,21 @@
|
|||||||
|
MIT License
|
||||||
|
|
||||||
|
Copyright (c) 2025 Kun Deng
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
SOFTWARE.
|
||||||
@@ -1,17 +1,29 @@
|
|||||||
|
# Icarus
|
||||||
|
Web API for the Icarus project.
|
||||||
|
|
||||||
|
|
||||||
# Getting Started
|
### Requires
|
||||||
## Docker
|
`icarus_auth` +v0.6.0
|
||||||
Make sure `icarus_auth` is located in the root of the parent directory if using docker.
|
`songparser` +v0.4.1
|
||||||
|
|
||||||
Create a `.env` file for both projects - `icarus_auth` and `icarus` - in the root of each project.
|
## Getting Started
|
||||||
|
Quickest way to get started is with docker. Make sure `icarus_auth` and `songparser` repositories
|
||||||
|
are located in the root of the parent directory. Check the respective repositories to ensure they
|
||||||
|
are setup correctly before configuring `Icarus`.
|
||||||
|
|
||||||
|
Copy the `.env.docker.sample` file to `.env`. Ensure that the `ROOT_DIRECTORY` variable is populated
|
||||||
|
and exists on the docker image's filesystem. The credentials for the database doesn't need to be
|
||||||
|
changed for development, but if deploying it, it should be modified.
|
||||||
|
|
||||||
Build containers
|
Build containers
|
||||||
```
|
```
|
||||||
docker compose build --ssh default api auth_api
|
docker compose build --ssh default
|
||||||
```
|
```
|
||||||
|
|
||||||
Bring it up
|
Bring it up
|
||||||
```
|
```
|
||||||
docker compose up -d --force-recreate api auth_api
|
docker compose up -d --force-recreate
|
||||||
```
|
```
|
||||||
|
|
||||||
|
To view the OpenAPI spec, run the project and access `/swagger-ui`. If running through docker,
|
||||||
|
the url would be something like `http://localhost:8000/swagger-ui`.
|
||||||
|
|||||||
+26
-9
@@ -10,14 +10,14 @@ services:
|
|||||||
ports:
|
ports:
|
||||||
# Map host port 8000 to container port 3000 (adjust as needed)
|
# Map host port 8000 to container port 3000 (adjust as needed)
|
||||||
# Format: "HOST_PORT:CONTAINER_PORT"
|
# Format: "HOST_PORT:CONTAINER_PORT"
|
||||||
- "8000:3000"
|
- "8000:8000"
|
||||||
env_file:
|
env_file:
|
||||||
- .env
|
- .env
|
||||||
depends_on:
|
depends_on:
|
||||||
main_db:
|
main_db:
|
||||||
condition: service_healthy
|
condition: service_healthy
|
||||||
networks:
|
networks:
|
||||||
- main-api-network
|
- icarus-network
|
||||||
restart: unless-stopped # Optional: Restart policy
|
restart: unless-stopped # Optional: Restart policy
|
||||||
|
|
||||||
# --- Web API auth ---
|
# --- Web API auth ---
|
||||||
@@ -29,7 +29,7 @@ services:
|
|||||||
container_name: auth_api
|
container_name: auth_api
|
||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
ports:
|
ports:
|
||||||
- "8001:3000" # Map host port 8000 to container port 8000 (adjust container port based on your app's EXPOSE in Dockerfile)
|
- "8001:8001" # Map host port 8000 to container port 8000 (adjust container port based on your app's EXPOSE in Dockerfile)
|
||||||
# environment:
|
# environment:
|
||||||
# Environment variables your API needs, e.g., database connection
|
# Environment variables your API needs, e.g., database connection
|
||||||
# Add other necessary environment variables
|
# Add other necessary environment variables
|
||||||
@@ -39,11 +39,30 @@ services:
|
|||||||
auth_db:
|
auth_db:
|
||||||
condition: service_healthy
|
condition: service_healthy
|
||||||
networks:
|
networks:
|
||||||
- auth-api-network
|
- icarus-network
|
||||||
# Optional: Mount local code for development (live reload)
|
# Optional: Mount local code for development (live reload)
|
||||||
# volumes:
|
# volumes:
|
||||||
# - ./path/to/your/web-api-repo:/app
|
# - ./path/to/your/web-api-repo:/app
|
||||||
|
|
||||||
|
# --- songparser service ---
|
||||||
|
songparser:
|
||||||
|
build:
|
||||||
|
context: ../songparser
|
||||||
|
ssh: ["default"]
|
||||||
|
dockerfile: Dockerfile
|
||||||
|
container_name: songparser
|
||||||
|
restart: unless-stopped
|
||||||
|
env_file:
|
||||||
|
- ../songparser/.env
|
||||||
|
depends_on:
|
||||||
|
- api
|
||||||
|
- main_db
|
||||||
|
- auth_api
|
||||||
|
- auth_db
|
||||||
|
networks:
|
||||||
|
- icarus-network
|
||||||
|
|
||||||
|
|
||||||
# PostgreSQL Database Service
|
# PostgreSQL Database Service
|
||||||
# --- icarus web api db ---
|
# --- icarus web api db ---
|
||||||
main_db:
|
main_db:
|
||||||
@@ -69,7 +88,7 @@ services:
|
|||||||
start_period: 10s
|
start_period: 10s
|
||||||
restart: always # Optional: Restart policy
|
restart: always # Optional: Restart policy
|
||||||
networks:
|
networks:
|
||||||
- main-api-network
|
- icarus-network
|
||||||
|
|
||||||
# --- icarus web auth api db ---
|
# --- icarus web auth api db ---
|
||||||
auth_db:
|
auth_db:
|
||||||
@@ -95,7 +114,7 @@ services:
|
|||||||
start_period: 10s
|
start_period: 10s
|
||||||
restart: always # Optional: Restart policy
|
restart: always # Optional: Restart policy
|
||||||
networks:
|
networks:
|
||||||
- auth-api-network
|
- icarus-network
|
||||||
|
|
||||||
# Define the named volume for data persistence
|
# Define the named volume for data persistence
|
||||||
volumes:
|
volumes:
|
||||||
@@ -106,7 +125,5 @@ volumes:
|
|||||||
|
|
||||||
# Define the network (optional, but good practice)
|
# Define the network (optional, but good practice)
|
||||||
networks:
|
networks:
|
||||||
main-api-network:
|
icarus-network:
|
||||||
driver: bridge
|
|
||||||
auth-api-network:
|
|
||||||
driver: bridge
|
driver: bridge
|
||||||
|
|||||||
+116
@@ -0,0 +1,116 @@
|
|||||||
|
use axum::{
|
||||||
|
Json,
|
||||||
|
http::{Request, StatusCode},
|
||||||
|
middleware::Next,
|
||||||
|
response::IntoResponse,
|
||||||
|
};
|
||||||
|
use axum_extra::extract::cookie::CookieJar;
|
||||||
|
use jsonwebtoken::{DecodingKey, Validation, decode};
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use thiserror::Error;
|
||||||
|
|
||||||
|
fn deserialize_i64_from_f64<'de, D>(deserializer: D) -> Result<i64, D::Error>
|
||||||
|
where
|
||||||
|
D: serde::Deserializer<'de>,
|
||||||
|
{
|
||||||
|
let val = f64::deserialize(deserializer)?;
|
||||||
|
// Handle NaN and infinity cases
|
||||||
|
if val.is_nan() || val.is_infinite() {
|
||||||
|
return Err(serde::de::Error::custom("invalid float value"));
|
||||||
|
}
|
||||||
|
// Round to nearest integer and convert
|
||||||
|
let rounded = val.round();
|
||||||
|
// Check if the rounded value can fit in i64
|
||||||
|
if rounded < (i64::MIN as f64) || rounded > (i64::MAX as f64) {
|
||||||
|
return Err(serde::de::Error::custom("float out of i64 range"));
|
||||||
|
}
|
||||||
|
Ok(rounded as i64)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Serialize, Deserialize)]
|
||||||
|
pub struct UserClaims {
|
||||||
|
pub iss: String,
|
||||||
|
pub aud: String, // Audience
|
||||||
|
pub sub: String, // Subject (user ID)
|
||||||
|
#[serde(deserialize_with = "deserialize_i64_from_f64")]
|
||||||
|
pub exp: i64, // Expiration time (UTC timestamp)
|
||||||
|
#[serde(deserialize_with = "deserialize_i64_from_f64")]
|
||||||
|
pub iat: i64, // Issued at (UTC timestamp)
|
||||||
|
// pub azp: String,
|
||||||
|
// pub gty: String,
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
pub roles: Option<Vec<String>>, // Optional roles
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Error, Debug)]
|
||||||
|
pub enum JwtError {
|
||||||
|
#[error("Token creation failed")]
|
||||||
|
TokenCreation,
|
||||||
|
#[error("Token verification failed")]
|
||||||
|
TokenVerification,
|
||||||
|
#[error("Invalid token")]
|
||||||
|
InvalidToken,
|
||||||
|
#[error("Token expired")]
|
||||||
|
ExpiredToken,
|
||||||
|
#[error("Invalid key")]
|
||||||
|
InvalidKey,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Serialize)]
|
||||||
|
pub struct ErrorResponse {
|
||||||
|
pub status: &'static str,
|
||||||
|
pub message: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn auth<B>(
|
||||||
|
cookie_jar: CookieJar,
|
||||||
|
req: Request<axum::body::Body>,
|
||||||
|
next: Next,
|
||||||
|
) -> Result<impl IntoResponse, (StatusCode, Json<ErrorResponse>)> {
|
||||||
|
println!("Cookie: {cookie_jar:?}");
|
||||||
|
|
||||||
|
let token = cookie_jar
|
||||||
|
.get("token")
|
||||||
|
.map(|cookie| cookie.value().to_string())
|
||||||
|
.or_else(|| {
|
||||||
|
req.headers()
|
||||||
|
.get(axum::http::header::AUTHORIZATION)
|
||||||
|
.and_then(|auth_header| auth_header.to_str().ok())
|
||||||
|
.and_then(|auth_value| {
|
||||||
|
println!("Auth value: {auth_value:?}");
|
||||||
|
auth_value.strip_prefix("Bearer ").map(String::from)
|
||||||
|
})
|
||||||
|
});
|
||||||
|
|
||||||
|
let token = token.ok_or_else(|| {
|
||||||
|
let json_error = ErrorResponse {
|
||||||
|
status: "fail",
|
||||||
|
message: "You are not logged in, please provide token".to_string(),
|
||||||
|
};
|
||||||
|
(StatusCode::UNAUTHORIZED, Json(json_error))
|
||||||
|
})?;
|
||||||
|
|
||||||
|
let secret_key = icarus_envy::environment::get_secret_main_key().await.value;
|
||||||
|
|
||||||
|
let mut validation = Validation::new(jsonwebtoken::Algorithm::HS256);
|
||||||
|
validation.set_audience(&["icarus"]); // Must match exactly what's in the token
|
||||||
|
let claims = decode::<UserClaims>(
|
||||||
|
&token,
|
||||||
|
// TODO: Replace with code to get secret from env
|
||||||
|
&DecodingKey::from_secret(secret_key.as_ref()),
|
||||||
|
&validation,
|
||||||
|
)
|
||||||
|
.map_err(|err| {
|
||||||
|
eprintln!("Error: {err:?}");
|
||||||
|
let json_error = ErrorResponse {
|
||||||
|
status: "fail",
|
||||||
|
message: "Invalid token - Error decoding claims".to_string(),
|
||||||
|
};
|
||||||
|
(StatusCode::UNAUTHORIZED, Json(json_error))
|
||||||
|
})?
|
||||||
|
.claims;
|
||||||
|
|
||||||
|
println!("Claims: {claims:?}");
|
||||||
|
|
||||||
|
Ok(next.run(req).await)
|
||||||
|
}
|
||||||
+343
-19
@@ -1,14 +1,26 @@
|
|||||||
// TODO: Separate queue and coverart endpoints
|
// TODO: Separate queue and coverart endpoints
|
||||||
#[derive(Debug, Default, serde::Deserialize, serde::Serialize)]
|
#[derive(Debug, Default, serde::Deserialize, serde::Serialize, utoipa::ToSchema)]
|
||||||
pub struct CoverArtQueue {
|
pub struct CoverArtQueue {
|
||||||
pub id: uuid::Uuid,
|
pub id: uuid::Uuid,
|
||||||
pub song_queue_id: uuid::Uuid,
|
pub song_queue_id: uuid::Uuid,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub mod request {
|
pub mod request {
|
||||||
|
pub mod queue {
|
||||||
|
#[derive(utoipa::ToSchema)]
|
||||||
|
pub struct Request {
|
||||||
|
/// Filename
|
||||||
|
pub file: String,
|
||||||
|
#[schema(rename = "type")]
|
||||||
|
/// File type. Should be a file and not a value
|
||||||
|
pub file_type: String,
|
||||||
|
/// Raw data of the cover art file
|
||||||
|
pub value: Vec<u8>,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub mod link {
|
pub mod link {
|
||||||
#[derive(Debug, Default, serde::Deserialize, serde::Serialize)]
|
#[derive(Debug, Default, serde::Deserialize, serde::Serialize, utoipa::ToSchema)]
|
||||||
pub struct Request {
|
pub struct Request {
|
||||||
pub coverart_id: uuid::Uuid,
|
pub coverart_id: uuid::Uuid,
|
||||||
pub song_queue_id: uuid::Uuid,
|
pub song_queue_id: uuid::Uuid,
|
||||||
@@ -16,7 +28,7 @@ pub mod request {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub mod fetch_coverart_no_data {
|
pub mod fetch_coverart_no_data {
|
||||||
#[derive(Debug, Default, serde::Deserialize, serde::Serialize)]
|
#[derive(Debug, Default, serde::Deserialize, serde::Serialize, utoipa::ToSchema)]
|
||||||
pub struct Params {
|
pub struct Params {
|
||||||
pub id: Option<uuid::Uuid>,
|
pub id: Option<uuid::Uuid>,
|
||||||
pub song_queue_id: Option<uuid::Uuid>,
|
pub song_queue_id: Option<uuid::Uuid>,
|
||||||
@@ -24,7 +36,7 @@ pub mod request {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub mod fetch_coverart_with_data {
|
pub mod fetch_coverart_with_data {
|
||||||
#[derive(Debug, serde::Deserialize, serde::Serialize)]
|
#[derive(Debug, serde::Deserialize, serde::Serialize, utoipa::ToSchema)]
|
||||||
pub struct Params {
|
pub struct Params {
|
||||||
pub id: Option<uuid::Uuid>,
|
pub id: Option<uuid::Uuid>,
|
||||||
pub song_queue_id: Option<uuid::Uuid>,
|
pub song_queue_id: Option<uuid::Uuid>,
|
||||||
@@ -32,7 +44,7 @@ pub mod request {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub mod create_coverart {
|
pub mod create_coverart {
|
||||||
#[derive(Debug, serde::Deserialize, serde::Serialize)]
|
#[derive(Debug, serde::Deserialize, serde::Serialize, utoipa::ToSchema)]
|
||||||
pub struct Request {
|
pub struct Request {
|
||||||
pub song_id: uuid::Uuid,
|
pub song_id: uuid::Uuid,
|
||||||
pub coverart_queue_id: uuid::Uuid,
|
pub coverart_queue_id: uuid::Uuid,
|
||||||
@@ -40,28 +52,35 @@ pub mod request {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub mod wipe_data_from_coverart_queue {
|
pub mod wipe_data_from_coverart_queue {
|
||||||
#[derive(Debug, serde::Deserialize, serde::Serialize)]
|
#[derive(Debug, serde::Deserialize, serde::Serialize, utoipa::ToSchema)]
|
||||||
pub struct Request {
|
pub struct Request {
|
||||||
pub coverart_queue_id: uuid::Uuid,
|
pub coverart_queue_id: uuid::Uuid,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub mod get_coverart {
|
||||||
|
#[derive(Debug, serde::Deserialize, serde::Serialize, utoipa::ToSchema)]
|
||||||
|
pub struct Params {
|
||||||
|
pub id: Option<uuid::Uuid>,
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub mod response {
|
pub mod response {
|
||||||
#[derive(Debug, Default, serde::Deserialize, serde::Serialize)]
|
#[derive(Debug, Default, serde::Deserialize, serde::Serialize, utoipa::ToSchema)]
|
||||||
pub struct Response {
|
pub struct Response {
|
||||||
pub message: String,
|
pub message: String,
|
||||||
pub data: Vec<uuid::Uuid>,
|
pub data: Vec<uuid::Uuid>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub mod link {
|
pub mod link {
|
||||||
#[derive(Debug, serde::Deserialize, serde::Serialize)]
|
#[derive(Debug, serde::Deserialize, serde::Serialize, utoipa::ToSchema)]
|
||||||
pub struct Id {
|
pub struct Id {
|
||||||
pub coverart_id: uuid::Uuid,
|
pub coverart_id: uuid::Uuid,
|
||||||
pub song_queue_id: uuid::Uuid,
|
pub song_queue_id: uuid::Uuid,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Default, serde::Deserialize, serde::Serialize)]
|
#[derive(Debug, Default, serde::Deserialize, serde::Serialize, utoipa::ToSchema)]
|
||||||
pub struct Response {
|
pub struct Response {
|
||||||
pub message: String,
|
pub message: String,
|
||||||
pub data: Vec<Id>,
|
pub data: Vec<Id>,
|
||||||
@@ -69,7 +88,7 @@ pub mod response {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub mod fetch_coverart_no_data {
|
pub mod fetch_coverart_no_data {
|
||||||
#[derive(Debug, Default, serde::Deserialize, serde::Serialize)]
|
#[derive(Debug, Default, serde::Deserialize, serde::Serialize, utoipa::ToSchema)]
|
||||||
pub struct Response {
|
pub struct Response {
|
||||||
pub message: String,
|
pub message: String,
|
||||||
pub data: Vec<super::super::CoverArtQueue>,
|
pub data: Vec<super::super::CoverArtQueue>,
|
||||||
@@ -77,7 +96,7 @@ pub mod response {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub mod fetch_coverart_with_data {
|
pub mod fetch_coverart_with_data {
|
||||||
#[derive(Debug, Default, serde::Deserialize, serde::Serialize)]
|
#[derive(Debug, Default, serde::Deserialize, serde::Serialize, utoipa::ToSchema)]
|
||||||
pub struct Response {
|
pub struct Response {
|
||||||
pub message: String,
|
pub message: String,
|
||||||
pub data: Vec<Vec<u8>>,
|
pub data: Vec<Vec<u8>>,
|
||||||
@@ -85,7 +104,7 @@ pub mod response {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub mod create_coverart {
|
pub mod create_coverart {
|
||||||
#[derive(Debug, Default, serde::Deserialize, serde::Serialize)]
|
#[derive(Debug, Default, serde::Deserialize, serde::Serialize, utoipa::ToSchema)]
|
||||||
pub struct Response {
|
pub struct Response {
|
||||||
pub message: String,
|
pub message: String,
|
||||||
pub data: Vec<icarus_models::coverart::CoverArt>,
|
pub data: Vec<icarus_models::coverart::CoverArt>,
|
||||||
@@ -93,12 +112,20 @@ pub mod response {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub mod wipe_data_from_coverart_queue {
|
pub mod wipe_data_from_coverart_queue {
|
||||||
#[derive(Debug, Default, serde::Deserialize, serde::Serialize)]
|
#[derive(Debug, Default, serde::Deserialize, serde::Serialize, utoipa::ToSchema)]
|
||||||
pub struct Response {
|
pub struct Response {
|
||||||
pub message: String,
|
pub message: String,
|
||||||
pub data: Vec<uuid::Uuid>,
|
pub data: Vec<uuid::Uuid>,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub mod get_coverart {
|
||||||
|
#[derive(Debug, Default, serde::Deserialize, serde::Serialize, utoipa::ToSchema)]
|
||||||
|
pub struct Response {
|
||||||
|
pub message: String,
|
||||||
|
pub data: Vec<icarus_models::coverart::CoverArt>,
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub mod db {
|
pub mod db {
|
||||||
@@ -321,13 +348,145 @@ pub mod cov_db {
|
|||||||
Err(_err) => Err(sqlx::Error::RowNotFound),
|
Err(_err) => Err(sqlx::Error::RowNotFound),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn get_coverart(
|
||||||
|
pool: &sqlx::PgPool,
|
||||||
|
id: &uuid::Uuid,
|
||||||
|
) -> Result<icarus_models::coverart::CoverArt, sqlx::Error> {
|
||||||
|
let result = sqlx::query(
|
||||||
|
r#"
|
||||||
|
SELECT id, title, path, song_id FROM "coverart" WHERE id = $1;
|
||||||
|
"#,
|
||||||
|
)
|
||||||
|
.bind(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 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 {
|
||||||
use std::io::Write;
|
|
||||||
|
|
||||||
use axum::response::IntoResponse;
|
use axum::response::IntoResponse;
|
||||||
|
|
||||||
|
/// Endpoint to queue cover art
|
||||||
|
#[utoipa::path(
|
||||||
|
post,
|
||||||
|
path = super::super::endpoints::QUEUECOVERART,
|
||||||
|
request_body(
|
||||||
|
content = super::request::queue::Request,
|
||||||
|
),
|
||||||
|
responses(
|
||||||
|
(status = 200, description = "Successful", body = super::response::Response),
|
||||||
|
(status = 400, description = "Error queueing cover art", body = super::response::Response)
|
||||||
|
)
|
||||||
|
)]
|
||||||
pub async fn queue(
|
pub async fn queue(
|
||||||
axum::Extension(pool): axum::Extension<sqlx::PgPool>,
|
axum::Extension(pool): axum::Extension<sqlx::PgPool>,
|
||||||
mut multipart: axum::extract::Multipart,
|
mut multipart: axum::extract::Multipart,
|
||||||
@@ -373,6 +532,20 @@ pub mod endpoint {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Endpoint to link queued cover art
|
||||||
|
#[utoipa::path(
|
||||||
|
post,
|
||||||
|
path = super::super::endpoints::QUEUECOVERARTLINK,
|
||||||
|
request_body(
|
||||||
|
content = super::request::link::Request,
|
||||||
|
description = "Linking queued cover art to queued song",
|
||||||
|
content_type = "application/json"
|
||||||
|
),
|
||||||
|
responses(
|
||||||
|
(status = 200, description = "Queued cover art linked", body = super::response::link::Response),
|
||||||
|
(status = 400, description = "Linkage failed", body = super::response::link::Response)
|
||||||
|
)
|
||||||
|
)]
|
||||||
pub async fn link(
|
pub async fn link(
|
||||||
axum::Extension(pool): axum::Extension<sqlx::PgPool>,
|
axum::Extension(pool): axum::Extension<sqlx::PgPool>,
|
||||||
axum::Json(payload): axum::Json<super::request::link::Request>,
|
axum::Json(payload): axum::Json<super::request::link::Request>,
|
||||||
@@ -400,6 +573,19 @@ pub mod endpoint {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Endpoint to fetch cover art details
|
||||||
|
#[utoipa::path(
|
||||||
|
get,
|
||||||
|
path = super::super::endpoints::QUEUECOVERART,
|
||||||
|
params(
|
||||||
|
("id" = uuid::Uuid, Path, description = "Queued cover art Id"),
|
||||||
|
("song_queue_id" = uuid::Uuid, Path, description = "Queued song Id")
|
||||||
|
),
|
||||||
|
responses(
|
||||||
|
(status = 200, description = "Queued song linked", body = super::response::fetch_coverart_no_data::Response),
|
||||||
|
(status = 400, description = "Linkage failed", body = super::response::fetch_coverart_no_data::Response)
|
||||||
|
)
|
||||||
|
)]
|
||||||
pub async fn fetch_coverart_no_data(
|
pub async fn fetch_coverart_no_data(
|
||||||
axum::Extension(pool): axum::Extension<sqlx::PgPool>,
|
axum::Extension(pool): axum::Extension<sqlx::PgPool>,
|
||||||
axum::extract::Query(params): axum::extract::Query<
|
axum::extract::Query(params): axum::extract::Query<
|
||||||
@@ -447,6 +633,16 @@ pub mod endpoint {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Endpoint to fetch the queued cover art data
|
||||||
|
#[utoipa::path(
|
||||||
|
get,
|
||||||
|
path = super::super::endpoints::QUEUECOVERARTDATA,
|
||||||
|
params(("id" = uuid::Uuid, Path, description = "Queued cover art Id")),
|
||||||
|
responses(
|
||||||
|
(status = 200, description = "Queued cover art data", body = Vec<u8>),
|
||||||
|
(status = 400, description = "Error fetching queued cover art data", body = Vec<u8>)
|
||||||
|
)
|
||||||
|
)]
|
||||||
pub async fn fetch_coverart_with_data(
|
pub async fn fetch_coverart_with_data(
|
||||||
axum::Extension(pool): axum::Extension<sqlx::PgPool>,
|
axum::Extension(pool): axum::Extension<sqlx::PgPool>,
|
||||||
axum::extract::Path(id): axum::extract::Path<uuid::Uuid>,
|
axum::extract::Path(id): axum::extract::Path<uuid::Uuid>,
|
||||||
@@ -475,6 +671,20 @@ pub mod endpoint {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Endpoint to create cover art
|
||||||
|
#[utoipa::path(
|
||||||
|
post,
|
||||||
|
path = super::super::endpoints::CREATECOVERART,
|
||||||
|
request_body(
|
||||||
|
content = super::request::create_coverart::Request,
|
||||||
|
description = "Data required to create cover art",
|
||||||
|
content_type = "application/json"
|
||||||
|
),
|
||||||
|
responses(
|
||||||
|
(status = 200, description = "Cover art created", body = super::response::create_coverart::Response),
|
||||||
|
(status = 400, description = "Failure in creating cover art", body = super::response::create_coverart::Response)
|
||||||
|
)
|
||||||
|
)]
|
||||||
pub async fn create_coverart(
|
pub async fn create_coverart(
|
||||||
axum::Extension(pool): axum::Extension<sqlx::PgPool>,
|
axum::Extension(pool): axum::Extension<sqlx::PgPool>,
|
||||||
axum::Json(payload): axum::Json<super::request::create_coverart::Request>,
|
axum::Json(payload): axum::Json<super::request::create_coverart::Request>,
|
||||||
@@ -490,7 +700,7 @@ pub mod endpoint {
|
|||||||
let song_id = payload.song_id;
|
let song_id = payload.song_id;
|
||||||
match crate::callers::song::song_db::get_song(&pool, &song_id).await {
|
match crate::callers::song::song_db::get_song(&pool, &song_id).await {
|
||||||
Ok(song) => {
|
Ok(song) => {
|
||||||
let directory = icarus_envy::environment::get_root_directory().await;
|
let directory = icarus_envy::environment::get_root_directory().await.value;
|
||||||
let dir = std::path::Path::new(&directory);
|
let dir = std::path::Path::new(&directory);
|
||||||
|
|
||||||
// TODO: Make this random and the file extension should not be hard coded
|
// TODO: Make this random and the file extension should not be hard coded
|
||||||
@@ -502,12 +712,14 @@ pub mod endpoint {
|
|||||||
coverart.title = song.album.clone();
|
coverart.title = song.album.clone();
|
||||||
coverart.data = data;
|
coverart.data = data;
|
||||||
|
|
||||||
let mut file = std::fs::File::create(&save_path).unwrap();
|
match coverart.save_to_filesystem() {
|
||||||
file.write_all(&coverart.data).unwrap();
|
Ok(_) => {
|
||||||
|
|
||||||
match super::cov_db::create(&pool, &coverart, &song.id).await {
|
match super::cov_db::create(&pool, &coverart, &song.id).await {
|
||||||
Ok(id) => {
|
Ok(id) => {
|
||||||
|
coverart.song_id = song_id;
|
||||||
coverart.id = id;
|
coverart.id = id;
|
||||||
|
println!("Cover Art created");
|
||||||
|
|
||||||
response.message = String::from("Successful");
|
response.message = String::from("Successful");
|
||||||
response.data.push(coverart);
|
response.data.push(coverart);
|
||||||
|
|
||||||
@@ -519,6 +731,15 @@ pub mod endpoint {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Err(err) => {
|
||||||
|
response.message = err.to_string();
|
||||||
|
(
|
||||||
|
axum::http::StatusCode::INTERNAL_SERVER_ERROR,
|
||||||
|
axum::Json(response),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
response.message = err.to_string();
|
response.message = err.to_string();
|
||||||
(axum::http::StatusCode::BAD_REQUEST, axum::Json(response))
|
(axum::http::StatusCode::BAD_REQUEST, axum::Json(response))
|
||||||
@@ -532,6 +753,21 @@ pub mod endpoint {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Endpoint to wipe data from the cover art queue
|
||||||
|
#[utoipa::path(
|
||||||
|
patch,
|
||||||
|
path = super::super::endpoints::QUEUECOVERARTDATAWIPE,
|
||||||
|
request_body(
|
||||||
|
content = super::request::wipe_data_from_coverart_queue::Request,
|
||||||
|
description = "Data required to wipe the data from the cover art queue",
|
||||||
|
content_type = "application/json"
|
||||||
|
),
|
||||||
|
responses(
|
||||||
|
(status = 200, description = "Data wiped from cover art queue", body = super::response::wipe_data_from_coverart_queue::Response),
|
||||||
|
(status = 400, description = "Error wiping the data", body = super::response::wipe_data_from_coverart_queue::Response),
|
||||||
|
(status = 404, description = "Cover art not found", body = super::response::wipe_data_from_coverart_queue::Response)
|
||||||
|
)
|
||||||
|
)]
|
||||||
pub async fn wipe_data_from_coverart_queue(
|
pub async fn wipe_data_from_coverart_queue(
|
||||||
axum::Extension(pool): axum::Extension<sqlx::PgPool>,
|
axum::Extension(pool): axum::Extension<sqlx::PgPool>,
|
||||||
axum::Json(payload): axum::Json<super::request::wipe_data_from_coverart_queue::Request>,
|
axum::Json(payload): axum::Json<super::request::wipe_data_from_coverart_queue::Request>,
|
||||||
@@ -560,4 +796,92 @@ pub mod endpoint {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Endpoint to get cover art with criteria
|
||||||
|
#[utoipa::path(
|
||||||
|
get,
|
||||||
|
path = super::super::endpoints::GETCOVERART,
|
||||||
|
params(
|
||||||
|
("id" = uuid::Uuid, Path, description = "Cover art Id")
|
||||||
|
),
|
||||||
|
responses(
|
||||||
|
(status = 200, description = "Cover art retrieved", body = super::response::get_coverart::Response),
|
||||||
|
(status = 400, description = "Error retrieving cover art", body = super::response::get_coverart::Response)
|
||||||
|
)
|
||||||
|
)]
|
||||||
|
pub async fn get_coverart(
|
||||||
|
axum::Extension(pool): axum::Extension<sqlx::PgPool>,
|
||||||
|
axum::extract::Query(params): axum::extract::Query<super::request::get_coverart::Params>,
|
||||||
|
) -> (
|
||||||
|
axum::http::StatusCode,
|
||||||
|
axum::Json<super::response::get_coverart::Response>,
|
||||||
|
) {
|
||||||
|
let mut response = super::response::get_coverart::Response::default();
|
||||||
|
|
||||||
|
match params.id {
|
||||||
|
Some(id) => match super::cov_db::get_coverart(&pool, &id).await {
|
||||||
|
Ok(coverart) => {
|
||||||
|
response.data.push(coverart);
|
||||||
|
response.message = String::from(super::super::response::SUCCESSFUL);
|
||||||
|
(axum::http::StatusCode::OK, axum::Json(response))
|
||||||
|
}
|
||||||
|
Err(err) => {
|
||||||
|
response.message = err.to_string();
|
||||||
|
(axum::http::StatusCode::BAD_REQUEST, axum::Json(response))
|
||||||
|
}
|
||||||
|
},
|
||||||
|
None => {
|
||||||
|
response.message = String::from("Invalid parameters");
|
||||||
|
(axum::http::StatusCode::BAD_REQUEST, axum::Json(response))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Endpoint to download cover art
|
||||||
|
#[utoipa::path(
|
||||||
|
get,
|
||||||
|
path = super::super::endpoints::DOWNLOADCOVERART,
|
||||||
|
params(
|
||||||
|
("id" = uuid::Uuid, Path, description = "Cover art Id")
|
||||||
|
),
|
||||||
|
responses(
|
||||||
|
(status = 200, description = "Cover art downloading", body = Vec<u8>),
|
||||||
|
(status = 404, description = "Cover art not found", body = Vec<u8>)
|
||||||
|
)
|
||||||
|
)]
|
||||||
|
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 icarus_models::coverart::io::to_data(&coverart) {
|
||||||
|
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(),
|
||||||
|
),
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+40
-8
@@ -1,10 +1,9 @@
|
|||||||
// TODO: Explicitly make this module target queueing a song's metadata
|
// TODO: Explicitly make this module target queueing a song's metadata
|
||||||
pub mod request {
|
pub mod request {
|
||||||
|
|
||||||
pub mod queue_metadata {
|
pub mod queue_metadata {
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
#[derive(Debug, Default, Deserialize, Serialize, sqlx::FromRow)]
|
#[derive(Debug, Default, Deserialize, Serialize, sqlx::FromRow, utoipa::ToSchema)]
|
||||||
pub struct Request {
|
pub struct Request {
|
||||||
pub song_queue_id: uuid::Uuid,
|
pub song_queue_id: uuid::Uuid,
|
||||||
pub album: String,
|
pub album: String,
|
||||||
@@ -43,7 +42,13 @@ pub mod request {
|
|||||||
|
|
||||||
pub mod fetch_metadata {
|
pub mod fetch_metadata {
|
||||||
#[derive(
|
#[derive(
|
||||||
Debug, Default, serde::Deserialize, serde::Serialize, sqlx::FromRow, sqlx::Decode,
|
Debug,
|
||||||
|
Default,
|
||||||
|
serde::Deserialize,
|
||||||
|
serde::Serialize,
|
||||||
|
sqlx::FromRow,
|
||||||
|
sqlx::Decode,
|
||||||
|
utoipa::ToSchema,
|
||||||
)]
|
)]
|
||||||
pub struct Params {
|
pub struct Params {
|
||||||
pub id: Option<uuid::Uuid>,
|
pub id: Option<uuid::Uuid>,
|
||||||
@@ -53,11 +58,10 @@ pub mod request {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub mod response {
|
pub mod response {
|
||||||
|
|
||||||
pub mod queue_metadata {
|
pub mod queue_metadata {
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
#[derive(Default, Deserialize, Serialize)]
|
#[derive(Default, Deserialize, Serialize, utoipa::ToSchema)]
|
||||||
pub struct Response {
|
pub struct Response {
|
||||||
pub message: String,
|
pub message: String,
|
||||||
pub data: Vec<uuid::Uuid>,
|
pub data: Vec<uuid::Uuid>,
|
||||||
@@ -67,7 +71,7 @@ pub mod response {
|
|||||||
pub mod fetch_metadata {
|
pub mod fetch_metadata {
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
#[derive(Default, Deserialize, Serialize)]
|
#[derive(Default, Deserialize, Serialize, utoipa::ToSchema)]
|
||||||
pub struct Response {
|
pub struct Response {
|
||||||
pub message: String,
|
pub message: String,
|
||||||
pub data: Vec<crate::callers::metadata::metadata_queue::MetadataQueue>,
|
pub data: Vec<crate::callers::metadata::metadata_queue::MetadataQueue>,
|
||||||
@@ -83,7 +87,7 @@ pub mod metadata_queue {
|
|||||||
pub id: uuid::Uuid,
|
pub id: uuid::Uuid,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, serde::Deserialize, serde::Serialize, sqlx::FromRow)]
|
#[derive(Debug, serde::Deserialize, serde::Serialize, sqlx::FromRow, utoipa::ToSchema)]
|
||||||
pub struct MetadataQueue {
|
pub struct MetadataQueue {
|
||||||
pub id: uuid::Uuid,
|
pub id: uuid::Uuid,
|
||||||
pub metadata: serde_json::Value,
|
pub metadata: serde_json::Value,
|
||||||
@@ -204,9 +208,24 @@ pub mod metadata_queue {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Module for metadata related endpoints
|
||||||
pub mod endpoint {
|
pub mod endpoint {
|
||||||
use axum::{Json, http::StatusCode};
|
use axum::{Json, http::StatusCode};
|
||||||
|
|
||||||
|
/// Endpoint to create queued metadata
|
||||||
|
#[utoipa::path(
|
||||||
|
post,
|
||||||
|
path = super::super::endpoints::QUEUEMETADATA,
|
||||||
|
request_body(
|
||||||
|
content = super::request::queue_metadata::Request,
|
||||||
|
description = "Data required to create queued metadata",
|
||||||
|
content_type = "application/json"
|
||||||
|
),
|
||||||
|
responses(
|
||||||
|
(status = 200, description = "Queued metadata created", body = super::response::queue_metadata::Response),
|
||||||
|
(status = 400, description = "Error creating queued metadata", body = super::response::queue_metadata::Response)
|
||||||
|
)
|
||||||
|
)]
|
||||||
pub async fn queue_metadata(
|
pub async fn queue_metadata(
|
||||||
axum::Extension(pool): axum::Extension<sqlx::PgPool>,
|
axum::Extension(pool): axum::Extension<sqlx::PgPool>,
|
||||||
Json(payload): Json<super::request::queue_metadata::Request>,
|
Json(payload): Json<super::request::queue_metadata::Request>,
|
||||||
@@ -221,7 +240,7 @@ pub mod endpoint {
|
|||||||
response.message = if response.data.is_empty() {
|
response.message = if response.data.is_empty() {
|
||||||
String::from("Error")
|
String::from("Error")
|
||||||
} else {
|
} else {
|
||||||
String::from("Success")
|
String::from(super::super::response::SUCCESSFUL)
|
||||||
};
|
};
|
||||||
|
|
||||||
(StatusCode::OK, Json(response))
|
(StatusCode::OK, Json(response))
|
||||||
@@ -233,6 +252,19 @@ pub mod endpoint {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Endpoint to get queued metadata
|
||||||
|
#[utoipa::path(
|
||||||
|
get,
|
||||||
|
path = super::super::endpoints::QUEUEMETADATA,
|
||||||
|
params(
|
||||||
|
("id" = uuid::Uuid, Path, description = "Id of queued metadata"),
|
||||||
|
("song_queue_id" = uuid::Uuid, Path, description = "Id of queued song")
|
||||||
|
),
|
||||||
|
responses(
|
||||||
|
(status = 200, description = "Queued metadata retrieved", body = super::response::fetch_metadata::Response),
|
||||||
|
(status = 400, description = "Error retrieving queued metadata", body = super::response::fetch_metadata::Response)
|
||||||
|
)
|
||||||
|
)]
|
||||||
pub async fn fetch_metadata(
|
pub async fn fetch_metadata(
|
||||||
axum::Extension(pool): axum::Extension<sqlx::PgPool>,
|
axum::Extension(pool): axum::Extension<sqlx::PgPool>,
|
||||||
axum::extract::Query(params): axum::extract::Query<super::request::fetch_metadata::Params>,
|
axum::extract::Query(params): axum::extract::Query<super::request::fetch_metadata::Params>,
|
||||||
|
|||||||
@@ -17,7 +17,13 @@ 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 GETALLSONGS: &str = "/api/v2/song/all";
|
||||||
|
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 DOWNLOADCOVERART: &str = "/api/v2/coverart/download/{id}";
|
||||||
}
|
}
|
||||||
|
|
||||||
pub mod response {
|
pub mod response {
|
||||||
|
|||||||
+613
-48
@@ -7,8 +7,21 @@ pub mod request {
|
|||||||
pub message: String,
|
pub message: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub mod song_queue {
|
||||||
|
#[derive(utoipa::ToSchema)]
|
||||||
|
pub struct SongQueueRequest {
|
||||||
|
/// Filename
|
||||||
|
pub file: String,
|
||||||
|
#[schema(rename = "type")]
|
||||||
|
/// File type. Should be a file and not a value
|
||||||
|
pub file_type: String,
|
||||||
|
/// Raw data of the flac file
|
||||||
|
pub value: Vec<u8>,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub mod update_status {
|
pub mod update_status {
|
||||||
#[derive(Default, serde::Deserialize, serde::Serialize)]
|
#[derive(Default, serde::Deserialize, serde::Serialize, utoipa::ToSchema)]
|
||||||
pub struct Request {
|
pub struct Request {
|
||||||
pub id: uuid::Uuid,
|
pub id: uuid::Uuid,
|
||||||
pub status: String,
|
pub status: String,
|
||||||
@@ -16,7 +29,7 @@ pub mod request {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub mod create_metadata {
|
pub mod create_metadata {
|
||||||
#[derive(Debug, serde::Deserialize, serde::Serialize)]
|
#[derive(Debug, serde::Deserialize, serde::Serialize, utoipa::ToSchema)]
|
||||||
pub struct Request {
|
pub struct Request {
|
||||||
pub title: String,
|
pub title: String,
|
||||||
pub artist: String,
|
pub artist: String,
|
||||||
@@ -79,14 +92,14 @@ pub mod request {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub mod wipe_data_from_song_queue {
|
pub mod wipe_data_from_song_queue {
|
||||||
#[derive(Debug, serde::Deserialize, serde::Serialize)]
|
#[derive(Debug, serde::Deserialize, serde::Serialize, utoipa::ToSchema)]
|
||||||
pub struct Request {
|
pub struct Request {
|
||||||
pub song_queue_id: uuid::Uuid,
|
pub song_queue_id: uuid::Uuid,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub mod link_user_id {
|
pub mod link_user_id {
|
||||||
#[derive(Debug, serde::Deserialize, serde::Serialize)]
|
#[derive(Debug, serde::Deserialize, serde::Serialize, utoipa::ToSchema)]
|
||||||
pub struct Request {
|
pub struct Request {
|
||||||
pub song_queue_id: uuid::Uuid,
|
pub song_queue_id: uuid::Uuid,
|
||||||
pub user_id: uuid::Uuid,
|
pub user_id: uuid::Uuid,
|
||||||
@@ -94,7 +107,7 @@ pub mod request {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub mod get_songs {
|
pub mod get_songs {
|
||||||
#[derive(Debug, Default, serde::Deserialize, serde::Serialize)]
|
#[derive(Debug, Default, serde::Deserialize, serde::Serialize, utoipa::ToSchema)]
|
||||||
pub struct Params {
|
pub struct Params {
|
||||||
pub id: Option<uuid::Uuid>,
|
pub id: Option<uuid::Uuid>,
|
||||||
}
|
}
|
||||||
@@ -104,16 +117,18 @@ pub mod request {
|
|||||||
pub mod response {
|
pub mod response {
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
#[derive(Default, Deserialize, Serialize)]
|
/// Song queue response
|
||||||
|
#[derive(Default, Deserialize, Serialize, utoipa::ToSchema)]
|
||||||
pub struct Response {
|
pub struct Response {
|
||||||
pub message: String,
|
pub message: String,
|
||||||
|
/// Id of the queued song
|
||||||
pub data: Vec<uuid::Uuid>,
|
pub data: Vec<uuid::Uuid>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub mod fetch_queue_song {
|
pub mod fetch_queue_song {
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
#[derive(Default, Deserialize, Serialize)]
|
#[derive(Default, Deserialize, Serialize, utoipa::ToSchema)]
|
||||||
pub struct Response {
|
pub struct Response {
|
||||||
pub message: String,
|
pub message: String,
|
||||||
pub data: Vec<crate::callers::song::song_queue::SongQueue>,
|
pub data: Vec<crate::callers::song::song_queue::SongQueue>,
|
||||||
@@ -121,13 +136,13 @@ pub mod response {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub mod update_status {
|
pub mod update_status {
|
||||||
#[derive(serde::Deserialize, serde::Serialize)]
|
#[derive(serde::Deserialize, serde::Serialize, utoipa::ToSchema)]
|
||||||
pub struct ChangedStatus {
|
pub struct ChangedStatus {
|
||||||
pub old_status: String,
|
pub old_status: String,
|
||||||
pub new_status: String,
|
pub new_status: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Default, serde::Deserialize, serde::Serialize)]
|
#[derive(Default, serde::Deserialize, serde::Serialize, utoipa::ToSchema)]
|
||||||
pub struct Response {
|
pub struct Response {
|
||||||
pub message: String,
|
pub message: String,
|
||||||
pub data: Vec<ChangedStatus>,
|
pub data: Vec<ChangedStatus>,
|
||||||
@@ -135,7 +150,7 @@ pub mod response {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub mod update_song_queue {
|
pub mod update_song_queue {
|
||||||
#[derive(Default, serde::Deserialize, serde::Serialize)]
|
#[derive(Default, serde::Deserialize, serde::Serialize, utoipa::ToSchema)]
|
||||||
pub struct Response {
|
pub struct Response {
|
||||||
pub message: String,
|
pub message: String,
|
||||||
pub data: Vec<uuid::Uuid>,
|
pub data: Vec<uuid::Uuid>,
|
||||||
@@ -143,7 +158,7 @@ pub mod response {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub mod create_metadata {
|
pub mod create_metadata {
|
||||||
#[derive(Debug, Default, serde::Deserialize, serde::Serialize)]
|
#[derive(Debug, Default, serde::Deserialize, serde::Serialize, utoipa::ToSchema)]
|
||||||
pub struct Response {
|
pub struct Response {
|
||||||
pub message: String,
|
pub message: String,
|
||||||
pub data: Vec<icarus_models::song::Song>,
|
pub data: Vec<icarus_models::song::Song>,
|
||||||
@@ -151,7 +166,7 @@ pub mod response {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub mod wipe_data_from_song_queue {
|
pub mod wipe_data_from_song_queue {
|
||||||
#[derive(Debug, Default, serde::Deserialize, serde::Serialize)]
|
#[derive(Debug, Default, serde::Deserialize, serde::Serialize, utoipa::ToSchema)]
|
||||||
pub struct Response {
|
pub struct Response {
|
||||||
pub message: String,
|
pub message: String,
|
||||||
pub data: Vec<uuid::Uuid>,
|
pub data: Vec<uuid::Uuid>,
|
||||||
@@ -159,7 +174,7 @@ pub mod response {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub mod link_user_id {
|
pub mod link_user_id {
|
||||||
#[derive(Debug, Default, serde::Deserialize, serde::Serialize)]
|
#[derive(Debug, Default, serde::Deserialize, serde::Serialize, utoipa::ToSchema)]
|
||||||
pub struct Response {
|
pub struct Response {
|
||||||
pub message: String,
|
pub message: String,
|
||||||
pub data: Vec<uuid::Uuid>,
|
pub data: Vec<uuid::Uuid>,
|
||||||
@@ -167,12 +182,26 @@ pub mod response {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub mod get_songs {
|
pub mod get_songs {
|
||||||
#[derive(Debug, Default, serde::Deserialize, serde::Serialize)]
|
#[derive(Debug, Default, serde::Deserialize, serde::Serialize, utoipa::ToSchema)]
|
||||||
pub struct Response {
|
pub struct Response {
|
||||||
pub message: String,
|
pub message: String,
|
||||||
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, utoipa::ToSchema)]
|
||||||
|
pub struct SongAndCoverArt {
|
||||||
|
pub song: icarus_models::song::Song,
|
||||||
|
pub coverart: icarus_models::coverart::CoverArt,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Default, serde::Deserialize, serde::Serialize, utoipa::ToSchema)]
|
||||||
|
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,17 +365,214 @@ pub mod song_db {
|
|||||||
Err(_) => Err(sqlx::Error::RowNotFound),
|
Err(_) => Err(sqlx::Error::RowNotFound),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn get_all_songs(
|
||||||
|
pool: &sqlx::PgPool,
|
||||||
|
) -> Result<Vec<icarus_models::song::Song>, sqlx::Error> {
|
||||||
|
let result = sqlx::query(
|
||||||
|
r#"
|
||||||
|
SELECT * FROM "song";
|
||||||
|
"#,
|
||||||
|
)
|
||||||
|
.fetch_all(pool)
|
||||||
|
.await
|
||||||
|
.map_err(|e| {
|
||||||
|
eprintln!("Error querying data: {e:?}");
|
||||||
|
});
|
||||||
|
|
||||||
|
match result {
|
||||||
|
Ok(rows) => {
|
||||||
|
let mut songs: Vec<icarus_models::song::Song> = Vec::new();
|
||||||
|
|
||||||
|
for row in rows {
|
||||||
|
let date_created_time: time::OffsetDateTime = row
|
||||||
|
.try_get("date_created")
|
||||||
|
.map_err(|_e| sqlx::Error::RowNotFound)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let song = 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(),
|
||||||
|
};
|
||||||
|
|
||||||
|
songs.push(song);
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(songs)
|
||||||
|
}
|
||||||
|
Err(_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 {
|
||||||
use sqlx::Row;
|
use sqlx::Row;
|
||||||
|
|
||||||
#[derive(Debug, serde::Serialize, sqlx::FromRow)]
|
// TODO: Move this somewhere else at some point
|
||||||
pub struct InsertedData {
|
#[derive(Debug, serde::Deserialize, serde::Serialize, sqlx::FromRow, utoipa::ToSchema)]
|
||||||
pub id: uuid::Uuid,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, serde::Deserialize, serde::Serialize, sqlx::FromRow)]
|
|
||||||
pub struct SongQueue {
|
pub struct SongQueue {
|
||||||
pub id: uuid::Uuid,
|
pub id: uuid::Uuid,
|
||||||
pub filename: String,
|
pub filename: String,
|
||||||
@@ -639,12 +865,25 @@ mod song_queue {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Module for song related endpoints
|
||||||
pub mod endpoint {
|
pub mod endpoint {
|
||||||
use axum::{Json, http::StatusCode, response::IntoResponse};
|
use axum::{Json, http::StatusCode, response::IntoResponse};
|
||||||
use std::io::Write;
|
|
||||||
|
|
||||||
use crate::callers::song::song_queue;
|
use crate::callers::song::song_queue;
|
||||||
|
|
||||||
|
/// Endpoint to queue a song. Starts the process and places the song in a queue
|
||||||
|
#[utoipa::path(
|
||||||
|
post,
|
||||||
|
path = super::super::endpoints::QUEUESONG,
|
||||||
|
request_body(
|
||||||
|
content = super::request::song_queue::SongQueueRequest,
|
||||||
|
description = "Multipart form data for uploading song",
|
||||||
|
content_type = "multipart/form-data"
|
||||||
|
),
|
||||||
|
responses(
|
||||||
|
(status = 200, description = "Song queued", body = super::response::Response)
|
||||||
|
)
|
||||||
|
)]
|
||||||
pub async fn queue_song(
|
pub async fn queue_song(
|
||||||
axum::Extension(pool): axum::Extension<sqlx::PgPool>,
|
axum::Extension(pool): axum::Extension<sqlx::PgPool>,
|
||||||
mut multipart: axum::extract::Multipart,
|
mut multipart: axum::extract::Multipart,
|
||||||
@@ -666,11 +905,6 @@ pub mod endpoint {
|
|||||||
data.len()
|
data.len()
|
||||||
);
|
);
|
||||||
|
|
||||||
// TODO: Remove this
|
|
||||||
// Save the file to disk
|
|
||||||
let mut file = std::fs::File::create(&file_name).unwrap();
|
|
||||||
file.write_all(&data).unwrap();
|
|
||||||
|
|
||||||
let raw_data: Vec<u8> = data.to_vec();
|
let raw_data: Vec<u8> = data.to_vec();
|
||||||
let queue_repo = song_queue::insert(
|
let queue_repo = song_queue::insert(
|
||||||
&pool,
|
&pool,
|
||||||
@@ -693,6 +927,20 @@ pub mod endpoint {
|
|||||||
(StatusCode::OK, Json(response))
|
(StatusCode::OK, Json(response))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Endpoint to link a user id to a queued song
|
||||||
|
#[utoipa::path(
|
||||||
|
patch,
|
||||||
|
path = super::super::endpoints::QUEUESONGLINKUSERID,
|
||||||
|
request_body(
|
||||||
|
content = super::request::link_user_id::Request,
|
||||||
|
description = "User Id and queued song id",
|
||||||
|
content_type = "application/json"
|
||||||
|
),
|
||||||
|
responses(
|
||||||
|
(status = 200, description = "Queued song linked", body = super::response::link_user_id::Response),
|
||||||
|
(status = 400, description = "Linkage failed", body = super::response::link_user_id::Response)
|
||||||
|
)
|
||||||
|
)]
|
||||||
pub async fn link_user_id(
|
pub async fn link_user_id(
|
||||||
axum::Extension(pool): axum::Extension<sqlx::PgPool>,
|
axum::Extension(pool): axum::Extension<sqlx::PgPool>,
|
||||||
axum::Json(payload): axum::Json<super::request::link_user_id::Request>,
|
axum::Json(payload): axum::Json<super::request::link_user_id::Request>,
|
||||||
@@ -724,6 +972,15 @@ pub mod endpoint {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Endpoint to fetch the next queued song as long as it is available
|
||||||
|
#[utoipa::path(
|
||||||
|
get,
|
||||||
|
path = super::super::endpoints::NEXTQUEUESONG,
|
||||||
|
responses(
|
||||||
|
(status = 200, description = "Queued song is present and available", body = super::response::fetch_queue_song::Response),
|
||||||
|
(status = 400, description = "Linkage failed", body = super::response::fetch_queue_song::Response)
|
||||||
|
)
|
||||||
|
)]
|
||||||
pub async fn fetch_queue_song(
|
pub async fn fetch_queue_song(
|
||||||
axum::Extension(pool): axum::Extension<sqlx::PgPool>,
|
axum::Extension(pool): axum::Extension<sqlx::PgPool>,
|
||||||
) -> (
|
) -> (
|
||||||
@@ -745,6 +1002,17 @@ pub mod endpoint {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: Rename
|
||||||
|
/// Endpoint to download the queued song
|
||||||
|
#[utoipa::path(
|
||||||
|
get,
|
||||||
|
path = super::super::endpoints::QUEUESONGDATA,
|
||||||
|
params(("id" = uuid::Uuid, Path, description = "Queued song Id")),
|
||||||
|
responses(
|
||||||
|
(status = 200, description = "Queued song linked", body = Vec<u8>),
|
||||||
|
(status = 400, description = "Linkage failed", body = Vec<u8>)
|
||||||
|
)
|
||||||
|
)]
|
||||||
pub async fn download_flac(
|
pub async fn download_flac(
|
||||||
axum::Extension(pool): axum::Extension<sqlx::PgPool>,
|
axum::Extension(pool): axum::Extension<sqlx::PgPool>,
|
||||||
axum::extract::Path(id): axum::extract::Path<uuid::Uuid>,
|
axum::extract::Path(id): axum::extract::Path<uuid::Uuid>,
|
||||||
@@ -773,6 +1041,20 @@ pub mod endpoint {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Endpoint to update the status of a queued song
|
||||||
|
#[utoipa::path(
|
||||||
|
patch,
|
||||||
|
path = super::super::endpoints::QUEUESONG,
|
||||||
|
request_body(
|
||||||
|
content = super::request::update_status::Request,
|
||||||
|
description = "Update the status of a queued song",
|
||||||
|
content_type = "application/json"
|
||||||
|
),
|
||||||
|
responses(
|
||||||
|
(status = 200, description = "Status has been updated", body = super::response::update_status::Response),
|
||||||
|
(status = 400, description = "Error updating status of queued song", body = super::response::update_status::Response)
|
||||||
|
)
|
||||||
|
)]
|
||||||
pub async fn update_song_queue_status(
|
pub async fn update_song_queue_status(
|
||||||
axum::Extension(pool): axum::Extension<sqlx::PgPool>,
|
axum::Extension(pool): axum::Extension<sqlx::PgPool>,
|
||||||
axum::Json(payload): axum::Json<super::request::update_status::Request>,
|
axum::Json(payload): axum::Json<super::request::update_status::Request>,
|
||||||
@@ -825,6 +1107,22 @@ pub mod endpoint {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Endpoint to update the queued song data
|
||||||
|
#[utoipa::path(
|
||||||
|
patch,
|
||||||
|
path = super::super::endpoints::QUEUESONGUPDATE,
|
||||||
|
request_body(
|
||||||
|
content = super::request::song_queue::SongQueueRequest,
|
||||||
|
description = "Multipart form data for uploading song",
|
||||||
|
content_type = "multipart/form-data"
|
||||||
|
),
|
||||||
|
params(("id" = uuid::Uuid, Path, description = "Queued song Id")),
|
||||||
|
responses(
|
||||||
|
(status = 200, description = "Queued song updated", body = super::response::update_song_queue::Response),
|
||||||
|
(status = 400, description = "Error updating queued song", body = super::response::update_song_queue::Response),
|
||||||
|
(status = 404, description = "Queued song not found", body = super::response::update_song_queue::Response)
|
||||||
|
)
|
||||||
|
)]
|
||||||
pub async fn update_song_queue(
|
pub async fn update_song_queue(
|
||||||
axum::extract::Path(id): axum::extract::Path<uuid::Uuid>,
|
axum::extract::Path(id): axum::extract::Path<uuid::Uuid>,
|
||||||
axum::Extension(pool): axum::Extension<sqlx::PgPool>,
|
axum::Extension(pool): axum::Extension<sqlx::PgPool>,
|
||||||
@@ -871,6 +1169,21 @@ pub mod endpoint {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Endpoint to create song
|
||||||
|
#[utoipa::path(
|
||||||
|
post,
|
||||||
|
path = super::super::endpoints::QUEUEMETADATA,
|
||||||
|
request_body(
|
||||||
|
content = super::request::create_metadata::Request,
|
||||||
|
description = "Data needed to create the song and save it to the filesystem",
|
||||||
|
content_type = "application/json"
|
||||||
|
),
|
||||||
|
responses(
|
||||||
|
(status = 200, description = "Song created", body = super::response::create_metadata::Response),
|
||||||
|
(status = 400, description = "Error", body = super::response::create_metadata::Response),
|
||||||
|
(status = 505, description = "Error creating song", body = super::response::create_metadata::Response)
|
||||||
|
)
|
||||||
|
)]
|
||||||
pub async fn create_metadata(
|
pub async fn create_metadata(
|
||||||
axum::Extension(pool): axum::Extension<sqlx::PgPool>,
|
axum::Extension(pool): axum::Extension<sqlx::PgPool>,
|
||||||
axum::Json(payload): axum::Json<super::request::create_metadata::Request>,
|
axum::Json(payload): axum::Json<super::request::create_metadata::Request>,
|
||||||
@@ -882,9 +1195,11 @@ pub mod endpoint {
|
|||||||
|
|
||||||
if payload.is_valid() {
|
if payload.is_valid() {
|
||||||
let mut song = payload.to_song();
|
let mut song = payload.to_song();
|
||||||
song.filename =
|
song.filename = icarus_models::song::generate_filename(
|
||||||
song.generate_filename(icarus_models::types::MusicTypes::FlacExtension, true);
|
icarus_models::types::MusicTypes::FlacExtension,
|
||||||
song.directory = icarus_envy::environment::get_root_directory().await;
|
true,
|
||||||
|
);
|
||||||
|
song.directory = icarus_envy::environment::get_root_directory().await.value;
|
||||||
|
|
||||||
match song_queue::get_data(&pool, &payload.song_queue_id).await {
|
match song_queue::get_data(&pool, &payload.song_queue_id).await {
|
||||||
Ok(data) => {
|
Ok(data) => {
|
||||||
@@ -902,13 +1217,7 @@ pub mod endpoint {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let save_path = dir.join(&song.filename);
|
match song.save_to_filesystem() {
|
||||||
|
|
||||||
match std::fs::File::create(&save_path) {
|
|
||||||
Ok(mut file) => {
|
|
||||||
file.write_all(&song.data).unwrap();
|
|
||||||
|
|
||||||
match song.song_path() {
|
|
||||||
Ok(_) => match super::song_db::insert(&pool, &song).await {
|
Ok(_) => match super::song_db::insert(&pool, &song).await {
|
||||||
Ok((date_created, id)) => {
|
Ok((date_created, id)) => {
|
||||||
song.id = id;
|
song.id = id;
|
||||||
@@ -919,23 +1228,12 @@ pub mod endpoint {
|
|||||||
(axum::http::StatusCode::OK, axum::Json(response))
|
(axum::http::StatusCode::OK, axum::Json(response))
|
||||||
}
|
}
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
response.message =
|
response.message = format!("{:?} song {:?}", err.to_string(), song);
|
||||||
format!("{:?} song {:?}", err.to_string(), song);
|
|
||||||
(axum::http::StatusCode::BAD_REQUEST, axum::Json(response))
|
(axum::http::StatusCode::BAD_REQUEST, axum::Json(response))
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
response.message = err.to_string();
|
response.message = err.to_string();
|
||||||
(axum::http::StatusCode::BAD_REQUEST, axum::Json(response))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Err(err) => {
|
|
||||||
let song_path = song.song_path();
|
|
||||||
response.message = format!(
|
|
||||||
"{err:?} Song directory: {} Filename: {} Save Path: {:?} Song Path: {:?}",
|
|
||||||
song.directory, song.filename, save_path, song_path
|
|
||||||
);
|
|
||||||
(
|
(
|
||||||
axum::http::StatusCode::INTERNAL_SERVER_ERROR,
|
axum::http::StatusCode::INTERNAL_SERVER_ERROR,
|
||||||
axum::Json(response),
|
axum::Json(response),
|
||||||
@@ -954,6 +1252,20 @@ pub mod endpoint {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Endpoint to wipe the data from a queued song
|
||||||
|
#[utoipa::path(
|
||||||
|
patch,
|
||||||
|
path = super::super::endpoints::QUEUESONGDATAWIPE,
|
||||||
|
request_body(
|
||||||
|
content = super::request::wipe_data_from_song_queue::Request,
|
||||||
|
description = "Pass the queued song Id to wipe the data",
|
||||||
|
content_type = "application/json"
|
||||||
|
),
|
||||||
|
responses(
|
||||||
|
(status = 200, description = "Queued song data wiped", body = super::response::wipe_data_from_song_queue::Response),
|
||||||
|
(status = 404, description = "Queued song cannot be found", body = super::response::wipe_data_from_song_queue::Response)
|
||||||
|
)
|
||||||
|
)]
|
||||||
pub async fn wipe_data_from_song_queue(
|
pub async fn wipe_data_from_song_queue(
|
||||||
axum::Extension(pool): axum::Extension<sqlx::PgPool>,
|
axum::Extension(pool): axum::Extension<sqlx::PgPool>,
|
||||||
axum::Json(payload): axum::Json<super::request::wipe_data_from_song_queue::Request>,
|
axum::Json(payload): axum::Json<super::request::wipe_data_from_song_queue::Request>,
|
||||||
@@ -984,6 +1296,18 @@ pub mod endpoint {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Endpoint to get songs
|
||||||
|
#[utoipa::path(
|
||||||
|
get,
|
||||||
|
path = super::super::endpoints::GETSONGS,
|
||||||
|
params(
|
||||||
|
("id" = uuid::Uuid, Path, description = "Id of song")
|
||||||
|
),
|
||||||
|
responses(
|
||||||
|
(status = 200, description = "Songs found", body = super::response::get_songs::Response),
|
||||||
|
(status = 400, description = "Error getting songs", body = super::response::get_songs::Response)
|
||||||
|
)
|
||||||
|
)]
|
||||||
pub async fn get_songs(
|
pub async fn get_songs(
|
||||||
axum::Extension(pool): axum::Extension<sqlx::PgPool>,
|
axum::Extension(pool): axum::Extension<sqlx::PgPool>,
|
||||||
axum::extract::Query(params): axum::extract::Query<super::request::get_songs::Params>,
|
axum::extract::Query(params): axum::extract::Query<super::request::get_songs::Params>,
|
||||||
@@ -1011,4 +1335,245 @@ pub mod endpoint {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Endpoint to get all songs
|
||||||
|
#[utoipa::path(
|
||||||
|
get,
|
||||||
|
path = super::super::endpoints::GETALLSONGS,
|
||||||
|
responses(
|
||||||
|
(status = 200, description = "Getting all songs", body = super::response::get_songs::Response),
|
||||||
|
(status = 404, description = "Song not found", body = super::response::get_songs::Response)
|
||||||
|
)
|
||||||
|
)]
|
||||||
|
pub async fn get_all_songs(
|
||||||
|
axum::Extension(pool): axum::Extension<sqlx::PgPool>,
|
||||||
|
) -> (
|
||||||
|
axum::http::StatusCode,
|
||||||
|
axum::Json<super::response::get_songs::Response>,
|
||||||
|
) {
|
||||||
|
let mut response = super::response::get_songs::Response::default();
|
||||||
|
|
||||||
|
match super::song_db::get_all_songs(&pool).await {
|
||||||
|
Ok(songs) => {
|
||||||
|
response.message = String::from(super::super::response::SUCCESSFUL);
|
||||||
|
response.data = songs;
|
||||||
|
(axum::http::StatusCode::OK, axum::Json(response))
|
||||||
|
}
|
||||||
|
Err(err) => {
|
||||||
|
response.message = err.to_string();
|
||||||
|
(axum::http::StatusCode::NOT_FOUND, axum::Json(response))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Ednpoint to stream song
|
||||||
|
#[utoipa::path(
|
||||||
|
get,
|
||||||
|
path = super::super::endpoints::STREAMSONG,
|
||||||
|
params(("id" = uuid::Uuid, Path, description = "Song Id")),
|
||||||
|
responses(
|
||||||
|
(status = 200, description = "Stream song", body = Vec<u8>),
|
||||||
|
(status = 500, description = "Error streaming song", body = (u64, String))
|
||||||
|
)
|
||||||
|
)]
|
||||||
|
pub async fn stream_song(
|
||||||
|
axum::Extension(pool): axum::Extension<sqlx::PgPool>,
|
||||||
|
axum::extract::Path(id): axum::extract::Path<uuid::Uuid>,
|
||||||
|
) -> impl IntoResponse {
|
||||||
|
match super::song_db::get_song(&pool, &id).await {
|
||||||
|
Ok(song) => {
|
||||||
|
let song_path = song.song_path().unwrap();
|
||||||
|
let path = std::path::Path::new(&song_path);
|
||||||
|
|
||||||
|
if !path.starts_with(&song.directory) || !path.exists() {
|
||||||
|
return Err((axum::http::StatusCode::NOT_FOUND, "File not found"));
|
||||||
|
}
|
||||||
|
|
||||||
|
let file = match tokio::fs::File::open(&path).await {
|
||||||
|
Ok(file) => file,
|
||||||
|
Err(_) => return Err((axum::http::StatusCode::NOT_FOUND, "File not found")),
|
||||||
|
};
|
||||||
|
|
||||||
|
let file_size = match file.metadata().await {
|
||||||
|
Ok(meta) => meta.len(),
|
||||||
|
Err(_) => {
|
||||||
|
return Err((
|
||||||
|
axum::http::StatusCode::INTERNAL_SERVER_ERROR,
|
||||||
|
"Could not read file",
|
||||||
|
));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let mime = mime_guess::from_path(path).first_or_octet_stream();
|
||||||
|
let stream = tokio_util::io::ReaderStream::new(file);
|
||||||
|
|
||||||
|
let rep = axum::response::Response::builder()
|
||||||
|
.header("content-type", mime.to_string())
|
||||||
|
.header("accept-ranges", "bytes")
|
||||||
|
.header("content-length", file_size.to_string())
|
||||||
|
.body(axum::body::Body::from_stream(stream))
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
Ok(rep)
|
||||||
|
}
|
||||||
|
Err(_err) => Err((
|
||||||
|
axum::http::StatusCode::INTERNAL_SERVER_ERROR,
|
||||||
|
"Could not find file",
|
||||||
|
)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Endpoint to download song
|
||||||
|
#[utoipa::path(
|
||||||
|
get,
|
||||||
|
path = super::super::endpoints::DOWNLOADSONG,
|
||||||
|
params(("id" = uuid::Uuid, Path, description = "Song Id")),
|
||||||
|
responses(
|
||||||
|
(status = 200, description = "Download song", body = (u64, Vec<u8>)),
|
||||||
|
(status = 404, description = "Song not found", body = (u64, Vec<u8>)),
|
||||||
|
(status = 400, description = "Error downloading song", body = (u64, Vec<u8>))
|
||||||
|
)
|
||||||
|
)]
|
||||||
|
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 icarus_models::song::io::to_data(&song) {
|
||||||
|
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(),
|
||||||
|
),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Endpoint to delete the song
|
||||||
|
#[utoipa::path(
|
||||||
|
delete,
|
||||||
|
path = super::super::endpoints::DELETESONG,
|
||||||
|
params(("id" = uuid::Uuid, Path, description = "Song Id")),
|
||||||
|
responses(
|
||||||
|
(status = 200, description = "Song deleted", body = super::response::delete_song::Response),
|
||||||
|
(status = 404, description = "Song not found", body = super::response::delete_song::Response),
|
||||||
|
(status = 500, description = "Error deleting song", body = super::response::delete_song::Response)
|
||||||
|
)
|
||||||
|
)]
|
||||||
|
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))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+743
-38
File diff suppressed because it is too large
Load Diff
@@ -56,7 +56,3 @@ CREATE TABLE IF NOT EXISTS "coverart" (
|
|||||||
path TEXT NOT NULL,
|
path TEXT NOT NULL,
|
||||||
song_id UUID NOT NULL
|
song_id UUID NOT NULL
|
||||||
);
|
);
|
||||||
|
|
||||||
-- Might not to disable constraints on fields
|
|
||||||
-- 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');
|
|
||||||
-- Re-enable the constraints on the fields
|
|
||||||
|
|||||||
@@ -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