Skip to content

Commit

Permalink
#21 feat: S3StorageService uploadToService 메서드 추출
Browse files Browse the repository at this point in the history
  • Loading branch information
jinno321 committed Sep 19, 2024
1 parent fe0e67d commit a564c9c
Showing 1 changed file with 13 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,8 @@ public String uploadPdf(MultipartFile file, Wallet wallet,String pdfInfo,String

//첫 등록일때 => 생성해줘야함
if (wallet.getPdfUrl() == null){
log.info("file = {}", file);

fileName = (file.getSize() > 0) ? getFileName(file, wallet): getEmptyFilename(wallet);

log.info("fileName = {}", fileName);

// PDF 파일 확장자 검증
//validatePdfFile(fileName);
result = (file.getSize() > 0) ? file.getBytes():createEmptyPdf();
Expand All @@ -57,16 +53,12 @@ public String uploadPdf(MultipartFile file, Wallet wallet,String pdfInfo,String
//이미 있을시 -> pdf 병합
String destination = wallet.getPdfUrl();
fileName = extractKeyFromUrl(destination);
log.info("destination = {}", destination);
log.info("fileName = {}", fileName);

byte[] first = getPdf(destination).readAllBytes();//원래 파일
byte[] second = (file.getSize() > 0) ? file.getBytes() : createEmptyPdf(); //뒤에 들어온 파일

nowPage = getPdfPageCount(first)+1;
result = mergePdfs(first, second);
log.info("merge Success");


metadata= getPdfMetadata(fileName);
}
Expand All @@ -78,14 +70,7 @@ public String uploadPdf(MultipartFile file, Wallet wallet,String pdfInfo,String

try {
// S3에 PDF 파일 업로드
PutObjectRequest putRequest = PutObjectRequest.builder()
.bucket(s3Properties.getS3BucketName())
.key(fileName)
.contentType(file.getContentType())
.metadata(metadata)
.build();

s3Client.putObject(putRequest, RequestBody.fromBytes(result));
uploadToS3(file, fileName, metadata, result);

} catch (S3Exception e) {
throw new IOException("Failed to upload pdf to S3: " + e.getMessage());
Expand All @@ -97,6 +82,17 @@ public String uploadPdf(MultipartFile file, Wallet wallet,String pdfInfo,String
return getpdfUrl(fileName);
}

private void uploadToS3(MultipartFile file, String fileName, HashMap<String, String> metadata, byte[] result) {
PutObjectRequest putRequest = PutObjectRequest.builder()
.bucket(s3Properties.getS3BucketName())
.key(fileName)
.contentType(file.getContentType())
.metadata(metadata)
.build();

s3Client.putObject(putRequest, RequestBody.fromBytes(result));
}

private static String getFileName(MultipartFile file, Wallet wallet) {
return wallet.getAddress() + "_" + System.currentTimeMillis() + "_" + wallet.getAddress();
}
Expand Down Expand Up @@ -161,14 +157,7 @@ public String replacePdfPage(Wallet wallet, int pageNumberToRemove, MultipartFil

// 최종 PDF를 S3에 업로드
try {
PutObjectRequest putRequest = PutObjectRequest.builder()
.bucket(s3Properties.getS3BucketName())
.key(fileName)
.contentType(newPdfFile.getContentType())
.metadata(metadata)
.build();
s3Client.putObject(putRequest, RequestBody.fromBytes(finalPdfBytes));

uploadToS3(newPdfFile, fileName, metadata, finalPdfBytes);
} catch (S3Exception e) {
throw new IOException("Failed to upload pdf to S3: " + e.getMessage());
} finally {
Expand Down

0 comments on commit a564c9c

Please sign in to comment.