Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BE]: 스터디 thumbnail Image CRUD 기능 #438

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions backend/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down
Original file line number Diff line number Diff line change
@@ -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<Void> upload(@RequestParam("thumbnail") MultipartFile multipartFile) throws IOException {
final ThumbnailImage thumbnailImage = imageService.saveUploadFile(multipartFile);
return ResponseEntity.ok().build();
}
}
Original file line number Diff line number Diff line change
@@ -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 = "thumbnail/" + storeFileName;

try (InputStream inputStream = multipartFile.getInputStream()) {
amazonS3.putObject(new PutObjectRequest(bucket, key, inputStream, objectMetadata)
.withCannedAcl(CannedAccessControlList.PublicRead));
}

String storeFileUrl = amazonS3.getUrl(bucket, key).toString();
ThumbnailImage thumbnailImage = new ThumbnailImage(originalFilename, storeFileUrl);
return thumbnailRepository.save(thumbnailImage);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.woowacourse.moamoa.study;

import org.springframework.data.jpa.repository.JpaRepository;

public interface JpaThumbnailRepository extends JpaRepository<ThumbnailImage, Long>, ThumbnailRepository {
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.woowacourse.moamoa.study;

public interface ThumbnailRepository {

ThumbnailImage save(ThumbnailImage thumbnailImage);

ThumbnailImage findByFileName(String fileName);
}
13 changes: 13 additions & 0 deletions backend/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,16 @@ spring:
url: jdbc:h2:mem://localhost/~/testdb;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
11 changes: 9 additions & 2 deletions backend/src/main/resources/schema.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
CREATE TABLE IF NOT EXISTS thumbnail_image
(
id BIGINT PRIMARY KEY AUTO_INCREMENT,
file_name VARCHAR(50) NOT NULL,
file_url VARCHAR(50) NOT NULL
);

CREATE TABLE IF NOT EXISTS member
(
id BIGINT PRIMARY KEY AUTO_INCREMENT,
Expand Down Expand Up @@ -66,7 +73,7 @@ CREATE TABLE IF NOT EXISTS tag
description VARCHAR(255) NOT NULL,
category_id BIGINT,
FOREIGN KEY (category_id) REFERENCES category (id)
);
);

CREATE TABLE IF NOT EXISTS study_tag
(
Expand Down Expand Up @@ -100,4 +107,4 @@ CREATE TABLE IF NOT EXISTS article
deleted boolean NOT NULL,
FOREIGN KEY (author_id) REFERENCES member (id),
FOREIGN KEY (study_id) REFERENCES study (id)
);
);
13 changes: 13 additions & 0 deletions backend/src/test/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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