-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #36 from closeup-raffle/feature/create-raffle-product
[FEAT] 래플 생성하기
- Loading branch information
Showing
10 changed files
with
224 additions
and
18 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
2 changes: 1 addition & 1 deletion
2
...a/farmSystem/closeUp/config/S3Config.java → ...armSystem/closeUp/config/s3/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
71 changes: 71 additions & 0 deletions
71
src/main/java/farmSystem/closeUp/config/s3/S3Uploader.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 farmSystem.closeUp.config.s3; | ||
|
||
import com.amazonaws.services.s3.AmazonS3Client; | ||
import com.amazonaws.services.s3.model.CannedAccessControlList; | ||
import com.amazonaws.services.s3.model.PutObjectRequest; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
import java.io.File; | ||
import java.io.FileOutputStream; | ||
import java.io.IOException; | ||
import java.util.Optional; | ||
|
||
@Slf4j | ||
@RequiredArgsConstructor | ||
@Component | ||
public class S3Uploader { | ||
private final AmazonS3Client amazonS3Client; | ||
|
||
@Value("${cloud.aws.s3.bucket}") | ||
private String bucket; | ||
|
||
// MultipartFile을 전달받아 File로 전환한 후 S3에 업로드 | ||
public String upload(MultipartFile multipartFile, String dirName) throws IOException { | ||
File uploadFile = convert(multipartFile) | ||
.orElseThrow(() -> new IllegalArgumentException("MultipartFile -> File 전환 실패")); | ||
return upload(uploadFile, dirName); | ||
} | ||
|
||
private String upload(File uploadFile, String dirName) { | ||
String fileName = dirName + "/" + uploadFile.getName(); | ||
String uploadImageUrl = putS3(uploadFile, fileName); | ||
|
||
removeNewFile(uploadFile); // 로컬에 생성된 File 삭제 (MultipartFile -> File 전환 하며 로컬에 파일 생성됨) | ||
|
||
return uploadImageUrl; // 업로드된 파일의 S3 URL 주소 반환 | ||
} | ||
|
||
private void removeNewFile(File targetFile) { | ||
if(targetFile.delete()) { | ||
log.info("파일이 삭제되었습니다."); | ||
}else { | ||
log.info("파일이 삭제되지 못했습니다."); | ||
} | ||
} | ||
|
||
|
||
private String putS3(File uploadFile, String fileName) { | ||
amazonS3Client.putObject( | ||
new PutObjectRequest(bucket, fileName, uploadFile) | ||
.withCannedAcl(CannedAccessControlList.PublicRead) // PublicRead 권한으로 업로드 됨 | ||
); | ||
return amazonS3Client.getUrl(bucket, fileName).toString(); | ||
} | ||
|
||
|
||
private Optional<File> convert(MultipartFile file) throws IOException { | ||
File convertFile = new File(file.getOriginalFilename()); | ||
if(convertFile.createNewFile()) { | ||
try (FileOutputStream fos = new FileOutputStream(convertFile)) { | ||
fos.write(file.getBytes()); | ||
} | ||
return Optional.of(convertFile); | ||
} | ||
return Optional.empty(); | ||
} | ||
|
||
} |
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
14 changes: 14 additions & 0 deletions
14
...ava/farmSystem/closeUp/dto/raffleProduct/request/PostCreateRaffleProductExtraRequest.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,14 @@ | ||
package farmSystem.closeUp.dto.raffleProduct.request; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Getter | ||
@Builder | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public class PostCreateRaffleProductExtraRequest { | ||
private String file; | ||
} |
19 changes: 19 additions & 0 deletions
19
...ain/java/farmSystem/closeUp/dto/raffleProduct/request/PostCreateRaffleProductRequest.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 farmSystem.closeUp.dto.raffleProduct.request; | ||
|
||
import lombok.Builder; | ||
import lombok.Getter; | ||
|
||
import java.time.LocalDateTime; | ||
|
||
@Builder | ||
@Getter | ||
public class PostCreateRaffleProductRequest { | ||
private String title; | ||
private LocalDateTime startDate; | ||
private LocalDateTime endDate; | ||
private String content; | ||
private Long winnerCount; | ||
private Long rafflePrice; | ||
private Long categoryId; | ||
private PostCreateRaffleProductExtraRequest extraRequest; | ||
} |
18 changes: 18 additions & 0 deletions
18
...n/java/farmSystem/closeUp/dto/raffleProduct/response/PostCreateRaffleProductResponse.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,18 @@ | ||
package farmSystem.closeUp.dto.raffleProduct.response; | ||
|
||
import lombok.Builder; | ||
import lombok.Getter; | ||
|
||
import java.time.LocalDateTime; | ||
|
||
@Getter | ||
@Builder | ||
public class PostCreateRaffleProductResponse { | ||
private LocalDateTime winnerDate; | ||
|
||
public static PostCreateRaffleProductResponse of(LocalDateTime winnerDate) { | ||
return PostCreateRaffleProductResponse.builder() | ||
.winnerDate(winnerDate) | ||
.build(); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/farmSystem/closeUp/repository/category/CategoryRepository.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,10 @@ | ||
package farmSystem.closeUp.repository.category; | ||
|
||
import farmSystem.closeUp.domain.Category; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
import java.util.Optional; | ||
|
||
public interface CategoryRepository extends JpaRepository<Category, Long> { | ||
Optional<Category> findByCategoryId(Long id); | ||
} |
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