Files
ImageApi/src/main.rs
Cameron Cordes 36f7351627 Initial API setup
Right now we are just listing files in a given subdirectory with not
authentication.
2020-07-07 19:53:12 -04:00

51 lines
1.2 KiB
Rust

use actix_web::web::{HttpResponse, Json};
use actix_web::{get, post, App, HttpServer, Responder};
use data::{LoginRequest, ThumbnailRequest};
use std::path::PathBuf;
use crate::files::list_files;
mod data;
mod files;
#[post("/register")]
async fn register() -> impl Responder {
"".to_owned()
}
#[post("/login")]
async fn login(_creds: Json<LoginRequest>) -> impl Responder {
"".to_owned()
}
#[get("/photos")]
async fn list_photos(req: Json<ThumbnailRequest>) -> impl Responder {
println!("{}", req.path);
let path = &req.path;
match path {
path if path.contains("..") => HttpResponse::BadRequest().finish(),
path => {
let path = PathBuf::from(path);
if path.is_relative() {
let mut full_path = std::env::current_dir().unwrap();
full_path.push(path);
let files = list_files(full_path);
HttpResponse::Ok().json(files.unwrap_or_default())
} else {
HttpResponse::BadRequest().finish()
}
}
}
}
#[actix_rt::main]
async fn main() -> std::io::Result<()> {
HttpServer::new(|| App::new().service(login).service(list_photos))
.bind("127.0.0.1:8088")?
.run()
.await
}