Skip to content

Commit

Permalink
[feat] 파일업로드 기능 구현 (공통기능) (#26)
Browse files Browse the repository at this point in the history
* docs : 최초 실행시 Sample Data 넣는 방법 추가

* style : 오탈자수정

* feat : Naver Object Storage Properties기능 구현
- Notion 설정 방법 추가
- ObjectStorage 설정값 추가
- NaverObjectStorageProperties Bean 생성 구현

* chore : Server Port 분리
- Local, Dev모두 8080인것 확인하여 변경
- local : 8080, dev : 80번포트 변경

* chore : 파일관련 의존성 및 설정 추가
- bucket-name 추가
    - 파일 관리시 Bucket-Name필요하나 누락으로 인한 추가
    - Properties 속성 추가
- Dependency 의존성 추가
    - AWS-SDK추가 : ObjectStorage 파일 Management
    - Apache Common IO 추가 : 유틸리티 사용을 하고자 추가

* feat : File사용 관련 Common Exception 생성
- Upload Exception
- Delete Exception

* feat : 파일 업로드 기능 구현완료
- FileDeleteException 불필요 의존성 제거

- application.yml 불필요 속성 삭제

- FileIO작업관련 Exception
    - Enum추가
    - Exception 추N

- File Upload관련 예제 추가
    - NaverController, upload-form.html

- ObjectStorage기능 구현
    - NaverObjectStorageConfig Bean 구현
    - NaverObjectStorageUsageType Enum 구현
        - Type을 통한 분류
    - NaverObjectStorageUtil 공통 파일업로드 기능 구현

* refactor : 오탈자 및 누락 속성 추가
  • Loading branch information
donsonioc2010 authored Sep 16, 2023
1 parent bc3dbde commit 98d5c64
Show file tree
Hide file tree
Showing 16 changed files with 384 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Api/src/main/java/picasso/server/api/HomeController.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
public class HomeController {
@GetMapping("/")
public String index() {
return "/index";
return "index";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package picasso.server.api.naver_test;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import picasso.server.common.properties.NaverObjectStorageProperties;
import picasso.server.common.util.NaverObjectStorageUsageType;
import picasso.server.common.util.NaverObjectStorageUtil;

import java.util.ArrayList;
import java.util.List;
/*
TODO : 기능개발 완료이후 삭제해야 할 파일
*/

@Slf4j
@RequiredArgsConstructor
@Controller
@RequestMapping("/naver")
public class NaverController {
private final NaverObjectStorageProperties naverObjectStorageProperties;
private final NaverObjectStorageUtil naverObjectStorageUtil;

@GetMapping("/properties-test")
@ResponseBody
public ResponseEntity<String> naverTest() {
return ResponseEntity.ok(
naverObjectStorageProperties.getEndPoint() + " " +
naverObjectStorageProperties.getRegionName()
);
}

@GetMapping("/file-upload-test-form")
public String uploadTest() {
return "test/upload-form";
}

@PostMapping("/file-upload-test")
@ResponseBody
public ResponseEntity fileUploadTest(
@RequestPart(value = "profile", required = false) MultipartFile profile,
@RequestPart(value = "paint", required = false) MultipartFile paint
) {
List<String> filePath = new ArrayList<>();
if (!profile.isEmpty())
filePath.add(
naverObjectStorageUtil.storageFileUpload(
NaverObjectStorageUsageType.PROFILE, profile
)
);
if (!paint.isEmpty())
filePath.add(
naverObjectStorageUtil.storageFileUpload(
NaverObjectStorageUsageType.PAINT, paint
)
);
if (filePath.isEmpty())
filePath.add("업로드한 파일이 없음");

return ResponseEntity.ok(filePath);
}

}
11 changes: 6 additions & 5 deletions Api/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ spring:
group:
local:
- infra-local
- commons-local
- common-local
- domain-local
dev:
- infra-dev
Expand All @@ -18,9 +18,6 @@ spring:
static-path-pattern: static/**
servlet:
multipart:
file-size-threshold: 1MB
# TODO : MultiPart 활용시 경로 변경 필요
location: ~/template-project/upload
max-file-size: 20MB
max-request-size: 200MB
web:
Expand All @@ -31,7 +28,6 @@ spring:


server:
port: 8080
shutdown: graceful
servlet:
session:
Expand All @@ -45,6 +41,8 @@ spring:
devtools:
livereload:
enabled: true
server:
port: 8080


test-message : local-test
Expand All @@ -53,5 +51,8 @@ spring:
config:
activate:
on-profile: dev
server:
port: 80

test-message : dev-test

20 changes: 20 additions & 0 deletions Api/src/main/resources/templates/test/upload-form.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<!DOCTYPE html>
<html
lang="ko"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
>
<!-- TODO : 기능개발 완료이후 삭제해야 할 파일 -->
<head th:replace="~{fragment/config :: configFragment}"></head>
<body>
<h1>Upload Test Page</h1>
<form action='/naver/file-upload-test' method='post' enctype='multipart/form-data'>
업로드 대상 파일 (프로필) : <input type='file' name="profile" /><br>
업로드 대상 파일 (미술품게시판) : <input type="file" name="paint" /><br>
<button>등록</button>
</form>
</body>
<script>
</script>
</html>
6 changes: 6 additions & 0 deletions Common/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,10 @@ dependencies {

api 'org.springframework.boot:spring-boot-starter-aop'
api 'org.springframework.boot:spring-boot-starter-validation'

//File
api 'commons-io:commons-io:2.13.0'

// Amazon AWS Java SDK s3
api 'com.amazonaws:aws-java-sdk-s3:1.12.530'
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@

import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import picasso.server.common.properties.NaverObjectStorageProperties;

@Configuration
@EnableConfigurationProperties({
})
@EnableConfigurationProperties(
{
NaverObjectStorageProperties.class
}
)
public class ConfigurationPropertiesConfig {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package picasso.server.common.config;

import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.client.builder.AwsClientBuilder;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import picasso.server.common.properties.NaverObjectStorageProperties;

@Slf4j
@RequiredArgsConstructor
@Configuration
public class NaverObjectStorageConfig {
private final NaverObjectStorageProperties naverObjectStorageProperties;

@Bean
public AmazonS3 storageObject() {
log.info("Create NaverObjectStorageConfig Bean");
return AmazonS3ClientBuilder.standard()
.withEndpointConfiguration(this.getEndpointConfig())
.withCredentials(this.getCredentialsProvier())
.build();
}

private AwsClientBuilder.EndpointConfiguration getEndpointConfig() {
log.info(
"Create EndPoint Object >>> EndPoint : {}, RegionName : {}",
this.naverObjectStorageProperties.getEndPoint(),
this.naverObjectStorageProperties.getRegionName()
);

return new AwsClientBuilder.EndpointConfiguration(
this.naverObjectStorageProperties.getEndPoint(),
this.naverObjectStorageProperties.getRegionName()
);
}

private AWSStaticCredentialsProvider getCredentialsProvier() {
log.info(
"Create CredentialsProvider >>> AccessKey : {}, SecretKey : {}",
this.naverObjectStorageProperties.getAccessKey(),
this.naverObjectStorageProperties.getSecretKey()
);

return new AWSStaticCredentialsProvider(
new BasicAWSCredentials(
naverObjectStorageProperties.getAccessKey(),
naverObjectStorageProperties.getSecretKey()
)
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package picasso.server.common.exception;

import static picasso.server.common.exception.GlobalException.FILE_DELETE_ERROR;

public class FileDeleteException extends BaseException {

public static final BaseException EXCEPTION = new FileDeleteException();

private FileDeleteException() {
super(FILE_DELETE_ERROR);
}

}

Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package picasso.server.common.exception;

import static picasso.server.common.exception.GlobalException.FILE_IO_ERROR;

public class FileIOException extends BaseException {

public static final BaseException EXCEPTION = new FileIOException();

private FileIOException() {
super(FILE_IO_ERROR);
}

}

Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package picasso.server.common.exception;

import static picasso.server.common.exception.GlobalException.FILE_UPLOAD_ERROR;

public class FileUploadException extends BaseException {

public static final BaseException EXCEPTION = new FileUploadException();

private FileUploadException() {
super(FILE_UPLOAD_ERROR);
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ public enum GlobalException implements BaseErrorCode {
METHOD_ARGUMENT_ERROR(BAD_REQUEST.value(), "메서드 인자가 유효하지 않거나 @Valid를 통과하지 못하여 발생하는 예외입니다."),
INTERNAL_SERVER_ERRORS(INTERNAL_SERVER_ERROR.value(), "서버 내부 오류입니다."),
DATE_FORMAT_ERROR(BAD_REQUEST.value(), "날짜 형식을 확인해주세요."),
FILE_UPLOAD_ERROR(INTERNAL_SERVER_ERROR.value(), "파일 업로드 중 오류가 발생하였습니다"),
FILE_DELETE_ERROR(INTERNAL_SERVER_ERROR.value(), "파일 삭제 중 오류가 발생하였습니다"),
FILE_IO_ERROR(INTERNAL_SERVER_ERROR.value(), "파일 변환 중 오류가 발생하였습니다")
;

private final Integer statusCode;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package picasso.server.common.properties;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.ToString;
import org.springframework.boot.context.properties.ConfigurationProperties;

@Getter
@ToString
@AllArgsConstructor
@ConfigurationProperties(prefix = "naver.storage")
public class NaverObjectStorageProperties {

private String endPoint;
private String regionName;
private String accessKey;
private String secretKey;
private String bucketName;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package picasso.server.common.util;

import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor
public enum NaverObjectStorageUsageType {
PROFILE("profile")
, PAINT("paint");


private String path;
}
Loading

0 comments on commit 98d5c64

Please sign in to comment.