Skip to content

Commit

Permalink
Merge pull request #34 from Team-J9/feature/storage
Browse files Browse the repository at this point in the history
Feature/storage
  • Loading branch information
SJ70 authored Aug 8, 2024
2 parents d76b579 + aab5d67 commit 0354a8b
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 15 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;
}

}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);
}

}
4 changes: 3 additions & 1 deletion src/main/java/com/j9/bestmoments/service/MemberService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@

public interface StorageService {

String uploadFile(MultipartFile file);
String uploadFile(MultipartFile file, String fileName);

}
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());
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
27 changes: 27 additions & 0 deletions src/main/java/com/j9/bestmoments/util/FileNameGenerator.java
Original file line number Diff line number Diff line change
@@ -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);
}

}

0 comments on commit 0354a8b

Please sign in to comment.