Fix upload avatar

This commit is contained in:
Adrian Woźniak 2021-10-08 13:56:28 +02:00
parent 09e941ff8a
commit 25d5991ffb
No known key found for this signature in database
GPG Key ID: DE43476F72AD3F6C
13 changed files with 52 additions and 19 deletions

1
Cargo.lock generated
View File

@ -1901,6 +1901,7 @@ dependencies = [
"lettre", "lettre",
"lettre_email", "lettre_email",
"libc", "libc",
"log",
"openssl-sys", "openssl-sys",
"serde", "serde",
"toml", "toml",

View File

@ -51,6 +51,9 @@ impl actix::Handler<CreateFile> for FileSystemExecutor {
fn handle(&mut self, msg: CreateFile, _ctx: &mut Self::Context) -> Self::Result { fn handle(&mut self, msg: CreateFile, _ctx: &mut Self::Context) -> Self::Result {
let Configuration { store_path, .. } = &self.config; let Configuration { store_path, .. } = &self.config;
if std::fs::metadata(&store_path).is_err() {
let _ = std::fs::create_dir_all(&store_path);
}
let CreateFile { let CreateFile {
mut source, mut source,
file_name, file_name,

View File

@ -30,6 +30,8 @@ libc = { version = "0.2.0", default-features = false }
lettre = { version = "*" } lettre = { version = "*" }
lettre_email = { version = "*" } lettre_email = { version = "*" }
log = { version = "*" }
[dependencies.jirs-config] [dependencies.jirs-config]
path = "../../shared/jirs-config" path = "../../shared/jirs-config"
features = ["mail", "web"] features = ["mail", "web"]

View File

@ -41,6 +41,9 @@ impl Handler<Welcome> for MailExecutor {
"#, "#,
bind_token = msg.bind_token, bind_token = msg.bind_token,
); );
if cfg!(debug_assetrions) {
log::info!("Sending email:\n{}", html);
}
let email = lettre_email::Email::builder() let email = lettre_email::Email::builder()
.from(from) .from(from)

View File

@ -15,7 +15,7 @@ path = "./src/lib.rs"
[features] [features]
local-storage = ["filesystem-actor"] local-storage = ["filesystem-actor"]
aws-s3 = ["amazon-actor"] aws-s3 = ["amazon-actor"]
default = ["local-storage", "aws-s3"] default = ["local-storage"]
[dependencies] [dependencies]
common = { path = "../../shared/common" } common = { path = "../../shared/common" }

View File

@ -13,7 +13,7 @@ use database_actor::DbExecutor;
#[cfg(feature = "local-storage")] #[cfg(feature = "local-storage")]
use futures::executor::block_on; use futures::executor::block_on;
use futures::{StreamExt, TryStreamExt}; use futures::{StreamExt, TryStreamExt};
use jirs_data::msg::WsMsgUser; use jirs_data::msg::{WsMsg, WsMsgUser};
use jirs_data::{User, UserId}; use jirs_data::{User, UserId};
use websocket_actor::server::InnerMsg::BroadcastToChannel; use websocket_actor::server::InnerMsg::BroadcastToChannel;
use websocket_actor::server::WsServer; use websocket_actor::server::WsServer;
@ -136,7 +136,7 @@ pub async fn upload(
let user = update_user_avatar(user_id, avatar_url.clone(), db).await?; let user = update_user_avatar(user_id, avatar_url.clone(), db).await?;
ws.send(BroadcastToChannel( ws.send(BroadcastToChannel(
project_id, project_id,
WsMsg::AvatarUrlChanged(user.id, avatar_url), WsMsg::User(WsMsgUser::AvatarUrlChanged(user.id, avatar_url)),
)) ))
.await .await
.map_err(|_| HttpResponse::UnprocessableEntity().finish())?; .map_err(|_| HttpResponse::UnprocessableEntity().finish())?;

View File

@ -134,7 +134,7 @@ async fn aws_s3(
async fn local_storage_write( async fn local_storage_write(
system_file_name: String, system_file_name: String,
fs: Data<Addr<filesystem_actor::FileSystemExecutor>>, fs: Data<Addr<filesystem_actor::FileSystemExecutor>>,
user_id: jirs_data::UserId, _user_id: jirs_data::UserId,
receiver: Receiver<bytes::Bytes>, receiver: Receiver<bytes::Bytes>,
) -> Option<String> { ) -> Option<String> {
let web_config = jirs_config::web::config(); let web_config = jirs_config::web::config();
@ -148,15 +148,9 @@ async fn local_storage_write(
.await .await
{ {
Ok(Ok(_)) => Some(format!( Ok(Ok(_)) => Some(format!(
"{proto}://{bind}{port}{client_path}/{user_id}-{filename}", "{addr}{client_path}/{filename}",
proto = if web_config.ssl { "https" } else { "http" }, addr = web_config.full_addr(),
bind = web_config.bind,
port = match web_config.port.as_str() {
"80" | "443" => "".to_string(),
p => format!(":{}", p),
},
client_path = fs_config.client_path, client_path = fs_config.client_path,
user_id = user_id,
filename = system_file_name filename = system_file_name
)), )),
Ok(_) => None, Ok(_) => None,

View File

@ -47,6 +47,7 @@ path = "../actors/database-actor"
[dependencies.web-actor] [dependencies.web-actor]
path = "../actors/web-actor" path = "../actors/web-actor"
features = ["local-storage"]
[dependencies.websocket-actor] [dependencies.websocket-actor]
path = "../actors/websocket-actor" path = "../actors/websocket-actor"

View File

@ -70,7 +70,7 @@ async fn main() -> Result<(), String> {
app app
}) })
.workers(web_config.concurrency) .workers(web_config.concurrency)
.bind(web_config.addr()) .bind(web_config.bind_addr())
.map_err(|e| format!("{}", e))? .map_err(|e| format!("{}", e))?
.run() .run()
.await .await

View File

@ -12,6 +12,7 @@ pub struct Configuration {
pub port: String, pub port: String,
pub bind: String, pub bind: String,
pub ssl: bool, pub ssl: bool,
pub public_addr: Option<String>,
} }
impl Default for Configuration { impl Default for Configuration {
@ -21,12 +22,20 @@ impl Default for Configuration {
port: "5000".to_string(), port: "5000".to_string(),
bind: "0.0.0.0".to_string(), bind: "0.0.0.0".to_string(),
ssl: false, ssl: false,
public_addr: None,
} }
} }
} }
impl Configuration { impl Configuration {
pub fn addr(&self) -> String { pub fn addr(&self) -> String {
match self.public_addr.as_deref() {
Some(s) => String::from(s),
_ => format!("{}:{}", self.bind, self.port),
}
}
pub fn bind_addr(&self) -> String {
format!("{}:{}", self.bind, self.port) format!("{}:{}", self.bind, self.port)
} }

View File

@ -34,8 +34,8 @@ mod shared;
pub mod validations; pub mod validations;
mod ws; mod ws;
// #[global_allocator] #[global_allocator]
// static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT; static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
#[derive(Debug)] #[derive(Debug)]
#[repr(C)] #[repr(C)]
@ -190,6 +190,7 @@ fn update(msg: Msg, model: &mut model::Model, orders: &mut impl Orders<Msg>) {
})); }));
} }
Ok(m) => { Ok(m) => {
log::info!("INCOMING {:?}", m);
orders orders
.skip() .skip()
.send_msg(Msg::WebSocketChange(WebSocketChanged::WsMsg(m))); .send_msg(Msg::WebSocketChange(WebSocketChanged::WsMsg(m)));
@ -200,6 +201,7 @@ fn update(msg: Msg, model: &mut model::Model, orders: &mut impl Orders<Msg>) {
} }
WebSocketChanged::WebSocketClosed => { WebSocketChanged::WebSocketClosed => {
open_socket(model, orders); open_socket(model, orders);
return;
} }
WebSocketChanged::Bounced(ws_msg) => { WebSocketChanged::Bounced(ws_msg) => {
model.ws_queue.push(ws_msg); model.ws_queue.push(ws_msg);
@ -207,7 +209,6 @@ fn update(msg: Msg, model: &mut model::Model, orders: &mut impl Orders<Msg>) {
return; return;
} }
}; };
Msg::WebSocketChange(change)
} }
_ => msg, _ => msg,
}; };

View File

@ -5,9 +5,9 @@ use crate::validator;
validator!(EmailFormat, is_email, "Not a valid e-mail address"); validator!(EmailFormat, is_email, "Not a valid e-mail address");
validator!(UuidFormat, is_token, "Malformed token"); validator!(UuidFormat, is_token, "Malformed token");
pub type UsernameValidator = Touched<Between<4, 20>>; pub type UsernameValidator = Touched<Between<4, 36>>;
pub type EmailValidator = Touched<Chain<Changed<AtLeast<6>>, Changed<EmailFormat>>>; pub type EmailValidator = Touched<Chain<Changed<AtLeast<6>>, Changed<EmailFormat>>>;
pub type TokenValidator = Touched<Chain<Between<10, 20>, Changed<UuidFormat>>>; pub type TokenValidator = Touched<Chain<Between<10, 36>, Changed<UuidFormat>>>;
#[derive(Debug, Default)] #[derive(Debug, Default)]
pub struct SignInPage { pub struct SignInPage {

View File

@ -432,7 +432,26 @@ pub fn update(msg: WsMsg, model: &mut Model, orders: &mut impl Orders<Msg>) {
Some(id), Some(id),
)); ));
} }
_ => (), WsMsg::Session(WsMsgSession::AuthenticateSuccess) => {
let page = crate::match_page_mut!(model, SignIn);
page.login_success = true;
}
WsMsg::Session(WsMsgSession::BindTokenOk(access_token)) => {
match write_auth_token(Some(access_token)) {
Ok(msg) => {
orders.skip().send_msg(msg);
}
Err(e) => {
log::error!("{}", e);
}
}
}
_ => {
log::info!(
"got web socket message but don't know what to do with it {:?}",
msg
);
}
}; };
} }