Skip to content

Commit

Permalink
Merge pull request #76 from kookmin-sw/feat/familarity2
Browse files Browse the repository at this point in the history
feat: 친밀도에 따른 이미지 변화 기능 구현
  • Loading branch information
imjanghyeok authored May 17, 2024
2 parents f095f44 + ed7d946 commit 5d0a7cf
Show file tree
Hide file tree
Showing 18 changed files with 564 additions and 68 deletions.
3 changes: 3 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ dependencies {

// JAXB deprecated warning
implementation 'javax.xml.bind:jaxb-api:2.3.0'

// aop
implementation 'org.springframework.boot:spring-boot-starter-aop'
}

tasks.named('test') {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package capstone.facefriend;

import capstone.facefriend.resume.domain.Resume;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
Expand Down
109 changes: 90 additions & 19 deletions src/main/java/capstone/facefriend/bucket/BucketService.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
package capstone.facefriend.bucket;


import capstone.facefriend.chat.domain.ChatRoom;
import capstone.facefriend.chat.domain.ChatRoomMember;
import capstone.facefriend.chat.exception.ChatException;
import capstone.facefriend.chat.exception.ChatExceptionType;
import capstone.facefriend.chat.repository.ChatMessageRepository;
import capstone.facefriend.chat.repository.ChatRoomMemberRepository;
import capstone.facefriend.chat.repository.ChatRoomRepository;
import capstone.facefriend.member.domain.member.Member;
import capstone.facefriend.member.domain.member.MemberRepository;
import capstone.facefriend.member.exception.member.MemberException;
Expand All @@ -24,26 +31,33 @@
import java.util.List;
import java.util.UUID;

import static capstone.facefriend.chat.exception.ChatExceptionType.*;

@Transactional
@Slf4j
@RequiredArgsConstructor
@Service
public class BucketService {

@Value("${spring.cloud.aws.s3.bucket}")
private String bucketName;
private String BUCKET_NAME;

@Value("${spring.cloud.aws.s3.default-faceInfo-s3url}")
private String defaultFaceInfoS3url;
private String DEFAULT_FACE_INFO_S3_URL;
@Value("${spring.cloud.aws.s3.origin-postfix}")
private String originPostfix;
private String ORIGIN_POSTFIX;
@Value("${spring.cloud.aws.s3.generated-postfix}")
private String generatedPostfix;
private String GENERATED_POSTFIX;
@Value("${spring.cloud.aws.s3.resume-postfix}")
private String resumePostfix;
private String RESUME_POSTFIX;
@Value("${spring.cloud.aws.s3.generated-by-level-postfix}")
private String GENERATED_BY_LEVEL_POSTFIX;

private final AmazonS3 amazonS3;
private final MemberRepository memberRepository;
private final ChatMessageRepository chatMessageRepository;
private final ChatRoomRepository chatRoomRepository;
private final ChatRoomMemberRepository chatRoomMemberRepository;

// FaceInfo : origin 업로드 & generated 업로드
public List<String> uploadOriginAndGenerated(
Expand All @@ -56,37 +70,38 @@ public List<String> uploadOriginAndGenerated(
originMetadata.setContentLength(origin.getInputStream().available());
originMetadata.setContentType(origin.getContentType());

String originObjectName = UUID.randomUUID() + originPostfix;
String originObjectName = UUID.randomUUID() + ORIGIN_POSTFIX;
amazonS3.putObject(
new PutObjectRequest(
bucketName,
BUCKET_NAME,
originObjectName,
origin.getInputStream(), // origin
originMetadata
).withCannedAcl(CannedAccessControlList.PublicRead)
);
String originS3url = amazonS3.getUrl(bucketName, originObjectName).toString();
String originS3url = amazonS3.getUrl(BUCKET_NAME, originObjectName).toString();

/** upload generated to s3 */
// set metadata
ObjectMetadata generatedMetadata = new ObjectMetadata();
generatedMetadata.setContentLength(generated.getInputStream().available());
generatedMetadata.setContentType(generatedMetadata.getContentType());

String generatedObjectName = UUID.randomUUID() + generatedPostfix;
String generatedObjectName = UUID.randomUUID() + GENERATED_POSTFIX;
amazonS3.putObject(
new PutObjectRequest(
bucketName,
BUCKET_NAME,
generatedObjectName,
generated.getInputStream(), // generated
generatedMetadata
).withCannedAcl(CannedAccessControlList.PublicRead)
);
String generatedS3url = amazonS3.getUrl(bucketName, generatedObjectName).toString();
String generatedS3url = amazonS3.getUrl(BUCKET_NAME, generatedObjectName).toString();

return List.of(originS3url, generatedS3url);
}


// FaceInfo : origin 수정 -> generated 수정
public List<String> updateOriginAndGenerated(
MultipartFile origin,
Expand All @@ -98,7 +113,7 @@ public List<String> updateOriginAndGenerated(
String originS3url = member.getFaceInfo().getOriginS3url();
String generatedS3url = member.getFaceInfo().getGeneratedS3url();

if (originS3url.equals(defaultFaceInfoS3url) || generatedS3url.equals(defaultFaceInfoS3url)) {
if (originS3url.equals(DEFAULT_FACE_INFO_S3_URL) || generatedS3url.equals(DEFAULT_FACE_INFO_S3_URL)) {
return uploadOriginAndGenerated(origin, generated);
}

Expand All @@ -114,13 +129,13 @@ public String deleteOriginAndGenerated(

String originS3url = member.getFaceInfo().getOriginS3url();
String originObjectName = originS3url.substring(originS3url.lastIndexOf("/") + 1);
amazonS3.deleteObject(new DeleteObjectRequest(bucketName, originObjectName));
amazonS3.deleteObject(new DeleteObjectRequest(BUCKET_NAME, originObjectName));

String generatedS3url = member.getFaceInfo().getGeneratedS3url();
String generatedObjectName = generatedS3url.substring(generatedS3url.lastIndexOf("/") + 1);
amazonS3.deleteObject(new DeleteObjectRequest(bucketName, generatedObjectName));
amazonS3.deleteObject(new DeleteObjectRequest(BUCKET_NAME, generatedObjectName));

return defaultFaceInfoS3url;
return DEFAULT_FACE_INFO_S3_URL;
}


Expand All @@ -137,17 +152,17 @@ public List<String> uploadResumeImages(
metadata.setContentLength(image.getInputStream().available());
metadata.setContentType(image.getContentType());

String imageObjectName = UUID.randomUUID() + resumePostfix;
String imageObjectName = UUID.randomUUID() + RESUME_POSTFIX;

amazonS3.putObject(
new PutObjectRequest(
bucketName,
BUCKET_NAME,
imageObjectName,
image.getInputStream(),
metadata
).withCannedAcl(CannedAccessControlList.PublicRead)
);
resumeImageS3urls.add(amazonS3.getUrl(bucketName, imageObjectName).toString());
resumeImageS3urls.add(amazonS3.getUrl(BUCKET_NAME, imageObjectName).toString());
}
}
return resumeImageS3urls;
Expand All @@ -170,10 +185,66 @@ public void deleteResumeImages(

for (String resumeImageS3url : resumeImageS3urls) {
String resumeImageObjectName = resumeImageS3url.substring(resumeImageS3url.lastIndexOf("/") + 1);
amazonS3.deleteObject(new DeleteObjectRequest(bucketName, resumeImageObjectName));
amazonS3.deleteObject(new DeleteObjectRequest(BUCKET_NAME, resumeImageObjectName));
}
}


// FaceInfoByLevel : generatedByLevel 업로드
public String uploadGeneratedByLevel(
MultipartFile generatedByLevel
) throws IOException {
// set metadata
ObjectMetadata generatedByLevelMetaData = new ObjectMetadata();
generatedByLevelMetaData.setContentLength(generatedByLevel.getInputStream().available());
generatedByLevelMetaData.setContentType(generatedByLevel.getContentType());

String generatedByLevelObjectName = UUID.randomUUID() + GENERATED_BY_LEVEL_POSTFIX;
amazonS3.putObject(
new PutObjectRequest(
BUCKET_NAME,
generatedByLevelObjectName,
generatedByLevel.getInputStream(), // origin
generatedByLevelMetaData
).withCannedAcl(CannedAccessControlList.PublicRead)
);
return amazonS3.getUrl(BUCKET_NAME, generatedByLevelObjectName).toString();
}

// FaceInfoByLevel : generatedByLevel 수정 = generatedByLevel 삭제 후 업로드
public String updateGeneratedByLevel(
ByteArrayMultipartFile generatedByLevel,
Long roomId
) throws IOException {
deleteGeneratedByLevel(roomId);
return uploadGeneratedByLevel(generatedByLevel);
}

// FaceInfo : generatedByLevel 삭제
public void deleteGeneratedByLevel(
Long roomId // 삭제 요청된 방
) {
ChatRoom chatRoom = findChatRoomByRoomId(roomId);
ChatRoomMember chatRoomMember = findChatRoomMemberByChatRoom(chatRoom);

String senderGeneratedByLevelS3url = chatRoomMember.getSenderFaceInfoByLevel().getGeneratedByLevelS3url();
String receiverGeneratedByLevelS3url = chatRoomMember.getReceiverFaceInfoByLevel().getGeneratedByLevelS3url();

String senderGeneratedByLevelObjectName = senderGeneratedByLevelS3url.substring(senderGeneratedByLevelS3url.lastIndexOf("/") + 1);
String receiverGeneratedByLevelObjectName = receiverGeneratedByLevelS3url.substring(receiverGeneratedByLevelS3url.lastIndexOf("/") + 1);

amazonS3.deleteObject(new DeleteObjectRequest(BUCKET_NAME, senderGeneratedByLevelObjectName));
amazonS3.deleteObject(new DeleteObjectRequest(BUCKET_NAME, receiverGeneratedByLevelObjectName));
}

private ChatRoomMember findChatRoomMemberByChatRoom(ChatRoom chatRoom) {
return chatRoomMemberRepository.findChatRoomMemberByChatRoom(chatRoom).orElseThrow(() -> new ChatException(NOT_FOUND_CHAT_ROOM_MEMBER));
}

private ChatRoom findChatRoomByRoomId(Long roomId) {
return chatRoomRepository.findById(roomId).orElseThrow(() -> new ChatException(NOT_FOUND_CHAT_ROOM));
}

private Member findMemberById(Long memberId) {
return memberRepository.findById(memberId)
.orElseThrow(() -> new MemberException(MemberExceptionType.NOT_FOUND));
Expand Down
Loading

0 comments on commit 5d0a7cf

Please sign in to comment.