Serving files is working
Right now we're not doing any streaming and this isn't ideal. I'll need to figure it out at some point.
This commit is contained in:
@@ -9,7 +9,7 @@ pub fn list_files(dir: PathBuf) -> io::Result<Vec<PathBuf>> {
|
||||
.filter(|entry| is_image_or_video(&entry.path()) || entry.file_type().unwrap().is_dir())
|
||||
.map(|entry| entry.path())
|
||||
.map(|path: PathBuf| {
|
||||
let relative = path.strip_prefix(std::env::current_dir().unwrap()).unwrap();
|
||||
let relative = path.strip_prefix(dotenv::var("BASE_PATH").unwrap()).unwrap();
|
||||
relative.to_path_buf()
|
||||
})
|
||||
.collect::<Vec<PathBuf>>();
|
||||
|
||||
35
src/main.rs
35
src/main.rs
@@ -1,7 +1,8 @@
|
||||
#[macro_use]
|
||||
extern crate diesel;
|
||||
|
||||
use actix_web::web::{HttpResponse, Json};
|
||||
use actix_files::NamedFile;
|
||||
use actix_web::web::{HttpResponse, HttpRequest, Json};
|
||||
use actix_web::{get, post, App, HttpServer, Responder};
|
||||
use chrono::{Duration, Utc};
|
||||
use data::{LoginRequest, ThumbnailRequest};
|
||||
@@ -55,24 +56,45 @@ async fn list_photos(_claims: Claims, req: Json<ThumbnailRequest>) -> impl Respo
|
||||
println!("{}", req.path);
|
||||
|
||||
let path = &req.path;
|
||||
if let Some(path) = is_valid_path(path) {
|
||||
let files = list_files(path);
|
||||
HttpResponse::Ok().json(files.unwrap_or_default())
|
||||
} else {
|
||||
HttpResponse::BadRequest().finish()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
fn is_valid_path(path: &str) -> Option<PathBuf> {
|
||||
match path {
|
||||
path if path.contains("..") => HttpResponse::BadRequest().finish(),
|
||||
path if path.contains("..") => None,
|
||||
|
||||
path => {
|
||||
let path = PathBuf::from(path);
|
||||
if path.is_relative() {
|
||||
let mut full_path = std::env::current_dir().unwrap();
|
||||
let mut full_path = PathBuf::from(dotenv::var("BASE_PATH").unwrap());
|
||||
full_path.push(path);
|
||||
let files = list_files(full_path);
|
||||
HttpResponse::Ok().json(files.unwrap_or_default())
|
||||
Some(full_path)
|
||||
} else {
|
||||
HttpResponse::BadRequest().finish()
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[get("/image")]
|
||||
async fn image(_claims: Claims, request: HttpRequest, req: Json<ThumbnailRequest>) -> impl Responder {
|
||||
if let Some(path) = is_valid_path(&req.path) {
|
||||
if let Ok(file) = NamedFile::open(path) {
|
||||
file.into_response(&request).unwrap()
|
||||
} else {
|
||||
HttpResponse::NotFound().finish()
|
||||
}
|
||||
} else {
|
||||
HttpResponse::NotFound().finish()
|
||||
}
|
||||
}
|
||||
|
||||
#[actix_rt::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
HttpServer::new(|| {
|
||||
@@ -80,6 +102,7 @@ async fn main() -> std::io::Result<()> {
|
||||
.service(login)
|
||||
.service(list_photos)
|
||||
.service(register)
|
||||
.service(image)
|
||||
})
|
||||
.bind("127.0.0.1:8088")?
|
||||
.run()
|
||||
|
||||
Reference in New Issue
Block a user