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 d76b579 commit 38e3734
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 6 deletions.
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);

}
2 changes: 1 addition & 1 deletion src/main/java/com/j9/bestmoments/service/VideoService.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class VideoService {

@Transactional
public Video upload(Member member, VideoCreateDto createDto) {
String fileUrl = storageService.uploadFile(createDto.file());
String fileUrl = storageService.uploadFile(createDto.file(), createDto.title());
Video video = Video.builder()
.fileUrl(fileUrl)
.uploader(member)
Expand Down
21 changes: 21 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,21 @@
package com.j9.bestmoments.util;

import com.j9.bestmoments.domain.Member;
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);
}

private static String generateDateString() {
LocalDateTime now = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
return now.format(formatter);
}

}

0 comments on commit 38e3734

Please sign in to comment.