Skip to content

Commit 5bea2bd

Browse files
[EA3-126] refactor: 파일 업로드 유효성 및 GCS 메모리 효율 개선
1 parent 8bd45c4 commit 5bea2bd

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

src/main/java/grep/neogul_coder/global/utils/upload/AbstractFileManager.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ public FileUploadResponse upload(MultipartFile file, Long uploaderId, FileUsageT
3232
throw new FileUploadException(FileUploadErrorCode.FILE_EMPTY);
3333
}
3434

35+
String contentType = file.getContentType();
36+
if(contentType == null || !ALLOWED_TYPES.contains(contentType)) {
37+
throw new FileUploadException(FileUploadErrorCode.FILE_TYPE_INVALID);
38+
}
39+
3540
String originFileName = file.getOriginalFilename();
3641
if (originFileName == null) {
3742
throw new FileUploadException(FileUploadErrorCode.FILE_NAME_INVALID);
@@ -88,4 +93,9 @@ protected String generateRenameFileName(String originFileName) {
8893
protected String buildFullPath(String savePath, String renameFileName) {
8994
return savePath + "/" + renameFileName;
9095
}
96+
97+
// MIME 타입 허용 목록 (이미지 파일만 허용)
98+
protected static final List<String> ALLOWED_TYPES = List.of(
99+
"image/png", "image/jpeg", "image/jpg", "image/gif", "image/webp"
100+
);
91101
}

src/main/java/grep/neogul_coder/global/utils/upload/uploader/GcpFileUploader.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import grep.neogul_coder.global.utils.upload.AbstractFileManager;
99
import grep.neogul_coder.global.utils.upload.exception.FileUploadErrorCode;
1010
import java.io.IOException;
11+
import java.io.InputStream;
1112
import lombok.extern.slf4j.Slf4j;
1213
import org.springframework.beans.factory.annotation.Value;
1314
import org.springframework.context.annotation.Profile;
@@ -27,13 +28,13 @@ public class GcpFileUploader extends AbstractFileManager {
2728
// GCP Cloud Storage 에 파일을 업로드
2829
@Override
2930
protected void uploadFile(MultipartFile file, String fullPath) throws IOException {
30-
try {
31+
try (InputStream inputStream = file.getInputStream()) {
3132
Storage storage = StorageOptions.getDefaultInstance().getService();
3233
BlobId blobId = BlobId.of(bucket, fullPath);
3334
BlobInfo blobInfo = BlobInfo.newBuilder(blobId)
3435
.setContentType(file.getContentType())
3536
.build();
36-
storage.create(blobInfo, file.getBytes());
37+
storage.createFrom(blobInfo, inputStream);
3738
} catch (IOException e) {
3839
log.error("GCP 파일 업로드 실패 - 원본 파일명: {}", file.getOriginalFilename(), e);
3940
throw new FileUploadException(FileUploadErrorCode.GCP_UPLOAD_FAIL, e);

0 commit comments

Comments
 (0)