From 3712faa52d0d492acd8369744a38ad56009b86e8 Mon Sep 17 00:00:00 2001 From: HEY <50323157+SSung023@users.noreply.github.com> Date: Mon, 6 Jan 2025 11:59:26 +0900 Subject: [PATCH] =?UTF-8?q?[REFACTOR]=20=ED=8C=8C=EC=9D=BC=20=EC=8B=9C?= =?UTF-8?q?=EC=8A=A4=ED=85=9C=20=EB=B2=84=EC=A0=84=EC=97=90=20=EB=94=B0?= =?UTF-8?q?=EB=A5=B8=20=EB=A1=9C=EC=A7=81=20=EB=B6=84=EB=A6=AC=20(#271)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat: 파일 버전 표시를 위한 클래스 추가 - yml 파일의 값을 읽은 후, 로컬/프로덕션 버전 정보를 저장하는 클래스 작성 * feat: 파일 응답 클래스 구조 변경 - accessURI를 source로 변경 - 파일 버전 정보(environment) 추가 - LOCAL / PROD * fix: 쿼리문 오류 수정 - PK 관련 에러 수정 * refactor: 버전 별 처리 방법 변경 - LOCAL 버전의 경우 파일을 다운로드 받아 BASE64로 인코딩한 문자열을 반환하도록 변경 - PROD 버전의 경우 파일에 접근할 수 있는 URI 반환 --- .../gitget/global/file/dto/FileEnv.java | 19 +++++++++++++++++++ .../gitget/global/file/dto/FileResponse.java | 7 ++++--- .../global/file/service/LocalFileService.java | 11 ++++++++++- .../global/file/service/S3FileService.java | 5 ++--- src/main/resources/data.sql | 3 +-- .../gitget/global/file/domain/FilesTest.java | 4 ++-- 6 files changed, 38 insertions(+), 11 deletions(-) create mode 100644 src/main/java/com/genius/gitget/global/file/dto/FileEnv.java diff --git a/src/main/java/com/genius/gitget/global/file/dto/FileEnv.java b/src/main/java/com/genius/gitget/global/file/dto/FileEnv.java new file mode 100644 index 00000000..b8a7d667 --- /dev/null +++ b/src/main/java/com/genius/gitget/global/file/dto/FileEnv.java @@ -0,0 +1,19 @@ +package com.genius.gitget.global.file.dto; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.core.env.Environment; +import org.springframework.stereotype.Component; + +@Component +public class FileEnv { + private static Environment environment; + + @Autowired + public FileEnv(Environment env) { + environment = env; + } + + public static String getFileEnvironment() { + return environment.getProperty("file.mode").toUpperCase(); + } +} \ No newline at end of file diff --git a/src/main/java/com/genius/gitget/global/file/dto/FileResponse.java b/src/main/java/com/genius/gitget/global/file/dto/FileResponse.java index a5ac6f09..793b51c4 100644 --- a/src/main/java/com/genius/gitget/global/file/dto/FileResponse.java +++ b/src/main/java/com/genius/gitget/global/file/dto/FileResponse.java @@ -2,13 +2,14 @@ public record FileResponse( Long fileId, - String accessURI) { + String source, + String environment) { public static FileResponse createExistFile(Long filesId, String accessURI) { - return new FileResponse(filesId, accessURI); + return new FileResponse(filesId, accessURI, FileEnv.getFileEnvironment()); } public static FileResponse createNotExistFile() { - return new FileResponse(0L, ""); + return new FileResponse(0L, "", FileEnv.getFileEnvironment()); } } diff --git a/src/main/java/com/genius/gitget/global/file/service/LocalFileService.java b/src/main/java/com/genius/gitget/global/file/service/LocalFileService.java index 9c5fe69d..56298525 100644 --- a/src/main/java/com/genius/gitget/global/file/service/LocalFileService.java +++ b/src/main/java/com/genius/gitget/global/file/service/LocalFileService.java @@ -13,8 +13,11 @@ import com.genius.gitget.global.util.exception.BusinessException; import java.io.File; import java.io.IOException; +import java.nio.charset.StandardCharsets; import java.nio.file.StandardCopyOption; +import java.util.Base64; import org.springframework.beans.factory.annotation.Value; +import org.springframework.core.io.UrlResource; import org.springframework.web.multipart.MultipartFile; public class LocalFileService implements FileService { @@ -45,7 +48,13 @@ public FileDTO upload(MultipartFile multipartFile, FileType fileType) { @Override public String getFileAccessURI(Files files) { - return files.getFileURI(); + try { + UrlResource urlResource = new UrlResource("file:" + files.getFileURI()); + byte[] encode = Base64.getEncoder().encode(urlResource.getContentAsByteArray()); + return new String(encode, StandardCharsets.UTF_8); + } catch (IOException e) { + return ""; + } } @Override diff --git a/src/main/java/com/genius/gitget/global/file/service/S3FileService.java b/src/main/java/com/genius/gitget/global/file/service/S3FileService.java index dccba2af..44ad4201 100644 --- a/src/main/java/com/genius/gitget/global/file/service/S3FileService.java +++ b/src/main/java/com/genius/gitget/global/file/service/S3FileService.java @@ -13,7 +13,6 @@ import com.genius.gitget.global.file.dto.UpdateDTO; import com.genius.gitget.global.util.exception.BusinessException; import java.io.IOException; -import java.net.URL; import org.springframework.web.multipart.MultipartFile; public class S3FileService implements FileService { @@ -21,6 +20,7 @@ public class S3FileService implements FileService { private final FileUtil fileUtil; private final String bucket; private final String cloudFrontDomain; + public S3FileService(AmazonS3 amazonS3, FileUtil fileUtil, String bucket, String cloudFrontDomain) { this.amazonS3 = amazonS3; this.fileUtil = fileUtil; @@ -30,8 +30,7 @@ public S3FileService(AmazonS3 amazonS3, FileUtil fileUtil, String bucket, String @Override public String getFileAccessURI(Files files) { - URL url = amazonS3.getUrl(bucket, files.getFileURI()); - return cloudFrontDomain + url.getFile(); + return cloudFrontDomain + files.getFileURI(); } @Override diff --git a/src/main/resources/data.sql b/src/main/resources/data.sql index d865b7e3..a101bb92 100644 --- a/src/main/resources/data.sql +++ b/src/main/resources/data.sql @@ -44,9 +44,8 @@ FROM (SELECT 1 AS identifier, 'PROFILE_FRAME') AS new_items WHERE (SELECT COUNT(*) FROM item) < 3; -INSERT INTO users (`point`, user_id, nickname, information, identifier, tags, provider_info, `role`) +INSERT INTO users (`point`, nickname, information, identifier, tags, provider_info, `role`) SELECT 0, - 104, 'Guest', '자기 소개입니다.', 'Guest', diff --git a/src/test/java/com/genius/gitget/global/file/domain/FilesTest.java b/src/test/java/com/genius/gitget/global/file/domain/FilesTest.java index 4abda17a..f45070d2 100644 --- a/src/test/java/com/genius/gitget/global/file/domain/FilesTest.java +++ b/src/test/java/com/genius/gitget/global/file/domain/FilesTest.java @@ -20,13 +20,13 @@ public void should_updateFiles_when_passUpdateDTO() { .fileType(FileType.INSTANCE) .originalFilename("originalFilename") .savedFilename("savedFilename") - .fileURI("accessURI") + .fileURI("source") .build(); UpdateDTO updateDTO = UpdateDTO.builder() .savedFilename("new savedFilename") .originalFilename("new originalFilename") - .fileURI("new accessURI") + .fileURI("new source") .build(); //when