From 34b1d5ec2d6343d0a29d7202c48d64fc302803f9 Mon Sep 17 00:00:00 2001 From: Varga Marcell <128537619+marci1175@users.noreply.github.com> Date: Sun, 10 Dec 2023 22:05:23 +0100 Subject: [PATCH] impl images --- src/app/backend.rs | 39 ++++++++++++++++++++++++++++++++++++++- src/app/server.rs | 18 +++++++++++++----- src/app/ui/client.rs | 40 ++++++++++++++++++++++++++++++++++------ 3 files changed, 85 insertions(+), 12 deletions(-) diff --git a/src/app/backend.rs b/src/app/backend.rs index d9feb311..442f3921 100644 --- a/src/app/backend.rs +++ b/src/app/backend.rs @@ -2,7 +2,7 @@ use chrono::Utc; use rand::rngs::ThreadRng; use std::collections::BTreeMap; -use std::fs::File; +use std::fs::{File, self}; use std::path::PathBuf; use std::sync::atomic::AtomicBool; @@ -336,6 +336,26 @@ impl Message { Destination: ip, } } + pub fn construct_image_msg( + file_name: PathBuf, + ip: String, + password: String, + author: String, + replying_to: Option, + ) -> Message { + Message { + replying_to: replying_to, + //Dont execute me please :3 | + // | + // V + MessageType: MessageType::Image(Image { bytes: fs::read(file_name).unwrap_or_default() }), + + Password: password, + Author: author, + MessageDate: { Utc::now().format("%Y.%m.%d. %H:%M").to_string() }, + Destination: ip, + } + } } #[derive(serde::Serialize, serde::Deserialize, Debug, Clone)] @@ -389,6 +409,23 @@ impl ServerOutput { MessageDate: normal_msg.MessageDate, } } + pub fn convert_picture_to_servermsg(normal_msg: Message) -> ServerOutput { + //Convert a client output to a server output (Message -> ServerOutput), trim some useless info + ServerOutput { + replying_to: normal_msg.replying_to, + MessageType: ServerMessageType::Image(ServerImage { + bytes: match normal_msg.MessageType { + MessageType::SyncMessage(_) => todo!(), + MessageType::FileRequest(_) => todo!(), + MessageType::FileUpload(_) => todo!(), + MessageType::Image(img) => img.bytes, + MessageType::NormalMessage(_) => todo!(), + }, + }), + Author: normal_msg.Author, + MessageDate: normal_msg.MessageDate, + } + } pub fn convert_upload_to_servermsg(normal_msg: Message, index: i32) -> ServerOutput { //Convert a client output to a server output (Message -> ServerOutput), trim some useless info ServerOutput { diff --git a/src/app/server.rs b/src/app/server.rs index 40036fd1..7efa70f1 100644 --- a/src/app/server.rs +++ b/src/app/server.rs @@ -55,10 +55,9 @@ impl ServerMessage for MessageService { if &req.Password == self.passw.trim() { match &req.MessageType { NormalMessage(msg) => self.NormalMessage(req).await, - SyncMessage(msg) => { /*Dont do anything we will always reply with the list of msgs*/ - } - Image(msg) => { - todo!() + SyncMessage(msg) => { /*Dont do anything we will always reply with the list of msgs*/} + Image(_) => { + self.ImageMessage(req).await; } FileRequest(msg) => { let (file_bytes, file_name) = &self.serve_file(msg.index).await; @@ -278,7 +277,16 @@ impl MessageService { let reply = MessageResponse { message: final_msg }; Ok(Response::new(reply)) } - + pub async fn ImageMessage(&self, req: Message) { + match self.messages.lock() { + Ok(mut ok) => { + ok.push(ServerOutput::convert_picture_to_servermsg(req)); + } + Err(err) => { + println!("{err}") + } + }; + } pub async fn recive_file(&self, request: Message) { /* diff --git a/src/app/ui/client.rs b/src/app/ui/client.rs index 0695dfd4..a98d51f2 100644 --- a/src/app/ui/client.rs +++ b/src/app/ui/client.rs @@ -138,6 +138,8 @@ impl TemplateApp { size: self.font_size, }; + ui.painter().rect_filled(egui::Rect::EVERYTHING, 0., Color32::from_rgba_premultiplied(0, 0, 0, (self.how_on / 3.) as u8)); + Area::new("drop_warning").show(ctx, |ui|{ ui.painter() .rect(egui::Rect { min: Pos2::new(window_size[0] / 3., window_size[0] / 5. + self.how_on / 50.), max: Pos2::new(window_size[0] / 1.5, window_size[0] / 3. + self.how_on / 50.) }, 5.0, Color32::from_rgba_unmultiplied(0, 0, 0, self.how_on as u8 / 8), Stroke::default()); @@ -180,7 +182,7 @@ impl TemplateApp { }); } let mut test: Vec = Vec::new(); - let mut has_been_reply_clicked = (false, 0); + let mut reply_to_got_to = (false, 0); for (index, item) in self.incoming_msg.clone().struct_list.iter().enumerate() { let mut i: &String = &Default::default(); @@ -209,7 +211,7 @@ impl TemplateApp { .clicked() { //implement scrolling to message - has_been_reply_clicked = (true, replied_to); + reply_to_got_to = (true, replied_to); } } @@ -301,6 +303,9 @@ impl TemplateApp { }); } } + if let ServerMessageType::Image(picture) = &item.MessageType { + ui.add(egui::widgets::Image::from_bytes("bytes://", picture.bytes.clone())); + } ui.label(RichText::from(format!("{}", item.MessageDate)).size(self.font_size / 1.5).color(Color32::DARK_GRAY)); } ).response.context_menu(|ui|{ @@ -313,8 +318,8 @@ impl TemplateApp { }); test.push(fasz); - if has_been_reply_clicked.0 { - test[has_been_reply_clicked.1].scroll_to_me(Some(Align::Center)); + if reply_to_got_to.0 { + test[reply_to_got_to.1].scroll_to_me(Some(Align::Center)); } }; }); @@ -411,6 +416,7 @@ impl TemplateApp { ui.horizontal(|ui| { ui.label(RichText::from("Replying to:").size(self.font_size).weak()); ui.label(RichText::from(match &self.incoming_msg.struct_list[replying_to].MessageType { + ServerMessageType::Image(_img) => format!("Image"), ServerMessageType::Upload(upload) => format!("Upload {}", upload.file_name), @@ -505,9 +511,19 @@ impl TemplateApp { self.replying_to = None; self.usr_msg.clear(); - + for file_path in self.files_to_send.clone() { - self.send_file(file_path); + match file_path.extension().unwrap().to_string_lossy().as_str() { + "png" => { + self.send_picture(file_path); + } + "jpeg" => { + self.send_picture(file_path); + } + _ => { + self.send_file(file_path); + } + } } //clear vectors @@ -614,6 +630,18 @@ impl TemplateApp { let message = Message::construct_file_msg(file, ip, passw, author, replying_to); + tokio::spawn(async move { + let _ = client::send_msg(message).await; + }); + } + fn send_picture(&mut self, file: std::path::PathBuf) { + let passw = self.client_password.clone(); + let ip = self.send_on_ip.clone(); + let author = self.login_username.clone(); + let replying_to = self.replying_to.clone(); + + let message = Message::construct_image_msg(file, ip, passw, author, replying_to); + tokio::spawn(async move { let _ = client::send_msg(message).await; });