From a0b65828dbcc0ba60d813df39e174332bb9d84d5 Mon Sep 17 00:00:00 2001 From: jinno Yun Date: Sun, 22 Sep 2024 01:41:58 +0900 Subject: [PATCH] =?UTF-8?q?#21=20feat:=20=ED=8A=B9=EC=A0=95=20=EC=9D=B8?= =?UTF-8?q?=EC=A6=9D=EC=84=9C(pdf)=20=EC=82=AD=EC=A0=9C=ED=95=98=EB=8A=94?= =?UTF-8?q?=20API=20=EC=83=9D=EC=84=B1(=EB=A9=94=ED=83=80=EB=8D=B0?= =?UTF-8?q?=EC=9D=B4=ED=84=B0=20=EC=88=98=EC=A0=95=20=EB=A1=9C=EC=A7=81=20?= =?UTF-8?q?=EC=A0=9C=EC=99=B8)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/S3StorageController.java | 18 +++++-- .../s3Storage/service/S3StorageService.java | 52 +++++++++++++++---- 2 files changed, 57 insertions(+), 13 deletions(-) diff --git a/web3-credential-server/src/main/java/web3/s3Storage/controller/S3StorageController.java b/web3-credential-server/src/main/java/web3/s3Storage/controller/S3StorageController.java index 76139e7..9b3f1ca 100644 --- a/web3-credential-server/src/main/java/web3/s3Storage/controller/S3StorageController.java +++ b/web3-credential-server/src/main/java/web3/s3Storage/controller/S3StorageController.java @@ -30,7 +30,7 @@ public S3StorageController(S3StorageService s3StorageService, WalletService wall this.walletService = walletService; } - @Operation(summary = "pdf 업로드",description = "s3 스토리지에 pdf 파일을 업로드 합니다.") + @Operation(summary = "인증서(pdf) 업로드",description = "인증서를 업로드합니다. 즉,s3 스토리지에 pdf 파일을 업로드 합니다.") @PostMapping("/upload-pdf") public ResponseEntity uploadPdf( @Parameter(description = "업로드할 pdf 파일",required = true) @@ -52,9 +52,9 @@ public ResponseEntity uploadPdf( } - @Operation(summary = "pdf 업로드",description = "s3 스토리지에 pdf 파일을 업로드 합니다.") + @Operation(summary = "모든 인증서 삭제(지갑 삭제)",description = "모든 인증서를 삭제합니다. 즉,s3 스토리지에 pdf 파일을 모두 삭제합니다.") @DeleteMapping("/delete-pdf") - public ResponseEntity deletePdf( + public ResponseEntity deleteWallet( @Parameter(description = "지울 pdf 파일 경로 관련 Dto",required = true) @RequestBody DeletePdfRequest request) { String urlToDelete = request.getUrlToDelete(); @@ -63,6 +63,18 @@ public ResponseEntity deletePdf( return ResponseEntity.noContent().build(); } + @Operation(summary = "특정 인증서 삭제",description = "지갑에서 특정 인증서를 삭제합니다. 즉,S3 스토리지에 pdf 파일에서 특정 페이지를 삭제합니다.") + @DeleteMapping("/delete-onepdf") + public ResponseEntity deleteCertForPage( + @Parameter(description = "지울 지갑 ID",required = true) + @RequestBody Long walletId, + @Parameter(description = "지울 페이지 번호",required = true) + @RequestBody int page) { + Wallet wallet = walletService.getWalletById(walletId).orElseThrow(()-> new EntityNotFoundException("Wallet does not exist")); + s3StorageService.deletePdfForPage(wallet,page); + return ResponseEntity.noContent().build(); + } + @Operation(summary = "인증서 리스트 얻기",description = "개인의 인증서들을 모두 가져옵니다. 형식 : (페이지 - 설명)") @GetMapping("/certs") public ResponseEntity> getCertList( diff --git a/web3-credential-server/src/main/java/web3/s3Storage/service/S3StorageService.java b/web3-credential-server/src/main/java/web3/s3Storage/service/S3StorageService.java index 7f35a7e..a95b293 100644 --- a/web3-credential-server/src/main/java/web3/s3Storage/service/S3StorageService.java +++ b/web3-credential-server/src/main/java/web3/s3Storage/service/S3StorageService.java @@ -18,7 +18,6 @@ import java.io.*; import java.util.HashMap; import java.util.Map; -import java.util.UUID; @Slf4j @Service @@ -69,7 +68,7 @@ public String uploadPdf(MultipartFile file, Wallet wallet,String pdfInfo,String log.info("metadata = {}", metadata); try { - uploadToS3(file, fileName, metadata, result); + uploadToS3(fileName, metadata, result); } catch (S3Exception e) { throw new IOException("Failed to upload pdf to S3: " + e.getMessage()); @@ -81,11 +80,11 @@ public String uploadPdf(MultipartFile file, Wallet wallet,String pdfInfo,String return getpdfUrl(fileName); } - private void uploadToS3(MultipartFile file, String fileName, HashMap metadata, byte[] result) { + private void uploadToS3(String fileName, HashMap metadata, byte[] result) { PutObjectRequest putRequest = PutObjectRequest.builder() .bucket(s3Properties.getS3BucketName()) .key(fileName) - .contentType(file.getContentType()) + .contentType("application/pdf") .metadata(metadata) .build(); @@ -156,7 +155,7 @@ public String replacePdfPage(Wallet wallet, int pageNumberToRemove, MultipartFil // 최종 PDF를 S3에 업로드 try { - uploadToS3(newPdfFile, fileName, metadata, finalPdfBytes); + uploadToS3(fileName, metadata, finalPdfBytes); } catch (S3Exception e) { throw new IOException("Failed to upload pdf to S3: " + e.getMessage()); } finally { @@ -228,8 +227,6 @@ public HashMap getCertList(String pdfUrl) { return certList; } - - public HashMap getPdfMetadata( String pdfUrl) { HashMap metadata; String fileName = extractKeyFromUrl(pdfUrl); @@ -318,11 +315,8 @@ private void validatePdfFile(String filename) { } } - - //예외처리하기 public void deletePdf(String urlToDelete) { String key = extractKeyFromUrl(urlToDelete); - System.out.println("key = " + key); try { s3Client.deleteObject(DeleteObjectRequest.builder() .bucket(s3Properties.getS3BucketName()) @@ -333,6 +327,43 @@ public void deletePdf(String urlToDelete) { } } + public void deletePdfForPage(Wallet wallet, int pageNumberToRemove) throws IOException { + String pdfUrl = wallet.getPdfUrl(); + byte[] originalPdfBytes = getPdf(pdfUrl).readAllBytes(); + String fileName = extractKeyFromUrl(pdfUrl); + + // 기존 PDF 로드 + PDDocument originalDocument = PDDocument.load(new ByteArrayInputStream(originalPdfBytes)); + int totalPages = originalDocument.getNumberOfPages(); + + // 페이지 번호는 0부터 시작하므로 1을 빼줌 + int pageIndexToRemove = pageNumberToRemove - 1; + + // 페이지가 존재하는지 확인 + if (pageIndexToRemove < 0 || pageIndexToRemove >= totalPages) { + throw new IllegalArgumentException("Page number out of range: " + pageNumberToRemove); + } + + byte[] frontPart = createPdfBytesPart(originalDocument, 0, pageIndexToRemove); + byte[] backPart = createPdfBytesPart(originalDocument, pageIndexToRemove + 1, originalDocument.getNumberOfPages()); + + // PDF 합치기 + byte[] finalPdfBytes = mergePdfs(frontPart,backPart); + HashMap metadata = getPdfMetadata(fileName); + + //메타데이터 수정 로직 필요 + + // 최종 PDF를 S3에 업로드 + try { + uploadToS3(fileName, metadata, finalPdfBytes); + } catch (S3Exception e) { + throw new IOException("Failed to upload pdf to S3: " + e.getMessage()); + } finally { + originalDocument.close(); + } + + } + public ResponseInputStream getPdf(String pdfUrl) { String key = extractKeyFromUrl(pdfUrl); GetObjectRequest getObjectRequest = GetObjectRequest.builder() @@ -341,6 +372,7 @@ public ResponseInputStream getPdf(String pdfUrl) { .build(); return s3Client.getObject(getObjectRequest); } + private String extractKeyFromUrl(String url) { // Assuming the URL is in the format: https://s3.ap-northeast-2.amazonaws.com/bucketName/fileName int index = url.lastIndexOf('/');