From bb071c2b9bfaa8afd41f0e1d57b6e80d76f69cdb Mon Sep 17 00:00:00 2001 From: kms Date: Mon, 13 Nov 2023 16:11:40 +0900 Subject: [PATCH] :hammer: feat: add count pdf pages and check limit #56 --- .../common/support/PdfConverter.java | 19 ++++++++ .../controller/LearningFileController.java | 43 ++++++++++++++----- 2 files changed, 52 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/smart/watchboard/common/support/PdfConverter.java b/src/main/java/com/smart/watchboard/common/support/PdfConverter.java index 51d9924..29bd52f 100644 --- a/src/main/java/com/smart/watchboard/common/support/PdfConverter.java +++ b/src/main/java/com/smart/watchboard/common/support/PdfConverter.java @@ -3,7 +3,9 @@ import com.itextpdf.text.Document; import com.itextpdf.text.DocumentException; import com.itextpdf.text.Paragraph; +import com.itextpdf.text.pdf.PdfReader; import com.itextpdf.text.pdf.PdfWriter; +import org.springframework.web.multipart.MultipartFile; import java.io.ByteArrayOutputStream; import java.io.File; @@ -36,4 +38,21 @@ public static File saveByteArrayToFile(byte[] bytes, String fileName) throws IOE System.out.println(outputFile); return outputFile; } + + public static Integer countPdfPage(MultipartFile pdfFile) throws IOException { + byte[] bytes = pdfFile.getBytes(); + PdfReader reader = new PdfReader(bytes); + + // PDF 파일 전체 페이지 수 구하기 + int pageCount = reader.getNumberOfPages(); + + return pageCount; + } + + public static Boolean checkPageLimit(int page) { + if (page > 30) { + return false; + } + return true; + } } diff --git a/src/main/java/com/smart/watchboard/controller/LearningFileController.java b/src/main/java/com/smart/watchboard/controller/LearningFileController.java index a7dd425..ad87479 100644 --- a/src/main/java/com/smart/watchboard/controller/LearningFileController.java +++ b/src/main/java/com/smart/watchboard/controller/LearningFileController.java @@ -1,8 +1,9 @@ package com.smart.watchboard.controller; import com.fasterxml.jackson.core.JsonProcessingException; +import com.itextpdf.text.pdf.PdfDocument; +import com.itextpdf.text.pdf.PdfReader; import com.smart.watchboard.common.support.AwsS3Uploader; -import com.smart.watchboard.domain.File; import com.smart.watchboard.dto.*; import com.smart.watchboard.service.*; import io.swagger.v3.oas.annotations.tags.Tag; @@ -15,10 +16,11 @@ import javax.sound.sampled.UnsupportedAudioFileException; import java.io.IOException; -import java.util.ArrayList; -import java.util.List; import java.util.Optional; +import static com.smart.watchboard.common.support.PdfConverter.checkPageLimit; +import static com.smart.watchboard.common.support.PdfConverter.countPdfPage; + @RestController @RequestMapping("/documents") @Tag(name = "pdf 학습파일 API", description = "학습파일 관련 API") @@ -30,12 +32,17 @@ public class LearningFileController { private final WhiteboardService whiteboardService; private final JwtService jwtService; private final SseService sseService; + private final RequestService requestService; @PostMapping("/{documentID}/pdf") public ResponseEntity uploadLearningFile(@PathVariable(value = "documentID") long documentId, @RequestParam("pdf") MultipartFile pdfFile, @RequestHeader("Authorization") String accessToken) throws UnsupportedAudioFileException, IOException { Optional id = jwtService.extractUserId(accessToken); Long userId = id.orElse(null); + if (!checkPageLimit(countPdfPage(pdfFile))) { + return new ResponseEntity<>(HttpStatus.UNPROCESSABLE_ENTITY); + } + S3Dto s3Dto = new S3Dto(pdfFile, documentId, userId, "pdf"); String path = awsS3Uploader.uploadFile(s3Dto); sseService.notifyKeywords(documentId, path); @@ -56,6 +63,10 @@ public ResponseEntity updateLearningFile(@PathVariable(value = "documentID") Optional id = jwtService.extractUserId(accessToken); Long userId = id.orElse(null); + if (!checkPageLimit(countPdfPage(pdfFile))) { + return new ResponseEntity<>(HttpStatus.UNPROCESSABLE_ENTITY); + } + S3Dto s3Dto = new S3Dto(pdfFile, documentId, userId, "pdf"); String path = awsS3Uploader.uploadFile(s3Dto); sseService.notifyKeywords(documentId, path); @@ -85,13 +96,25 @@ public ResponseEntity getLearningFile(@PathVariable(value = "documentID") lon return responseEntity; } - @GetMapping("/test") - public ResponseEntity test(@RequestHeader("Authorization") String accessToken, @RequestParam("pdf") MultipartFile pdfFile) throws JsonProcessingException { - Optional id = jwtService.extractUserId(accessToken); - Long userId = id.orElse(null); + @PostMapping("/test") + public ResponseEntity test(@RequestHeader("Authorization") String accessToken, @RequestParam("pdf") MultipartFile pdfFile) throws IOException { +// Optional id = jwtService.extractUserId(accessToken); +// Long userId = id.orElse(null); +// +// S3Dto s3Dto = new S3Dto(pdfFile, 11L, userId, "pdf"); +// String path = awsS3Uploader.uploadFile(s3Dto); + + String path = "https://watchboard-record-bucket.s3.ap-northeast-2.amazonaws.com/application/pdf/36.감정분류.pdf"; + ResponseEntity responseEntity = requestService.requestPdfKeywords(path); + + ResponseEntity responseEntity1 = requestService.requestPdfMindmap(path, 36L, responseEntity.getBody().getKeywords()); + +//// PDF 파일 읽기 +// PdfReader reader = new PdfReader(pdfFile); +// +// // PDF 파일 전체 페이지 수 구하기 +// int pageCount = reader.getNumberOfPages(); - S3Dto s3Dto = new S3Dto(pdfFile, 11L, userId, "pdf"); - String path = awsS3Uploader.uploadFile(s3Dto); //System.out.println(path); //String path = "abcd"; //ResponseEntity responseEntity = requestService.requestPdfKeywords(path); @@ -117,7 +140,7 @@ public ResponseEntity test(@RequestHeader("Authorization") String accessToken //String body = fileService.getPdfUrl(4L); //System.out.println(body); - return new ResponseEntity<>("", HttpStatus.OK); + return new ResponseEntity<>(responseEntity1, HttpStatus.OK); } }