Skip to content

Commit

Permalink
Merge pull request #59 from SWM-SMART/feat/#58
Browse files Browse the repository at this point in the history
PDF 업로드 페이지 제한 기능 구현
  • Loading branch information
noparamin authored Nov 13, 2023
2 parents ea0315a + bb071c2 commit 48e89ab
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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")
Expand All @@ -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<Long> 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);
Expand All @@ -56,6 +63,10 @@ public ResponseEntity<?> updateLearningFile(@PathVariable(value = "documentID")
Optional<Long> 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);
Expand Down Expand Up @@ -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<Long> 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<Long> 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<KeywordsBodyDto> responseEntity = requestService.requestPdfKeywords(path);

ResponseEntity<MindmapResponseDto> 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<String> responseEntity = requestService.requestPdfKeywords(path);
Expand All @@ -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);

}
}

0 comments on commit 48e89ab

Please sign in to comment.