mirror of
https://github.com/Akomry/sae302_applicom.git
synced 2025-12-06 11:43:53 +00:00
feat(avatar): Gestion des avatars en base64, corruption lors d'un changement en étant connecté à fix
This commit is contained in:
parent
8c9dfc58f8
commit
cba4d6b81b
4 changed files with 71 additions and 18 deletions
|
|
@ -170,11 +170,12 @@ public class ChatController implements Initializable {
|
||||||
File selectedFile = fileChooser.showOpenDialog(stage);
|
File selectedFile = fileChooser.showOpenDialog(stage);
|
||||||
if (selectedFile != null) {
|
if (selectedFile != null) {
|
||||||
avatarImageView.setImage(new Image(selectedFile.toURI().toString()));
|
avatarImageView.setImage(new Image(selectedFile.toURI().toString()));
|
||||||
|
contact.setAvatarFromFile(selectedFile);
|
||||||
}
|
}
|
||||||
|
client.sendEvent(new rtgre.modeles.Event("CONT", this.contact.toJsonObject()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
private void handleConnection(Observable observable) {
|
private void handleConnection(Observable observable) {
|
||||||
if (connectionButton.isSelected()) {
|
if (connectionButton.isSelected()) {
|
||||||
java.awt.Image img = SwingFXUtils.fromFXImage(this.avatarImageView.getImage(), null);
|
java.awt.Image img = SwingFXUtils.fromFXImage(this.avatarImageView.getImage(), null);
|
||||||
|
|
@ -198,7 +199,7 @@ public class ChatController implements Initializable {
|
||||||
client.sendAuthEvent(contact);
|
client.sendAuthEvent(contact);
|
||||||
client.sendListRoomEvent();
|
client.sendListRoomEvent();
|
||||||
client.sendEvent(new rtgre.modeles.Event(rtgre.modeles.Event.LIST_CONTACTS, new JSONObject()));
|
client.sendEvent(new rtgre.modeles.Event(rtgre.modeles.Event.LIST_CONTACTS, new JSONObject()));
|
||||||
|
client.sendEvent(new rtgre.modeles.Event(rtgre.modeles.Event.CONT, contact.toJsonObject()));
|
||||||
initContactListView();
|
initContactListView();
|
||||||
initPostListView();
|
initPostListView();
|
||||||
this.statusLabel.setText("Connected to %s@%s:%s".formatted(this.contact.getLogin(), host, port));
|
this.statusLabel.setText("Connected to %s@%s:%s".formatted(this.contact.getLogin(), host, port));
|
||||||
|
|
@ -432,18 +433,29 @@ public class ChatController implements Initializable {
|
||||||
}
|
}
|
||||||
private void handleContEvent(JSONObject content) {
|
private void handleContEvent(JSONObject content) {
|
||||||
Contact contact = contactMap.getContact(content.getString("login"));
|
Contact contact = contactMap.getContact(content.getString("login"));
|
||||||
|
java.awt.Image avatar = null;
|
||||||
|
if (!content.getString("avatar").isEmpty()) {
|
||||||
|
avatar = Contact.base64ToImage(content.getString("avatar"));
|
||||||
|
}
|
||||||
|
System.out.println(avatar);
|
||||||
if (contact != null) {
|
if (contact != null) {
|
||||||
LOGGER.info(contactMap.toString());
|
LOGGER.info(contactMap.toString());
|
||||||
contactMap.getContact(content.getString("login")).setConnected(content.getBoolean("connected"));
|
contactMap.getContact(content.getString("login")).setConnected(content.getBoolean("connected"));
|
||||||
|
if (avatar != null) {
|
||||||
|
contactMap.getContact(content.getString("login")).setAvatar(avatar);
|
||||||
|
}
|
||||||
contactsListView.refresh();
|
contactsListView.refresh();
|
||||||
LOGGER.info(contactMap.toString());
|
LOGGER.info(contactMap.toString());
|
||||||
} else {
|
} else {
|
||||||
|
System.out.println(content);
|
||||||
LOGGER.info(contactMap.toString());
|
LOGGER.info(contactMap.toString());
|
||||||
Contact user = Contact.fromJSON(
|
Contact user = Contact.fromJSON(
|
||||||
content,
|
content,
|
||||||
new File("chat/src/main/resources/rtgre/chat/avatars.png")
|
new File("chat/src/main/resources/rtgre/chat/avatars.png")
|
||||||
);
|
);
|
||||||
System.out.println(user.getAvatar());
|
if (avatar != null) {
|
||||||
|
user.setAvatar(avatar);
|
||||||
|
}
|
||||||
contactMap.add(user);
|
contactMap.add(user);
|
||||||
contactObservableList.add(user);
|
contactObservableList.add(user);
|
||||||
LOGGER.info(contactMap.toString());
|
LOGGER.info(contactMap.toString());
|
||||||
|
|
|
||||||
|
|
@ -4,9 +4,13 @@ package rtgre.modeles;
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
|
|
||||||
import javax.imageio.ImageIO;
|
import javax.imageio.ImageIO;
|
||||||
|
import java.awt.*;
|
||||||
import java.awt.image.BufferedImage;
|
import java.awt.image.BufferedImage;
|
||||||
|
import java.io.ByteArrayInputStream;
|
||||||
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.util.Base64;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
|
||||||
import static rtgre.chat.ChatApplication.LOGGER;
|
import static rtgre.chat.ChatApplication.LOGGER;
|
||||||
|
|
@ -108,7 +112,8 @@ public class Contact {
|
||||||
public JSONObject toJsonObject() {
|
public JSONObject toJsonObject() {
|
||||||
return new JSONObject()
|
return new JSONObject()
|
||||||
.put("login", this.login)
|
.put("login", this.login)
|
||||||
.put("connected", this.connected);
|
.put("connected", this.connected)
|
||||||
|
.put("avatar", Contact.imageToBase64((BufferedImage) avatar));
|
||||||
}
|
}
|
||||||
|
|
||||||
public String toJson() {
|
public String toJson() {
|
||||||
|
|
@ -140,8 +145,34 @@ public class Contact {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setAvatar(java.awt.Image avatar) {
|
||||||
|
this.avatar = avatar;
|
||||||
|
}
|
||||||
|
|
||||||
public void setCurrentRoom(String currentRoom) {
|
public void setCurrentRoom(String currentRoom) {
|
||||||
this.currentRoom = currentRoom;
|
this.currentRoom = currentRoom;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static String imageToBase64(BufferedImage img) {
|
||||||
|
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
||||||
|
try {
|
||||||
|
ImageIO.write(img, "png", bos);
|
||||||
|
return Base64.getEncoder().encodeToString(bos.toByteArray());
|
||||||
|
} catch (IOException e) {
|
||||||
|
LOGGER.severe("Impossible de convertir l'image en base64");
|
||||||
|
}
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
public static java.awt.Image base64ToImage(String avatar64) {
|
||||||
|
byte[] bytes64 = Base64.getDecoder().decode(avatar64);
|
||||||
|
try {
|
||||||
|
BufferedImage image = ImageIO.read(new ByteArrayInputStream(bytes64));
|
||||||
|
return image;
|
||||||
|
} catch (IOException e) {
|
||||||
|
LOGGER.severe("Impossible de convertir le base64 en image");
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
File diff suppressed because one or more lines are too long
|
|
@ -268,12 +268,23 @@ public class ChatServer {
|
||||||
} else if (event.getType().equals(Event.QUIT)) {
|
} else if (event.getType().equals(Event.QUIT)) {
|
||||||
LOGGER.info("Déconnexion");
|
LOGGER.info("Déconnexion");
|
||||||
return false;
|
return false;
|
||||||
|
} else if (event.getType().equals(Event.CONT)) {
|
||||||
|
doCont(event.getContent());
|
||||||
|
LOGGER.info("Update de contact");
|
||||||
|
return true;
|
||||||
} else {
|
} else {
|
||||||
LOGGER.warning("Unhandled event type: " + event.getType());
|
LOGGER.warning("Unhandled event type: " + event.getType());
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void doCont(JSONObject content) {
|
||||||
|
if (user.isConnected()) {
|
||||||
|
sendEventToAllContacts(new Event("CONT", content));
|
||||||
|
contactMap.getContact(content.getString("login")).setAvatar(Contact.base64ToImage(content.getString("avatar")));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void doJoin(JSONObject content) {
|
private void doJoin(JSONObject content) {
|
||||||
if (content.getString("room").isEmpty()) {
|
if (content.getString("room").isEmpty()) {
|
||||||
user.setCurrentRoom(null);
|
user.setCurrentRoom(null);
|
||||||
|
|
@ -368,11 +379,7 @@ public class ChatServer {
|
||||||
private void doListContact(JSONObject content) throws JSONException, IllegalStateException {
|
private void doListContact(JSONObject content) throws JSONException, IllegalStateException {
|
||||||
for (Contact contact: contactMap.values()) {
|
for (Contact contact: contactMap.values()) {
|
||||||
if (contactMap.getContact(user.getLogin()).isConnected()) {
|
if (contactMap.getContact(user.getLogin()).isConnected()) {
|
||||||
try {
|
sendEventToContact(user, new Event(Event.CONT, contact.toJsonObject()));
|
||||||
send(new Event(Event.CONT, contact.toJsonObject()).toJson());
|
|
||||||
} catch (IOException e) {
|
|
||||||
throw new IllegalStateException();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue