From 93eaa378dc3c3f471e254d33c527c5dfb67adde8 Mon Sep 17 00:00:00 2001 From: jaejae-yoo Date: Tue, 18 Oct 2022 14:21:14 +0900 Subject: [PATCH 1/2] =?UTF-8?q?=EC=9E=84=EC=8B=9C=20commit?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/build.gradle | 2 + .../common/advice/CommonControllerAdvice.java | 1 + .../moamoa/study/ImageController.java | 22 +++++++++ .../moamoa/study/ImageService.java | 48 +++++++++++++++++++ .../moamoa/study/JpaThumbnailRepository.java | 6 +++ .../moamoa/study/ThumbnailImage.java | 26 ++++++++++ .../moamoa/study/ThumbnailRepository.java | 8 ++++ backend/src/main/resources/application.yml | 23 +++++++-- backend/src/main/resources/schema.sql | 7 +++ 9 files changed, 139 insertions(+), 4 deletions(-) create mode 100644 backend/src/main/java/com/woowacourse/moamoa/study/ImageController.java create mode 100644 backend/src/main/java/com/woowacourse/moamoa/study/ImageService.java create mode 100644 backend/src/main/java/com/woowacourse/moamoa/study/JpaThumbnailRepository.java create mode 100644 backend/src/main/java/com/woowacourse/moamoa/study/ThumbnailImage.java create mode 100644 backend/src/main/java/com/woowacourse/moamoa/study/ThumbnailRepository.java diff --git a/backend/build.gradle b/backend/build.gradle index 4741ceafe..91731d309 100644 --- a/backend/build.gradle +++ b/backend/build.gradle @@ -27,6 +27,8 @@ dependencies { implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'org.springframework.boot:spring-boot-starter-validation' + implementation 'org.springframework.cloud:spring-cloud-starter-aws:2.2.6.RELEASE' + compileOnly 'org.projectlombok:lombok' annotationProcessor 'org.projectlombok:lombok' diff --git a/backend/src/main/java/com/woowacourse/moamoa/common/advice/CommonControllerAdvice.java b/backend/src/main/java/com/woowacourse/moamoa/common/advice/CommonControllerAdvice.java index b70f27590..d89d00060 100644 --- a/backend/src/main/java/com/woowacourse/moamoa/common/advice/CommonControllerAdvice.java +++ b/backend/src/main/java/com/woowacourse/moamoa/common/advice/CommonControllerAdvice.java @@ -60,6 +60,7 @@ public ResponseEntity handleNotFound(final Exception e) { @ExceptionHandler(RuntimeException.class) public ResponseEntity handleInternalServerError(RuntimeException e) { log.debug("RuntimeException : {}", e.getMessage()); + System.out.println("e.getMessage() = " + e.getMessage()); return ResponseEntity.status(INTERNAL_SERVER_ERROR).body(new ErrorResponse("요청을 처리할 수 없습니다.")); } } diff --git a/backend/src/main/java/com/woowacourse/moamoa/study/ImageController.java b/backend/src/main/java/com/woowacourse/moamoa/study/ImageController.java new file mode 100644 index 000000000..32b58fe04 --- /dev/null +++ b/backend/src/main/java/com/woowacourse/moamoa/study/ImageController.java @@ -0,0 +1,22 @@ +package com.woowacourse.moamoa.study; + +import java.io.IOException; +import lombok.RequiredArgsConstructor; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.multipart.MultipartFile; + +@RequiredArgsConstructor +@RestController +public class ImageController { + + private final ImageService imageService; + + @PostMapping("/api/study/thumbnail") + public ResponseEntity upload(@RequestParam("thumbnail") MultipartFile multipartFile) throws IOException { + final ThumbnailImage thumbnailImage = imageService.saveUploadFile(multipartFile); + return ResponseEntity.ok().build(); + } +} diff --git a/backend/src/main/java/com/woowacourse/moamoa/study/ImageService.java b/backend/src/main/java/com/woowacourse/moamoa/study/ImageService.java new file mode 100644 index 000000000..1bae1de57 --- /dev/null +++ b/backend/src/main/java/com/woowacourse/moamoa/study/ImageService.java @@ -0,0 +1,48 @@ +package com.woowacourse.moamoa.study; + +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 java.io.IOException; +import java.io.InputStream; +import java.util.UUID; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.transaction.annotation.Transactional; +import org.springframework.web.multipart.MultipartFile; + +@Service +@RequiredArgsConstructor +public class ImageService { + + private final AmazonS3 amazonS3; + private final ThumbnailRepository thumbnailRepository; + + @Value("${cloud.aws.s3.bucket}") + private String bucket; + + @Transactional + public ThumbnailImage saveUploadFile(MultipartFile multipartFile) throws IOException { + ObjectMetadata objectMetadata = new ObjectMetadata(); + objectMetadata.setContentType(multipartFile.getContentType()); + objectMetadata.setContentLength(multipartFile.getSize()); + + String originalFilename = multipartFile.getOriginalFilename(); + int index = originalFilename.lastIndexOf("."); + String ext = originalFilename.substring(index + 1); + + String storeFileName = UUID.randomUUID() + "." + ext; + String key = "test/" + storeFileName; + + try (InputStream inputStream = multipartFile.getInputStream()) { + amazonS3.putObject(new PutObjectRequest(bucket, key, inputStream, objectMetadata) + .withCannedAcl(CannedAccessControlList.PublicReadWrite)); + } + + String storeFileUrl = amazonS3.getUrl(bucket, key).toString(); + ThumbnailImage thumbnailImage = new ThumbnailImage(originalFilename, storeFileUrl); + return thumbnailRepository.save(thumbnailImage); + } +} diff --git a/backend/src/main/java/com/woowacourse/moamoa/study/JpaThumbnailRepository.java b/backend/src/main/java/com/woowacourse/moamoa/study/JpaThumbnailRepository.java new file mode 100644 index 000000000..df5b84fdc --- /dev/null +++ b/backend/src/main/java/com/woowacourse/moamoa/study/JpaThumbnailRepository.java @@ -0,0 +1,6 @@ +package com.woowacourse.moamoa.study; + +import org.springframework.data.jpa.repository.JpaRepository; + +public interface JpaThumbnailRepository extends JpaRepository, ThumbnailRepository { +} diff --git a/backend/src/main/java/com/woowacourse/moamoa/study/ThumbnailImage.java b/backend/src/main/java/com/woowacourse/moamoa/study/ThumbnailImage.java new file mode 100644 index 000000000..4bfca57d1 --- /dev/null +++ b/backend/src/main/java/com/woowacourse/moamoa/study/ThumbnailImage.java @@ -0,0 +1,26 @@ +package com.woowacourse.moamoa.study; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import lombok.AccessLevel; +import static javax.persistence.GenerationType.IDENTITY; + +import lombok.Getter; +import lombok.NoArgsConstructor; +import javax.persistence.Id; + +@NoArgsConstructor(access = AccessLevel.PROTECTED) +@Entity(name = "thumbnail_image") +@Getter +public class ThumbnailImage { + + @Id @GeneratedValue(strategy = IDENTITY) + private Long id; + private String fileName; + private String fileUrl; + + public ThumbnailImage(String fileName, String fileUrl) { + this.fileName = fileName; + this.fileUrl = fileUrl; + } +} diff --git a/backend/src/main/java/com/woowacourse/moamoa/study/ThumbnailRepository.java b/backend/src/main/java/com/woowacourse/moamoa/study/ThumbnailRepository.java new file mode 100644 index 000000000..9ca04aaae --- /dev/null +++ b/backend/src/main/java/com/woowacourse/moamoa/study/ThumbnailRepository.java @@ -0,0 +1,8 @@ +package com.woowacourse.moamoa.study; + +public interface ThumbnailRepository { + + ThumbnailImage save(ThumbnailImage thumbnailImage); + + ThumbnailImage findByFileName(String fileName); +} diff --git a/backend/src/main/resources/application.yml b/backend/src/main/resources/application.yml index a5a4c48a6..34cfc7c5f 100644 --- a/backend/src/main/resources/application.yml +++ b/backend/src/main/resources/application.yml @@ -1,17 +1,32 @@ spring: sql: init: - mode: never + mode: always jpa: hibernate: - ddl-auto: validate + ddl-auto: none properties: hibernate: - dialect: org.hibernate.dialect.MySQL8Dialect + dialect: org.hibernate.dialect.H2Dialect + show_sql: true + format_sql: true datasource: driver-class-name: org.h2.Driver username: sa password: - url: jdbc:h2:mem://localhost/~/testdb;MODE=MYSQL + url: jdbc:h2:mem:~/moamoa;MODE=MYSQL profiles: include: security + servlet: + multipart: + max-file-size: 100MB + max-request-size: 100MB + +cloud: + aws: + region: + static: ap-northeast-2 + stack: + auto: false + s3: + bucket: 2022-moamoa-thumbnail diff --git a/backend/src/main/resources/schema.sql b/backend/src/main/resources/schema.sql index 58a9939c6..d0dabbaf0 100644 --- a/backend/src/main/resources/schema.sql +++ b/backend/src/main/resources/schema.sql @@ -9,6 +9,13 @@ DROP TABLE IF EXISTS study; DROP TABLE IF EXISTS member; DROP TABLE IF EXISTS token; +CREATE TABLE thumbnail_image +( + id BIGINT PRIMARY KEY AUTO_INCREMENT, + fileName VARCHAR(50), + fileUrl VARCHAR(50) +); + CREATE TABLE member ( id BIGINT PRIMARY KEY AUTO_INCREMENT, From 76d647cbd43a53338844f715296266f22e225e32 Mon Sep 17 00:00:00 2001 From: jaejae-yoo Date: Tue, 18 Oct 2022 15:41:55 +0900 Subject: [PATCH 2/2] =?UTF-8?q?feat:=20=EC=9D=B4=EB=AF=B8=EC=A7=80=20?= =?UTF-8?q?=EC=97=85=EB=A1=9C=EB=93=9C=20=EA=B8=B0=EB=8A=A5=20=EB=B0=8F=20?= =?UTF-8?q?S3=20=EC=84=A4=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: tco0427 --- .../com/woowacourse/moamoa/study/ImageService.java | 4 ++-- backend/src/test/resources/application.yml | 13 +++++++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/backend/src/main/java/com/woowacourse/moamoa/study/ImageService.java b/backend/src/main/java/com/woowacourse/moamoa/study/ImageService.java index 1bae1de57..fa0bd37b1 100644 --- a/backend/src/main/java/com/woowacourse/moamoa/study/ImageService.java +++ b/backend/src/main/java/com/woowacourse/moamoa/study/ImageService.java @@ -34,11 +34,11 @@ public ThumbnailImage saveUploadFile(MultipartFile multipartFile) throws IOExcep String ext = originalFilename.substring(index + 1); String storeFileName = UUID.randomUUID() + "." + ext; - String key = "test/" + storeFileName; + String key = "thumbnail/" + storeFileName; try (InputStream inputStream = multipartFile.getInputStream()) { amazonS3.putObject(new PutObjectRequest(bucket, key, inputStream, objectMetadata) - .withCannedAcl(CannedAccessControlList.PublicReadWrite)); + .withCannedAcl(CannedAccessControlList.PublicRead)); } String storeFileUrl = amazonS3.getUrl(bucket, key).toString(); diff --git a/backend/src/test/resources/application.yml b/backend/src/test/resources/application.yml index 1030aead9..a9239ab03 100644 --- a/backend/src/test/resources/application.yml +++ b/backend/src/test/resources/application.yml @@ -17,3 +17,16 @@ spring: url: jdbc:h2:mem:~/moamoa;MODE=MYSQL profiles: include: security + servlet: + multipart: + max-file-size: 100MB + max-request-size: 100MB + +cloud: + aws: + region: + static: ap-northeast-2 + stack: + auto: false + s3: + bucket: 2022-moamoa-thumbnail \ No newline at end of file