Skip to content

Commit

Permalink
fix: user profile image size
Browse files Browse the repository at this point in the history
  • Loading branch information
LAPLACE4A committed Nov 7, 2024
1 parent 92630a4 commit a2cf87f
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/main/java/com/kert/controller/UserController.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.kert.controller;

import com.kert.model.User;
import com.kert.service.AdminService;
import com.kert.service.UserService;
import com.kert.service.AdminService;
import com.kert.dto.SignUpDTO;
import com.kert.dto.LoginDTO;
import com.kert.dto.TokenResponse;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/kert/model/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class User {

private String name;
private String email;
@Column(columnDefinition = "TEXT")
@Column(columnDefinition = "MEDIUMTEXT")
private String profilePicture;
private String generation;
private String major;
Expand Down
17 changes: 14 additions & 3 deletions src/main/java/com/kert/service/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import lombok.RequiredArgsConstructor;

import java.util.List;
import java.nio.charset.StandardCharsets;

@Service
@RequiredArgsConstructor
Expand All @@ -18,6 +19,7 @@ public class UserService {
private final UserRepository userRepository;
private final PasswordRepository passwordRepository;
private final PasswordEncoder passwordEncoder;
private static final long MAX_PROFILE_PICTURE_SIZE = 2 * 1024 * 1024;

public List<User> getAllUsers() {
return userRepository.findAll();
Expand All @@ -27,22 +29,31 @@ public User getUserById(Long studentId) {
return userRepository.findById(studentId).orElse(null);
}

private boolean isProfilePictureSizeValid(String profilePicture) {
if (profilePicture == null) {
return true;
}
byte[] profilePictureBytes = profilePicture.getBytes(StandardCharsets.UTF_8);
return profilePictureBytes.length <= MAX_PROFILE_PICTURE_SIZE;
}

public User createUser(SignUpDTO signUpDTO) {
User user = new User();
if (!isProfilePictureSizeValid(signUpDTO.getProfilePicture())) {
throw new IllegalArgumentException("프로필 사진 크기가 2MB를 초과합니다.");
}

User user = new User();
user.setStudentId(signUpDTO.getStudentId());
user.setEmail(signUpDTO.getEmail());
user.setName(signUpDTO.getName());
user.setProfilePicture(signUpDTO.getProfilePicture());
user.setGeneration(signUpDTO.getGeneration());
user.setMajor(signUpDTO.getMajor());

System.out.println(user);

if (userRepository.existsById(user.getStudentId())) {
throw new IllegalArgumentException("이미 존재하는 사용자입니다.");
}

Password password = new Password();
password.setUserId(user.getStudentId());
password.setHash(passwordEncoder.encode(signUpDTO.getPassword()));
Expand Down

0 comments on commit a2cf87f

Please sign in to comment.