allow listing assets directories, no filters currently (maybe should add for web and non-image stuff)

This commit is contained in:
Rusty Striker 2025-06-30 19:44:39 +03:00
parent 56ab0642f2
commit 1595c5e8eb
Signed by: RustyStriker
GPG key ID: 87E4D691632DFF15

View file

@ -1,5 +1,9 @@
use axum::{ use axum::{
extract::ws::{self, Message}, http::StatusCode, response::{self, IntoResponse}, routing, Router Router,
extract::ws::{self, Message},
http::StatusCode,
response::{self, IntoResponse},
routing,
}; };
use futures_util::{ use futures_util::{
SinkExt, StreamExt, SinkExt, StreamExt,
@ -195,13 +199,31 @@ async fn style() -> impl response::IntoResponse {
async fn get_asset(asset: axum::extract::Path<String>) -> impl IntoResponse { async fn get_asset(asset: axum::extract::Path<String>) -> impl IntoResponse {
println!("Asset requested: {}", asset.0); println!("Asset requested: {}", asset.0);
let supported_file_types = [ let supported_file_types = [
(".jpg", "image/jpeg"), (".jpeg", "image/jpeg"), (".jpg", "image/jpeg"),
(".png", "image/png"), (".jxl", "image/jxl"), (".jpeg", "image/jpeg"),
(".png", "image/png"),
(".jxl", "image/jxl"),
]; ];
let mime = match supported_file_types.iter() // check if file is a folder. if so, list it
if let Ok(mut dir) = tokio::fs::read_dir(format!("assets/{}", asset.0)).await {
let mut files = Vec::new();
while let Ok(Some(entry)) = dir.next_entry().await {
if let Ok(name) = entry.file_name().into_string() {
files.push(name);
}
}
return Ok((
[(axum::http::header::CONTENT_TYPE, "application/json")],
axum::body::Body::from(serde_json::to_string(&files).unwrap_or(String::new())),
));
}
let mime = match supported_file_types
.iter()
.filter(|t| asset.0.ends_with(t.0)) .filter(|t| asset.0.ends_with(t.0))
.map(|t| t.1) .map(|t| t.1)
.next() { .next()
{
Some(t) => t, Some(t) => t,
None => return Err((StatusCode::UNSUPPORTED_MEDIA_TYPE, "Unsupported file type".to_string())), None => return Err((StatusCode::UNSUPPORTED_MEDIA_TYPE, "Unsupported file type".to_string())),
}; };
@ -212,8 +234,6 @@ async fn get_asset(asset: axum::extract::Path<String>) -> impl IntoResponse {
let stream = ReaderStream::new(file); let stream = ReaderStream::new(file);
let body = axum::body::Body::from_stream(stream); let body = axum::body::Body::from_stream(stream);
let headers = [ let headers = [(axum::http::header::CONTENT_TYPE, mime)];
(axum::http::header::CONTENT_TYPE, mime)
];
Ok((headers, body)) Ok((headers, body))
} }