Skip to content
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.moplus.moplus_server.domain.problem.controller;

import com.moplus.moplus_server.domain.problem.domain.problem.ProblemImageType;
import com.moplus.moplus_server.domain.problem.service.ImageUploadService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Tag(name = "이미지 업로드 API", description = "이미지 업로드 관련 API")
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/v1/images")
public class ImageUploadController {

private final ImageUploadService imageUploadService;

@Operation(summary = "이미지 업로드를 위한 presigned URL 발급")
@GetMapping("/problem/{problemId}/presigned-url")
public ResponseEntity<String> getProblemImagePresignedUrl(
@PathVariable("problemId") String problemId,
@RequestParam(value = "image-type") ProblemImageType imageType) {
String presignedUrl = imageUploadService.generateProblemImagePresignedUrl(problemId, imageType);
return ResponseEntity.ok(presignedUrl);
}

@Operation(summary = "이미지 업로드 완료 후 URL 조회")
@GetMapping("/{fileName}")
public ResponseEntity<String> getImageUrl(
@PathVariable("fileName") String fileName) {
String imageUrl = imageUploadService.getImageUrl(fileName);
return ResponseEntity.ok(imageUrl);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.moplus.moplus_server.domain.problem.domain.problem;

import lombok.Getter;
import lombok.RequiredArgsConstructor;

@Getter
@RequiredArgsConstructor
public enum ProblemImageType {
MAIN_PROBLEM("main-problem", "문제 이미지"),
MAIN_ANALYSIS("main-analysis", "분석 이미지"),
MAIN_HANDWRITING_EXPLANATION("main-handwriting-explanation", "손글씨 설명 이미지"),
READING_TIP("reading-tip", "읽기 팁 이미지"),
SENIOR_TIP("senior-tip", "선배 팁 이미지"),
PRESCRIPTION("prescription", "처방전 이미지"),
CHILD_PROBLEM("child-problem", "하위 문제 이미지");

private final String type;
private final String description;
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ default void existsByIdElseThrow(Long id) {
}
}

default void existsByProblemAdminIdElseThrow(ProblemAdminId problemAdminId) {
if (!existsByProblemAdminId(problemAdminId)) {
throw new NotFoundException(ErrorCode.PROBLEM_NOT_FOUND);
}
}

default Problem findByIdElseThrow(Long id) {
return findById(id).orElseThrow(() -> new NotFoundException(ErrorCode.PROBLEM_NOT_FOUND));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.moplus.moplus_server.domain.problem.service;

import com.amazonaws.HttpMethod;
import com.moplus.moplus_server.domain.problem.domain.problem.ProblemAdminId;
import com.moplus.moplus_server.domain.problem.domain.problem.ProblemImageType;
import com.moplus.moplus_server.domain.problem.repository.ProblemRepository;
import com.moplus.moplus_server.global.utils.s3.S3Util;
import java.util.UUID;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class ImageUploadService {

private static final String PROBLEM_IMAGE_PREFIX = "problems/";
private final S3Util s3Util;
private final ProblemRepository problemRepository;

public String generateProblemImagePresignedUrl(String problemId, ProblemImageType imageType) {
problemRepository.existsByProblemAdminIdElseThrow(new ProblemAdminId(problemId));
String fileName = generateProblemImageFileName(problemId, imageType);
return s3Util.getS3PresignedUrl(fileName, HttpMethod.PUT);
}

private String generateProblemImageFileName(String problemId, ProblemImageType imageType) {
String uuid = UUID.randomUUID().toString();
return String.format("%s%s/%s/%s.jpg",
PROBLEM_IMAGE_PREFIX,
problemId,
imageType.getType(),
uuid
);
}

public String getImageUrl(String fileName) {
return s3Util.getS3ObjectUrl(fileName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ public enum ErrorCode {
PRACTICE_TEST_NOT_FOUND(HttpStatus.NOT_FOUND, "해당 모의고사를 찾을 수 없습니다"),

//문항
PROBLEM_NOT_FOUND(HttpStatus.NOT_FOUND, "해당 문제를 찾을 수 없습니다"),
PROBLEM_ALREADY_EXIST(HttpStatus.CONFLICT, "해당 문제는 이미 존재합니다"),
INVALID_MULTIPLE_CHOICE_ANSWER(HttpStatus.BAD_REQUEST, "객관식 문제의 정답은 1~5 사이의 숫자여야 합니다"),
INVALID_SHORT_NUMBER_ANSWER(HttpStatus.BAD_REQUEST, "주관식 문제의 정답은 0~999 사이의 숫자여야 합니다"),
PROBLEM_NOT_FOUND(HttpStatus.NOT_FOUND, "해당 문항을 찾을 수 없습니다"),
PROBLEM_ALREADY_EXIST(HttpStatus.CONFLICT, "해당 문항는 이미 존재합니다"),
INVALID_MULTIPLE_CHOICE_ANSWER(HttpStatus.BAD_REQUEST, "객관식 문항의 정답은 1~5 사이의 숫자여야 합니다"),
INVALID_SHORT_NUMBER_ANSWER(HttpStatus.BAD_REQUEST, "주관식 문항의 정답은 0~999 사이의 숫자여야 합니다"),
INVALID_CONFIRM_PROBLEM(HttpStatus.BAD_REQUEST, "유효하지 않은 문항들 : "),
INVALID_DIFFICULTY(HttpStatus.BAD_REQUEST, "난이도는 1~10 사이의 숫자여야 합니다"),

Expand Down
22 changes: 22 additions & 0 deletions src/main/java/com/moplus/moplus_server/global/utils/s3/S3Util.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package com.moplus.moplus_server.global.utils.s3;

import com.amazonaws.HttpMethod;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.model.GeneratePresignedUrlRequest;
import com.amazonaws.services.s3.model.PutObjectRequest;
import com.moplus.moplus_server.global.error.exception.ErrorCode;
import com.moplus.moplus_server.global.error.exception.NotFoundException;
import java.io.File;
import java.util.Date;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
Expand All @@ -29,5 +32,24 @@ public String getS3ObjectUrl(String fileName) {
return amazonS3.getUrl(bucketName, fileName).toString();
}

public String getS3PresignedUrl(String fileName, HttpMethod httpMethod) {

GeneratePresignedUrlRequest generatePresignedUrlRequest =
new GeneratePresignedUrlRequest(bucketName, fileName)
.withMethod(httpMethod)
.withExpiration(getPreSignedUrlExpiration());

return amazonS3.generatePresignedUrl(generatePresignedUrlRequest).toString();
}

private Date getPreSignedUrlExpiration() {
final int PRESIGNED_EXPIRATION = 1000 * 60 * 30; //30분

Date expiration = new Date();
var expTimeMillis = expiration.getTime();
expTimeMillis += PRESIGNED_EXPIRATION;
expiration.setTime(expTimeMillis);
return expiration;
}

}