Skip to content

Commit

Permalink
[REFACTOR] 파일 시스템 버전에 따른 로직 분리 (#271)
Browse files Browse the repository at this point in the history
* feat: 파일 버전 표시를 위한 클래스 추가

-  yml 파일의 값을 읽은 후, 로컬/프로덕션 버전 정보를 저장하는 클래스 작성

* feat: 파일 응답 클래스 구조 변경

- accessURI를 source로 변경
- 파일 버전 정보(environment) 추가 - LOCAL / PROD

* fix: 쿼리문 오류 수정

- PK 관련 에러 수정

* refactor: 버전 별 처리 방법 변경

- LOCAL 버전의 경우 파일을 다운로드 받아 BASE64로 인코딩한 문자열을 반환하도록 변경
- PROD 버전의 경우 파일에 접근할 수 있는 URI 반환
  • Loading branch information
SSung023 authored Jan 6, 2025
1 parent 2e54fb6 commit 3712faa
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 11 deletions.
19 changes: 19 additions & 0 deletions src/main/java/com/genius/gitget/global/file/dto/FileEnv.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@
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 {
private final AmazonS3 amazonS3;
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;
Expand All @@ -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
Expand Down
3 changes: 1 addition & 2 deletions src/main/resources/data.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 3712faa

Please sign in to comment.