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