diff --git a/layer-api/src/main/java/org/layer/domain/space/service/SpaceService.java b/layer-api/src/main/java/org/layer/domain/space/service/SpaceService.java index bd9af3d1..444ff730 100644 --- a/layer-api/src/main/java/org/layer/domain/space/service/SpaceService.java +++ b/layer-api/src/main/java/org/layer/domain/space/service/SpaceService.java @@ -5,6 +5,7 @@ import lombok.extern.slf4j.Slf4j; import org.layer.common.dto.Meta; import org.layer.domain.actionItem.repository.ActionItemRepository; +import org.layer.domain.external.ncp.service.NcpService; import org.layer.domain.retrospect.repository.RetrospectRepository; import org.layer.domain.space.controller.dto.SpaceRequest; import org.layer.domain.space.controller.dto.SpaceResponse; @@ -31,6 +32,7 @@ @Slf4j @Transactional(readOnly = true) public class SpaceService { + private final NcpService ncpService; private final SpaceRepository spaceRepository; private final MemberSpaceRelationRepository memberSpaceRelationRepository; private final ActionItemRepository actionItemRepository; @@ -55,8 +57,9 @@ public SpaceResponse.SpacePage getSpaceListFromMemberId(Long memberId, SpaceRequ @Transactional public Long createSpace(Long memberId, SpaceRequest.CreateSpaceRequest createSpaceRequest) { - - + if (createSpaceRequest.bannerUrl() != null) { + ncpService.checkObjectExistOrThrow(createSpaceRequest.bannerUrl()); + } var newSpace = spaceRepository.save(createSpaceRequest.toEntity(memberId)); var memberSpaceRelation = MemberSpaceRelation.builder().memberId(memberId).space(newSpace).build(); diff --git a/layer-external/src/main/java/org/layer/domain/external/ncp/service/NcpService.java b/layer-external/src/main/java/org/layer/domain/external/ncp/service/NcpService.java index 49b7265b..f81cdf55 100644 --- a/layer-external/src/main/java/org/layer/domain/external/ncp/service/NcpService.java +++ b/layer-external/src/main/java/org/layer/domain/external/ncp/service/NcpService.java @@ -9,12 +9,15 @@ import lombok.extern.slf4j.Slf4j; import org.layer.domain.external.ncp.dto.NcpResponse; import org.layer.domain.external.ncp.enums.ImageDomain; +import org.layer.domain.external.ncp.exception.ExternalException; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import java.util.Date; import java.util.UUID; +import static org.layer.common.exception.ExternalExceptionType.OBJECT_INVALID_ERROR; + @Service @Slf4j @RequiredArgsConstructor @@ -37,6 +40,15 @@ public NcpResponse.PresignedResult getPreSignedUrl(Long memberId, ImageDomain im ); } + public void checkObjectExistOrThrow(String url) { + + String objectKey = extractObjectKey(url); + boolean isExist = amazonS3Client.doesObjectExist(bucket, objectKey); + if (!isExist) { + throw new ExternalException(OBJECT_INVALID_ERROR); + } + } + private GeneratePresignedUrlRequest getGeneratePreSignedUrlRequest(String fileName) { GeneratePresignedUrlRequest generatePresignedUrlRequest = new GeneratePresignedUrlRequest(bucket, fileName) @@ -57,4 +69,13 @@ private Date getPreSignedUrlExpiration() { expiration.setTime(expTimeMillis); return expiration; } + + private String extractObjectKey(String url) { + String expectedPrefix = "https://layer-bucket.kr.object.ncloudstorage.com"; + + if (!url.startsWith(expectedPrefix)) { + throw new ExternalException(OBJECT_INVALID_ERROR); + } + return url.substring(expectedPrefix.length() + 1); // "/" 이후부터 추출 + } }