Fixed file upload
This commit is contained in:
@@ -6,6 +6,6 @@ CREATE TABLE IF NOT EXISTS users (
|
|||||||
last_name TEXT NOT NULL,
|
last_name TEXT NOT NULL,
|
||||||
created_at TIMESTAMP NOT NULL DEFAULT NOW(),
|
created_at TIMESTAMP NOT NULL DEFAULT NOW(),
|
||||||
updated_at TIMESTAMP NOT NULL DEFAULT NOW(),
|
updated_at TIMESTAMP NOT NULL DEFAULT NOW(),
|
||||||
profile TEXT,
|
profile_picture TEXT,
|
||||||
verified BOOLEAN NOT NULL DEFAULT FALSE
|
verified BOOLEAN NOT NULL DEFAULT FALSE
|
||||||
);
|
);
|
||||||
@@ -29,7 +29,7 @@ impl RegisterUser {
|
|||||||
last_name: self.last_name,
|
last_name: self.last_name,
|
||||||
updated_at: chrono::Utc::now().naive_utc(),
|
updated_at: chrono::Utc::now().naive_utc(),
|
||||||
created_at: chrono::Utc::now().naive_utc(),
|
created_at: chrono::Utc::now().naive_utc(),
|
||||||
profile: None,
|
profile_picture: None,
|
||||||
verified: false,
|
verified: false,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -51,7 +51,7 @@ pub struct QueryUser {
|
|||||||
pub last_name: String,
|
pub last_name: String,
|
||||||
pub updated_at: chrono::NaiveDateTime,
|
pub updated_at: chrono::NaiveDateTime,
|
||||||
pub created_at: chrono::NaiveDateTime,
|
pub created_at: chrono::NaiveDateTime,
|
||||||
pub profile: Option<String>,
|
pub profile_picture: Option<String>,
|
||||||
pub verified: bool,
|
pub verified: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -77,7 +77,7 @@ pub struct InsertUser {
|
|||||||
pub last_name: String,
|
pub last_name: String,
|
||||||
pub updated_at: chrono::NaiveDateTime,
|
pub updated_at: chrono::NaiveDateTime,
|
||||||
pub created_at: chrono::NaiveDateTime,
|
pub created_at: chrono::NaiveDateTime,
|
||||||
pub profile: Option<String>,
|
pub profile_picture: Option<String>,
|
||||||
pub verified: bool,
|
pub verified: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -90,11 +90,11 @@ impl InsertUser {
|
|||||||
Ok(user)
|
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 mut conn = connection()?;
|
||||||
let user = diesel::update(users::table)
|
let user = diesel::update(users::table)
|
||||||
.filter(users::email.eq(&email))
|
.filter(users::email.eq(&email))
|
||||||
.set(users::profile.eq(profile))
|
.set(users::profile_picture.eq(profile_picture))
|
||||||
.get_result(&mut conn)?;
|
.get_result(&mut conn)?;
|
||||||
Ok(user)
|
Ok(user)
|
||||||
}
|
}
|
||||||
@@ -106,6 +106,7 @@ pub struct ResponseUser {
|
|||||||
pub role: String,
|
pub role: String,
|
||||||
pub first_name: String,
|
pub first_name: String,
|
||||||
pub last_name: String,
|
pub last_name: String,
|
||||||
|
pub profile_picture: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<QueryUser> for ResponseUser {
|
impl From<QueryUser> for ResponseUser {
|
||||||
@@ -115,6 +116,7 @@ impl From<QueryUser> for ResponseUser {
|
|||||||
role: user.role,
|
role: user.role,
|
||||||
first_name: user.first_name,
|
first_name: user.first_name,
|
||||||
last_name: user.last_name,
|
last_name: user.last_name,
|
||||||
|
profile_picture: user.profile_picture,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -48,7 +48,7 @@ diesel::table! {
|
|||||||
last_name -> Text,
|
last_name -> Text,
|
||||||
updated_at -> Timestamp,
|
updated_at -> Timestamp,
|
||||||
created_at -> Timestamp,
|
created_at -> Timestamp,
|
||||||
profile -> Nullable<Text>,
|
profile_picture -> Nullable<Text>,
|
||||||
verified -> Bool,
|
verified -> Bool,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -81,7 +81,7 @@ async fn get_picture(auth: JwtAuth) -> HttpResponse {
|
|||||||
return ResponseError::error_response(&err);
|
return ResponseError::error_response(&err);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
if let Some(path) = user.profile {
|
if let Some(path) = user.profile_picture {
|
||||||
match get_file(&path).await {
|
match get_file(&path).await {
|
||||||
Ok(bytes) => return HttpResponse::Ok().body(bytes),
|
Ok(bytes) => return HttpResponse::Ok().body(bytes),
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
@@ -98,7 +98,7 @@ async fn get_picture(auth: JwtAuth) -> HttpResponse {
|
|||||||
async fn delete_picture(auth: JwtAuth) -> HttpResponse {
|
async fn delete_picture(auth: JwtAuth) -> HttpResponse {
|
||||||
match QueryUser::get_by_email(&auth.user.email) {
|
match QueryUser::get_by_email(&auth.user.email) {
|
||||||
Ok(user) => {
|
Ok(user) => {
|
||||||
match user.profile {
|
match user.profile_picture {
|
||||||
Some(path) => {
|
Some(path) => {
|
||||||
match delete_file(&path).await {
|
match delete_file(&path).await {
|
||||||
Ok(_) => {
|
Ok(_) => {
|
||||||
|
|||||||
@@ -15,4 +15,5 @@ export interface User {
|
|||||||
role: string;
|
role: string;
|
||||||
first_name: string;
|
first_name: string;
|
||||||
last_name: string;
|
last_name: string;
|
||||||
|
profile_picture?: string;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,19 +21,23 @@ interface PostOptions {
|
|||||||
|
|
||||||
export async function post(endpoint: string, body: any, options?: PostOptions): Promise<Response> {
|
export async function post(endpoint: string, body: any, options?: PostOptions): Promise<Response> {
|
||||||
const url = `${baseURL}/${endpoint}`;
|
const url = `${baseURL}/${endpoint}`;
|
||||||
const headers = options?.headers || {};
|
let response;
|
||||||
if (!options?.type || options.type === 'json') {
|
if (!options?.type || options.type === 'json') {
|
||||||
body = JSON.stringify(body);
|
response = await fetch(url, {
|
||||||
headers['Content-Type'] = 'application/json';
|
method: 'POST',
|
||||||
} else if (options.type === 'form') {
|
headers: {
|
||||||
headers['Content-Type'] = 'multipart/form-data';
|
'Content-Type': 'application/json'
|
||||||
}
|
},
|
||||||
const response = await fetch(url, {
|
credentials: 'include',
|
||||||
|
body: JSON.stringify(body)
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
response = await fetch(url, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: headers,
|
|
||||||
credentials: 'include',
|
credentials: 'include',
|
||||||
body
|
body
|
||||||
});
|
});
|
||||||
|
}
|
||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -28,12 +28,14 @@ export default function Header() {
|
|||||||
if (response) {
|
if (response) {
|
||||||
setRefreshId(refreshLoggedIn());
|
setRefreshId(refreshLoggedIn());
|
||||||
setUser(response.user);
|
setUser(response.user);
|
||||||
|
if (response.user.profile_picture) {
|
||||||
getPicture().then((response) => {
|
getPicture().then((response) => {
|
||||||
if (response) {
|
if (response) {
|
||||||
setProfilePicture(response as File);
|
setProfilePicture(response as File);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}, [user]);
|
}, [user]);
|
||||||
@@ -172,11 +174,13 @@ export default function Header() {
|
|||||||
toggle={toggle}
|
toggle={toggle}
|
||||||
setUser={(u) => {
|
setUser={(u) => {
|
||||||
setUser(u);
|
setUser(u);
|
||||||
|
if (u.profile_picture) {
|
||||||
getPicture().then((response) => {
|
getPicture().then((response) => {
|
||||||
if (response) {
|
if (response) {
|
||||||
setProfilePicture(response as File);
|
setProfilePicture(response as File);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
}
|
||||||
}}
|
}}
|
||||||
setRefreshId={setRefreshId}
|
setRefreshId={setRefreshId}
|
||||||
/>
|
/>
|
||||||
|
|||||||
Reference in New Issue
Block a user