Skip to content

Commit

Permalink
fix: 스토리지 객체 유무 확인 로직 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
raymondanythings committed Aug 20, 2024
1 parent 03ab0f9 commit c69f638
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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); // "/" 이후부터 추출
}
}

0 comments on commit c69f638

Please sign in to comment.