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

[FEAT] 파일 PreSigned-Url 발급 API 개발 #119

Merged
merged 3 commits into from
Aug 24, 2023
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
Expand Up @@ -4,6 +4,7 @@
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import site.katchup.katchupserver.api.file.domain.File;
import site.katchup.katchupserver.api.link.domain.Link;
import site.katchup.katchupserver.api.screenshot.domain.Screenshot;
import site.katchup.katchupserver.api.subTask.domain.SubTask;
Expand Down Expand Up @@ -64,18 +65,6 @@ public Card(Long placementOrder, String content, String note, SubTask subTask) {
this.subTask.addCard(this);
}

public void addScreenshot(Screenshot screenshot) {
screenshots.add(screenshot);
}

public void addFile(File file) {
files.add(file);
}

public void addLink(Link link) {
links.add(link);
}

public void deletedCard() {
this.isDeleted = true;
this.deletedAt = LocalDateTime.now();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import jakarta.validation.constraints.NotNull;
import lombok.AllArgsConstructor;
import lombok.Getter;
import site.katchup.katchupserver.api.file.dto.request.FileCreateRequestDto;
import site.katchup.katchupserver.api.screenshot.dto.request.ScreenshotCreateRequestDto;
import site.katchup.katchupserver.api.sticker.dto.request.StickerCreateRequestDto;

Expand All @@ -28,6 +29,8 @@ public class CardCreateRequestDto {

private List<ScreenshotCreateRequestDto> screenshotList;

private List<FileCreateRequestDto> fileList;

private String note;

@NotBlank(message = "CD-114")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import lombok.AllArgsConstructor;
import lombok.Getter;
import site.katchup.katchupserver.api.file.dto.response.FileGetResponseDto;
import site.katchup.katchupserver.api.keyword.dto.response.KeywordGetResponseDto;
import site.katchup.katchupserver.api.screenshot.dto.response.ScreenshotGetResponseDto;

Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@
import site.katchup.katchupserver.api.card.dto.request.CardDeleteRequestDto;
import site.katchup.katchupserver.api.card.dto.response.CardGetResponseDto;
import site.katchup.katchupserver.api.card.dto.response.CardListGetResponseDto;
import site.katchup.katchupserver.api.card.dto.response.FileGetResponseDto;
import site.katchup.katchupserver.api.file.domain.File;
import site.katchup.katchupserver.api.file.dto.request.FileCreateRequestDto;
import site.katchup.katchupserver.api.file.dto.response.FileGetResponseDto;
import site.katchup.katchupserver.api.card.repository.CardRepository;
import site.katchup.katchupserver.api.card.service.CardService;
import site.katchup.katchupserver.api.category.domain.Category;
import site.katchup.katchupserver.api.category.repository.CategoryRepository;
import site.katchup.katchupserver.api.file.repository.FileRepository;
import site.katchup.katchupserver.api.keyword.domain.CardKeyword;
import site.katchup.katchupserver.api.keyword.dto.response.KeywordGetResponseDto;
import site.katchup.katchupserver.api.keyword.repository.CardKeywordRepository;
Expand All @@ -33,7 +36,6 @@
import site.katchup.katchupserver.api.trash.repository.TrashRepository;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
Expand All @@ -42,6 +44,7 @@
public class CardServiceImpl implements CardService {

private static final String SUB_TASK_ETC_NAME = "기타";
private static final Long SUB_TASK_ETC_ID = 0L;

private final SubTaskRepository subTaskRepository;
private final CardKeywordRepository cardKeywordRepository;
Expand All @@ -52,6 +55,7 @@ public class CardServiceImpl implements CardService {
private final ScreenshotRepository screenshotRepository;
private final KeywordRepository keywordRepository;
private final StickerRepository stickerRepository;
private final FileRepository fileRepository;

@Override
public List<CardListGetResponseDto> getCardList(Long taskId) {
Expand Down Expand Up @@ -87,7 +91,7 @@ public void deleteCardList(CardDeleteRequestDto cardDeleteRequestDto) {
public void createCard(CardCreateRequestDto requestDto) {

SubTask subTask;
if (requestDto.getSubTaskId() == 0) {
if (requestDto.getSubTaskId() == SUB_TASK_ETC_ID) {
Task task = taskRepository.findByIdOrThrow(requestDto.getTaskId());
subTask = subTaskRepository.findOrCreateEtcSubTask(task, SUB_TASK_ETC_NAME);
} else {
Expand Down Expand Up @@ -130,6 +134,19 @@ public void createCard(CardCreateRequestDto requestDto) {
stickerRepository.save(newSticker);
}
}

for (FileCreateRequestDto fileInfo : requestDto.getFileList()) {
File newFile = File.builder()
.id(fileInfo.getFileUUID())
.url(fileInfo.getFileUrl())
.name(fileInfo.getFileName())
.size(fileInfo.getSize())
.card(savedCard)
.build();

fileRepository.save(newFile);
}

}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
@Schema(description = "카테고리 생성 요청 DTO")
public class CategoryCreateRequestDto {
@Schema(description = "카테고리 이름", example = "Katchup Design")
@Pattern(regexp = "^[a-zA-Z0-9가-힣_\s]*$", message = "KC-103")
@Pattern(regexp = "^[\\p{L}\\p{N}\\s]+$", message = "KC-103")
@NotBlank(message = "CG-108")
private String name;
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
@Schema(description = "카테고리 수정 요청 DTO")
public class CategoryUpdateRequestDto {
@Schema(description = "카테고리 이름", example = "Katchup Design")
@Pattern(regexp = "^[a-zA-Z0-9가-힣_\s]*$", message = "KC-103")
@Pattern(regexp = "^[\\p{L}\\p{N}\\s]+$", message = "KC-103")
@NotBlank(message = "CG-108")
private String name;
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public void deleteCategory(Long categoryId) {

private void checkDuplicateCategoryName(Long memberId, String name) {
if (categoryRepository.existsByMemberIdAndName(memberId, name))
throw new BadRequestException(ErrorCode.DUPLICATE_TASK_NAME);
throw new BadRequestException(ErrorCode.DUPLICATE_CATEGORY_NAME);
}

private void deleteTaskAndSubTaskAndCard(Task task) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package site.katchup.katchupserver.api.file.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import site.katchup.katchupserver.api.file.dto.request.FileGetPreSignedRequestDto;
import site.katchup.katchupserver.api.file.dto.response.FileGetPreSignedResponseDto;
import site.katchup.katchupserver.api.file.service.FileService;
import site.katchup.katchupserver.common.dto.ApiResponseDto;
import site.katchup.katchupserver.common.util.MemberUtil;

import java.security.Principal;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/v1")
@Tag(name = "[File] 파일 관련 API (V1)")
public class FileController {
private final FileService fileService;

@Operation(summary = "파일 Presigned-Url 요청 API")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "파일 Presigned-Url 요청 성공"),
@ApiResponse(responseCode = "400", description = "파일 Presigned-Url 요청 실패", content = @Content),
@ApiResponse(responseCode = "500", description = "서버 오류", content = @Content)
}
)
@GetMapping("/files/presigned")
@ResponseStatus(HttpStatus.OK)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

p2;

statusCode와 @ResponseStatus 가 일치하지 않습니다~!

public ApiResponseDto<FileGetPreSignedResponseDto> createPresigned(Principal principal, @RequestBody FileGetPreSignedRequestDto presignedRequestDto
) {
Long memberId = MemberUtil.getMemberId(principal);
return ApiResponseDto.success(fileService.getFilePreSignedUrl(memberId, presignedRequestDto));
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package site.katchup.katchupserver.api.card.domain;
package site.katchup.katchupserver.api.file.domain;

import jakarta.persistence.*;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import site.katchup.katchupserver.api.card.domain.Card;
import site.katchup.katchupserver.common.domain.BaseEntity;

import java.util.UUID;

import static jakarta.persistence.FetchType.LAZY;
import static jakarta.persistence.GenerationType.*;
import static lombok.AccessLevel.*;

@Entity
Expand All @@ -16,8 +18,7 @@
public class File extends BaseEntity {

@Id
@GeneratedValue(strategy = IDENTITY)
private Long id;
private UUID id;

@Column(nullable = false)
private String name;
Expand All @@ -26,27 +27,18 @@ public class File extends BaseEntity {
private String url;

@Column(nullable = false)
private Double size;
private int size;

@ManyToOne(fetch = LAZY)
@JoinColumn(name = "card_id")
private Card card;

@Builder
public File(Long id, String name, String url, Double size, Card card) {
public File(UUID id, String name, String url, int size, Card card) {
this.id = id;
this.name = name;
this.url = url;
this.card = card;
this.size = size;
this.card.addFile(this);
}

@Builder
public File(String name, String url, Double size, Card card) {
this.name = name;
this.url = url;
this.size = size;
this.card = card;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package site.katchup.katchupserver.api.file.dto.request;

import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotNull;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.UUID;

@Getter
@AllArgsConstructor
@Schema(description = "파일 업로드 요청 DTO")
public class FileCreateRequestDto {

@Schema(description = "업로드하는 파일 고유 UUID", example = "ddwd-wdwd-wdwd-wdwdwdwd")
@NotNull
private UUID fileUUID;

@Schema(description = "파일 이름", example = "와이어프레임 및 드라이브 사용법.pdf")
@NotNull
private String fileName;

@Schema(description = "파일 url", example = "https://abde.s3.ap-northeast-2.amazonaws.com/1.pdf")
@NotNull
private String fileUrl;

@Schema(description = "파일 사이즈 (KB 단위로 저장)", example = "189277")
@NotNull
private int size;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package site.katchup.katchupserver.api.file.dto.request;

import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotNull;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

import static lombok.AccessLevel.PRIVATE;

@Getter
@AllArgsConstructor
@NoArgsConstructor(access = PRIVATE)
@Schema(description = "파일 Presigned-Url 요청 DTO")
public class FileGetPreSignedRequestDto {
@Schema(description = "업로드하는 파일 이름", example = "와이어프레임 및 드라이브 사용법.pdf")
@NotNull
private String fileName;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package site.katchup.katchupserver.api.file.dto.response;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

import static lombok.AccessLevel.PRIVATE;

@Getter
@NoArgsConstructor(access = PRIVATE)
@AllArgsConstructor(staticName = "of")
@Schema(description = "파일 PreSigned-Url 응답 DTO")
public class FileGetPreSignedResponseDto {

@Schema(description = "파일 UUID", example = "file-uuid")
private String fileUUID;

@Schema(description = "파일 이름", example = "file-name")
private String fileName;

@Schema(description = "파일 PreSigned-Url", example ="preSigned-url")
private String filePreSignedUrl;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package site.katchup.katchupserver.api.file.dto.response;

import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotNull;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.UUID;

@Getter
@AllArgsConstructor(staticName = "of")
public class FileGetResponseDto {
@Schema(description = "파일 고유 id", example = "ddwd-wdwd-wdwd-wdwdwdwd")
private UUID id;

@Schema(description = "파일 이름", example = "와이어프레임 및 드라이브 사용법.pdf")
private String name;

@Schema(description = "파일 url", example = "https://abde.s3.ap-northeast-2.amazonaws.com/1.png")
private String url;

@Schema(description = "파일 사이즈 (KB 단위로 저장)", example = "189277")
private int size;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package site.katchup.katchupserver.api.file.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import site.katchup.katchupserver.api.file.domain.File;
import site.katchup.katchupserver.api.screenshot.domain.Screenshot;
import site.katchup.katchupserver.common.exception.NotFoundException;
import site.katchup.katchupserver.common.response.ErrorCode;

import java.util.List;
import java.util.UUID;

public interface FileRepository extends JpaRepository<File, UUID> {

List<File> findAllByCardId(Long cardId);

default File findByIdOrThrow(UUID uuid) {
return findById(uuid).orElseThrow(
() -> new NotFoundException(ErrorCode.NOT_FOUND_FILE));
}
}
Loading
Loading