Skip to content

Commit

Permalink
refactor: 동영상 저장 시 Storage 내 경로 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
SJ70 committed Aug 8, 2024
1 parent 38e3734 commit aab5d67
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
14 changes: 6 additions & 8 deletions src/main/java/com/j9/bestmoments/domain/Video.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -58,9 +52,9 @@ public class Video {
private List<String> 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;
Expand Down Expand Up @@ -90,4 +84,8 @@ public void setVideoTags(List<String> tags) {
this.tags = tags;
}

public void setFileUrl(String fileUrl) {
this.fileUrl = fileUrl;
}

}
6 changes: 4 additions & 2 deletions src/main/java/com/j9/bestmoments/service/VideoService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/com/j9/bestmoments/util/FileNameGenerator.java
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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");
Expand Down

0 comments on commit aab5d67

Please sign in to comment.