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:
@@ -21,7 +21,6 @@ impl FromStr for Claims {
|
|||||||
|
|
||||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||||
let token = *(s.split("Bearer ").collect::<Vec<_>>().last().unwrap_or(&""));
|
let token = *(s.split("Bearer ").collect::<Vec<_>>().last().unwrap_or(&""));
|
||||||
println!("Parsing token: '{}'", token);
|
|
||||||
|
|
||||||
match decode::<Claims>(
|
match decode::<Claims>(
|
||||||
&token,
|
&token,
|
||||||
|
|||||||
@@ -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())
|
.filter(|entry| is_image_or_video(&entry.path()) || entry.file_type().unwrap().is_dir())
|
||||||
.map(|entry| entry.path())
|
.map(|entry| entry.path())
|
||||||
.map(|path: PathBuf| {
|
.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()
|
relative.to_path_buf()
|
||||||
})
|
})
|
||||||
.collect::<Vec<PathBuf>>();
|
.collect::<Vec<PathBuf>>();
|
||||||
|
|||||||
38
src/main.rs
38
src/main.rs
@@ -2,11 +2,12 @@
|
|||||||
extern crate diesel;
|
extern crate diesel;
|
||||||
|
|
||||||
use actix_files::NamedFile;
|
use actix_files::NamedFile;
|
||||||
use actix_web::web::{HttpResponse, HttpRequest, Json};
|
use actix_web::web::{HttpRequest, HttpResponse, Json};
|
||||||
use actix_web::{get, post, App, HttpServer, Responder};
|
use actix_web::{get, post, web, App, HttpServer, Responder};
|
||||||
use chrono::{Duration, Utc};
|
use chrono::{Duration, Utc};
|
||||||
use data::{LoginRequest, ThumbnailRequest};
|
use data::{LoginRequest, ThumbnailRequest};
|
||||||
use jsonwebtoken::{encode, EncodingKey, Header};
|
use jsonwebtoken::{encode, EncodingKey, Header};
|
||||||
|
use serde::Serialize;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
|
||||||
use crate::data::{Claims, CreateAccountRequest, Token};
|
use crate::data::{Claims, CreateAccountRequest, Token};
|
||||||
@@ -34,6 +35,7 @@ async fn register(user: Json<CreateAccountRequest>) -> impl Responder {
|
|||||||
|
|
||||||
#[post("/login")]
|
#[post("/login")]
|
||||||
async fn login(creds: Json<LoginRequest>) -> impl Responder {
|
async fn login(creds: Json<LoginRequest>) -> impl Responder {
|
||||||
|
println!("Logging in: {}", creds.username);
|
||||||
if let Some(user) = get_user(&creds.username, &creds.password) {
|
if let Some(user) = get_user(&creds.username, &creds.password) {
|
||||||
let claims = Claims {
|
let claims = Claims {
|
||||||
sub: user.id.to_string(),
|
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 {
|
async fn list_photos(_claims: Claims, req: Json<ThumbnailRequest>) -> impl Responder {
|
||||||
println!("{}", req.path);
|
println!("{}", req.path);
|
||||||
|
|
||||||
let path = &req.path;
|
let path = &req.path;
|
||||||
if let Some(path) = is_valid_path(path) {
|
if let Some(path) = is_valid_path(path) {
|
||||||
let files = list_files(path);
|
let files = list_files(path).unwrap_or_default();
|
||||||
HttpResponse::Ok().json(files.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 {
|
} else {
|
||||||
HttpResponse::BadRequest().finish()
|
HttpResponse::BadRequest().finish()
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize)]
|
||||||
|
struct PhotosResponse<'a> {
|
||||||
|
photos: &'a [String],
|
||||||
|
dirs: &'a [String],
|
||||||
}
|
}
|
||||||
|
|
||||||
fn is_valid_path(path: &str) -> Option<PathBuf> {
|
fn is_valid_path(path: &str) -> Option<PathBuf> {
|
||||||
@@ -83,7 +103,11 @@ fn is_valid_path(path: &str) -> Option<PathBuf> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[get("/image")]
|
#[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 Some(path) = is_valid_path(&req.path) {
|
||||||
if let Ok(file) = NamedFile::open(path) {
|
if let Ok(file) = NamedFile::open(path) {
|
||||||
file.into_response(&request).unwrap()
|
file.into_response(&request).unwrap()
|
||||||
@@ -91,7 +115,7 @@ async fn image(_claims: Claims, request: HttpRequest, req: Json<ThumbnailRequest
|
|||||||
HttpResponse::NotFound().finish()
|
HttpResponse::NotFound().finish()
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
HttpResponse::NotFound().finish()
|
HttpResponse::BadRequest().finish()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user