From ece4154b4e25a6d5e9d7f810a1b7b5853f2d27b5 Mon Sep 17 00:00:00 2001 From: Benjamin Sherriff Date: Tue, 24 Oct 2023 08:42:21 -0400 Subject: [PATCH] Fixed file upload --- service/migrations/000011_create_users/up.sql | 2 +- service/src/auth/model.rs | 12 +++++---- service/src/storage/schema.rs | 2 +- service/src/users/routes.rs | 4 +-- ui/src/api/auth.types.ts | 1 + ui/src/api/index.ts | 26 +++++++++++-------- ui/src/components/Header/index.tsx | 24 ++++++++++------- 7 files changed, 41 insertions(+), 30 deletions(-) diff --git a/service/migrations/000011_create_users/up.sql b/service/migrations/000011_create_users/up.sql index 4911cc2..97e07e6 100644 --- a/service/migrations/000011_create_users/up.sql +++ b/service/migrations/000011_create_users/up.sql @@ -6,6 +6,6 @@ CREATE TABLE IF NOT EXISTS users ( last_name TEXT NOT NULL, created_at TIMESTAMP NOT NULL DEFAULT NOW(), updated_at TIMESTAMP NOT NULL DEFAULT NOW(), - profile TEXT, + profile_picture TEXT, verified BOOLEAN NOT NULL DEFAULT FALSE ); \ No newline at end of file diff --git a/service/src/auth/model.rs b/service/src/auth/model.rs index 6aac1de..af557ef 100644 --- a/service/src/auth/model.rs +++ b/service/src/auth/model.rs @@ -29,7 +29,7 @@ impl RegisterUser { last_name: self.last_name, updated_at: chrono::Utc::now().naive_utc(), created_at: chrono::Utc::now().naive_utc(), - profile: None, + profile_picture: None, verified: false, }) } @@ -51,7 +51,7 @@ pub struct QueryUser { pub last_name: String, pub updated_at: chrono::NaiveDateTime, pub created_at: chrono::NaiveDateTime, - pub profile: Option, + pub profile_picture: Option, pub verified: bool, } @@ -77,7 +77,7 @@ pub struct InsertUser { pub last_name: String, pub updated_at: chrono::NaiveDateTime, pub created_at: chrono::NaiveDateTime, - pub profile: Option, + pub profile_picture: Option, pub verified: bool, } @@ -90,11 +90,11 @@ impl InsertUser { Ok(user) } - pub fn update_profile(email: &str, profile: Option<&str>) -> Result { + pub fn update_profile(email: &str, profile_picture: Option<&str>) -> Result { let mut conn = connection()?; let user = diesel::update(users::table) .filter(users::email.eq(&email)) - .set(users::profile.eq(profile)) + .set(users::profile_picture.eq(profile_picture)) .get_result(&mut conn)?; Ok(user) } @@ -106,6 +106,7 @@ pub struct ResponseUser { pub role: String, pub first_name: String, pub last_name: String, + pub profile_picture: Option, } impl From for ResponseUser { @@ -115,6 +116,7 @@ impl From for ResponseUser { role: user.role, first_name: user.first_name, last_name: user.last_name, + profile_picture: user.profile_picture, } } } diff --git a/service/src/storage/schema.rs b/service/src/storage/schema.rs index 24fb11f..1a64dad 100644 --- a/service/src/storage/schema.rs +++ b/service/src/storage/schema.rs @@ -48,7 +48,7 @@ diesel::table! { last_name -> Text, updated_at -> Timestamp, created_at -> Timestamp, - profile -> Nullable, + profile_picture -> Nullable, verified -> Bool, } } \ No newline at end of file diff --git a/service/src/users/routes.rs b/service/src/users/routes.rs index d9c1990..0e4babd 100644 --- a/service/src/users/routes.rs +++ b/service/src/users/routes.rs @@ -81,7 +81,7 @@ async fn get_picture(auth: JwtAuth) -> HttpResponse { return ResponseError::error_response(&err); } }; - if let Some(path) = user.profile { + if let Some(path) = user.profile_picture { match get_file(&path).await { Ok(bytes) => return HttpResponse::Ok().body(bytes), Err(err) => { @@ -98,7 +98,7 @@ async fn get_picture(auth: JwtAuth) -> HttpResponse { async fn delete_picture(auth: JwtAuth) -> HttpResponse { match QueryUser::get_by_email(&auth.user.email) { Ok(user) => { - match user.profile { + match user.profile_picture { Some(path) => { match delete_file(&path).await { Ok(_) => { diff --git a/ui/src/api/auth.types.ts b/ui/src/api/auth.types.ts index 8228ac1..76ac70a 100644 --- a/ui/src/api/auth.types.ts +++ b/ui/src/api/auth.types.ts @@ -15,4 +15,5 @@ export interface User { role: string; first_name: string; last_name: string; + profile_picture?: string; } diff --git a/ui/src/api/index.ts b/ui/src/api/index.ts index b5f4076..16f2132 100644 --- a/ui/src/api/index.ts +++ b/ui/src/api/index.ts @@ -21,19 +21,23 @@ interface PostOptions { export async function post(endpoint: string, body: any, options?: PostOptions): Promise { const url = `${baseURL}/${endpoint}`; - const headers = options?.headers || {}; + let response; if (!options?.type || options.type === 'json') { - body = JSON.stringify(body); - headers['Content-Type'] = 'application/json'; - } else if (options.type === 'form') { - headers['Content-Type'] = 'multipart/form-data'; + response = await fetch(url, { + method: 'POST', + headers: { + 'Content-Type': 'application/json' + }, + credentials: 'include', + body: JSON.stringify(body) + }); + } else { + response = await fetch(url, { + method: 'POST', + credentials: 'include', + body + }); } - const response = await fetch(url, { - method: 'POST', - headers: headers, - credentials: 'include', - body - }); return response; } diff --git a/ui/src/components/Header/index.tsx b/ui/src/components/Header/index.tsx index ce51b26..815a319 100644 --- a/ui/src/components/Header/index.tsx +++ b/ui/src/components/Header/index.tsx @@ -28,11 +28,13 @@ export default function Header() { if (response) { setRefreshId(refreshLoggedIn()); setUser(response.user); - getPicture().then((response) => { - if (response) { - setProfilePicture(response as File); - } - }); + if (response.user.profile_picture) { + getPicture().then((response) => { + if (response) { + setProfilePicture(response as File); + } + }); + } } }); } @@ -172,11 +174,13 @@ export default function Header() { toggle={toggle} setUser={(u) => { setUser(u); - getPicture().then((response) => { - if (response) { - setProfilePicture(response as File); - } - }); + if (u.profile_picture) { + getPicture().then((response) => { + if (response) { + setProfilePicture(response as File); + } + }); + } }} setRefreshId={setRefreshId} />