Skip to content

Commit

Permalink
impl images
Browse files Browse the repository at this point in the history
  • Loading branch information
marci1175 committed Dec 10, 2023
1 parent 700cbf9 commit 34b1d5e
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 12 deletions.
39 changes: 38 additions & 1 deletion src/app/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<usize>,
) -> 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)]
Expand Down Expand Up @@ -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 {
Expand Down
18 changes: 13 additions & 5 deletions src/app/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
/*
Expand Down
40 changes: 34 additions & 6 deletions src/app/ui/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -180,7 +182,7 @@ impl TemplateApp {
});
}
let mut test: Vec<Response> = 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();
Expand Down Expand Up @@ -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);

}
}
Expand Down Expand Up @@ -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|{
Expand All @@ -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));
}
};
});
Expand Down Expand Up @@ -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),

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
});
Expand Down

0 comments on commit 34b1d5e

Please sign in to comment.