Skip to content

Commit

Permalink
[Feat] AWS S3를 이용한 이미지파일 업로드 #57
Browse files Browse the repository at this point in the history
  • Loading branch information
eunki96 committed Aug 5, 2023
1 parent 87a1e25 commit 990ae5b
Show file tree
Hide file tree
Showing 15 changed files with 348 additions and 53 deletions.
9 changes: 7 additions & 2 deletions favor/src/main/java/com/favor/favor/gift/Gift.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
import com.favor.favor.common.enums.Category;
import com.favor.favor.common.enums.Emotion;
import com.favor.favor.common.TimeStamped;
import com.favor.favor.photo.Photo;
import com.favor.favor.photo.GiftPhoto;
import com.favor.favor.user.User;
import lombok.*;

import javax.persistence.*;
import javax.transaction.Transactional;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

@Entity
Expand Down Expand Up @@ -75,5 +76,9 @@ public void setFriendNoList(List<Long> friendNoList){
this.friendNoList = friendNoList;
}

// @ManyToOne(cascade = Cas)
@OneToMany(cascade = CascadeType.ALL)
private List<GiftPhoto> giftPhotoList = new ArrayList<>();
public void setGiftPhotoList(List<GiftPhoto> giftPhotoList){
this.giftPhotoList = giftPhotoList;
}
}
7 changes: 6 additions & 1 deletion favor/src/main/java/com/favor/favor/gift/GiftController.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import io.swagger.annotations.ApiResponses;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
Expand All @@ -22,7 +23,7 @@
@RestController
@RequestMapping("/gifts")
@RequiredArgsConstructor
@Log4j2
@Slf4j
public class GiftController {
private final GiftService giftService;

Expand Down Expand Up @@ -181,10 +182,14 @@ public ResponseEntity<DefaultResponseDto<Object>> deleteGift(

giftService.isExistingGiftNo(giftNo);


Gift gift = giftService.findGiftByGiftNo(giftNo);
log.info("[SYSTEM] giftService.findGiftByGiftNo(giftNo) 완료");
GiftResponseDto dto = giftService.returnDto(gift);
log.info("[SYSTEM] giftService.returnDto(gift) 완료");

giftService.deleteGift(giftNo);
log.info("[SYSTEM] giftService.deleteGift(giftNo) 완료");

return ResponseEntity.status(200)
.body(DefaultResponseDto.builder()
Expand Down
122 changes: 122 additions & 0 deletions favor/src/main/java/com/favor/favor/gift/GiftPhotoController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package com.favor.favor.gift;

import com.favor.favor.common.DefaultResponseDto;
import com.favor.favor.photo.GiftPhoto;
import com.favor.favor.photo.UserPhoto;
import com.favor.favor.user.User;
import com.favor.favor.user.UserPhotoService;
import com.favor.favor.user.UserResponseDto;
import com.favor.favor.user.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.transaction.Transactional;
import java.util.List;

@Api(tags = "Gift-Photo")
@RestController
@RequestMapping("/giftphotos")
@RequiredArgsConstructor
public class GiftPhotoController {
private final GiftPhotoService giftPhotoService;
private final GiftService giftService;

@ApiOperation(value = "선물 사진 추가")
@ApiResponses(value={
@ApiResponse(code = 201,
message = "GIFT_PHOTO_LIST_ADDED",
response = UserResponseDto.class),
@ApiResponse(code = 400,
message = "FIELD_REQUIRED / *_CHARACTER_INVALID / *_LENGTH_INVALID"),
@ApiResponse(code = 401,
message = "UNAUTHORIZED_USER"),
@ApiResponse(code = 404,
message = "GIFT_NOT_FOUND"),
@ApiResponse(code = 500,
message = "SERVER_ERROR")
})
@ResponseStatus(HttpStatus.CREATED)
@PostMapping
public ResponseEntity<DefaultResponseDto<Object>> addGiftPhotoList(
@ModelAttribute MultipartFile file,
Long giftNo
) {
giftService.isExistingGiftNo(giftNo);

Gift gift = giftPhotoService.addGiftPhoto(giftNo, file);

GiftResponseDto dto = giftService.returnDto(gift);

return ResponseEntity.status(201)
.body(DefaultResponseDto.builder()
.responseCode("GIFT_PHOTO_LIST_ADDED")
.responseMessage("선물 사진 추가")
.data(dto)
.build());
}

@ApiOperation("선물 사진 목록 조회")
@ApiResponses(value={
@ApiResponse(code = 200,
message = "GIFT_PHOTO_LIST_FOUND",
response = UserResponseDto.class),
@ApiResponse(code = 401,
message = "UNAUTHORIZED_USER"),
@ApiResponse(code = 404,
message = "GIFT_NOT_FOUND | FILE_NOT_FOUND"),
@ApiResponse(code = 500,
message = "SERVER_ERROR")
})
@ResponseStatus(HttpStatus.OK)
@Transactional
@GetMapping
public ResponseEntity<DefaultResponseDto<Object>> getUserProfilePhoto(Long giftNo) {
Gift gift = giftService.findGiftByGiftNo(giftNo);

List<GiftPhoto> dto = giftPhotoService.getGiftPhotoList(giftNo);

return ResponseEntity.status(200)
.body(DefaultResponseDto.builder()
.responseCode("GIFT_PHOTO_LIST_FOUND")
.responseMessage("선물 사진 목록 조회 완료")
.data(dto)
.build());
}

@ApiOperation("선물 사진 삭제")
@ApiResponses(value={
@ApiResponse(code = 200,
message = "GIFT_PHOTO_DELETED",
response = UserResponseDto.class),
@ApiResponse(code = 401,
message = "UNAUTHORIZED_USER"),
@ApiResponse(code = 404,
message = "GIFT_NOT_FOUND | FILE_NOT_FOUND"),
@ApiResponse(code = 500,
message = "SERVER_ERROR")
})
@ResponseStatus(HttpStatus.OK)
@Transactional
@DeleteMapping
public ResponseEntity<DefaultResponseDto<Object>> deleteUserProfilePhoto(
Long giftNo, String fileUrl) {

Gift gift = giftPhotoService.deleteGiftPhoto(giftNo, fileUrl);
GiftResponseDto dto = new GiftResponseDto(gift);

return ResponseEntity.status(200)
.body(DefaultResponseDto.builder()
.responseCode("GIFT_PHOTO_DELETED")
.responseMessage("선물 사진 삭제 완료")
.data(dto)
.build());
}
}
110 changes: 110 additions & 0 deletions favor/src/main/java/com/favor/favor/gift/GiftPhotoService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package com.favor.favor.gift;

import com.favor.favor.exception.CustomException;
import com.favor.favor.photo.GiftPhoto;
import com.favor.favor.photo.PhotoService;
import com.favor.favor.user.User;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.transaction.Transactional;
import java.net.URL;
import java.util.LinkedList;
import java.util.List;
import java.util.UUID;

import static com.favor.favor.exception.ExceptionCode.FILE_NOT_FOUND;
import static com.favor.favor.exception.ExceptionCode.SERVER_ERROR;

@Service
@RequiredArgsConstructor
public class GiftPhotoService {
private final GiftRepository giftRepository;
private final GiftService giftService;
private final PhotoService photoService;

//선물 사진 목록에 사진 추가
@Transactional
public Gift addGiftPhoto(Long giftNo, MultipartFile file){
Gift gift = giftService.findGiftByGiftNo(giftNo);
List<GiftPhoto> giftPhotoList = gift.getGiftPhotoList();

String fileName = file.getOriginalFilename();
String storedFileName = getGiftFileName(fileName);

String giftPhotoUrl = photoService.uploadFileToS3(storedFileName, file);

try{
GiftPhoto giftPhoto = GiftPhoto.builder()
.photoUrl(giftPhotoUrl)
.build();

giftPhotoList.add(giftPhoto);

}catch(RuntimeException e){
throw new CustomException(e, SERVER_ERROR);
}

// for(MultipartFile file : fileList){
// String fileName = file.getOriginalFilename();
// String storedFileName = getGiftFileName(fileName);
//
// String giftPhotoUrl = photoService.uploadFileToS3(storedFileName, file);
// try{
// GiftPhoto giftPhoto = GiftPhoto.builder()
// .photoUrl(giftPhotoUrl)
// .build();
//
// giftPhotoList.add(giftPhoto);
//
// }catch(RuntimeException e){
// throw new CustomException(e, SERVER_ERROR);
// }
// }
gift.setGiftPhotoList(giftPhotoList);
return giftRepository.save(gift);
}

//선물 사진 목록 조회
public List<GiftPhoto> getGiftPhotoList(Long giftNo){
return giftService.findGiftByGiftNo(giftNo).getGiftPhotoList();
}

//선물 사진 삭제
public Gift deleteGiftPhoto(Long giftNo, String photoUrl){
Gift gift = giftService.findGiftByGiftNo(giftNo);
List<GiftPhoto> giftPhotoList = gift.getGiftPhotoList();

GiftPhoto temp = null;

for(GiftPhoto giftPhoto : giftPhotoList){
if(photoUrl.equals(giftPhoto.getPhotoUrl())){
temp = giftPhoto;
String fileName = extractGiftPhotoFileName(photoUrl);
photoService.deleteFileFromS3(fileName);
break;
}
}
giftPhotoList.remove(temp);

return giftRepository.save(gift);
}

//선물 사진 이름 생성
public String getGiftFileName(String fileName){
return "gift/" + UUID.randomUUID()+ fileName.substring(fileName.lastIndexOf('.'));
}

//선물 사진 이름 추출
public static String extractGiftPhotoFileName(String photoUrl) {
String fileName;
try {
fileName = photoUrl.substring(photoUrl.indexOf("gift/"));
} catch (Exception e) {
throw new CustomException(e, FILE_NOT_FOUND);
}

return fileName;
}
}
2 changes: 2 additions & 0 deletions favor/src/main/java/com/favor/favor/gift/GiftRequestDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import javax.transaction.Transactional;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

@Getter
Expand Down Expand Up @@ -53,6 +54,7 @@ public Gift toEntity(User user, LocalDate giftDate){
.isGiven(isGiven)
.user(user)
.friendNoList(friendNoList)
.giftPhotoList(new ArrayList<>())
.build();
}
}
4 changes: 4 additions & 0 deletions favor/src/main/java/com/favor/favor/gift/GiftResponseDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.favor.favor.common.enums.Category;
import com.favor.favor.common.enums.Emotion;
import com.favor.favor.friend.FriendResponseDto;
import com.favor.favor.photo.GiftPhoto;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
Expand Down Expand Up @@ -33,6 +34,7 @@ public class GiftResponseDto {
private List<FriendResponseDto> friendList;
private LocalDateTime createdAt;
private LocalDateTime modifiedAt;
private List<GiftPhoto> giftPhotoList;


@Builder
Expand All @@ -50,6 +52,7 @@ public GiftResponseDto(Gift gift){
this.friendList = new ArrayList<>();
this.createdAt = gift.getCreatedAt();
this.modifiedAt = gift.getModifiedAt();
this.giftPhotoList = gift.getGiftPhotoList();
log.info("[GiftResponseDto] 실행 완료");
}
@Builder
Expand All @@ -67,6 +70,7 @@ public GiftResponseDto(Gift gift, List<FriendResponseDto> friendList){
this.friendList = friendList;
this.createdAt = gift.getCreatedAt();
this.modifiedAt = gift.getModifiedAt();
this.giftPhotoList = gift.getGiftPhotoList();
log.info("[GiftResponseDto] 실행 완료");
}
}
Loading

0 comments on commit 990ae5b

Please sign in to comment.