Some checks failed
Push Workflows / docs (push) Successful in 1m35s
Push Workflows / rustfmt (push) Successful in 11s
Push Workflows / clippy (push) Failing after 58s
Push Workflows / leptos-test (push) Successful in 3m4s
Push Workflows / test (push) Successful in 3m22s
Push Workflows / build (push) Successful in 4m43s
Push Workflows / docker-build (push) Failing after 14m42s
Push Workflows / nix-build (push) Successful in 17m22s
80 lines
2.2 KiB
Rust
80 lines
2.2 KiB
Rust
use crate::app::App;
|
|
use axum::response::Response as AxumResponse;
|
|
use axum::{
|
|
body::Body,
|
|
extract::State,
|
|
http::{Request, Response, StatusCode, Uri},
|
|
response::IntoResponse,
|
|
};
|
|
use leptos::prelude::*;
|
|
use std::str::FromStr;
|
|
use tower::ServiceExt;
|
|
use tower_http::services::ServeDir;
|
|
|
|
pub async fn file_and_error_handler(
|
|
uri: Uri,
|
|
State(options): State<LeptosOptions>,
|
|
req: Request<Body>,
|
|
) -> AxumResponse {
|
|
let root = options.site_root.clone();
|
|
let res = get_static_file(uri.clone(), &root).await.unwrap();
|
|
|
|
if res.status() == StatusCode::OK {
|
|
res.into_response()
|
|
} else {
|
|
let handler = leptos_axum::render_app_to_stream(App);
|
|
handler(req).await.into_response()
|
|
}
|
|
}
|
|
|
|
pub async fn get_static_file(uri: Uri, root: &str) -> Result<Response<Body>, (StatusCode, String)> {
|
|
let req = Request::builder()
|
|
.uri(uri.clone())
|
|
.body(Body::empty())
|
|
.unwrap();
|
|
|
|
// `ServeDir` implements `tower::Service` so we can call it with `tower::ServiceExt::oneshot`
|
|
// This path is relative to the cargo root
|
|
match ServeDir::new(root).oneshot(req).await.ok() {
|
|
Some(res) => Ok(res.into_response()),
|
|
None => Err((
|
|
StatusCode::INTERNAL_SERVER_ERROR,
|
|
"Something went wrong".to_string(),
|
|
)),
|
|
}
|
|
}
|
|
|
|
pub enum AssetType {
|
|
Audio,
|
|
Image,
|
|
}
|
|
|
|
pub async fn get_asset_file(
|
|
filename: String,
|
|
asset_type: AssetType,
|
|
) -> Result<Response<Body>, (StatusCode, String)> {
|
|
const DEFAULT_AUDIO_PATH: &str = "assets/audio";
|
|
const DEFAULT_IMAGE_PATH: &str = "assets/images";
|
|
|
|
let root = match asset_type {
|
|
AssetType::Audio => {
|
|
std::env::var("LIBRETUNES_AUDIO_PATH").unwrap_or(DEFAULT_AUDIO_PATH.to_string())
|
|
}
|
|
AssetType::Image => {
|
|
std::env::var("LIBRETUNES_IMAGE_PATH").unwrap_or(DEFAULT_IMAGE_PATH.to_string())
|
|
}
|
|
};
|
|
|
|
// Create a Uri from the filename
|
|
// ServeDir expects a leading `/`
|
|
let uri = Uri::from_str(format!("/{filename}").as_str());
|
|
|
|
match uri {
|
|
Ok(uri) => get_static_file(uri, root.as_str()).await,
|
|
Err(_) => Err((
|
|
StatusCode::INTERNAL_SERVER_ERROR,
|
|
"Attempted to serve an invalid file".to_string(),
|
|
)),
|
|
}
|
|
}
|