2020-12-24 16:24:04 +01:00
|
|
|
use std::io::Write;
|
|
|
|
|
2021-04-16 15:20:25 +02:00
|
|
|
use actix::Addr;
|
|
|
|
use actix_multipart::{Field, Multipart};
|
|
|
|
use actix_web::http::header::ContentDisposition;
|
|
|
|
use actix_web::web::Data;
|
|
|
|
use actix_web::{post, web, Error, HttpResponse};
|
2021-08-13 12:42:29 +02:00
|
|
|
use common::*;
|
2021-04-16 15:20:25 +02:00
|
|
|
use database_actor::authorize_user::AuthorizeUser;
|
|
|
|
use database_actor::user_projects::CurrentUserProject;
|
|
|
|
use database_actor::users::UpdateAvatarUrl;
|
|
|
|
use database_actor::DbExecutor;
|
2020-12-24 16:24:04 +01:00
|
|
|
#[cfg(feature = "local-storage")]
|
2021-04-16 15:20:25 +02:00
|
|
|
use futures::executor::block_on;
|
|
|
|
use futures::{StreamExt, TryStreamExt};
|
2021-10-08 13:56:28 +02:00
|
|
|
use jirs_data::msg::{WsMsg, WsMsgUser};
|
2021-08-13 23:03:52 +02:00
|
|
|
use jirs_data::{User, UserId};
|
2021-04-16 15:20:25 +02:00
|
|
|
use websocket_actor::server::InnerMsg::BroadcastToChannel;
|
|
|
|
use websocket_actor::server::WsServer;
|
2020-12-24 16:24:04 +01:00
|
|
|
|
2021-08-12 16:10:30 +02:00
|
|
|
#[cfg(feature = "aws-s3")]
|
2020-12-24 16:24:04 +01:00
|
|
|
#[post("/")]
|
|
|
|
pub async fn upload(
|
|
|
|
mut payload: Multipart,
|
|
|
|
db: Data<Addr<DbExecutor>>,
|
|
|
|
ws: Data<Addr<WsServer>>,
|
|
|
|
fs: Data<Addr<filesystem_actor::FileSystemExecutor>>,
|
2021-01-06 18:47:54 +01:00
|
|
|
amazon: Data<Addr<amazon_actor::AmazonExecutor>>,
|
2020-12-24 16:24:04 +01:00
|
|
|
) -> Result<HttpResponse, Error> {
|
|
|
|
let mut user_id: Option<UserId> = None;
|
|
|
|
let mut avatar_url: Option<String> = None;
|
|
|
|
|
|
|
|
while let Ok(Some(field)) = payload.try_next().await {
|
|
|
|
let disposition: ContentDisposition = match field.content_disposition() {
|
|
|
|
Some(d) => d,
|
|
|
|
_ => continue,
|
|
|
|
};
|
|
|
|
if !disposition.is_form_data() {
|
|
|
|
return Ok(HttpResponse::BadRequest().finish());
|
|
|
|
}
|
|
|
|
match disposition.get_name() {
|
|
|
|
Some("token") => {
|
|
|
|
user_id = Some(handle_token(field, db.clone()).await?);
|
|
|
|
}
|
|
|
|
Some("avatar") => {
|
|
|
|
let id = user_id.ok_or_else(|| HttpResponse::Unauthorized().finish())?;
|
|
|
|
avatar_url = Some(
|
|
|
|
crate::handlers::upload_avatar_image::handle_image(
|
|
|
|
id,
|
|
|
|
field,
|
|
|
|
disposition,
|
|
|
|
fs.clone(),
|
2021-01-06 18:47:54 +01:00
|
|
|
amazon.clone(),
|
2020-12-24 16:24:04 +01:00
|
|
|
)
|
2020-12-24 16:24:47 +01:00
|
|
|
.await?,
|
2020-12-24 16:24:04 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
_ => continue,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
let user_id = match user_id {
|
|
|
|
Some(id) => id,
|
|
|
|
_ => return Ok(HttpResponse::Unauthorized().finish()),
|
|
|
|
};
|
|
|
|
|
|
|
|
let project_id = match block_on(db.send(CurrentUserProject { user_id })) {
|
|
|
|
Ok(Ok(user_project)) => user_project.project_id,
|
|
|
|
_ => return Ok(HttpResponse::UnprocessableEntity().finish()),
|
|
|
|
};
|
|
|
|
|
|
|
|
match (user_id, avatar_url) {
|
|
|
|
(user_id, Some(avatar_url)) => {
|
|
|
|
let user = update_user_avatar(user_id, avatar_url.clone(), db).await?;
|
|
|
|
ws.send(BroadcastToChannel(
|
|
|
|
project_id,
|
2021-08-13 23:03:52 +02:00
|
|
|
WsMsgUser::AvatarUrlChanged(user.id, avatar_url).into(),
|
2020-12-24 16:24:04 +01:00
|
|
|
))
|
|
|
|
.await
|
|
|
|
.map_err(|_| HttpResponse::UnprocessableEntity().finish())?;
|
|
|
|
Ok(HttpResponse::NoContent().finish())
|
|
|
|
}
|
|
|
|
_ => Ok(HttpResponse::UnprocessableEntity().finish()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-12 16:10:30 +02:00
|
|
|
#[cfg(not(feature = "aws-s3"))]
|
|
|
|
#[post("/")]
|
|
|
|
pub async fn upload(
|
|
|
|
mut payload: Multipart,
|
|
|
|
db: Data<Addr<DbExecutor>>,
|
|
|
|
ws: Data<Addr<WsServer>>,
|
|
|
|
fs: Data<Addr<filesystem_actor::FileSystemExecutor>>,
|
|
|
|
) -> Result<HttpResponse, Error> {
|
|
|
|
let mut user_id: Option<UserId> = None;
|
|
|
|
let mut avatar_url: Option<String> = None;
|
|
|
|
|
|
|
|
while let Ok(Some(field)) = payload.try_next().await {
|
|
|
|
let disposition: ContentDisposition = match field.content_disposition() {
|
|
|
|
Some(d) => d,
|
|
|
|
_ => continue,
|
|
|
|
};
|
|
|
|
if !disposition.is_form_data() {
|
|
|
|
return Ok(HttpResponse::BadRequest().finish());
|
|
|
|
}
|
|
|
|
match disposition.get_name() {
|
|
|
|
Some("token") => {
|
|
|
|
user_id = Some(handle_token(field, db.clone()).await?);
|
|
|
|
}
|
|
|
|
Some("avatar") => {
|
|
|
|
let id = user_id.ok_or_else(|| HttpResponse::Unauthorized().finish())?;
|
|
|
|
avatar_url = Some(
|
|
|
|
crate::handlers::upload_avatar_image::handle_image(
|
|
|
|
id,
|
|
|
|
field,
|
|
|
|
disposition,
|
|
|
|
fs.clone(),
|
|
|
|
)
|
|
|
|
.await?,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
_ => continue,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
let user_id = match user_id {
|
|
|
|
Some(id) => id,
|
|
|
|
_ => return Ok(HttpResponse::Unauthorized().finish()),
|
|
|
|
};
|
|
|
|
|
|
|
|
let project_id = match block_on(db.send(CurrentUserProject { user_id })) {
|
|
|
|
Ok(Ok(user_project)) => user_project.project_id,
|
|
|
|
_ => return Ok(HttpResponse::UnprocessableEntity().finish()),
|
|
|
|
};
|
|
|
|
|
|
|
|
match (user_id, avatar_url) {
|
|
|
|
(user_id, Some(avatar_url)) => {
|
|
|
|
let user = update_user_avatar(user_id, avatar_url.clone(), db).await?;
|
|
|
|
ws.send(BroadcastToChannel(
|
|
|
|
project_id,
|
2021-10-08 13:56:28 +02:00
|
|
|
WsMsg::User(WsMsgUser::AvatarUrlChanged(user.id, avatar_url)),
|
2021-08-12 16:10:30 +02:00
|
|
|
))
|
|
|
|
.await
|
|
|
|
.map_err(|_| HttpResponse::UnprocessableEntity().finish())?;
|
|
|
|
Ok(HttpResponse::NoContent().finish())
|
|
|
|
}
|
|
|
|
_ => Ok(HttpResponse::UnprocessableEntity().finish()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-24 16:24:04 +01:00
|
|
|
async fn update_user_avatar(
|
|
|
|
user_id: UserId,
|
|
|
|
new_url: String,
|
|
|
|
db: Data<Addr<DbExecutor>>,
|
2021-08-12 16:10:30 +02:00
|
|
|
) -> Result<User, actix_web::Error> {
|
2020-12-24 16:24:04 +01:00
|
|
|
match db
|
|
|
|
.send(UpdateAvatarUrl {
|
|
|
|
user_id,
|
|
|
|
avatar_url: Some(new_url),
|
|
|
|
})
|
|
|
|
.await
|
|
|
|
{
|
|
|
|
Ok(Ok(user)) => Ok(user),
|
|
|
|
|
|
|
|
Ok(Err(e)) => {
|
2021-08-13 12:56:11 +02:00
|
|
|
common::log::error!("{:?}", e);
|
2021-08-12 16:10:30 +02:00
|
|
|
Err(actix_web::Error::from(
|
|
|
|
HttpResponse::Unauthorized().finish(),
|
|
|
|
))
|
2020-12-24 16:24:04 +01:00
|
|
|
}
|
|
|
|
Err(e) => {
|
2021-08-13 12:56:11 +02:00
|
|
|
common::log::error!("{:?}", e);
|
2021-08-12 16:10:30 +02:00
|
|
|
Err(actix_web::Error::from(
|
|
|
|
HttpResponse::Unauthorized().finish(),
|
|
|
|
))
|
2020-12-24 16:24:04 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-12 16:10:30 +02:00
|
|
|
async fn handle_token(
|
|
|
|
mut field: Field,
|
|
|
|
db: Data<Addr<DbExecutor>>,
|
|
|
|
) -> Result<UserId, actix_web::Error> {
|
2020-12-24 16:24:04 +01:00
|
|
|
let mut f: Vec<u8> = vec![];
|
|
|
|
while let Some(chunk) = field.next().await {
|
|
|
|
let data = chunk.unwrap();
|
|
|
|
f = web::block(move || f.write_all(&data).map(|_| f)).await?;
|
|
|
|
}
|
|
|
|
let access_token = String::from_utf8(f)
|
|
|
|
.unwrap_or_default()
|
|
|
|
.parse::<uuid::Uuid>()
|
|
|
|
.map_err(|_| HttpResponse::Unauthorized().finish())?;
|
|
|
|
match db.send(AuthorizeUser { access_token }).await {
|
|
|
|
Ok(Ok(user)) => Ok(user.id),
|
|
|
|
|
|
|
|
Ok(Err(e)) => {
|
2021-08-13 12:56:11 +02:00
|
|
|
common::log::error!("{:?}", e);
|
2020-12-24 16:24:04 +01:00
|
|
|
Err(HttpResponse::Unauthorized().finish().into())
|
|
|
|
}
|
|
|
|
Err(e) => {
|
2021-08-13 12:56:11 +02:00
|
|
|
common::log::error!("{:?}", e);
|
2020-12-24 16:24:04 +01:00
|
|
|
Err(HttpResponse::Unauthorized().finish().into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|