From aab5d67845d6d32538004084ce9f07c4dc2962e8 Mon Sep 17 00:00:00 2001 From: SJ70 Date: Fri, 9 Aug 2024 00:07:10 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20=EB=8F=99=EC=98=81=EC=83=81=20?= =?UTF-8?q?=EC=A0=80=EC=9E=A5=20=EC=8B=9C=20Storage=20=EB=82=B4=20?= =?UTF-8?q?=EA=B2=BD=EB=A1=9C=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/j9/bestmoments/domain/Video.java | 14 ++++++-------- .../com/j9/bestmoments/service/VideoService.java | 6 ++++-- .../com/j9/bestmoments/util/FileNameGenerator.java | 6 ++++++ 3 files changed, 16 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/j9/bestmoments/domain/Video.java b/src/main/java/com/j9/bestmoments/domain/Video.java index cb75bfa..2dc6434 100644 --- a/src/main/java/com/j9/bestmoments/domain/Video.java +++ b/src/main/java/com/j9/bestmoments/domain/Video.java @@ -1,13 +1,10 @@ package com.j9.bestmoments.domain; -import jakarta.persistence.Column; import jakarta.persistence.ElementCollection; import jakarta.persistence.Entity; import jakarta.persistence.EntityListeners; import jakarta.persistence.EnumType; import jakarta.persistence.Enumerated; -import jakarta.persistence.GeneratedValue; -import jakarta.persistence.GenerationType; import jakarta.persistence.Id; import jakarta.persistence.JoinColumn; import jakarta.persistence.Lob; @@ -34,9 +31,6 @@ public class Video { @Id - @GeneratedValue(strategy = GenerationType.UUID) - @GenericGenerator(name="uuid2", strategy = "uuid2") - @Column(columnDefinition = "BINARY(16)") private UUID id; private String fileUrl; private String title; @@ -58,9 +52,9 @@ public class Video { private List tags = new ArrayList<>(); @Builder - public Video(Member uploader, String fileUrl, String title, String description, VideoStatus videoStatus) { + public Video(Member uploader, String title, String description, VideoStatus videoStatus) { + this.id = UUID.randomUUID(); this.uploader = uploader; - this.fileUrl = fileUrl; this.title = title; this.description = description; this.videoStatus = videoStatus; @@ -90,4 +84,8 @@ public void setVideoTags(List tags) { this.tags = tags; } + public void setFileUrl(String fileUrl) { + this.fileUrl = fileUrl; + } + } diff --git a/src/main/java/com/j9/bestmoments/service/VideoService.java b/src/main/java/com/j9/bestmoments/service/VideoService.java index 8c7ae71..130d8d2 100644 --- a/src/main/java/com/j9/bestmoments/service/VideoService.java +++ b/src/main/java/com/j9/bestmoments/service/VideoService.java @@ -7,6 +7,7 @@ import com.j9.bestmoments.dto.request.VideoCreateDto; import com.j9.bestmoments.dto.request.VideoUpdateDto; import com.j9.bestmoments.repository.VideoRepository; +import com.j9.bestmoments.util.FileNameGenerator; import jakarta.persistence.EntityNotFoundException; import jakarta.security.auth.message.AuthException; import java.util.List; @@ -28,14 +29,15 @@ public class VideoService { @Transactional public Video upload(Member member, VideoCreateDto createDto) { - String fileUrl = storageService.uploadFile(createDto.file(), createDto.title()); Video video = Video.builder() - .fileUrl(fileUrl) .uploader(member) .videoStatus(createDto.videoStatus()) .title(createDto.title()) .description(createDto.description()) .build(); + String fileName = FileNameGenerator.generateVideoFileName(video); + String fileUrl = storageService.uploadFile(createDto.file(), fileName); + video.setFileUrl(fileUrl); videoRepository.save(video); return video; } diff --git a/src/main/java/com/j9/bestmoments/util/FileNameGenerator.java b/src/main/java/com/j9/bestmoments/util/FileNameGenerator.java index b087ab5..2d8a749 100644 --- a/src/main/java/com/j9/bestmoments/util/FileNameGenerator.java +++ b/src/main/java/com/j9/bestmoments/util/FileNameGenerator.java @@ -1,6 +1,7 @@ package com.j9.bestmoments.util; import com.j9.bestmoments.domain.Member; +import com.j9.bestmoments.domain.Video; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; @@ -12,6 +13,11 @@ public static String generateProfileImageFileName(Member member) { return String.format("profile/%s/%s", memberId, dateString); } + public static String generateVideoFileName(Video video) { + String videoId = video.getId().toString(); + return String.format("video/%s/video-origin", videoId); + } + private static String generateDateString() { LocalDateTime now = LocalDateTime.now(); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");