-
Notifications
You must be signed in to change notification settings - Fork 3
feat : 문제 이미지 기능 ( S3, 업로드 만 가능 ) #99
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
Merged
Merged
Changes from all commits
Commits
Show all changes
33 commits
Select commit
Hold shift + click to select a range
6fbabfb
feat : S3 의존성 추가 (초기세팅)
Kimminu7 b7c4a35
refactor : 호환 이슈로 S3 버전 변경
Kimminu7 55c5806
refactor, feat : s3 버킷 변경, config, uploader 추가
Kimminu7 4862eca
feat : prefix 관리를 위한 enum 추가
Kimminu7 ea5d02d
docs : 주석 추가
Kimminu7 b600d4f
feat : S3 업로드 실패 예외처리 적용 ( 커스텀 예외처리 )
Kimminu7 727390a
refactor : 엔티티 수정 ( 문제, 문제 이미지 ), 메소드 추가
Kimminu7 8675969
feat : 컨트롤러 , 서비스 이미지 기능 추가
Kimminu7 cbf38af
refactor : S3Upload log 추가 ( 업로드가 성공적으로 되는지 체크 )
Kimminu7 a30c4d6
refactor : 주석 수정
Kimminu7 fe0aa77
feat : 이미지 파일만 업로드 가능 ( MIME )
Kimminu7 992c083
refactor : ProblemImage 엔티티 삭제 -> Problem 컬럼 추가
Kimminu7 c832545
refactor : Problem 컬럼 추가 ( 총 제출 횟수, 정답 제출 횟수 )
Kimminu7 998d300
feat : S3 의존성 추가 (초기세팅)
Kimminu7 169f5e2
refactor : 호환 이슈로 S3 버전 변경
Kimminu7 c21b48a
refactor, feat : s3 버킷 변경, config, uploader 추가
Kimminu7 69135e7
feat : prefix 관리를 위한 enum 추가
Kimminu7 1f7ad95
docs : 주석 추가
Kimminu7 3f8fe5e
feat : S3 업로드 실패 예외처리 적용 ( 커스텀 예외처리 )
Kimminu7 895e99f
refactor : 엔티티 수정 ( 문제, 문제 이미지 ), 메소드 추가
Kimminu7 cf1e394
feat : 컨트롤러 , 서비스 이미지 기능 추가
Kimminu7 145ba95
refactor : S3Upload log 추가 ( 업로드가 성공적으로 되는지 체크 )
Kimminu7 e462e46
refactor : 주석 수정
Kimminu7 d420f56
feat : 이미지 파일만 업로드 가능 ( MIME )
Kimminu7 d2634ea
refactor : ProblemImage 엔티티 삭제 -> Problem 컬럼 추가
Kimminu7 3463157
refactor : Problem 컬럼 추가 ( 총 제출 횟수, 정답 제출 횟수 )
Kimminu7 7ebd0ff
Merge remote-tracking branch 'origin/feature/problemS3' into feature/…
Kimminu7 eabee5e
refactor : HTTP 상태코드 수정
Kimminu7 153ea22
refactor : addImage 메소드 유효성 검증 추가
Kimminu7 cd607a6
refactor : S3Uploader 예외처리 개선
Kimminu7 978fd60
Merge remote-tracking branch 'origin/dev' into feature/problemS3
Kimminu7 00cea6c
fix : CodetestApplicationTests 테스트 코드 클래스 제거
Kimminu7 1912347
refactor : 커스텀 예외처리 리팩토링
Kimminu7 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 0 additions & 26 deletions
26
src/main/java/org/ezcode/codetest/domain/problem/model/entity/ProblemImage.java
This file was deleted.
Oops, something went wrong.
4 changes: 0 additions & 4 deletions
4
src/main/java/org/ezcode/codetest/domain/problem/repository/ProblemImageRepository.java
This file was deleted.
Oops, something went wrong.
7 changes: 0 additions & 7 deletions
7
...ode/codetest/infrastructure/persistence/repository/problem/ProblemImageJpaRepository.java
This file was deleted.
Oops, something went wrong.
20 changes: 20 additions & 0 deletions
20
src/main/java/org/ezcode/codetest/infrastructure/s3/S3Directory.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| package org.ezcode.codetest.infrastructure.s3; | ||
|
|
||
| /** | ||
| * S3 prefix를 enum으로 관리 | ||
| * */ | ||
| public enum S3Directory { | ||
| PROBLEM("problem"), | ||
| PROFILE("profile"); | ||
|
|
||
| // 소문자로 패키지 관리 하기 위해 dir추가 | ||
| private final String dir; | ||
|
|
||
| S3Directory(String dir) { | ||
| this.dir = dir; | ||
| } | ||
|
|
||
| public String getDir() { | ||
| return dir; | ||
| } | ||
| } |
61 changes: 61 additions & 0 deletions
61
src/main/java/org/ezcode/codetest/infrastructure/s3/S3Uploader.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| package org.ezcode.codetest.infrastructure.s3; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.UUID; | ||
|
|
||
| import org.ezcode.codetest.infrastructure.s3.exception.S3Exception; | ||
| import org.ezcode.codetest.infrastructure.s3.exception.code.S3ExceptionCode; | ||
| import org.springframework.beans.factory.annotation.Value; | ||
| import org.springframework.stereotype.Component; | ||
| import org.springframework.web.multipart.MultipartFile; | ||
|
|
||
| import com.amazonaws.services.s3.AmazonS3; | ||
| import com.amazonaws.services.s3.model.AmazonS3Exception; | ||
| import com.amazonaws.services.s3.model.ObjectMetadata; | ||
|
|
||
| import lombok.RequiredArgsConstructor; | ||
| import lombok.extern.slf4j.Slf4j; | ||
|
|
||
| @Component | ||
| @RequiredArgsConstructor | ||
| @Slf4j | ||
| public class S3Uploader { | ||
|
|
||
| private final AmazonS3 amazonS3; | ||
|
|
||
| @Value("${cloud.aws.s3.bucket}") | ||
| private String bucket; | ||
|
|
||
| public String upload(MultipartFile multipartFile, String dirName) { | ||
| try { | ||
| // MIME 타입 검사 (png, jpeg, jpg, webp 만 가능) | ||
| String contentType = multipartFile.getContentType(); | ||
| if (contentType == null || !contentType.startsWith("image/")) { | ||
| throw new S3Exception(S3ExceptionCode.S3_INVALID_FILE_TYPE); | ||
| } | ||
|
|
||
| // S3 파일명 지정 ( 디렉토리/UUID-원본파일명 ) | ||
| String fileName = dirName + "/" + UUID.randomUUID() + "-" + multipartFile.getOriginalFilename(); | ||
|
|
||
| ObjectMetadata metadata = new ObjectMetadata(); | ||
| metadata.setContentLength(multipartFile.getSize()); | ||
| metadata.setContentType(contentType); | ||
|
|
||
| amazonS3.putObject(bucket, fileName, multipartFile.getInputStream(), metadata); | ||
| String result = amazonS3.getUrl(bucket, fileName).toString(); // 업로드 파일 URL로 변환 ( 문자열 ) | ||
| log.info("S3 버킷 이미지 업로드 완료 {}", result); | ||
|
|
||
| return result; | ||
|
|
||
| } catch (IOException e) { | ||
| log.error("S3 업로드 중 IO 오류 발생",e); | ||
| throw new S3Exception(S3ExceptionCode.S3_UPLOAD_FAILED); | ||
| } catch (AmazonS3Exception e) { | ||
| log.error("S3 서비스 오류 발생: {}", e.getErrorMessage(), e); | ||
| throw new S3Exception(S3ExceptionCode.S3_UPLOAD_FAILED); | ||
| } catch (Exception e) { | ||
| log.error("예상치 못한 업로드 오류 발생", e); | ||
| throw new S3Exception(S3ExceptionCode.S3_UPLOAD_FAILED); | ||
| } | ||
| } | ||
| } |
39 changes: 39 additions & 0 deletions
39
src/main/java/org/ezcode/codetest/infrastructure/s3/config/S3Config.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| package org.ezcode.codetest.infrastructure.s3.config; | ||
|
|
||
| import org.springframework.beans.factory.annotation.Value; | ||
| import org.springframework.context.annotation.Bean; | ||
| import org.springframework.context.annotation.Configuration; | ||
|
|
||
| import com.amazonaws.auth.AWSStaticCredentialsProvider; | ||
| import com.amazonaws.auth.BasicAWSCredentials; | ||
| import com.amazonaws.services.s3.AmazonS3; | ||
| import com.amazonaws.services.s3.AmazonS3ClientBuilder; | ||
|
|
||
| import lombok.RequiredArgsConstructor; | ||
|
|
||
| /** | ||
| * S3 연동 | ||
| * */ | ||
| @Configuration | ||
| @RequiredArgsConstructor | ||
|
|
||
| public class S3Config { | ||
|
|
||
| @Value("${cloud.aws.credentials.access-key}") | ||
| private String accessKey; | ||
|
|
||
| @Value("${cloud.aws.credentials.secret-key}") | ||
| private String secretKey; | ||
|
|
||
| @Value("${cloud.aws.region.static}") | ||
| private String region; | ||
|
|
||
| @Bean | ||
| public AmazonS3 amazonS3() { | ||
| BasicAWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey); | ||
| return AmazonS3ClientBuilder.standard() | ||
| .withRegion(region) | ||
| .withCredentials(new AWSStaticCredentialsProvider(credentials)) | ||
| .build(); | ||
| } | ||
| } |
24 changes: 24 additions & 0 deletions
24
src/main/java/org/ezcode/codetest/infrastructure/s3/exception/S3Exception.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| package org.ezcode.codetest.infrastructure.s3.exception; | ||
|
|
||
| import org.ezcode.codetest.common.base.exception.BaseException; | ||
| import org.ezcode.codetest.common.base.exception.ResponseCode; | ||
| import org.ezcode.codetest.infrastructure.s3.exception.code.S3ExceptionCode; | ||
| import org.springframework.http.HttpStatus; | ||
|
|
||
| import lombok.Getter; | ||
|
|
||
| @Getter | ||
| public class S3Exception extends BaseException { | ||
|
|
||
| private final ResponseCode responseCode; | ||
|
|
||
| private final HttpStatus httpStatus; | ||
|
|
||
| private final String message; | ||
|
|
||
| public S3Exception(S3ExceptionCode responseCode) { | ||
| this.responseCode = responseCode; | ||
| this.httpStatus = responseCode.getStatus(); | ||
| this.message = responseCode.getMessage(); | ||
| } | ||
Kimminu7 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
21 changes: 21 additions & 0 deletions
21
src/main/java/org/ezcode/codetest/infrastructure/s3/exception/code/S3ExceptionCode.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| package org.ezcode.codetest.infrastructure.s3.exception.code; | ||
|
|
||
| import org.ezcode.codetest.common.base.exception.ResponseCode; | ||
| import org.springframework.http.HttpStatus; | ||
|
|
||
| import lombok.Getter; | ||
| import lombok.RequiredArgsConstructor; | ||
|
|
||
| @Getter | ||
| @RequiredArgsConstructor | ||
| public enum S3ExceptionCode implements ResponseCode { | ||
|
|
||
| S3_UPLOAD_FAILED(false, HttpStatus.INTERNAL_SERVER_ERROR, "S3 이미지 업로드 중 오류가 발생했습니다."), | ||
| S3_INVALID_FILE_TYPE(false, HttpStatus.BAD_REQUEST, "이미지 파일만 업로드할 수 있습니다."); | ||
|
|
||
| private final boolean success; | ||
|
|
||
| private final HttpStatus status; | ||
|
|
||
| private final String message; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
트랜잭션 범위 내 외부 서비스 호출 검토 필요
S3 업로드가 트랜잭션 내에서 실행되고 있습니다. 이는 다음과 같은 문제를 야기할 수 있습니다.
문제점:
개선 방안:
🤖 Prompt for AI Agents
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
throw new S3Exception(S3ExceptionCode.S3_UPLOAD_FAILED); 이렇게 커스텀 예외처리 해보는게 좋을거같아 적용해봐
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Kimminu7 좋은 제안이네요! 커스텀 예외를 사용하면 일관성 있는 예외 처리가 가능하겠어요. 제안하신 방식으로 개선해보겠습니다:
이렇게 하면 S3Uploader 내부의 예외 처리와 동일한 패턴을 유지하면서 일관성 있는 예외 처리가 가능하겠네요!
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
안되는데? S3ExceptionCode.S3_UPLOAD_FAILED 생성자 생성하라고하네
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
@Kimminu7 어떤 부분이 안 되는지 구체적으로 알려주실 수 있을까요?
컴파일 에러인지, 실행 시 에러인지, 아니면 다른 문제인지 확인해보고 싶어요. 현재 코드베이스 구조를 더 자세히 살펴보고 정확한 해결책을 제공해드리겠습니다.
구체적인 에러 메시지나 문제 상황을 알려주시면 더 정확한 도움을 드릴 수 있어요!
🏁 Script executed:
Length of output: 3056
@Kimminu7 아, 문제를 찾았어요! 몇 가지 이슈가 있었네요:
log객체가 없는데 사용하려고 했어요다음과 같이 수정하면 됩니다:
또는 더 간단하게 S3ExceptionCode의 정보를 직접 사용할 수도 있어요:
이제 제대로 작동할 거예요!