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

[FIX] 이미지 캐싱 오류 해결 및 일부 코드 개선 #391

Merged
merged 9 commits into from
Oct 16, 2024
36 changes: 16 additions & 20 deletions src/main/java/synk/meeteam/infra/aws/service/CloudFrontService.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import static synk.meeteam.infra.aws.S3FilePath.USER;
import static synk.meeteam.infra.aws.S3FilePath.getFileFullName;
import static synk.meeteam.infra.aws.S3FilePath.getPortfolioPath;
import static synk.meeteam.infra.aws.S3FilePath.getZipFileFullName;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
Expand All @@ -13,12 +12,10 @@
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.List;
import java.util.UUID;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import software.amazon.awssdk.services.cloudfront.CloudFrontUtilities;
import software.amazon.awssdk.services.cloudfront.model.CannedSignerRequest;
import software.amazon.awssdk.services.cloudfront.url.SignedUrl;
Expand All @@ -28,6 +25,9 @@
import synk.meeteam.infra.aws.exception.AwsException;
import synk.meeteam.infra.aws.exception.AwsExceptionType;
import synk.meeteam.infra.aws.service.vo.SignedUrlVO;
import synk.meeteam.infra.aws.strategy.NoPortfolioStrategy;
import synk.meeteam.infra.aws.strategy.PortfolioExistStrategy;
import synk.meeteam.infra.aws.strategy.PortfolioStrategy;

@Component
@RequiredArgsConstructor
Expand All @@ -41,10 +41,10 @@ public class CloudFrontService {
@Value("${aws-property.key-pair-id}")
private String keyPairId;

public String getSignedUrl(String path, String fileName) {
public String getSignedUrl(String path, String fileName, long version) {
try {
String resourcePath = getEncodedResourcePath(path, fileName);
String cloudFrontUrl = "https://" + distributionDomain + "/" + resourcePath;
String cloudFrontUrl = String.format("https://%s/%s?v=%d", distributionDomain, resourcePath, version);
Instant expirationTime = Instant.now().plus(PRE_SIGNED_URL_EXPIRE_SECONDS, ChronoUnit.SECONDS);
Path keyPath = Paths.get(privateKeyFilePath);
CloudFrontUtilities cloudFrontUtilities = CloudFrontUtilities.create();
Expand All @@ -64,29 +64,25 @@ public String getSignedUrl(String path, String fileName) {
}
}

public SignedUrlVO getProfileSignedUrl(String extension, Long userId) {
public SignedUrlVO getProfileSignedUrl(String extension, Long userId, long version) {
String filename = getFileFullName(Encryption.encryptLong(userId), extension);
String url = getSignedUrl(USER, filename);
String url = getSignedUrl(USER, filename, version);
return SignedUrlVO.of(ServiceType.PROFILE, filename, url);
}

public List<SignedUrlVO> getPortfolioSignedUrl(String thumbnailExtension, Portfolio portfolio, Long userId) {
PortfolioStrategy strategy =
(portfolio != null) ? new PortfolioExistStrategy(portfolio) : new NoPortfolioStrategy();

mikekks marked this conversation as resolved.
Show resolved Hide resolved
String zipFileName = getZipFileFullName(UUID.randomUUID().toString());
String thumbNailFileName = UUID.randomUUID().toString();
if (portfolio != null) {
zipFileName = portfolio.getZipFileName();
thumbNailFileName = StringUtils.stripFilenameExtension(portfolio.getMainImageFileName());
}
thumbNailFileName = getFileFullName(thumbNailFileName, thumbnailExtension);

String encryptedId = Encryption.encryptLong(userId);
String zipUrl = getSignedUrl(getPortfolioPath(encryptedId), zipFileName);
String thumbNailUrl = getSignedUrl(getPortfolioPath(encryptedId), thumbNailFileName);
String portfolioPath = getPortfolioPath(Encryption.encryptLong(userId));
String zipUrl = getSignedUrl(portfolioPath, strategy.getZipFileName(), strategy.getVersion());
String thumbNailUrl = getSignedUrl(portfolioPath, strategy.getThumbnailFileName(thumbnailExtension),
strategy.getVersion());

return List.of(
SignedUrlVO.of(ServiceType.PORTFOLIOS, zipFileName, zipUrl),
SignedUrlVO.of(ServiceType.THUMBNAIL_PORTFOLIO, thumbNailFileName, thumbNailUrl)
SignedUrlVO.of(ServiceType.PORTFOLIOS, strategy.getZipFileName(), zipUrl),
SignedUrlVO.of(ServiceType.THUMBNAIL_PORTFOLIO, strategy.getThumbnailFileName(thumbnailExtension),
thumbNailUrl)
);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package synk.meeteam.infra.aws.strategy;

import static synk.meeteam.infra.aws.S3FilePath.getFileFullName;
import static synk.meeteam.infra.aws.S3FilePath.getZipFileFullName;

import java.util.UUID;

public class NoPortfolioStrategy implements PortfolioStrategy {

private String fileName;
private String zipFileName;

public NoPortfolioStrategy() {
this.fileName = UUID.randomUUID().toString();
this.zipFileName = UUID.randomUUID().toString();
}

@Override
public String getZipFileName() {
return getZipFileFullName(fileName);
}

@Override
public String getThumbnailFileName(String thumbnailExtension) {
return getFileFullName(zipFileName, thumbnailExtension);
}

@Override
public long getVersion() {
return 1;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package synk.meeteam.infra.aws.strategy;

import static synk.meeteam.infra.aws.S3FilePath.getFileFullName;

import org.springframework.util.StringUtils;
import synk.meeteam.domain.portfolio.portfolio.entity.Portfolio;

public class PortfolioExistStrategy implements PortfolioStrategy {
private final Portfolio portfolio;

public PortfolioExistStrategy(Portfolio portfolio) {
this.portfolio = portfolio;
}

@Override
public String getZipFileName() {
return portfolio.getZipFileName();
}

@Override
public String getThumbnailFileName(String thumbnailExtension) {
return getFileFullName(StringUtils.stripFilenameExtension(portfolio.getMainImageFileName()),
thumbnailExtension);
}

@Override
public long getVersion() {
return portfolio.getVersion();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package synk.meeteam.infra.aws.strategy;

public interface PortfolioStrategy {
String getZipFileName();

String getThumbnailFileName(String thumbnailExtension);

long getVersion();
}