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/GoogleCloudStorageService.java b/src/main/java/com/j9/bestmoments/service/GoogleCloudStorageService.java index d8dd5b2..46d38eb 100644 --- a/src/main/java/com/j9/bestmoments/service/GoogleCloudStorageService.java +++ b/src/main/java/com/j9/bestmoments/service/GoogleCloudStorageService.java @@ -1,6 +1,8 @@ package com.j9.bestmoments.service; import com.google.auth.oauth2.GoogleCredentials; +import com.j9.bestmoments.domain.Member; +import com.j9.bestmoments.util.FileNameGenerator; import java.nio.file.Files; import java.nio.file.Paths; import java.util.UUID; @@ -35,17 +37,19 @@ public GoogleCloudStorageService( // 다운로드 링크를 반환 @Override - public String uploadFile(MultipartFile file) { - String fileName = String.format("%s.%s", UUID.randomUUID(), file.getContentType().split("/")[1]); + public String uploadFile(MultipartFile file, String fileName) { + String contentType = file.getContentType().split("/")[1]; BlobId blobId = BlobId.of(bucketName, fileName); BlobInfo blobInfo = BlobInfo.newBuilder(blobId).build(); try { storage.create(blobInfo, file.getInputStream()); } catch (Exception e) { + throw new RuntimeException("IOException"); } log.info("blobId = {}", blobId); - return String.format("https://storage.cloud.google.com/%s/%s", bucketName, fileName); + return String.format("https://storage.cloud.google.com/%s/%s.%s", bucketName, fileName, contentType); } + } diff --git a/src/main/java/com/j9/bestmoments/service/MemberService.java b/src/main/java/com/j9/bestmoments/service/MemberService.java index 74d9dd9..50a3e10 100644 --- a/src/main/java/com/j9/bestmoments/service/MemberService.java +++ b/src/main/java/com/j9/bestmoments/service/MemberService.java @@ -5,6 +5,7 @@ import com.j9.bestmoments.domain.MemberRole; import com.j9.bestmoments.domain.Member; import com.j9.bestmoments.repository.MemberRepository; +import com.j9.bestmoments.util.FileNameGenerator; import jakarta.persistence.EntityNotFoundException; import java.util.UUID; import lombok.RequiredArgsConstructor; @@ -65,7 +66,8 @@ public Member update(Member member, MemberUpdateDto memberUpdateDto) { member.setDescription(memberUpdateDto.description()); } if (memberUpdateDto.file() != null) { - String profileImageUrl = googleCloudStorageService.uploadFile(memberUpdateDto.file()); + String fileName = FileNameGenerator.generateProfileImageFileName(member); + String profileImageUrl = googleCloudStorageService.uploadFile(memberUpdateDto.file(), fileName); member.setProfileImageUrl(profileImageUrl); } return memberRepository.save(member); diff --git a/src/main/java/com/j9/bestmoments/service/StorageService.java b/src/main/java/com/j9/bestmoments/service/StorageService.java index f463463..20b5f7e 100644 --- a/src/main/java/com/j9/bestmoments/service/StorageService.java +++ b/src/main/java/com/j9/bestmoments/service/StorageService.java @@ -4,6 +4,6 @@ public interface StorageService { - String uploadFile(MultipartFile file); + String uploadFile(MultipartFile file, String fileName); } diff --git a/src/main/java/com/j9/bestmoments/service/VideoService.java b/src/main/java/com/j9/bestmoments/service/VideoService.java index 8a865a5..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()); 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 new file mode 100644 index 0000000..2d8a749 --- /dev/null +++ b/src/main/java/com/j9/bestmoments/util/FileNameGenerator.java @@ -0,0 +1,27 @@ +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; + +public final class FileNameGenerator { + + public static String generateProfileImageFileName(Member member) { + String memberId = member.getId().toString(); + String dateString = generateDateString(); + 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"); + return now.format(formatter); + } + +}