Skip to content

Commit

Permalink
이미지
Browse files Browse the repository at this point in the history
  • Loading branch information
Johyunik committed Dec 5, 2023
1 parent e180722 commit f710cdb
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -139,16 +139,16 @@ public String insertBoard(@RequestHeader("decodedToken") String nickName, @Reque
// 게시글 수정
@PatchMapping(value = "/edit/{boardId}", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
@ResponseBody
public List<String> patchBoard(
public boolean patchBoard(
@RequestHeader("decodedToken") String nickName,
@PathVariable int boardId,
@RequestPart("board") String boardJson,
@RequestPart(value = "images", required = false) MultipartFile[] images
) throws UnsupportedEncodingException, JsonProcessingException {

String decodedNickname = new String(Base64.getDecoder().decode(nickName), "UTF-8");
List<String> newImageUrls = boardService.patchBoard(boardId, boardJson, images, decodedNickname);
return newImageUrls;
boardService.patchBoard(boardId, boardJson, images, decodedNickname);
return true;
}

// 게시글 삭제
Expand Down
5 changes: 1 addition & 4 deletions src/main/java/com/weatherfit/board/service/BoardService.java
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public BoardEntity insertBoard(BoardEntity board) {

// 게시글 수정
@Transactional
public List<String> patchBoard(int boardId, String boardJson, MultipartFile[] images, String nickName) {
public void patchBoard(int boardId, String boardJson, MultipartFile[] images, String nickName) {
Optional<BoardEntity> optionalBoard = Optional.ofNullable(boardRepository.findById(boardId));

BoardEntity originalBoard = optionalBoard.orElseThrow(() -> new IllegalArgumentException("해당 게시글이 존재하지 않습니다. id=" + boardId));
Expand All @@ -171,10 +171,8 @@ public List<String> patchBoard(int boardId, String boardJson, MultipartFile[] im
}
originalBoard.getImages().clear();

List<String> newImageUrls = new ArrayList<>();
for (MultipartFile image : images) {
String imageUrl = imageService.saveImage(image);
newImageUrls.add(imageUrl);
String fileName = imageUrl.substring(imageUrl.lastIndexOf("/") + 1);

if (!imageRepository.existsByImageUrl(imageUrl)) {
Expand Down Expand Up @@ -210,7 +208,6 @@ public List<String> patchBoard(int boardId, String boardJson, MultipartFile[] im
// 카프카 전송
kafkaTemplate.send("category", 1, "category", afterJoiendString);
kafkaTemplate.send("hashtag", 1, "hashtag", afterJoiendString2);
return newImageUrls;
}


Expand Down
14 changes: 3 additions & 11 deletions src/main/java/com/weatherfit/board/service/ImageService.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,31 +26,27 @@ public class ImageService {


public String saveImage(MultipartFile file) {
String fileUrl = null;
try {
String originalFilename = file.getOriginalFilename();

LocalDateTime now = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
String formattedNow = now.format(formatter);
String fileName = formattedNow + "_weatherfit_" + originalFilename;
fileUrl = "https://" + bucketName + ".s3.amazonaws.com/" + fileName;
String fileUrl = "https://" + bucketName + ".s3.amazonaws.com/" + fileName;

if (!amazonS3Client.doesObjectExist(bucketName, fileName)) {
ObjectMetadata metadata = new ObjectMetadata();
metadata.setContentType(file.getContentType());
metadata.setContentLength(file.getSize());
amazonS3Client.putObject(bucketName, fileName, file.getInputStream(), metadata);
}

return fileUrl;
} catch (Exception e) {
e.printStackTrace();
// if (fileUrl != null) {
// deleteImageByUrl(fileUrl);
// }
throw new RuntimeException("Failed to upload image to S3", e);
}

return fileUrl;
}


Expand All @@ -60,10 +56,6 @@ public void deleteImage(ImageEntity imageEntity) {
amazonS3Client.deleteObject(bucketName, imageUrl);
}

// public void deleteImageByUrl(String imageUrl) {
// String fileName = imageUrl.substring(imageUrl.lastIndexOf("/") + 1);
// amazonS3Client.deleteObject(bucketName, fileName);
// }
}


Expand Down

0 comments on commit f710cdb

Please sign in to comment.