Skip to content

Commit

Permalink
[FEAT] 파일 PreSigned-Url 발급 API 개발
Browse files Browse the repository at this point in the history
  • Loading branch information
yeseul106 committed Aug 24, 2023
1 parent 39f1b73 commit 8947496
Show file tree
Hide file tree
Showing 24 changed files with 240 additions and 59 deletions.
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 @@ -52,6 +54,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 @@ -130,6 +133,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 = "201", description = "파일 Presigned-Url 요청 성공"),
@ApiResponse(responseCode = "400", description = "파일 Presigned-Url 요청 실패", content = @Content),
@ApiResponse(responseCode = "500", description = "서버 오류", content = @Content)
}
)
@PostMapping("/files/presigned")
@ResponseStatus(HttpStatus.OK)
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 @@ -33,20 +34,11 @@ public class File extends BaseEntity {
private Card card;

@Builder
public File(Long id, String name, String url, Double size, Card card) {
public File(UUID id, String name, String url, Double 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 = "파일 사이즈", example = "2.1")
@NotNull
private Double 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,23 @@
package site.katchup.katchupserver.api.file.dto.response;

import io.swagger.v3.oas.annotations.media.Schema;
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 = "파일 사이즈", example = "2.1")
private Double 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));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package site.katchup.katchupserver.api.file.service;


import site.katchup.katchupserver.api.file.dto.request.FileGetPreSignedRequestDto;
import site.katchup.katchupserver.api.file.dto.response.FileGetPreSignedResponseDto;

public interface FileService {
FileGetPreSignedResponseDto getFilePreSignedUrl(Long memberId, FileGetPreSignedRequestDto requestDto);
}
Loading

0 comments on commit 8947496

Please sign in to comment.