Fixed file upload

This commit is contained in:
Benjamin Sherriff
2023-10-24 08:42:21 -04:00
parent f3eff8e310
commit ece4154b4e
7 changed files with 41 additions and 30 deletions

View File

@@ -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
);

View File

@@ -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<String>,
pub profile_picture: Option<String>,
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<String>,
pub profile_picture: Option<String>,
pub verified: bool,
}
@@ -90,11 +90,11 @@ impl InsertUser {
Ok(user)
}
pub fn update_profile(email: &str, profile: Option<&str>) -> Result<QueryUser, ServiceError> {
pub fn update_profile(email: &str, profile_picture: Option<&str>) -> Result<QueryUser, ServiceError> {
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<String>,
}
impl From<QueryUser> for ResponseUser {
@@ -115,6 +116,7 @@ impl From<QueryUser> for ResponseUser {
role: user.role,
first_name: user.first_name,
last_name: user.last_name,
profile_picture: user.profile_picture,
}
}
}

View File

@@ -48,7 +48,7 @@ diesel::table! {
last_name -> Text,
updated_at -> Timestamp,
created_at -> Timestamp,
profile -> Nullable<Text>,
profile_picture -> Nullable<Text>,
verified -> Bool,
}
}

View File

@@ -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(_) => {

View File

@@ -15,4 +15,5 @@ export interface User {
role: string;
first_name: string;
last_name: string;
profile_picture?: string;
}

View File

@@ -21,19 +21,23 @@ interface PostOptions {
export async function post(endpoint: string, body: any, options?: PostOptions): Promise<Response> {
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';
}
const response = await fetch(url, {
response = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
credentials: 'include',
body: JSON.stringify(body)
});
} else {
response = await fetch(url, {
method: 'POST',
headers: headers,
credentials: 'include',
body
});
}
return response;
}

View File

@@ -28,12 +28,14 @@ export default function Header() {
if (response) {
setRefreshId(refreshLoggedIn());
setUser(response.user);
if (response.user.profile_picture) {
getPicture().then((response) => {
if (response) {
setProfilePicture(response as File);
}
});
}
}
});
}
}, [user]);
@@ -172,11 +174,13 @@ export default function Header() {
toggle={toggle}
setUser={(u) => {
setUser(u);
if (u.profile_picture) {
getPicture().then((response) => {
if (response) {
setProfilePicture(response as File);
}
});
}
}}
setRefreshId={setRefreshId}
/>