Skip to content

Commit

Permalink
Merge pull request #38 from Team-J9/feature/local-storage
Browse files Browse the repository at this point in the history
로컬 저장소 구현
  • Loading branch information
SJ70 authored Aug 14, 2024
2 parents a53860f + fd522fe commit 5847f76
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 21 deletions.
3 changes: 2 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.service.storageService.StorageService;
import com.j9.bestmoments.util.FileNameGenerator;
import jakarta.persistence.EntityNotFoundException;
import java.util.UUID;
Expand Down Expand Up @@ -66,7 +67,7 @@ public Member update(Member member, MemberUpdateDto memberUpdateDto) {
member.setDescription(memberUpdateDto.description());
}
if (memberUpdateDto.file() != null) {
String fileName = FileNameGenerator.generateProfileImageFileName(member);
String fileName = FileNameGenerator.generateProfileImageFileName(member, memberUpdateDto.file());
String profileImageUrl = googleCloudStorageService.uploadFile(memberUpdateDto.file(), fileName);
member.setProfileImageUrl(profileImageUrl);
}
Expand Down
12 changes: 5 additions & 7 deletions src/main/java/com/j9/bestmoments/service/VideoService.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
package com.j9.bestmoments.service;

import com.j9.bestmoments.domain.Member;
import com.j9.bestmoments.domain.MemberRole;
import com.j9.bestmoments.domain.Video;
import com.j9.bestmoments.domain.VideoStatus;
import com.j9.bestmoments.dto.request.VideoCreateDto;
import com.j9.bestmoments.dto.request.VideoUpdateDto;
import com.j9.bestmoments.repository.VideoRepository;
import com.j9.bestmoments.service.storageService.LocalStorageService;
import com.j9.bestmoments.util.FileNameGenerator;
import jakarta.persistence.EntityNotFoundException;
import jakarta.security.auth.message.AuthException;
import java.util.List;
import java.util.UUID;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand All @@ -25,7 +23,7 @@
public class VideoService {

private final VideoRepository videoRepository;
private final StorageService storageService;
private final LocalStorageService storageService;

@Transactional
public Video upload(Member member, VideoCreateDto createDto) {
Expand All @@ -36,11 +34,11 @@ public Video upload(Member member, VideoCreateDto createDto) {
.description(createDto.description())
.build();

String videoName = FileNameGenerator.generateVideoFileName(video);
String videoName = FileNameGenerator.generateVideoFileName(video, createDto.video());
String videoUrl = storageService.uploadFile(createDto.video(), videoName);
video.setVideoUrl(videoUrl);

String thumbnailName = FileNameGenerator.generateThumbnailImageFileName(video);
String thumbnailName = FileNameGenerator.generateThumbnailImageFileName(video, createDto.thumbnail());
String thumbnailUrl = storageService.uploadFile(createDto.thumbnail(), thumbnailName);
video.setThumbnailUrl(thumbnailUrl);

Expand Down Expand Up @@ -80,7 +78,7 @@ public Video findPublicById(UUID id) {
@Transactional
public Video update(Video video, VideoUpdateDto updateDto) {
if (updateDto.thumbnail() != null) {
String thumbnailName = FileNameGenerator.generateThumbnailImageFileName(video);
String thumbnailName = FileNameGenerator.generateThumbnailImageFileName(video, updateDto.thumbnail());
String thumbnailUrl = storageService.uploadFile(updateDto.thumbnail(), thumbnailName);
video.setThumbnailUrl(thumbnailUrl);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package com.j9.bestmoments.service;
package com.j9.bestmoments.service.storageService;

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;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
Expand Down Expand Up @@ -38,7 +35,6 @@ public GoogleCloudStorageService(
// 다운로드 링크를 반환
@Override
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 {
Expand All @@ -49,7 +45,7 @@ public String uploadFile(MultipartFile file, String fileName) {
throw new RuntimeException("IOException");
}
log.info("blobId = {}", blobId);
return String.format("https://storage.cloud.google.com/%s/%s.%s", bucketName, fileName, contentType);
return String.format("https://storage.cloud.google.com/%s/%s", bucketName, fileName);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.j9.bestmoments.service.storageService;

import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

@Service
@Slf4j
public class LocalStorageService implements StorageService {

@Value("${storage.local.dir}")
private String dir;

@Override
public String uploadFile(MultipartFile file, String fileName) {
try {
Path path = Paths.get(dir + fileName);
if (!path.toFile().exists()) {
path.toFile().mkdirs();
}
File destinationFile = new File(dir, fileName);
file.transferTo(destinationFile);
return destinationFile.getAbsolutePath();

} catch (IOException e) {
e.printStackTrace();
return "파일 업로드에 실패했습니다.";
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.j9.bestmoments.service;
package com.j9.bestmoments.service.storageService;

import org.springframework.web.multipart.MultipartFile;

Expand Down
23 changes: 17 additions & 6 deletions src/main/java/com/j9/bestmoments/util/FileNameGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,28 @@
import com.j9.bestmoments.domain.Video;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import org.springframework.web.multipart.MultipartFile;

public final class FileNameGenerator {

public static String generateProfileImageFileName(Member member) {
public static String generateProfileImageFileName(Member member, MultipartFile file) {
String memberId = member.getId().toString();
String dateString = generateDateString();
return String.format("profile/%s/%s", memberId, dateString);
String contentType = getContentType(file);
return String.format("profile/%s/%s.%s", memberId, dateString, contentType);
}

public static String generateVideoFileName(Video video) {
public static String generateVideoFileName(Video video, MultipartFile file) {
String videoId = video.getId().toString();
return String.format("video/%s/video-origin", videoId);
String contentType = getContentType(file);
return String.format("video/%s/video-origin.%s", videoId, contentType);
}

public static String generateThumbnailImageFileName(Video video) {
public static String generateThumbnailImageFileName(Video video, MultipartFile file) {
String videoId = video.getId().toString();
String dateString = generateDateString();
return String.format("video/%s/thumbnail-%s", videoId, dateString);
String contentType = getContentType(file);
return String.format("video/%s/thumbnail-%s.%s", videoId, dateString, contentType);
}

private static String generateDateString() {
Expand All @@ -30,4 +34,11 @@ private static String generateDateString() {
return now.format(formatter);
}

private static String getContentType(MultipartFile file) {
if (file.isEmpty()) {
throw new RuntimeException("파일이 존재하지 않음");
}
return file.getContentType().split("/")[1];
}

}

0 comments on commit 5847f76

Please sign in to comment.