Skip to content

Commit

Permalink
[JT-38] feat: 이미지 타입 별 s3 경로 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
kmebin committed Sep 5, 2023
1 parent 74d3ab2 commit d40ad5a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,21 @@ public class S3Service {
@Value("${spring.cloud.aws.cloud-front.url}")
private String IMAGE_URL;

public String upload(ImageType imageType, MultipartFile image) {
String uuid = UUID.randomUUID().toString();
//TODO 확장자 포함 + 파일명 변경
String key = imageType.getPath(uuid);
//TODO 확장자 포함 + 파일명 변경
public String upload(ImageType imageType, String webtoonTitle, MultipartFile image) {
String fileName = UUID.randomUUID().toString();
return uploadInternal(imageType, webtoonTitle, fileName, image);
}

public String upload(ImageType imageType, String webtoonTitle, int no, MultipartFile image) {
UUID uuid = UUID.randomUUID();
String fileName = String.format("%04d_", no) + uuid;

return uploadInternal(imageType, webtoonTitle, fileName, image);
}

private String uploadInternal(ImageType imageType, String webtoonTitle, String fileName, MultipartFile image) {
String key = imageType.getPath(webtoonTitle, fileName);
s3Uploader.upload(key, image);

return IMAGE_URL + key;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
public enum ImageType {

WEBTOON_THUMBNAIL("webtoons/thumbnail/%s"),
EPISODE_MAIN("webtoons/episodes/main/%s"),
EPISODE_THUMBNAIL("webtoons/episodes/thumbnail/%s"),
WEBTOON_THUMBNAIL("webtoons/%s/thumbnail/%s"),
EPISODE_MAIN("webtoons/%s/episodes/main/%s"),
EPISODE_THUMBNAIL("webtoons/%s/episodes/thumbnail/%s"),
;

private final String pathFormat;

public String getPath(String filename) {
return String.format(pathFormat, filename);
public String getPath(String webtoonTitle, String filename) {
return String.format(pathFormat, webtoonTitle, filename);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ public class WebtoonService {
@Transactional
public void createEpisode(Long webtoonId, CreateEpisodeReq req) {
Webtoon webtoon = getWebtoonById(webtoonId);
String mainUrl = s3Service.upload(EPISODE_MAIN, req.mainImage());
String thumbnailUrl = s3Service.upload(EPISODE_THUMBNAIL, req.thumbnailImage());
String mainUrl = s3Service.upload(EPISODE_MAIN, webtoon.getTitle(), req.no(), req.mainImage());
String thumbnailUrl = s3Service.upload(EPISODE_THUMBNAIL, webtoon.getTitle(), req.no(), req.thumbnailImage());
Episode episode = req.toEntity(webtoon, mainUrl, thumbnailUrl);
episodeRepository.save(episode);
}
Expand Down

0 comments on commit d40ad5a

Please sign in to comment.