Skip to content

Commit

Permalink
Merge pull request #30 from SMWU-POCHAK/feat/29-caching
Browse files Browse the repository at this point in the history
[FEAT/29-caching] 이미지 로딩 속도 개선
  • Loading branch information
5jisoo authored Jul 7, 2024
2 parents 33b200e + 29234cf commit c7285ae
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions src/main/java/com/apps/pochak/global/s3/S3Service.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ public class S3Service {
@Value("${cloud.aws.s3.bucket}")
private String bucket;

@Value("${cloud.aws.cloudfront.domain}")
private String domain;

public String upload(MultipartFile multipartFile, DirName dirName) {
if (multipartFile.isEmpty())
throw new GeneralException(NULL_FILE);
Expand All @@ -46,7 +49,8 @@ public String upload(MultipartFile multipartFile, DirName dirName) {

private String upload(File uploadFile, DirName dirName) {
String fileName = dirName.getDirName() + "/" + UUID.randomUUID() + uploadFile.getName();
String uploadImageUrl = putS3(uploadFile, fileName);
putS3(uploadFile, fileName);
String uploadImageUrl = domain+"/"+fileName;
deleteFile(uploadFile);
return uploadImageUrl;
}
Expand Down Expand Up @@ -81,8 +85,15 @@ private void deleteFile(File targetFile) {

public void deleteFileFromS3(String fileUrl) {
try {
String splitStr = ".com/";
String fileName = fileUrl.substring(fileUrl.lastIndexOf(splitStr) + splitStr.length());
String[] splitStrs = {".net/", ".com/"};
String fileName = null;

for (String splitStr : splitStrs) {
if (fileUrl.contains(splitStr)) {
fileName = fileUrl.substring(fileUrl.lastIndexOf(splitStr) + splitStr.length());
break;
}
}
amazonS3Client.deleteObject(new DeleteObjectRequest(bucket, fileName));
} catch (AmazonServiceException e) {
throw new ImageException(DELETE_FILE_ERROR);
Expand Down

0 comments on commit c7285ae

Please sign in to comment.