mirror of
https://github.com/danbulant/Portfolio
synced 2026-06-18 22:11:14 +00:00
feat/refactor: rework guards, add upload data guards, code refactor
This commit is contained in:
parent
f6f8f45350
commit
e2844a3866
8 changed files with 100 additions and 9 deletions
43
api/src/guards/data/letter.rs
Normal file
43
api/src/guards/data/letter.rs
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
use rocket::data::{self, Data, FromData, ToByteUnit};
|
||||
use rocket::http::{ContentType, Status};
|
||||
use rocket::outcome::Outcome;
|
||||
use rocket::request::Request;
|
||||
|
||||
struct Letter(Vec<u8>);
|
||||
|
||||
impl Into<Vec<u8>> for Letter {
|
||||
fn into(self) -> Vec<u8> {
|
||||
self.0
|
||||
}
|
||||
}
|
||||
|
||||
#[rocket::async_trait]
|
||||
impl<'r> FromData<'r> for Letter {
|
||||
type Error = Option<String>;
|
||||
|
||||
async fn from_data(req: &'r Request<'_>, data: Data<'r>) -> data::Outcome<'r, Self> {
|
||||
let content_type_pdf = ContentType::new("application", "application/pdf");
|
||||
|
||||
if req.content_type() != Some(&content_type_pdf) {
|
||||
return Outcome::Failure((Status::BadRequest, None))
|
||||
}
|
||||
|
||||
let data = data.open(11.megabytes());
|
||||
|
||||
let data_bytes = data.into_bytes().await.unwrap();
|
||||
|
||||
if !data_bytes.is_complete() {
|
||||
// TODO: Over limit
|
||||
}
|
||||
|
||||
let data_bytes = data_bytes.into_inner();
|
||||
|
||||
let is_pdf = portfolio_core::filetype::filetype_is_pdf(&data_bytes);
|
||||
|
||||
if !is_pdf {
|
||||
// TODO: Not ZIP
|
||||
}
|
||||
|
||||
Outcome::Success(Letter(data_bytes))
|
||||
}
|
||||
}
|
||||
2
api/src/guards/data/mod.rs
Normal file
2
api/src/guards/data/mod.rs
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
pub mod portfolio;
|
||||
pub mod letter;
|
||||
43
api/src/guards/data/portfolio.rs
Normal file
43
api/src/guards/data/portfolio.rs
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
use rocket::data::{self, Data, FromData, ToByteUnit};
|
||||
use rocket::http::{ContentType, Status};
|
||||
use rocket::outcome::Outcome;
|
||||
use rocket::request::Request;
|
||||
|
||||
struct Portfolio(Vec<u8>);
|
||||
|
||||
impl Into<Vec<u8>> for Portfolio {
|
||||
fn into(self) -> Vec<u8> {
|
||||
self.0
|
||||
}
|
||||
}
|
||||
|
||||
#[rocket::async_trait]
|
||||
impl<'r> FromData<'r> for Portfolio {
|
||||
type Error = Option<String>;
|
||||
|
||||
async fn from_data(req: &'r Request<'_>, data: Data<'r>) -> data::Outcome<'r, Self> {
|
||||
let content_type_pdf = ContentType::new("application", "application/zip");
|
||||
|
||||
if req.content_type() != Some(&content_type_pdf) {
|
||||
return Outcome::Failure((Status::BadRequest, None))
|
||||
}
|
||||
|
||||
let data = data.open(101.megabytes());
|
||||
|
||||
let data_bytes = data.into_bytes().await.unwrap();
|
||||
|
||||
if !data_bytes.is_complete() {
|
||||
// TODO: Over limit
|
||||
}
|
||||
|
||||
let data_bytes = data_bytes.into_inner();
|
||||
|
||||
let is_pdf = portfolio_core::filetype::filetype_is_zip(&data_bytes);
|
||||
|
||||
if !is_pdf {
|
||||
// TODO: Not ZIP
|
||||
}
|
||||
|
||||
Outcome::Success(Portfolio(data_bytes))
|
||||
}
|
||||
}
|
||||
2
api/src/guards/mod.rs
Normal file
2
api/src/guards/mod.rs
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
pub mod data;
|
||||
pub mod request;
|
||||
|
|
@ -6,22 +6,22 @@ use rocket::request::{FromRequest, Request};
|
|||
|
||||
pub struct UUIDCookie(Uuid);
|
||||
|
||||
impl UUIDCookie {
|
||||
pub fn value(self) -> Uuid {
|
||||
impl Into<Uuid> for UUIDCookie {
|
||||
fn into(self) -> Uuid {
|
||||
self.0
|
||||
}
|
||||
}
|
||||
|
||||
#[rocket::async_trait]
|
||||
impl<'r> FromRequest<'r> for UUIDCookie {
|
||||
type Error = Status;
|
||||
async fn from_request(req: &'r Request<'_>) -> Outcome<UUIDCookie, (Status, Status), ()> {
|
||||
type Error = Option<String>;
|
||||
async fn from_request(req: &'r Request<'_>) -> Outcome<UUIDCookie, (Status, Self::Error), ()> {
|
||||
let session_id = req.cookies().get("id").unwrap().name_value().1;
|
||||
println!("session_id: {}", session_id);
|
||||
|
||||
match Uuid::parse_str(&session_id) {
|
||||
Ok(uuid) => Outcome::Success(UUIDCookie(uuid)),
|
||||
Err(_) => return Outcome::Failure((Status::BadRequest, Status::BadRequest)),
|
||||
Err(_) => return Outcome::Failure((Status::BadRequest, None)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -18,8 +18,9 @@ use sea_orm_rocket::{Connection, Database};
|
|||
|
||||
|
||||
mod pool;
|
||||
mod guard;
|
||||
mod guards;
|
||||
mod requests;
|
||||
mod routes;
|
||||
|
||||
use pool::Db;
|
||||
|
||||
|
|
@ -28,7 +29,7 @@ pub use entity::candidate::Entity as Candidate;
|
|||
|
||||
use portfolio_core::crypto::random_8_char_string;
|
||||
|
||||
use crate::guard::candidate_refresh_token::UUIDCookie;
|
||||
use crate::guards::request::candidate_refresh_token::UUIDCookie;
|
||||
|
||||
fn custom_err_from_service_err(service_err: ServiceError) -> Custom<String> {
|
||||
Custom(Status::from_code(service_err.0.code).unwrap_or_default(), service_err.1.to_string())
|
||||
|
|
@ -49,9 +50,9 @@ async fn create(conn: Connection<'_, Db>, post_form: Json<RegisterRequest>) -> R
|
|||
}
|
||||
|
||||
#[get("/whoami")]
|
||||
async fn validate(conn: Connection<'_, Db>, uuid_cookie: Result<UUIDCookie, Status>) -> Result<String, Custom<String>> {
|
||||
async fn validate(conn: Connection<'_, Db>, uuid_cookie: Result<UUIDCookie, Option<String>>) -> Result<String, Custom<String>> {
|
||||
let db = conn.into_inner();
|
||||
let user = CandidateService::auth_user_session(db, uuid_cookie.ok().unwrap().value()).await;
|
||||
let user = CandidateService::auth_user_session(db, uuid_cookie.ok().unwrap().into()).await;
|
||||
|
||||
|
||||
match user {
|
||||
|
|
|
|||
0
api/src/routes/mod.rs
Normal file
0
api/src/routes/mod.rs
Normal file
Loading…
Reference in a new issue