Skip to content

Commit

Permalink
Merge pull request #20 from Sopo2023/feat/#1
Browse files Browse the repository at this point in the history
Feat/#1
  • Loading branch information
GayeongKimm authored Jul 26, 2024
2 parents 2ec4ab1 + 550d1c3 commit 55533a8
Show file tree
Hide file tree
Showing 10 changed files with 199 additions and 9 deletions.
19 changes: 13 additions & 6 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,16 @@ jobs:
docker rmi ${{ secrets.DOCKER_USERNAME }}/sopo_v2 || true
docker pull ${{ secrets.DOCKER_USERNAME }}/sopo_v2
docker run -d -p 8080:8080 --name sopo_v2 ${{ secrets.DOCKER_USERNAME }}/sopo_v2
docker stop redis || true
docker rm redis || true
docker rmi redis || true
docker pull redis:latest
docker run -d --name redis -p 6379:6379 redis:latest
mkdir -p /home/ubuntu/app
cd /home/ubuntu/app
# 기존 docker-compose.yml 파일을 덮어쓰거나 생성
echo "${{ secrets.DOCKER_COMPOSE }}" > docker-compose.yml

# Docker Compose 명령어 실행
docker-compose pull
docker-compose up -d

# 사용하지 않는 Docker 데이터 정리 (선택 사항)
docker system prune -f
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import kr.hs.dgsw.SOPO_server_v2.domain.board.dto.BoardUpdateReq;
import kr.hs.dgsw.SOPO_server_v2.domain.file.entity.FileEntity;
import kr.hs.dgsw.SOPO_server_v2.domain.member.entity.MemberEntity;
import kr.hs.dgsw.SOPO_server_v2.global.common.entity.BaseTimeEntity;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;
Expand All @@ -24,7 +25,7 @@
@Table(name = "tbl_board")
@NoArgsConstructor
@SuperBuilder
public class BoardEntity {
public class BoardEntity extends BaseTimeEntity {

// 게시물 아이디
@Id
Expand Down Expand Up @@ -58,4 +59,7 @@ public void update(BoardUpdateReq updateReq) {
this.boardContent = updateReq.boardContent();
}

public void likeUpdate(int boardLikeCount) {
this.boardLikeCount += boardLikeCount;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import kr.hs.dgsw.SOPO_server_v2.domain.contest.enums.ContestState;
import kr.hs.dgsw.SOPO_server_v2.domain.file.entity.FileEntity;
import kr.hs.dgsw.SOPO_server_v2.domain.member.entity.MemberEntity;
import kr.hs.dgsw.SOPO_server_v2.global.common.entity.BaseTimeEntity;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;
Expand All @@ -28,7 +29,7 @@
@Table(name = "tbl_contest")
@NoArgsConstructor
@SuperBuilder
public class ContestEntity {
public class ContestEntity extends BaseTimeEntity {

// 대회 아이디
@Id
Expand Down Expand Up @@ -80,4 +81,9 @@ public void update(ContestUpdateReq updateReq) {
this.contestPerson = updateReq.contestPerson();
this.contestDateTime = updateReq.contestDateTime();
}

public void likeUpdate(int contestLikeCount) {
this.contestLikeCount += contestLikeCount;
}

}
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);
}
}
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()
);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,27 @@

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;
import kr.hs.dgsw.SOPO_server_v2.domain.board.entity.BoardEntity;
import kr.hs.dgsw.SOPO_server_v2.domain.contest.entity.ContestEntity;
import kr.hs.dgsw.SOPO_server_v2.domain.file.enums.FileCategory;
import kr.hs.dgsw.SOPO_server_v2.domain.member.entity.MemberEntity;
import kr.hs.dgsw.SOPO_server_v2.global.common.entity.BaseTimeEntity;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;

@Getter
@Entity
@Table(name = "tbl_file")
@NoArgsConstructor
public class FileEntity {
@SuperBuilder
public class FileEntity extends BaseTimeEntity {

// 파일 아이디
@Id
Expand All @@ -31,6 +37,10 @@ public class FileEntity {
@Column(name = "file_url")
private String fileUrl;

// 파일 카테고리
@Enumerated(EnumType.STRING)
private FileCategory fileCategory;

// 게시물 아이디
@ManyToOne
@JoinColumn(name = "board_id")
Expand Down
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
}
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> {
}
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);
}
}
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();
}
}

0 comments on commit 55533a8

Please sign in to comment.