|
| 1 | +package com.example.muneoserver.domain.file.service; |
| 2 | + |
| 3 | +import com.example.muneoserver.domain.file.domain.FileUploadType; |
| 4 | +import com.example.muneoserver.domain.file.dto.PresignedUrlRequest; |
| 5 | +import com.example.muneoserver.domain.file.dto.PresignedUrlResponse; |
| 6 | +import com.example.muneoserver.global.config.s3.S3Properties; |
| 7 | +import com.example.muneoserver.global.error.exception.CommonException; |
| 8 | +import com.example.muneoserver.global.error.exception.ErrorCode; |
| 9 | +import com.example.muneoserver.global.security.auth.AuthUser; |
| 10 | +import java.time.Instant; |
| 11 | +import java.util.Map; |
| 12 | +import java.util.UUID; |
| 13 | +import lombok.RequiredArgsConstructor; |
| 14 | +import org.springframework.util.StringUtils; |
| 15 | +import org.springframework.stereotype.Service; |
| 16 | +import software.amazon.awssdk.core.exception.SdkException; |
| 17 | +import software.amazon.awssdk.services.s3.model.PutObjectRequest; |
| 18 | +import software.amazon.awssdk.services.s3.presigner.S3Presigner; |
| 19 | +import software.amazon.awssdk.services.s3.presigner.model.PresignedPutObjectRequest; |
| 20 | +import software.amazon.awssdk.services.s3.presigner.model.PutObjectPresignRequest; |
| 21 | + |
| 22 | +@Service |
| 23 | +@RequiredArgsConstructor |
| 24 | +public class FileServiceImpl implements FileService { |
| 25 | + |
| 26 | + private final S3Presigner s3Presigner; |
| 27 | + private final S3Properties s3Properties; |
| 28 | + |
| 29 | + @Override |
| 30 | + public PresignedUrlResponse issuePresignedUrl(AuthUser authUser, PresignedUrlRequest request) { |
| 31 | + validateAuthenticated(authUser); |
| 32 | + validateS3Configuration(); |
| 33 | + validateRequest(request); |
| 34 | + |
| 35 | + FileUploadType uploadType = FileUploadType.from(request.type()); |
| 36 | + String extension = extractExtension(request.filename()); |
| 37 | + String key = buildKey(uploadType, authUser.id(), extension); |
| 38 | + Instant expiresAt = Instant.now().plus(s3Properties.presignedUrlExpiration()); |
| 39 | + |
| 40 | + try { |
| 41 | + PutObjectRequest putObjectRequest = PutObjectRequest.builder() |
| 42 | + .bucket(s3Properties.bucket()) |
| 43 | + .key(key) |
| 44 | + .contentType(request.contentType()) |
| 45 | + .build(); |
| 46 | + |
| 47 | + PutObjectPresignRequest presignRequest = PutObjectPresignRequest.builder() |
| 48 | + .signatureDuration(s3Properties.presignedUrlExpiration()) |
| 49 | + .putObjectRequest(putObjectRequest) |
| 50 | + .build(); |
| 51 | + |
| 52 | + PresignedPutObjectRequest presignedRequest = s3Presigner.presignPutObject(presignRequest); |
| 53 | + |
| 54 | + return new PresignedUrlResponse( |
| 55 | + uploadType.requestType(), |
| 56 | + key, |
| 57 | + presignedRequest.url().toString(), |
| 58 | + buildFileUrl(key), |
| 59 | + "PUT", |
| 60 | + request.contentType(), |
| 61 | + expiresAt |
| 62 | + ); |
| 63 | + } catch (SdkException e) { |
| 64 | + throw new CommonException(ErrorCode.FILE_UPLOAD_PRESIGNED_URL_GENERATION_FAILED); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + private String buildKey(FileUploadType uploadType, Long userId, String extension) { |
| 69 | + String directory = switch (uploadType) { |
| 70 | + case ESTIMATE -> s3Properties.estimateDirectory(); |
| 71 | + case POST -> s3Properties.postDirectory(); |
| 72 | + }; |
| 73 | + |
| 74 | + return directory + "/" + userId + "/" + UUID.randomUUID() + "." + extension; |
| 75 | + } |
| 76 | + |
| 77 | + private String buildFileUrl(String key) { |
| 78 | + return "https://" + s3Properties.bucket() + ".s3." + s3Properties.region() + ".amazonaws.com/" + key; |
| 79 | + } |
| 80 | + |
| 81 | + private void validateAuthenticated(AuthUser authUser) { |
| 82 | + if (authUser == null) { |
| 83 | + throw new CommonException(ErrorCode.UNAUTHORIZED); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + private void validateS3Configuration() { |
| 88 | + if (isBlank(s3Properties.bucket()) |
| 89 | + || isBlank(s3Properties.region()) |
| 90 | + || isBlank(s3Properties.estimateDirectory()) |
| 91 | + || isBlank(s3Properties.postDirectory()) |
| 92 | + || s3Properties.presignedUrlExpiration() == null |
| 93 | + || s3Properties.presignedUrlExpiration().isZero() |
| 94 | + || s3Properties.presignedUrlExpiration().isNegative()) { |
| 95 | + throw new CommonException(ErrorCode.FILE_UPLOAD_CONFIGURATION_INVALID); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + private void validateRequest(PresignedUrlRequest request) { |
| 100 | + if (request == null) { |
| 101 | + throw new CommonException(ErrorCode.INVALID_REQUEST_FORMAT); |
| 102 | + } |
| 103 | + if (isBlank(request.type())) { |
| 104 | + throw new CommonException(ErrorCode.VALIDATION_FAILED, Map.of("type", "업로드 타입은 필수입니다.")); |
| 105 | + } |
| 106 | + if (isBlank(request.filename())) { |
| 107 | + throw new CommonException(ErrorCode.VALIDATION_FAILED, Map.of("filename", "파일명은 필수입니다.")); |
| 108 | + } |
| 109 | + if (isBlank(request.contentType())) { |
| 110 | + throw new CommonException(ErrorCode.VALIDATION_FAILED, Map.of("contentType", "Content-Type은 필수입니다.")); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + private String extractExtension(String filename) { |
| 115 | + String normalizedFilename = extractFilename(filename); |
| 116 | + String extension = StringUtils.getFilenameExtension(normalizedFilename); |
| 117 | + |
| 118 | + if (!StringUtils.hasText(extension)) { |
| 119 | + throw new CommonException(ErrorCode.VALIDATION_FAILED, Map.of("filename", "파일명에 확장자가 필요합니다.")); |
| 120 | + } |
| 121 | + |
| 122 | + return extension; |
| 123 | + } |
| 124 | + |
| 125 | + private String extractFilename(String filename) { |
| 126 | + String normalized = filename.trim().replace("\\", "/"); |
| 127 | + int lastSlashIndex = normalized.lastIndexOf('/'); |
| 128 | + |
| 129 | + if (lastSlashIndex >= 0) { |
| 130 | + normalized = normalized.substring(lastSlashIndex + 1); |
| 131 | + } |
| 132 | + |
| 133 | + if (!StringUtils.hasText(normalized)) { |
| 134 | + throw new CommonException(ErrorCode.VALIDATION_FAILED, Map.of("filename", "파일명은 필수입니다.")); |
| 135 | + } |
| 136 | + |
| 137 | + return normalized; |
| 138 | + } |
| 139 | + |
| 140 | + private boolean isBlank(String value) { |
| 141 | + return value == null || value.isBlank(); |
| 142 | + } |
| 143 | +} |
0 commit comments