Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[🚧 μž‘μ—…μ€‘] λ¦¬μ†ŒμŠ€ μ‚­μ œ μ‹œ κ΄€λ ¨ 이미지 νŒŒμΌλ„ μ‚­μ œλ˜λ„λ‘ κ΅¬ν˜„ #711

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/main/java/balancetalk/file/domain/FileHandler.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package balancetalk.file.domain;

import balancetalk.file.domain.repository.FileRepository;
import io.awspring.cloud.s3.S3Operations;
import java.util.List;
import lombok.RequiredArgsConstructor;
Expand All @@ -14,6 +15,7 @@ public class FileHandler {

private final S3Client s3Client;
private final S3Operations s3Operations;
private final FileRepository fileRepository;

@Value("${spring.cloud.aws.s3.bucket}")
private String bucket;
Expand Down Expand Up @@ -57,4 +59,11 @@ private void updateFile(File file, String s3Key, Long resourceId, FileType fileT
file.updateResourceId(resourceId);
file.updateFileType(fileType);
}

public void deleteFiles(List<File> files) {
for (File file : files) {
s3Operations.deleteObject(bucket, file.getS3Key());
}
fileRepository.deleteAll(files);
}
Comment on lines +63 to +68
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

μ˜ˆμ™Έ μ²˜λ¦¬μ™€ νŠΈλžœμž­μ…˜ 관리가 ν•„μš”ν•©λ‹ˆλ‹€.

ν˜„μž¬ κ΅¬ν˜„μ—λŠ” λ‹€μŒκ³Ό 같은 κ°œμ„ μ΄ ν•„μš”ν•©λ‹ˆλ‹€:

  1. S3 μž‘μ—… μ‹€νŒ¨ μ‹œμ˜ μ˜ˆμ™Έ 처리
  2. S3와 λ°μ΄ν„°λ² μ΄μŠ€ μž‘μ—… κ°„μ˜ νŠΈλžœμž­μ…˜ 관리

λ‹€μŒκ³Ό 같이 κ°œμ„ ν•˜λŠ” 것을 μ œμ•ˆλ“œλ¦½λ‹ˆλ‹€:

+@Transactional
 public void deleteFiles(List<File> files) {
+    try {
         for (File file : files) {
-            s3Operations.deleteObject(bucket, file.getS3Key());
+            try {
+                s3Operations.deleteObject(bucket, file.getS3Key());
+            } catch (Exception e) {
+                throw new FileDeleteException("S3 파일 μ‚­μ œ 쀑 였λ₯˜ λ°œμƒ: " + file.getS3Key(), e);
+            }
         }
         fileRepository.deleteAll(files);
+    } catch (Exception e) {
+        throw new FileDeleteException("파일 μ‚­μ œ 쀑 였λ₯˜κ°€ λ°œμƒν–ˆμŠ΅λ‹ˆλ‹€", e);
+    }
 }

μΆ”κ°€λ‘œ μ˜ˆμ™Έ ν΄λž˜μŠ€λ„ 생성이 ν•„μš”ν•©λ‹ˆλ‹€:

public class FileDeleteException extends RuntimeException {
    public FileDeleteException(String message, Throwable cause) {
        super(message, cause);
    }
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package balancetalk.file.domain.repository;

import balancetalk.file.domain.File;
import balancetalk.file.domain.FileType;
import java.util.List;

Expand All @@ -8,4 +9,6 @@ public interface FileRepositoryCustom {
List<String> findImgUrlsByResourceIdAndFileType(Long resourceId, FileType fileType);

List<Long> findIdsByResourceIdAndFileType(Long resourceId, FileType fileType);

List<File> findAllByResourceIdAndFileType(Long resourceId, FileType fileType);
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,12 @@ public List<Long> findIdsByResourceIdAndFileType(Long resourceId, FileType fileT
.where(file.fileType.eq(fileType), file.resourceId.eq(resourceId))
.fetch();
}

@Override
public List<File> findAllByResourceIdAndFileType(Long resourceId, FileType fileType) {
return queryFactory.select(file)
.from(file)
.where(file.fileType.eq(fileType), file.resourceId.eq(resourceId))
.fetch();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,5 +95,14 @@ public void deleteTalkPick(Long talkPickId, ApiMember apiMember) {
Member member = apiMember.toMember(memberRepository);
TalkPick talkPick = member.getTalkPickById(talkPickId);
talkPickRepository.delete(talkPick);
deleteFiles(talkPickId);
}
Comment on lines +98 to +99
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

πŸ› οΈ Refactor suggestion

파일 μ‚­μ œ μ‹€νŒ¨ μ‹œ νŠΈλžœμž­μ…˜ 처리 κ°œμ„  ν•„μš”

파일 μ‚­μ œκ°€ μ‹€νŒ¨ν•˜λ”λΌλ„ TalkPick μ—”ν‹°ν‹°λŠ” 이미 μ‚­μ œλœ μƒνƒœκ°€ 될 수 μžˆμŠ΅λ‹ˆλ‹€. 파일 μ‹œμŠ€ν…œμ˜ 일관성을 μœ„ν•΄ λ‹€μŒκ³Ό 같은 κ°œμ„ μ„ κ³ λ €ν•΄λ³΄μ„Έμš”:

  1. νŒŒμΌμ„ λ¨Όμ € μ‚­μ œν•œ ν›„ μ—”ν‹°ν‹°λ₯Ό μ‚­μ œ
  2. 파일 μ‚­μ œ μ‹€νŒ¨ μ‹œ μ˜ˆμ™Έ 처리 μΆ”κ°€
@Transactional
public void deleteTalkPick(Long talkPickId, ApiMember apiMember) {
    Member member = apiMember.toMember(memberRepository);
    TalkPick talkPick = member.getTalkPickById(talkPickId);
+   deleteFiles(talkPickId);
    talkPickRepository.delete(talkPick);
-   deleteFiles(talkPickId);
}
πŸ“ Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
deleteFiles(talkPickId);
}
deleteFiles(talkPickId);
talkPickRepository.delete(talkPick);
}


private void deleteFiles(Long talkPickId) {
List<File> files = fileRepository.findAllByResourceIdAndFileType(talkPickId, TALK_PICK);
if (files.isEmpty()) {
return;
}
fileHandler.deleteFiles(files);
}
}