|
| 1 | +package project.flipnote.image.service; |
| 2 | + |
| 3 | +import java.net.URL; |
| 4 | +import java.time.Duration; |
| 5 | +import java.util.Date; |
| 6 | + |
| 7 | +import org.springframework.beans.factory.annotation.Value; |
| 8 | +import org.springframework.stereotype.Service; |
| 9 | + |
| 10 | +import lombok.RequiredArgsConstructor; |
| 11 | +import project.flipnote.common.exception.BizException; |
| 12 | +import project.flipnote.common.security.dto.AuthPrinciple; |
| 13 | +import project.flipnote.image.exception.ImageErrorCode; |
| 14 | +import project.flipnote.image.model.ImageUploadResponseDto; |
| 15 | +import project.flipnote.user.entity.UserProfile; |
| 16 | +import project.flipnote.user.entity.UserStatus; |
| 17 | +import project.flipnote.user.exception.UserErrorCode; |
| 18 | +import project.flipnote.user.repository.UserProfileRepository; |
| 19 | +import software.amazon.awssdk.services.s3.S3Client; |
| 20 | +import software.amazon.awssdk.services.s3.model.HeadObjectRequest; |
| 21 | +import software.amazon.awssdk.services.s3.model.PutObjectRequest; |
| 22 | +import software.amazon.awssdk.services.s3.model.S3Exception; |
| 23 | +import software.amazon.awssdk.services.s3.presigner.S3Presigner; |
| 24 | +import software.amazon.awssdk.services.s3.presigner.model.PutObjectPresignRequest; |
| 25 | + |
| 26 | +@Service |
| 27 | +@RequiredArgsConstructor |
| 28 | +public class ImageUploadService { |
| 29 | + |
| 30 | + @Value("${cloud.s3.bucket}") |
| 31 | + private String bucket; |
| 32 | + |
| 33 | + private final S3Client s3Client; |
| 34 | + private final S3Presigner s3Presigner; |
| 35 | + private final UserProfileRepository userRepository; |
| 36 | + private static final int EXPIRE_MINUTES = 5; |
| 37 | + |
| 38 | + //유저 찾기 |
| 39 | + private void findUser(AuthPrinciple authPrinciple) { |
| 40 | + userRepository.findByIdAndStatus(authPrinciple.userId(), UserStatus.ACTIVE).orElseThrow( |
| 41 | + () -> new BizException(UserErrorCode.USER_NOT_FOUND) |
| 42 | + ); |
| 43 | + } |
| 44 | + |
| 45 | + //확장자 형식 찾기 |
| 46 | + private String getContentType(String fileName) { |
| 47 | + String extension = fileName.substring(fileName.lastIndexOf('.') + 1).toLowerCase(); |
| 48 | + |
| 49 | + return switch (extension) { |
| 50 | + case "jpg", "jpeg" -> "image/jpeg"; |
| 51 | + case "png" -> "image/png"; |
| 52 | + case "gif" -> "image/gif"; |
| 53 | + case "webp" -> "image/webp"; |
| 54 | + default -> "application/octet-stream"; |
| 55 | + }; |
| 56 | + } |
| 57 | + |
| 58 | + // presigned URL 생성 |
| 59 | + public ImageUploadResponseDto getPresignedUrl(AuthPrinciple authPrinciple, String fileName) { |
| 60 | + |
| 61 | + // 유저 찾기 |
| 62 | + findUser(authPrinciple); |
| 63 | + |
| 64 | + // S3에 동일한 파일명이 이미 존재하는지 확인 |
| 65 | + if (objectExists(fileName)) { |
| 66 | + throw new BizException(ImageErrorCode.CONFLICT_IMAGE); |
| 67 | + } |
| 68 | + |
| 69 | + // PutObjectRequest 정의 |
| 70 | + PutObjectRequest putObjectRequest = PutObjectRequest.builder() |
| 71 | + .bucket(bucket) |
| 72 | + .key(fileName) |
| 73 | + .contentType(getContentType(fileName)) |
| 74 | + .build(); |
| 75 | + |
| 76 | + // Presign 요청 생성 (5분 유효) |
| 77 | + PutObjectPresignRequest presignRequest = PutObjectPresignRequest.builder() |
| 78 | + .signatureDuration(Duration.ofMinutes(EXPIRE_MINUTES)) |
| 79 | + .putObjectRequest(putObjectRequest) |
| 80 | + .build(); |
| 81 | + |
| 82 | + URL presignedUrl = s3Presigner.presignPutObject(presignRequest).url(); |
| 83 | + |
| 84 | + return ImageUploadResponseDto.from(presignedUrl); |
| 85 | + } |
| 86 | + |
| 87 | + // 파일 존재 여부 확인 |
| 88 | + private boolean objectExists(String fileName) { |
| 89 | + try { |
| 90 | + s3Client.headObject( |
| 91 | + HeadObjectRequest.builder() |
| 92 | + .bucket(bucket) |
| 93 | + .key(fileName) |
| 94 | + .build() |
| 95 | + ); |
| 96 | + return true; |
| 97 | + } catch (S3Exception e) { |
| 98 | + // 404면 존재하지 않음 |
| 99 | + if (e.statusCode() == 404) { |
| 100 | + return false; |
| 101 | + } |
| 102 | + throw new BizException(ImageErrorCode.S3_SERVICE_ERROR); |
| 103 | + } |
| 104 | + } |
| 105 | +} |
0 commit comments