Update Photos API
Since there is a body to the photos api it should be a post for now. Also updated the response to separate files and directories.
This commit is contained in:
38
src/main.rs
38
src/main.rs
@@ -2,11 +2,12 @@
|
||||
extern crate diesel;
|
||||
|
||||
use actix_files::NamedFile;
|
||||
use actix_web::web::{HttpResponse, HttpRequest, Json};
|
||||
use actix_web::{get, post, App, HttpServer, Responder};
|
||||
use actix_web::web::{HttpRequest, HttpResponse, Json};
|
||||
use actix_web::{get, post, web, App, HttpServer, Responder};
|
||||
use chrono::{Duration, Utc};
|
||||
use data::{LoginRequest, ThumbnailRequest};
|
||||
use jsonwebtoken::{encode, EncodingKey, Header};
|
||||
use serde::Serialize;
|
||||
use std::path::PathBuf;
|
||||
|
||||
use crate::data::{Claims, CreateAccountRequest, Token};
|
||||
@@ -34,6 +35,7 @@ async fn register(user: Json<CreateAccountRequest>) -> impl Responder {
|
||||
|
||||
#[post("/login")]
|
||||
async fn login(creds: Json<LoginRequest>) -> impl Responder {
|
||||
println!("Logging in: {}", creds.username);
|
||||
if let Some(user) = get_user(&creds.username, &creds.password) {
|
||||
let claims = Claims {
|
||||
sub: user.id.to_string(),
|
||||
@@ -51,18 +53,36 @@ async fn login(creds: Json<LoginRequest>) -> impl Responder {
|
||||
}
|
||||
}
|
||||
|
||||
#[get("/photos")]
|
||||
#[post("/photos")]
|
||||
async fn list_photos(_claims: Claims, req: Json<ThumbnailRequest>) -> impl Responder {
|
||||
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())
|
||||
let files = list_files(path).unwrap_or_default();
|
||||
|
||||
let photos = &files
|
||||
.iter()
|
||||
.filter(|f| !f.extension().unwrap_or_default().is_empty())
|
||||
.map(|f| f.to_str().unwrap().to_string())
|
||||
.collect::<Vec<String>>();
|
||||
|
||||
let dirs = &files
|
||||
.iter()
|
||||
.filter(|f| f.extension().unwrap_or_default().is_empty())
|
||||
.map(|f| f.to_str().unwrap().to_string())
|
||||
.collect::<Vec<String>>();
|
||||
|
||||
HttpResponse::Ok().json(PhotosResponse { photos, dirs })
|
||||
} else {
|
||||
HttpResponse::BadRequest().finish()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
struct PhotosResponse<'a> {
|
||||
photos: &'a [String],
|
||||
dirs: &'a [String],
|
||||
}
|
||||
|
||||
fn is_valid_path(path: &str) -> Option<PathBuf> {
|
||||
@@ -83,7 +103,11 @@ fn is_valid_path(path: &str) -> Option<PathBuf> {
|
||||
}
|
||||
|
||||
#[get("/image")]
|
||||
async fn image(_claims: Claims, request: HttpRequest, req: Json<ThumbnailRequest>) -> impl Responder {
|
||||
async fn image(
|
||||
_claims: Claims,
|
||||
request: HttpRequest,
|
||||
req: web::Query<ThumbnailRequest>,
|
||||
) -> impl Responder {
|
||||
if let Some(path) = is_valid_path(&req.path) {
|
||||
if let Ok(file) = NamedFile::open(path) {
|
||||
file.into_response(&request).unwrap()
|
||||
@@ -91,7 +115,7 @@ async fn image(_claims: Claims, request: HttpRequest, req: Json<ThumbnailRequest
|
||||
HttpResponse::NotFound().finish()
|
||||
}
|
||||
} else {
|
||||
HttpResponse::NotFound().finish()
|
||||
HttpResponse::BadRequest().finish()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user