Skip to content

Commit

Permalink
FileService, ScreenshotService 코드 수정
Browse files Browse the repository at this point in the history
S3Service 변경에 따른 코드 리팩토링 진행
  • Loading branch information
unanchoi committed Oct 21, 2023
1 parent cf0b6d7 commit 1710291
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ public class FileServiceImpl implements FileService {
@Transactional
public FileGetUploadPreSignedResponseDto getUploadPreSignedUrl(Long memberId, String fileName) {
String fileUploadPrefix = makeUploadPrefix(memberId);
PreSignedUrlVO presignedUrlInfo = s3Service.generatePreSignedUrl(fileUploadPrefix, fileName);
PreSignedUrlVO preSignedUrlInfo = s3Service.getUploadPreSignedUrl(fileUploadPrefix, fileName);

return FileGetUploadPreSignedResponseDto.of(presignedUrlInfo.getFileName(), fileName,
presignedUrlInfo.getPreSignedUrl(), presignedUrlInfo.getFileUploadDate());
return FileGetUploadPreSignedResponseDto.of(preSignedUrlInfo.getFileName(), fileName,
preSignedUrlInfo.getPreSignedUrl(), preSignedUrlInfo.getFileUploadDate());
}

@Override
Expand All @@ -56,9 +56,8 @@ public String createKey(Long memberId, String fileDate, String fileUUID, String
@Transactional
public void deleteFile(Long memberId, String fileOriginalName, String fileUploadDate, String fileUUID) {
Optional<File> file = fileRepository.findById(UUID.fromString(fileUUID));
if (file.isPresent()) {
fileRepository.delete(file.get());
} s3Service.deleteFile(createKey(memberId, fileUploadDate, fileUUID, fileOriginalName));
file.ifPresent(fileRepository::delete);
s3Service.deleteFile(createKey(memberId, fileUploadDate, fileUUID, fileOriginalName));
}

private String makeUploadPrefix(Long memberId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class ScreenshotController {
public ApiResponseDto<ScreenshotGetPreSignedResponseDto> createPresigned(Principal principal, @RequestParam String screenshotName
) {
Long memberId = MemberUtil.getMemberId(principal);
return ApiResponseDto.success(screenshotService.getPreSignedUrl(memberId, screenshotName));
return ApiResponseDto.success(screenshotService.getUploadPreSignedUrl(memberId, screenshotName));
}

@Operation(summary = "스크린샷 삭제 API")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

public interface ScreenshotService {

ScreenshotGetPreSignedResponseDto getPreSignedUrl(Long memberId, String screenshotName);
ScreenshotGetPreSignedResponseDto getUploadPreSignedUrl(Long memberId, String screenshotName);

String createKey(Long memberId, String screenshotDate, String screenshotUUID, String screenshotName);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,21 @@ public class ScreenshotServiceImpl implements ScreenshotService {
private final MemberRepository memberRepository;

@Override
@Transactional
public ScreenshotGetPreSignedResponseDto getPreSignedUrl(Long memberId, String screenshotName) {
public ScreenshotGetPreSignedResponseDto getUploadPreSignedUrl(Long memberId, String screenshotName) {
String screenshotUploadPrefix = makeUploadPrefix(memberId);
PreSignedUrlVO preSignedUrlInfo = s3Service.generatePreSignedUrl(screenshotUploadPrefix, screenshotName);
PreSignedUrlVO preSignedUrlInfo = s3Service.getUploadPreSignedUrl(screenshotUploadPrefix, screenshotName);

return ScreenshotGetPreSignedResponseDto.of(preSignedUrlInfo.getFileName(), preSignedUrlInfo.getPreSignedUrl(),
screenshotName, preSignedUrlInfo.getFileUploadDate());
}

@Override
@Transactional
public String findUrl(Long memberId, ScreenshotCreateRequestDto requestDto) {
return s3Service.findUrlByName(createKey(memberId, requestDto.getScreenshotUploadDate(), requestDto.getScreenshotUUID().toString()
, requestDto.getScreenshotName()));
}

@Override
@Transactional
public String createKey(Long memberId, String screenshotDate, String screenshotUUID, String screenshotName) {
String screenshotUploadPrefix = makeUploadPrefix(memberId);
return screenshotUploadPrefix + "/" + screenshotDate + "/" + screenshotUUID + screenshotName;
Expand All @@ -55,9 +52,8 @@ public String createKey(Long memberId, String screenshotDate, String screenshotU
@Transactional
public void deleteFile(Long memberId, String screenshotName, String screenshotUploadDate, String screenshotUUID) {
Optional<Screenshot> screenshot = screenshotRepository.findById(UUID.fromString(screenshotUUID));
if (screenshot.isPresent()) {
screenshotRepository.delete(screenshot.get());
} s3Service.deleteFile(createKey(memberId, screenshotUploadDate, screenshotUUID, screenshotName));
screenshot.ifPresent(screenshotRepository::delete);
s3Service.deleteFile(createKey(memberId, screenshotUploadDate, screenshotUUID, screenshotName));
}

private String makeUploadPrefix(Long memberId) {
Expand Down

0 comments on commit 1710291

Please sign in to comment.