Notice
Recent Posts
Recent Comments
Link
개발 무지렁이
[Spring Boot] 프로필 이미지 제거 본문
[MemberController.java]
@Controller
@RequestMapping("/member")
@RequiredArgsConstructor
public class MemberController {
private final MemberService memberService;
@PostMapping("/remove/image");
public String remove(Principal principal) {
Member member = memberService.getMemberByUsername(principal.getName());
memberService.removeProfileImg(member);
}
}
[MemberService.java]
@Service
@RequiredArgsConstructor
public class MemberService {
private final MemberRepository memberRepository;
public void removeProfileImg(Member member) {
member.removeProfileImgOnStorage();
member.setProfileImg(null);
memberRepository.save(member);
}
}
[Member.java]
@Entity
@Setter
@Getter
@AllArgsConstructor
@NoArgsConstructor
@SuperBuilder
@ToString(callSuper = true)
public class Member extends BaseEntity {
@Column(unique = true)
private String username;
private String password;
private String email;
private String profileImg;
public void removeProfileImgOnStorage() {
if(profileImg == null || profileImg.trim().length() == 0 ) return;
String profileImgPath = getProfileImgPath();
new File(profileImgPath).delete();
}
private String getProfileImgPath() {
return AppConfig.GET_FILE_DIR_PATH + "/" + profileImg;
}
}
[AppConfig.java]
@Configuration
public class AppConfig {
public static String GET_FILE_DIR_PATH;
@Value("${custom.genFileDirPath}")
public void setFileDirPath(String genFileDirPath) {
GET_FILE_DIR_PATH = genFileDirPath;
}
}
'Backend > 스프링부트' 카테고리의 다른 글
[Spring Boot] Util클래스에서 원하는 포맷으로 날짜 가져오기 (0) | 2022.12.10 |
---|---|
[Spring Boot] 프로필 이미지 노출 (0) | 2022.12.08 |
[Spring Boot] 테스트용 샘플DB 분리, 샘플데이터 생성 (0) | 2022.12.07 |
[Spring Boot] MockMvc를 이용한 테스트 (0) | 2022.12.07 |
[Spring Boot] BaseEntity에 사용되는 @SuperBuilder와 @MappedSuperclass (0) | 2022.12.06 |
Comments