Skip to content
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

refactor: redirectUrl 추상화 #679

Merged
merged 9 commits into from
Dec 27, 2024
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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이 커밋에서 작업하신 개선 사항을 이전 커밋에서 필요해보여 길게 조심스럽게 제안해두었는데, 역시 포케도 저와 뜻이 같았군요 ㅎㅎ 훨씬 깔끔하고 좋네요! 이런 리팩터링 너무 좋습니다 👏

}

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