-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[POM-94] 가게 이미지 업로드 기능 추가 #38
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
629adb9
feat: 가게 이미지 경로 경로, 가게 로고 파일 경로 추가
juno-junho d475a6d
feat: image 경로 가져오는 bean 추가
juno-junho ea4fb52
feat: 가게 이미지 설정 기능 관련 dto 및 컨트롤러 메서드 추가
juno-junho b12f40f
feat: 가게와 가게 이미지 일대다 양방향 매핑 설정
juno-junho 2ff7c80
feat: 가게 이미지 저장 및 연관관계 설정 메서드 구현
juno-junho cad82c0
feat: 가게 이미지 repository 추가
juno-junho c4a3bc2
refactor: dto에서 List로 형식 변경
juno-junho 5f6fa97
feat: 파일 요청 사이즈 제한 설정
juno-junho 79e5edb
refactor: path 수정
juno-junho 96ffbd5
test: 각 레이어별 테스트 작성
juno-junho fc67cd4
feat: dev 환경 이미지 저장 directory 생성
juno-junho d5653f0
refactor: pr 리뷰 반영
juno-junho File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
20 changes: 20 additions & 0 deletions
20
src/main/java/com/ray/pominowner/global/config/ImagePathProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.ray.pominowner.global.config; | ||
|
||
import lombok.Getter; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Getter | ||
@Component | ||
public class ImagePathProvider { | ||
|
||
private final String storeImageRootPath; | ||
private final String storeLogoImageSubPath; | ||
|
||
public ImagePathProvider(@Value("${file.store.image.path}") String storeImageRootPath, | ||
@Value("${file.store.logo.path}") String storeLogoImageSubPath) { | ||
this.storeImageRootPath = storeImageRootPath; | ||
this.storeLogoImageSubPath = storeLogoImageSubPath; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
src/main/java/com/ray/pominowner/store/repository/StoreImageRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.ray.pominowner.store.repository; | ||
|
||
import com.ray.pominowner.store.domain.StoreImage; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface StoreImageRepository extends JpaRepository<StoreImage, Long> { | ||
|
||
} |
76 changes: 76 additions & 0 deletions
76
src/main/java/com/ray/pominowner/store/service/StoreImageService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package com.ray.pominowner.store.service; | ||
|
||
import com.ray.pominowner.global.config.ImagePathProvider; | ||
import com.ray.pominowner.store.domain.Store; | ||
import com.ray.pominowner.store.domain.StoreImage; | ||
import com.ray.pominowner.store.repository.StoreImageRepository; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.util.Assert; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.util.List; | ||
import java.util.UUID; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class StoreImageService { | ||
|
||
private static final String DOT = "."; | ||
|
||
private final ImagePathProvider imagePathProvider; | ||
|
||
private final StoreImageRepository storeImageRepository; | ||
|
||
public void saveImages(List<MultipartFile> images, Store store) { | ||
Assert.noNullElements(images, "올바르지 못한 파일입니다."); | ||
String rootPath = imagePathProvider.getStoreImageRootPath(); | ||
images.forEach(image -> saveEachFile(store, image, rootPath)); | ||
} | ||
|
||
private void saveEachFile(Store store, MultipartFile image, String rootPath) { | ||
String originalFilename = image.getOriginalFilename(); | ||
validateFileName(originalFilename); | ||
|
||
String createdFileName = createFileName(originalFilename); | ||
saveImageToPath(image, rootPath + createdFileName); | ||
|
||
storeImageRepository.save(StoreImage.builder() | ||
.path(rootPath + createdFileName) | ||
.uploadName(originalFilename) | ||
.fileName(createdFileName) | ||
.store(store) | ||
.build()); | ||
} | ||
|
||
private void validateFileName(String originalFilename) { | ||
Assert.notNull(originalFilename, "올바르지 못한 파일입니다."); | ||
|
||
if (!originalFilename.contains(DOT)) { | ||
throw new IllegalArgumentException("올바르지 못한 파일입니다."); | ||
} | ||
} | ||
|
||
private void saveImageToPath(MultipartFile image, String path) { | ||
try { | ||
image.transferTo(new File(path)); | ||
} catch (IOException e) { | ||
throw new RuntimeException("파일 저장에 실패했습니다.",e); | ||
} | ||
} | ||
|
||
private String createFileName(String originalFilename) { | ||
String fileExtension = extractFileExtension(originalFilename); | ||
return UUID.randomUUID() | ||
.toString() | ||
.concat(fileExtension); | ||
} | ||
|
||
private String extractFileExtension(String originalFilename) { | ||
int dotIndex = originalFilename.lastIndexOf(DOT); | ||
return originalFilename.substring(dotIndex); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
BaseTimeEntity를 상속하고 있으니
callSuper = false
해당 부분을 추가하면 좋을 것 같습니다!There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
callsuper = false는 default라 명시 안했어요