allow listing assets directories, no filters currently (maybe should add for web and non-image stuff)
This commit is contained in:
parent
56ab0642f2
commit
1595c5e8eb
1 changed files with 33 additions and 13 deletions
36
src/main.rs
36
src/main.rs
|
@ -1,5 +1,9 @@
|
|||
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::{
|
||||
SinkExt, StreamExt,
|
||||
|
@ -195,13 +199,31 @@ async fn style() -> impl response::IntoResponse {
|
|||
async fn get_asset(asset: axum::extract::Path<String>) -> impl IntoResponse {
|
||||
println!("Asset requested: {}", asset.0);
|
||||
let supported_file_types = [
|
||||
(".jpg", "image/jpeg"), (".jpeg", "image/jpeg"),
|
||||
(".png", "image/png"), (".jxl", "image/jxl"),
|
||||
(".jpg", "image/jpeg"),
|
||||
(".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))
|
||||
.map(|t| t.1)
|
||||
.next() {
|
||||
.next()
|
||||
{
|
||||
Some(t) => t,
|
||||
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 body = axum::body::Body::from_stream(stream);
|
||||
|
||||
let headers = [
|
||||
(axum::http::header::CONTENT_TYPE, mime)
|
||||
];
|
||||
let headers = [(axum::http::header::CONTENT_TYPE, mime)];
|
||||
Ok((headers, body))
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue