Skip to content

Commit 4c4f3c5

Browse files
committed
Remade Image Selector for Profil Picture in UserSettings
1 parent ea0c0d4 commit 4c4f3c5

File tree

3 files changed

+15
-2
lines changed

3 files changed

+15
-2
lines changed

src/main/java/de/jadenk/springcloud/controller/UserSettingController.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ private Map<String, Object> getUserAttributes(User user) {
6161

6262

6363
@GetMapping("/settings")
64-
public String userSettingsPage(Model model) {
64+
public String userSettingsPage(@RequestParam(value = "error", required = false) String error,Model model) {
6565
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
6666
String username = authentication.getName();
6767

@@ -107,6 +107,8 @@ public String updateUserSettings(@RequestParam("username") String username,
107107
user.setEmail(email);
108108
}
109109

110+
111+
110112
boolean wantsToChangePassword =
111113
currentPassword != null && !currentPassword.trim().isEmpty() &&
112114
newPassword != null && !newPassword.trim().isEmpty() &&
@@ -139,6 +141,16 @@ public String updateUserSettings(@RequestParam("username") String username,
139141
user.setNotificationsEnabled(notificationsEnabled);
140142

141143
if (profileImage != null && !profileImage.isEmpty()) {
144+
String originalFilename = profileImage.getOriginalFilename();
145+
if (originalFilename != null &&
146+
!originalFilename.toLowerCase().endsWith(".png") &&
147+
!originalFilename.toLowerCase().endsWith(".jpg") &&
148+
!originalFilename.toLowerCase().endsWith(".jpeg")) {
149+
model.addAttribute("error", messageService.getError("settings.upload.wrong.ending"));
150+
model.addAttribute("userAttributes", getUserAttributes(user));
151+
return "user-settings";
152+
}
153+
142154
try {
143155
InputStream is = profileImage.getInputStream();
144156
BufferedImage originalImage = ImageIO.read(is);

src/main/java/de/jadenk/springcloud/util/MessageService.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ public class MessageService {
1414
"changepassword.same.password", "You cant Use the same Password as before!",
1515
"auth.invalid", "Username or password is incorrect.",
1616
"settings.upload.error", "Error while uploading Profile Picture, try again later.",
17+
"settings.upload.wrong.ending", "You can only Upload .jpg, .png and .jpeg files.",
1718
"settings.username.exists", "This username is already in use.",
1819
"settings.email.exists", "This mail is already in use.",
1920
"register.username.exists", "Username already exists."

src/main/resources/templates/user-settings.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ <h2>Profil Information</h2>
6363
<label>Profilpicture:</label>
6464
<div class="profile-picture-upload">
6565
<img th:src="${userAttributes.profileImageData}" alt="Profilbild" class="profile-preview">
66-
<input type="file" name="profileImage" accept="image/*">
66+
<input type="file" name="profileImage" accept="image/png, image/jpeg, image/jpg">
6767
</div>
6868
</div>
6969

0 commit comments

Comments
 (0)