Skip to content

Commit

Permalink
Merge pull request #58 from TEAM-DHS/feat/image
Browse files Browse the repository at this point in the history
[FEAT] S3 이미지 업로드
  • Loading branch information
mingulmangul authored Nov 25, 2023
2 parents 2e44408 + d5f0b3e commit 98c8eff
Show file tree
Hide file tree
Showing 8 changed files with 120 additions and 20 deletions.
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ dependencies {
// querydsl
implementation "com.querydsl:querydsl-jpa:${queryDslVersion}"
annotationProcessor "com.querydsl:querydsl-apt:${queryDslVersion}"
// aws
implementation 'org.springframework.cloud:spring-cloud-starter-aws:2.2.6.RELEASE'
}

def querydslDir = "$buildDir/generated/querydsl"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,18 @@
import javax.validation.Valid;

import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
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.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.efub.dhs.domain.program.dto.request.NoticeCreationRequestDto;
import com.efub.dhs.domain.program.dto.request.ProgramCreationRequestDto;
Expand All @@ -27,15 +30,19 @@
import com.efub.dhs.domain.program.dto.response.ProgramRegisteredResponseDto;
import com.efub.dhs.domain.program.dto.response.ProgramRegistrationResponseDto;
import com.efub.dhs.domain.program.entity.Program;
import com.efub.dhs.domain.program.entity.ProgramImage;
import com.efub.dhs.domain.program.service.NoticeService;
import com.efub.dhs.domain.program.service.ProgramImageService;
import com.efub.dhs.domain.program.service.ProgramMemberService;
import com.efub.dhs.domain.program.service.ProgramService;
import com.efub.dhs.domain.registration.dto.RegistrationResponseDto;
import com.efub.dhs.domain.registration.entity.Registration;
import com.efub.dhs.domain.registration.service.RegistrationService;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/programs")
Expand All @@ -45,17 +52,22 @@ public class ProgramController {
private final ProgramMemberService programMemberService;
private final RegistrationService registrationService;
private final NoticeService noticeService;
private final ProgramImageService programImageService;

@GetMapping("/{programId}")
@ResponseStatus(value = HttpStatus.OK)
public ProgramDetailResponseDto findProgramById(@PathVariable Long programId) {
return programService.findProgramById(programId);
}

@PostMapping
@PostMapping(consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
@ResponseStatus(value = HttpStatus.CREATED)
public ProgramCreationResponseDto createProgram(@RequestBody @Valid ProgramCreationRequestDto requestDto) {
return new ProgramCreationResponseDto(programService.createProgram(requestDto));
public ProgramCreationResponseDto createProgram(
@RequestPart(value = "data") @Valid ProgramCreationRequestDto requestDto,
@RequestPart(value = "image") List<MultipartFile> images) {
List<ProgramImage> programImages = programImageService.createProgramImages(images);
Long programId = programService.createProgram(requestDto, programImages);
return new ProgramCreationResponseDto(programId);
}

@PostMapping("/{programId}")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package com.efub.dhs.domain.program.dto.request;

import java.time.LocalDateTime;
import java.util.List;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;

import com.efub.dhs.domain.member.entity.Member;
Expand Down Expand Up @@ -47,8 +45,6 @@ public class ProgramCreationRequestDto {
private String hostName;
@NotBlank
private String hostDescription;
@NotEmpty
private List<String> images;

public Program toEntity(Member member) {
return Program.builder()
Expand All @@ -67,7 +63,6 @@ public Program toEntity(Member member) {
.price(price)
.hostName(hostName)
.hostDescription(hostDescription)
.imageUrlList(images)
.build();
}
}
10 changes: 3 additions & 7 deletions src/main/java/com/efub/dhs/domain/program/entity/Program.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

import javax.persistence.Column;
import javax.persistence.Entity;
Expand Down Expand Up @@ -97,7 +96,7 @@ public class Program extends BaseTimeEntity {
public Program(Member host, String title, Category category, LocalDateTime schedule, String location,
String postalCode, LocalDateTime deadline, Integer targetNumber, String content, String depositBank,
String depositName, String depositAccount, String price, String hostName,
String hostDescription, List<String> imageUrlList) {
String hostDescription) {
this.host = host;
this.title = title;
this.category = category;
Expand All @@ -116,9 +115,6 @@ public Program(Member host, String title, Category category, LocalDateTime sched
this.price = price;
this.hostDescription = hostDescription;
this.hostName = hostName;
this.images = imageUrlList.stream()
.map(url -> ProgramImage.builder().program(this).url(url).build())
.collect(Collectors.toList());
this.notices = List.of();
}

Expand All @@ -128,8 +124,8 @@ public void increaseRegistrantNumber() {

public void decreaseRegistrantNumber() {
this.registrantNumber--;
}
}

public void closeProgram() {
this.isOpen = false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,8 @@ public ProgramImage(Program program, String url) {
this.program = program;
this.url = url;
}

public void setProgram(Program program) {
this.program = program;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.efub.dhs.domain.program.service;

import java.io.IOException;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.model.CannedAccessControlList;
import com.amazonaws.services.s3.model.ObjectMetadata;
import com.amazonaws.services.s3.model.PutObjectRequest;
import com.efub.dhs.domain.program.entity.ProgramImage;

import lombok.RequiredArgsConstructor;

@Service
@RequiredArgsConstructor
public class ProgramImageService {

private final AmazonS3 amazonS3;

@Value("${cloud.aws.s3.bucket}")
private String bucket;

private String uploadImageToS3(MultipartFile imageFile) throws IOException {
ObjectMetadata metadata = new ObjectMetadata();
metadata.setContentLength(imageFile.getSize());
metadata.setContentType(imageFile.getContentType());

String filename = UUID.randomUUID().toString();
PutObjectRequest putObjectRequest =
new PutObjectRequest(bucket, filename, imageFile.getInputStream(), metadata)
.withCannedAcl(CannedAccessControlList.PublicRead);
amazonS3.putObject(putObjectRequest);

return amazonS3.getUrl(bucket, filename).toString();
}

public List<ProgramImage> createProgramImages(List<MultipartFile> images) {
return images.stream().map(image -> {
String imageUrl;
try {
imageUrl = uploadImageToS3(image);
} catch (IOException e) {
throw new RuntimeException(e);
}
return ProgramImage.builder()
.url(imageUrl)
.build();
}).collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -149,13 +149,15 @@ public List<ProgramOutlineResponseDto> convertToProgramOutlineResponseDtoList(
}
}

public Long createProgram(ProgramCreationRequestDto requestDto) {
public Long createProgram(ProgramCreationRequestDto requestDto, List<ProgramImage> programImages) {
Member currentUser = memberService.getCurrentUser();
Program program = requestDto.toEntity(currentUser);
Long programId = programRepository.save(program).getProgramId();
List<ProgramImage> images = program.getImages();
programImageRepository.saveAll(images);
return programId;
Program savedProgram = programRepository.save(program);
for (ProgramImage programImage : programImages) {
programImage.setProgram(savedProgram);
programImageRepository.save(programImage);
}
return savedProgram.getProgramId();
}

public ProgramDetailResponseDto closeProgram(Long programId) {
Expand Down
33 changes: 33 additions & 0 deletions src/main/java/com/efub/dhs/global/config/S3Config.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.efub.dhs.global.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;

@Configuration
public class S3Config {

@Value("${cloud.aws.credentials.access-key}")
private String accessKey;

@Value("${cloud.aws.credentials.secret-key}")
private String secretKey;

@Value("${cloud.aws.region.static}")
private String region;

@Bean
public AmazonS3Client amazonS3Client() {
BasicAWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey);
return (AmazonS3Client)AmazonS3ClientBuilder
.standard()
.withRegion(region)
.withCredentials(new AWSStaticCredentialsProvider(credentials))
.build();
}
}

0 comments on commit 98c8eff

Please sign in to comment.