Skip to content

Commit

Permalink
Feat: 북마크 이미지 수정 API 구현 Merge
Browse files Browse the repository at this point in the history
Feat: 북마크 이미지 수정 API 구현
  • Loading branch information
Train0303 authored Nov 10, 2023
2 parents 55c5dcf + a4ddff1 commit e3864ce
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,16 @@ public ResponseEntity<?> updateBookmark(
return ResponseEntity.ok(ApiUtils.success(responseDto));
}

@PostMapping("/image/update/{bookmarkId}")
public ResponseEntity<?> updateBookmarkImage(
@RequestBody BookmarkRequestDto.BookmarkImageUpdateRequestDto dto,
@PathVariable Long bookmarkId,
@AuthenticationPrincipal CustomUserDetails userDetails
) {
bookmarkService.bookmarkImageUpdate(dto, bookmarkId, userDetails.getUser());
return ResponseEntity.ok(ApiUtils.success(null));
}

@PostMapping("/move")
public ResponseEntity<?> moveBookmark(
@RequestBody @Valid BookmarkRequestDto.BookmarkMoveRequestDto dto,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,22 @@ public Bookmark toEntity(Category category, String imageUrl) {
}

public record BookmarkUpdateRequestDto(
@NotBlank(message = "북마크 이름은 공백이 될 수 없습니다.")
@Size(max = 100, message = "북마크 이름은 최대 100자까지 가능합니다.")
String bookmarkName,
@Size(max = 200, message = "북마크 설명을 최대 200자까지 가능합니다.")
String description
) {
@Builder
public BookmarkUpdateRequestDto {
}
}

public record BookmarkImageUpdateRequestDto(
@NotBlank(message = "북마크 링크는 공백이 될 수 없습니다.")
String imageUrl
) {}

public record BookmarkMoveRequestDto(
List<Long> bookmarkIdList,
Long toCategoryId
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ public class Bookmark extends AuditingEntity {
public void moveCategory(Category category) {
this.category = category;
}
public void changeThumbnail(String imageUrl) {
this.bookmarkThumbnail = imageUrl;
}

@Builder
public Bookmark(Long bookmarkId, Category category, String bookmarkName, String bookmarkLink,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,20 @@ public BookmarkResponseDto.BookmarkUpdateResponseDto updateBookmark(
.build();
}

@Transactional
public void bookmarkImageUpdate(
BookmarkRequestDto.BookmarkImageUpdateRequestDto dto,
Long bookmarkId,
User user
) {
Bookmark bookmark = bookmarkJpaRepository.findByIdFetchJoinCategoryAndWorkspace(bookmarkId)
.orElseThrow(() -> new Exception404(BookmarkExceptionStatus.BOOKMARK_NOT_FOUND));

validUser(bookmark, user);
String imageUrl = s3ImageClient.base64ImageToS3(dto.imageUrl());
bookmark.changeThumbnail(imageUrl);
}

@Transactional
public void deleteBookmark(Long bookmarkId, User user) {
Bookmark bookmark = bookmarkJpaRepository.findByIdFetchJoinCategoryAndWorkspace(bookmarkId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,11 @@ public class S3ImageClient {
private final AmazonS3Client amazonS3Client;
private final JsoupUtils jsoupUtils;

public String base64ImageToS3(String base64Data, String bookmarkLink) {
public String base64ImageToS3(String base64Data) {

try {
// null 이면 메타데이터에서 썸네일 가져오기 시도, 실패하면 기본 이미지 url 반환
if (base64Data == null) {
return jsoupUtils.getImgUrl(bookmarkLink);
}

byte[] byteImage = java.util.Base64.getDecoder().decode(base64Data);


InputStream imageInputStream = getValidImageInputStream(byteImage);

// AWS S3 저장 로직
Expand All @@ -56,7 +50,14 @@ public String base64ImageToS3(String base64Data, String bookmarkLink) {
}
throw new Exception400(UtilExceptionStatus.NOT_BASE64_DATA);
}
}

public String base64ImageToS3(String base64Data, String bookmarkLink) {
if (base64Data == null) {
return jsoupUtils.getImgUrl(bookmarkLink);
}

return base64ImageToS3(base64Data);
}

private ByteArrayInputStream getValidImageInputStream(byte[] byteImage) {
Expand Down

0 comments on commit e3864ce

Please sign in to comment.