Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading