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:
Cameron Cordes
2020-07-09 18:22:03 -04:00
parent e0e12dcc7b
commit 78c066b7be
3 changed files with 34 additions and 9 deletions

View File

@@ -21,7 +21,6 @@ impl FromStr for Claims {
fn from_str(s: &str) -> Result<Self, Self::Err> {
let token = *(s.split("Bearer ").collect::<Vec<_>>().last().unwrap_or(&""));
println!("Parsing token: '{}'", token);
match decode::<Claims>(
&token,

View File

@@ -9,7 +9,9 @@ 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(dotenv::var("BASE_PATH").unwrap()).unwrap();
let relative = path
.strip_prefix(dotenv::var("BASE_PATH").unwrap())
.unwrap();
relative.to_path_buf()
})
.collect::<Vec<PathBuf>>();

View File

@@ -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()
}
}