Files
aindustries.be/src/pages/files.rs

20 lines
656 B
Rust

use crate::AppState;
use actix_web::web::Path;
use actix_web::{HttpResponse, Responder, get, web};
use std::fs::File;
use std::io::Read;
use std::path::PathBuf;
#[get("/static/{file_type}/{file_name}")]
async fn file(file: Path<(String, String)>, data: web::Data<AppState>) -> impl Responder {
let mut path = PathBuf::from(&data.assets);
path.push(file.0.replace("/", ""));
path.push(file.1.replace("/", ""));
let mut file = File::open(path).unwrap();
let mut bytes = Vec::new();
if file.read_to_end(&mut bytes).is_err() {
return HttpResponse::NotFound().body("File not found");
};
HttpResponse::Ok().body(bytes)
}