-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
10 changed files
with
199 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
src/main/java/kr/hs/dgsw/SOPO_server_v2/domain/file/controller/FileController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package kr.hs.dgsw.SOPO_server_v2.domain.file.controller; | ||
|
||
import kr.hs.dgsw.SOPO_server_v2.domain.file.dto.FileRes; | ||
import kr.hs.dgsw.SOPO_server_v2.domain.file.enums.FileCategory; | ||
import kr.hs.dgsw.SOPO_server_v2.domain.file.service.FileService; | ||
import kr.hs.dgsw.SOPO_server_v2.global.response.ResponseData; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.bind.annotation.RequestPart; | ||
import org.springframework.web.bind.annotation.RestController; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
import java.util.List; | ||
|
||
@RestController | ||
@RequestMapping("/file") | ||
@RequiredArgsConstructor | ||
public class FileController { | ||
private final FileService fileService; | ||
|
||
@PostMapping | ||
public ResponseData<List<FileRes>> fileUpload(@RequestParam FileCategory fileCategory, @RequestPart List<MultipartFile> fileList) { | ||
return fileService.fileUpload(fileCategory, fileList); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
src/main/java/kr/hs/dgsw/SOPO_server_v2/domain/file/dto/FileRes.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package kr.hs.dgsw.SOPO_server_v2.domain.file.dto; | ||
|
||
import kr.hs.dgsw.SOPO_server_v2.domain.file.entity.FileEntity; | ||
import lombok.Builder; | ||
|
||
@Builder | ||
public record FileRes ( | ||
String fileName, | ||
String fileUrl | ||
) { | ||
|
||
public static FileRes of(FileEntity file) { | ||
return new FileRes( | ||
file.getFileName(), | ||
file.getFileUrl() | ||
); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
src/main/java/kr/hs/dgsw/SOPO_server_v2/domain/file/enums/FileCategory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package kr.hs.dgsw.SOPO_server_v2.domain.file.enums; | ||
|
||
public enum FileCategory { | ||
BOARD, | ||
CONTEST, | ||
PROFILE | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/java/kr/hs/dgsw/SOPO_server_v2/domain/file/repository/FileRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package kr.hs.dgsw.SOPO_server_v2.domain.file.repository; | ||
|
||
import kr.hs.dgsw.SOPO_server_v2.domain.file.entity.FileEntity; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface FileRepository extends JpaRepository<FileEntity, Long> { | ||
} |
71 changes: 71 additions & 0 deletions
71
src/main/java/kr/hs/dgsw/SOPO_server_v2/domain/file/service/FileService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package kr.hs.dgsw.SOPO_server_v2.domain.file.service; | ||
|
||
import com.amazonaws.services.s3.AmazonS3; | ||
import com.amazonaws.services.s3.model.ObjectMetadata; | ||
import com.amazonaws.services.s3.model.PutObjectRequest; | ||
import kr.hs.dgsw.SOPO_server_v2.domain.file.dto.FileRes; | ||
import kr.hs.dgsw.SOPO_server_v2.domain.file.entity.FileEntity; | ||
import kr.hs.dgsw.SOPO_server_v2.domain.file.enums.FileCategory; | ||
import kr.hs.dgsw.SOPO_server_v2.domain.file.repository.FileRepository; | ||
import kr.hs.dgsw.SOPO_server_v2.global.response.ResponseData; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class FileService { | ||
|
||
@Value("${cloud.aws.s3.bucket}") | ||
private String bucket; | ||
|
||
private final AmazonS3 amazonS3; | ||
|
||
private final FileRepository fileRepository; | ||
|
||
@Transactional | ||
public ResponseData<List<FileRes>> fileUpload(FileCategory fileCategory, List<MultipartFile> fileList) { | ||
List<FileRes> fileLists = new ArrayList<>(); | ||
try { | ||
for (MultipartFile file : fileList) { | ||
String fileName = file.getOriginalFilename(); | ||
|
||
ObjectMetadata metadata = new ObjectMetadata(); | ||
metadata.setContentType(file.getContentType()); | ||
metadata.setContentLength(file.getSize()); | ||
|
||
// S3에 저장하는 코드 | ||
amazonS3.putObject( | ||
new PutObjectRequest(bucket, fileName, file.getInputStream(), metadata) | ||
); | ||
|
||
// FileRes 형식으로 만들기 | ||
FileRes oneFile = FileRes.builder() | ||
.fileName(fileName) | ||
.fileUrl(amazonS3.getUrl(bucket, fileName).toString()) | ||
.build(); | ||
|
||
|
||
fileLists.add(oneFile); | ||
|
||
// DB에 저장하는 코드 | ||
FileEntity fileEntity = FileEntity.builder() | ||
.fileName(fileName) | ||
.fileCategory(fileCategory) | ||
.fileUrl(amazonS3.getUrl(bucket, fileName).toString()) | ||
.build(); | ||
fileRepository.save(fileEntity); | ||
} | ||
} catch (IOException e) { | ||
throw new RuntimeException("Error uploading file to S3", e); | ||
} | ||
return ResponseData.of(HttpStatus.OK, "파일 업로드 완료", fileLists); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
src/main/java/kr/hs/dgsw/SOPO_server_v2/global/config/S3Config.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package kr.hs.dgsw.SOPO_server_v2.global.config; | ||
|
||
import com.amazonaws.auth.AWSStaticCredentialsProvider; | ||
import com.amazonaws.auth.BasicAWSCredentials; | ||
import com.amazonaws.services.s3.AmazonS3; | ||
import com.amazonaws.services.s3.AmazonS3ClientBuilder; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@Configuration | ||
public class S3Config { | ||
|
||
@Value("${cloud.aws.credentials.accessKey}") | ||
private String accessKey; | ||
|
||
@Value("${cloud.aws.credentials.secretKey}") | ||
private String secretKey; | ||
|
||
@Value("${cloud.aws.region.static}") | ||
private String region; | ||
|
||
@Bean | ||
public AmazonS3 amazonS3Client() { | ||
BasicAWSCredentials awsCredentials = new BasicAWSCredentials(accessKey, secretKey); | ||
return AmazonS3ClientBuilder.standard() | ||
.withRegion(region) | ||
.enablePathStyleAccess() | ||
.withCredentials(new AWSStaticCredentialsProvider(awsCredentials)) | ||
.build(); | ||
} | ||
} |