Add Favorite GET, and POST endpoints

This commit is contained in:
Cameron Cordes
2020-08-07 22:56:29 -04:00
parent 74043c5c6a
commit c774edd7dd
7 changed files with 86 additions and 5 deletions

View File

@@ -6,14 +6,14 @@ use actix_files::NamedFile;
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 data::{AddFavoriteRequest, LoginRequest, ThumbnailRequest};
use jsonwebtoken::{encode, EncodingKey, Header};
use rayon::prelude::*;
use serde::Serialize;
use std::path::{Path, PathBuf};
use crate::data::{secret_key, Claims, CreateAccountRequest, Token};
use crate::database::{create_user, get_user, user_exists};
use crate::database::{add_favorite, create_user, get_favorites, get_user, user_exists};
use crate::files::{is_valid_path, list_files};
use crate::video::*;
@@ -170,6 +170,28 @@ async fn get_video_part(
}
}
#[get("image/favorites")]
async fn favorites(claims: Claims) -> impl Responder {
let favorites = get_favorites(claims.sub.parse::<i32>().unwrap())
.into_iter()
.map(|favorite| favorite.path)
.collect::<Vec<String>>();
HttpResponse::Ok().json(PhotosResponse {
photos: &favorites,
dirs: &Vec::new(),
})
}
#[post("image/favorites")]
async fn post_add_favorite(claims: Claims, body: web::Json<AddFavoriteRequest>) -> impl Responder {
if let Ok(user_id) = claims.sub.parse::<i32>() {
add_favorite(user_id, body.path.clone());
HttpResponse::Ok()
} else {
HttpResponse::BadRequest()
}
}
async fn create_thumbnails() {
let thumbs = &dotenv::var("THUMBNAILS").expect("THUMBNAILS not defined");
let thumbnail_directory: &Path = Path::new(thumbs);
@@ -206,7 +228,7 @@ async fn create_thumbnails() {
let path = entry.path();
let relative_path = &path.strip_prefix(&images).unwrap();
let thumb_path = Path::new(thumbnail_directory).join(relative_path);
return !thumb_path.exists();
!thumb_path.exists()
})
.map(|entry| (image::open(entry.path()), entry.path().to_path_buf()))
.filter(|(img, _)| img.is_ok())
@@ -238,6 +260,8 @@ async fn main() -> std::io::Result<()> {
.service(generate_video)
.service(stream_video)
.service(get_video_part)
.service(favorites)
.service(post_add_favorite)
})
.bind(dotenv::var("BIND_URL").unwrap())?
.bind("localhost:8088")?