Skip to content

Commit

Permalink
refactor: redirectUrl 추상화 (#679)
Browse files Browse the repository at this point in the history
* refactor: 생성자 접근 레벨 수정

* chore: 저장소 설정 정보를 application 설정파일로 관리하도록 변경

* refactor: redirectUrl 을 resourceHost로 명칭 변경

* refactor: storage 위치를 외부에서 주입하도록 변경

* refactor: 이미지 Url 변환 메서드 간략하게 변경

* refactor: OfferingMetaResponse 이미지 주소 추상화

* style: eol 개행 추가

* style: eol 개행 추가

* refactor: 불필요한 생성자 제거
  • Loading branch information
fromitive authored Dec 27, 2024
1 parent 9f9b8c8 commit 5d7a739
Show file tree
Hide file tree
Showing 13 changed files with 59 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.zzang.chongdae.offeringmember.exception.OfferingMemberErrorCode;
import com.zzang.chongdae.offeringmember.repository.OfferingMemberRepository;
import com.zzang.chongdae.offeringmember.repository.entity.OfferingMemberEntity;
import com.zzang.chongdae.storage.service.StorageService;
import java.util.List;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
Expand All @@ -39,6 +40,7 @@ public class CommentService {
private final CommentRepository commentRepository;
private final OfferingRepository offeringRepository;
private final OfferingMemberRepository offeringMemberRepository;
private final StorageService storageService;

@WriterDatabase
public Long saveComment(CommentSaveRequest request, MemberEntity member) {
Expand Down Expand Up @@ -91,9 +93,10 @@ public CommentRoomInfoResponse getCommentRoomInfo(Long offeringId, MemberEntity
OfferingMemberEntity offeringMember = offeringMemberRepository.findByOfferingIdAndMember(offeringId, member)
.orElseThrow(() -> new MarketException(OfferingMemberErrorCode.NOT_FOUND));
if (offeringRepository.existsById(offeringId)) {
return new CommentRoomInfoResponse(offeringMember.getOffering(), offeringMember.getMember());
return new CommentRoomInfoResponse(offeringMember.getOffering(), offeringMember.getMember(),
storageService.getResourceHost());
}
return new CommentRoomInfoResponse(offeringMember);
return new CommentRoomInfoResponse(offeringMember, storageService.getResourceHost());
}

@WriterDatabase
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,18 @@ public record CommentRoomInfoResponse(CommentRoomStatus status,
String title,
boolean isProposer) {

public CommentRoomInfoResponse(OfferingEntity offering, MemberEntity member) {
public CommentRoomInfoResponse(OfferingEntity offering, MemberEntity member, String resourceHost) {
this(offering.getRoomStatus(),
ViewMapper.toImage(offering.getRoomStatus()),
ViewMapper.toImage(offering.getRoomStatus(), resourceHost),
ViewMapper.toButton(offering.getRoomStatus()),
ViewMapper.toMessage(offering.getRoomStatus()),
offering.getTitle(),
offering.isProposedBy(member));
}

public CommentRoomInfoResponse(OfferingMemberEntity offeringMember) {
public CommentRoomInfoResponse(OfferingMemberEntity offeringMember, String resourceHost) {
this(DELETED,
ViewMapper.toImage(DELETED),
ViewMapper.toImage(DELETED, resourceHost),
ViewMapper.toButton(DELETED),
ViewMapper.toMessage(DELETED),
"삭제된 공동구매입니다.",
Expand All @@ -41,11 +41,11 @@ public CommentRoomInfoResponse(OfferingMemberEntity offeringMember) {

private enum ViewMapper {

DELETED_VIEW(DELETED, imageUrl("DELETED"), "삭제된 공고", "삭제된 공동구매입니다."),
GROUPING_VIEW(GROUPING, imageUrl("GROUPING"), "인원확정", "공동구매에 참여할 인원이\n모이면 인원을 확정하세요."),
BUYING_VIEW(BUYING, imageUrl("BUYING"), "구매확정", "총대가 물품 구매를 완료하면 확정하세요."),
TRADING_VIEW(TRADING, imageUrl("TRADING"), "거래확정", "총대가 참여자들과\n거래를 완료하면 확정하세요."),
DONE_VIEW(DONE, imageUrl("DONE"), "거래완료", "거래가 완료되었어요.");
DELETED_VIEW(DELETED, "DELETED", "삭제된 공고", "삭제된 공동구매입니다."),
GROUPING_VIEW(GROUPING, "GROUPING", "인원확정", "공동구매에 참여할 인원이\n모이면 인원을 확정하세요."),
BUYING_VIEW(BUYING, "BUYING", "구매확정", "총대가 물품 구매를 완료하면 확정하세요."),
TRADING_VIEW(TRADING, "TRADING", "거래확정", "총대가 참여자들과\n거래를 완료하면 확정하세요."),
DONE_VIEW(DONE, "DONE", "거래완료", "거래가 완료되었어요.");

private final CommentRoomStatus roomStatus;
private final String image;
Expand All @@ -59,8 +59,8 @@ private enum ViewMapper {
this.message = message;
}

private static String toImage(CommentRoomStatus status) {
return findViewMapper(status).image;
private static String toImage(CommentRoomStatus status, String resourceHost) {
return "https://%s/common/%s.png".formatted(resourceHost, findViewMapper(status).image);
}

private static String toButton(CommentRoomStatus status) {
Expand All @@ -77,10 +77,5 @@ private static ViewMapper findViewMapper(CommentRoomStatus status) {
.findFirst()
.orElseThrow(() -> new MarketException(OfferingErrorCode.INVALID_CONDITION));
}

private static String imageUrl(String status) {
String imageUrlFormat = "https://d3a5rfnjdz82qu.cloudfront.net/chongdae-market/images/common/%s.png";
return String.format(imageUrlFormat, status);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,6 @@ private void validateInProgress(OfferingEntity offering) {
public OfferingMetaResponse getOfferingMetaInfo(Long offeringId) {
OfferingEntity offering = offeringRepository.findById(offeringId)
.orElseThrow(() -> new MarketException(OfferingErrorCode.NOT_FOUND));
return new OfferingMetaResponse(offering);
return new OfferingMetaResponse(offering, storageService.getResourceHost());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@

public record OfferingMetaResponse(String title, String thumbnailUrl) {

private static final String DEFAULT_THUMBNAIL_URL = "https://image.chongdae.site/common/no-image.png";
private static final String DEFAULT_RESOURCE_FILE_NAME = "no-image.png";

public OfferingMetaResponse(OfferingEntity offering) {
this(offering.getTitle(), getOrDefault(offering.getThumbnailUrl()));
public OfferingMetaResponse(OfferingEntity offering, String resourceHost) {
this(offering.getTitle(), getOrDefault(offering.getThumbnailUrl(), resourceHost));
}

private static String getOrDefault(String thumbnailUrl) {
private static String getOrDefault(String thumbnailUrl, String resourceHost) {
if (thumbnailUrl == null) {
return DEFAULT_THUMBNAIL_URL;
return "https://%s/common/%s".formatted(resourceHost, DEFAULT_RESOURCE_FILE_NAME);
}
return thumbnailUrl;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@
public class AmazonS3StorageService implements StorageService {

private final AmazonS3 s3Client;

@Value("${amazon.s3.bucket}")
private String bucketName;

@Value("${amazon.cloudfront.redirectUrl}")
private String redirectUrl;
@Value("${storage.resourceHost}")
private String resourceHost;

@Value("${amazon.cloudfront.storagePath}")
@Value("${storage.path}")
private String storagePath;

@Value("${amazon.s3.bucket}")
private String bucketName;

@Override
public String uploadFile(MultipartFile file) {
try {
Expand All @@ -54,9 +54,14 @@ private ObjectMetadata createMetadata(MultipartFile file) {
private String createUri(String objectKey) {
return UriComponentsBuilder.newInstance()
.scheme("https")
.host(redirectUrl)
.host(resourceHost)
.path("/" + objectKey)
.build(false)
.toString();
}

@Override
public String getResourceHost() {
return resourceHost;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,12 @@ public class LocalStorageService implements StorageService {

private static final Set<String> ALLOW_IMAGE_EXTENSIONS = Set.of("jpg", "jpeg", "png", "gif", "bmp", "svg");

@Value("${storage.redirectUrl}")
private String redirectUrl;
@Value("${storage.resourceHost}")
private String resourceHost;

@Value("${storage.path}")
private String storagePath;

LocalStorageService(String redirectUrl, String storagePath) {
this.redirectUrl = redirectUrl;
this.storagePath = storagePath;
}

@Override
public String uploadFile(MultipartFile file) {
String extension = getFileExtension(file);
Expand Down Expand Up @@ -67,9 +62,14 @@ private void validateFileExtension(String extension) {
private String createUri(String objectKey) {
return UriComponentsBuilder.newInstance()
.scheme("https")
.host(redirectUrl)
.host(resourceHost)
.path("/" + objectKey)
.build(false)
.toString();
}

@Override
public String getResourceHost() {
return resourceHost;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@
public interface StorageService {

String uploadFile(MultipartFile file);

String getResourceHost();
}
4 changes: 2 additions & 2 deletions backend/src/main/resources/application-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ security:
refresh-secret-key: ${JWT_REFRESH_SECRET_KEY}

storage:
path: /uploads
redirectUrl: image.dev.chongdae.site
path: ${STORAGE_PHYSICAL_PATH}
resourceHost: ${STORAGE_RESOURCE_HOST}
4 changes: 2 additions & 2 deletions backend/src/main/resources/application-prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ security:
refresh-secret-key: ${JWT_REFRESH_SECRET_KEY}

storage:
path: /uploads
redirectUrl: image.chongdae.site
path: ${STORAGE_PHYSICAL_PATH}
resourceHost: ${STORAGE_RESOURCE_HOST}
2 changes: 2 additions & 0 deletions backend/src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ SPRING_WRITER_DATASOURCE_USERNAME=[SPRING_WRITER_DATASOURCE_USERNAME]
SPRING_WRITER_DATASOURCE_PASSWORD=[SPRING_WRITER_DATASOURCE_PASSWORD]
JWT_ACCESS_SECRET_KEY=[JWT_ACCESS_SECRET_KEY]
JWT_REFRESH_SECRET_KEY=[JWT_REFRESH_SECRET_KEY]
STORAGE_RESOURCE_HOST=[STORAGE_RESOURCE_HOST]
STORAGE_PHYSICAL_PATH=[STORAGE_PHYSICAL_PATH]
4 changes: 4 additions & 0 deletions backend/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,7 @@ management:
fcm:
secret-key:
path: /fcm/chongdaemarket-fcm-key.json

storage:
path: /uploads
resourceHost: image.chongdae.site
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,9 @@ public class FakeStorageService implements StorageService {
public String uploadFile(MultipartFile file) {
return "https://upload-image-url.com/";
}

@Override
public String getResourceHost() {
return "upload-image-url.com";
}
}
4 changes: 0 additions & 4 deletions backend/src/test/resources/application-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,3 @@ security:
fcm:
secret-key:
path: /fcm/chongdaemarket-fcm-key.json

storage:
path: /uploads
redirectUrl: fake.image.chongdae.site

0 comments on commit 5d7a739

Please sign in to comment.