Skip to content

Commit

Permalink
feat: 사용자의 프로필 이미지를 저장하는 기능 추가
Browse files Browse the repository at this point in the history
- 프로필 이미지를 MultipartFile로 변환하여 메타데이터와 함께 저장한다.
  • Loading branch information
zzawang committed May 27, 2024
1 parent b1f8c92 commit 6561d30
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import org.apache.tika.Tika;
import org.springframework.web.multipart.MultipartFile;

/**
* byte 배열을 MultipartFile 객체로 변환하기 위한 클래스
*/
public class CustomMultipartFile implements MultipartFile {
private final Tika tika = new Tika();

private byte[] input;
private String filename;

Expand All @@ -21,7 +24,7 @@ public CustomMultipartFile(byte[] input, String filename) {

@Override
public String getName() {
return null;
return filename;
}

@Override
Expand All @@ -31,7 +34,11 @@ public String getOriginalFilename() {

@Override
public String getContentType() {
return null;
try {
return tika.detect(getInputStream());
} catch (IOException e) {
throw new RuntimeException(e);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ public static Member toMember(MemberCreateDto memberCreateDto) {
memberCreateDto.getNickname(), memberCreateDto.getEmail(), null);
}

public static MemberCreateDto toMemberCreateDto(GithubProfile profile) {
public static Member toMember(GithubProfile profile, Long fileId) {
String uuidPw = UUID.randomUUID().toString().substring(0, 12); // 깃허브로 가입한 사용자는 비밀번호를 UUID로 설정
return new MemberCreateDto(profile.getId(), uuidPw, profile.getNickname(), profile.getEmail());
return new Member(profile.getId(), uuidPw, profile.getNickname(), profile.getEmail(), fileId);
}

public static SimpleMemberDto toSimpleMemberDto(Member member, String imgUrl) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.issuetracker.oauth.controller;

import com.issuetracker.file.dto.UploadedFileDto;
import com.issuetracker.file.service.FileService;
import com.issuetracker.file.util.ImgUrlConverter;
import com.issuetracker.member.dto.LoginResponse;
Expand Down Expand Up @@ -32,8 +33,8 @@ public ResponseEntity<LoginResponse> login(@RequestParam String code) throws IOE

// 처음 사용자가 깃허브 로그인을 하는 것이라면 회원가입 로직을 수행한다.
if (memberService.isNewUser(profile.getId())) {
memberService.create(MemberMapper.toMemberCreateDto(profile));
fileService.uploadFile(ImgUrlConverter.toMultipartFile(profile.getImgUrl()));
UploadedFileDto file = fileService.uploadFile(ImgUrlConverter.toMultipartFile(profile.getImgUrl()));
memberService.create(MemberMapper.toMember(profile, file.getId()));
}
LoginResponse loginResponse = loginService.githubLogin(profile);
return ResponseEntity.ok().body(loginResponse);
Expand Down

0 comments on commit 6561d30

Please sign in to comment.