mirror of
https://github.com/Akomry/sae302_applicom.git
synced 2025-12-06 17:43:54 +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);
|
||||
if (selectedFile != null) {
|
||||
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) {
|
||||
if (connectionButton.isSelected()) {
|
||||
java.awt.Image img = SwingFXUtils.fromFXImage(this.avatarImageView.getImage(), null);
|
||||
|
|
@ -198,7 +199,7 @@ public class ChatController implements Initializable {
|
|||
client.sendAuthEvent(contact);
|
||||
client.sendListRoomEvent();
|
||||
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();
|
||||
initPostListView();
|
||||
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) {
|
||||
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) {
|
||||
LOGGER.info(contactMap.toString());
|
||||
contactMap.getContact(content.getString("login")).setConnected(content.getBoolean("connected"));
|
||||
if (avatar != null) {
|
||||
contactMap.getContact(content.getString("login")).setAvatar(avatar);
|
||||
}
|
||||
contactsListView.refresh();
|
||||
LOGGER.info(contactMap.toString());
|
||||
} else {
|
||||
System.out.println(content);
|
||||
LOGGER.info(contactMap.toString());
|
||||
Contact user = Contact.fromJSON(
|
||||
content,
|
||||
new File("chat/src/main/resources/rtgre/chat/avatars.png")
|
||||
);
|
||||
System.out.println(user.getAvatar());
|
||||
if (avatar != null) {
|
||||
user.setAvatar(avatar);
|
||||
}
|
||||
contactMap.add(user);
|
||||
contactObservableList.add(user);
|
||||
LOGGER.info(contactMap.toString());
|
||||
|
|
|
|||
|
|
@ -4,9 +4,13 @@ package rtgre.modeles;
|
|||
import org.json.JSONObject;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import java.awt.*;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.Base64;
|
||||
import java.util.Objects;
|
||||
|
||||
import static rtgre.chat.ChatApplication.LOGGER;
|
||||
|
|
@ -108,7 +112,8 @@ public class Contact {
|
|||
public JSONObject toJsonObject() {
|
||||
return new JSONObject()
|
||||
.put("login", this.login)
|
||||
.put("connected", this.connected);
|
||||
.put("connected", this.connected)
|
||||
.put("avatar", Contact.imageToBase64((BufferedImage) avatar));
|
||||
}
|
||||
|
||||
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) {
|
||||
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)) {
|
||||
LOGGER.info("Déconnexion");
|
||||
return false;
|
||||
} else if (event.getType().equals(Event.CONT)) {
|
||||
doCont(event.getContent());
|
||||
LOGGER.info("Update de contact");
|
||||
return true;
|
||||
} else {
|
||||
LOGGER.warning("Unhandled event type: " + event.getType());
|
||||
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) {
|
||||
if (content.getString("room").isEmpty()) {
|
||||
user.setCurrentRoom(null);
|
||||
|
|
@ -368,11 +379,7 @@ public class ChatServer {
|
|||
private void doListContact(JSONObject content) throws JSONException, IllegalStateException {
|
||||
for (Contact contact: contactMap.values()) {
|
||||
if (contactMap.getContact(user.getLogin()).isConnected()) {
|
||||
try {
|
||||
send(new Event(Event.CONT, contact.toJsonObject()).toJson());
|
||||
} catch (IOException e) {
|
||||
throw new IllegalStateException();
|
||||
}
|
||||
sendEventToContact(user, new Event(Event.CONT, contact.toJsonObject()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue