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

[TSK-58] image rendering fix #122

Merged
merged 6 commits into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added KakaoTalk_20240724_132729019.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added WIN_20240605_10_27_05_Pro.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added WIN_20240624_21_14_17_Pro.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ public ApiResponse<Map<String, String>> saveFaceTimeAnswer(
fileType = FileType.VIDEO;

// S3에 파일 업로드 및 URL 저장
String uploadedUrl = awsS3Service.uploadFile(answerFile);
String uploadedUrl = awsS3Service.uploadOriginalFile(answerFile);

// Answer 업데이트
answerService.modifyAnswer(challengeId, uploadedUrl);
Expand Down
65 changes: 61 additions & 4 deletions src/main/java/ongjong/namanmoo/service/AwsS3Service.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.time.Instant;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
Expand Down Expand Up @@ -89,6 +92,29 @@ public String uploadFile(MultipartFile multipartFile) throws IOException {
return uploadFileUrl;
}

/**
* MultipartFile을 S3에 업로드하고 업로드된 파일의 URL을 반환하는 메소드.
*
* @param multipartFile 업로드할 MultipartFile
* @return 업로드된 파일의 URL
* @throws IOException 파일 변환 또는 업로드 중 발생하는 예외
*/
public String uploadOriginalFile(MultipartFile multipartFile) throws IOException {
log.info("Converting MultipartFile to File...");
File uploadFile = convertFile(multipartFile)
.orElseThrow(() -> new IllegalArgumentException("MultipartFile -> File convert fail"));

String fileType = determineFileType(multipartFile);
String fileName = generateFileName(uploadFile, fileType);

log.info("Uploading file to S3: {}", fileName);
String uploadFileUrl = uploadFileToS3(uploadFile, fileName);
log.info("File uploaded to S3: {}", uploadFileUrl);

removeNewFile(uploadFile);
return uploadFileUrl;
}

/**
* 이미지를 최적화하는 메소드.
*
Expand Down Expand Up @@ -167,9 +193,8 @@ private String determineFileType(MultipartFile multipartFile) {
* @throws IOException 파일 변환 중 발생하는 예외
*/
private Optional<File> convertFile(MultipartFile file) throws IOException {
// String fileName = UUID.randomUUID() + "_" + file.getOriginalFilename();
String fileName = file.getOriginalFilename();
assert fileName != null;
String fileName = UUID.randomUUID() + "_" + file.getOriginalFilename();
// String fileName = file.getOriginalFilename();
File convertFile = new File(fileName);

if (convertFile.createNewFile()) {
Expand All @@ -190,7 +215,13 @@ private Optional<File> convertFile(MultipartFile file) throws IOException {
* @return String 고유한 파일 이름
*/
private String generateFileName(File uploadFile, String fileType) {
return fileType + "/" + UUID.randomUUID() + "_" + uploadFile.getName();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS")
.withZone(ZoneId.systemDefault());
String formattedDate = formatter.format(Instant.now());

// return fileType + "/" + UUID.randomUUID() + "_" + formattedDate + "_" + uploadFile.getName();
return fileType + "/" + formattedDate + "_" + uploadFile.getName();

}

/**
Expand Down Expand Up @@ -261,6 +292,32 @@ public void delete(String fileName) {
amazonS3Client.deleteObject(bucket, fileName);
}

// 업로드 실패 시 재시도 로직 추가
public String uploadFileWithRetry(MultipartFile multipartFile, int retries) throws IOException, InterruptedException {
File uploadFile = convertFile(multipartFile)
.orElseThrow(() -> new IllegalArgumentException("Incorrect conversion from MultipartFile to File"));

String fileType = determineFileType(multipartFile);
String fileName = generateFileName(uploadFile, fileType);

for (int i = 0; i < retries; i++) {
try {
amazonS3Client.putObject(new PutObjectRequest(bucket, fileName, uploadFile)
.withCannedAcl(CannedAccessControlList.PublicRead));

String uploadedUrl = getS3FileURL(fileName);
log.info("Successfully uploaded file to S3 on retry " + i + ": {}", uploadedUrl);

removeNewFile(uploadFile); // Remember to clean up local file after upload
return uploadedUrl;
} catch (Exception ex) {
log.warn("업로드에 실패하였습니다. 재시도하는중... (시도: {}/{})", i + 1, retries);
Thread.sleep(3000); // waiting for 3 seconds before the next retry
}
}
throw new RuntimeException("Failed to upload file after " + retries + " retries.");
}

// 오디오 파일 고정 경로 생성
public String uploadAudioFile(MultipartFile multipartFile, String s3Path) throws IOException {
log.info("Converting MultipartFile to File...");
Expand Down
Loading