티스토리 뷰

728x90

attributes에 어떤 값을 가져오는지 알아야 사용자 값을 활용할 수 있을 것이다. 

롬복의 @Slf4j를 추가하여 log를 찍어보고 결과를 출력해보자. 

attributes 출력

**참고**

System.out.println과 비슷하나 log.debug가 더 자세한 결과를 얻을 수 있고 debug는 지우지 않아도 상관없다. (*로그 레벨을 셋팅하면 운영모드에는 실행되지 않기 때문에)

logging:
  level:
    com.ll.exam.appname: debug

attributes 값을 출력해보면 properties에 nickname, profile_image, kakao_account등의 개인 정보를 포함하고 있다. 

attributesProperties

attributesProperties를 활용하여 프로필 값을 변수에 넣어주어 활용하였다. 

 

프로필 사진 url은 DB에 저장하고 추가적으로 개인 폴더에 따로 저장하기 위해 경로와 함수에 대해서 새로 정의하였다. 

memberService.setProfileImgByUrl(member, profile_image);

멤버 객체와 이전에 카카오 로그인으로 부터 얻은 profile_image 를 넘긴다.

setProfileImgByUrl

@Value("${custom.genFileDirPath}")
private String genFileDirPath;

경로 값은 application.yml에서 따로 변수처리 해주었다. 

custom:
  genFileDirPath: c:\Temp\~~

저장할 폴더 이름과 경로를 만들어주고 그안에 이미지를 저장하고 filePath를 출력하여 memberRepository에 저장한다.

private String getCurrentProfileImgDirName() {
    return "member/" + Util.date.getCurrentDateFormatted("yyyy_MM_dd");
}

public static String getCurrentDateFormatted(String pattern) {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
            return simpleDateFormat.format(new Date());
        }
public static String downloadImg(String url, String filePath) {
    new File(filePath).getParentFile().mkdirs();

    byte[] imageBytes = new RestTemplate().getForObject(url, byte[].class);
    try {
        Files.write(Paths.get(filePath), imageBytes);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }

    String mimeType = null;
    try {
        mimeType = new Tika().detect(new File(filePath));
        //Tika라는 라이브러리가 이게 JPG인지 PNG인지 알려준다.
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    String ext = mimeType.replaceAll("image/", "");
    ext = ext.replaceAll("jpeg", "jpg");

    String newFilePath = filePath + "." + ext;

    new File(filePath).renameTo(new File(newFilePath));

    return newFilePath;
}

 

공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
TAG
more
«   2024/07   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
글 보관함