Skip to content

Commit

Permalink
Merge pull request #162 from AppLinkers/develop
Browse files Browse the repository at this point in the history
Deploy for thumbnail Image
  • Loading branch information
yuseogi0218 authored Jan 9, 2024
2 parents 34f67fd + b93998d commit 271e417
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 15 deletions.
6 changes: 6 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ dependencies {
// aspose-cad
implementation 'com.aspose:aspose-cad:23.7'

// imgscalr
implementation 'org.imgscalr:imgscalr-lib:4.2'

// apache PDFBox
implementation 'org.apache.pdfbox:pdfbox:3.0.0'

// test
testImplementation 'org.springframework.boot:spring-boot-starter-test'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public class GlobalExceptionHandler {

@ExceptionHandler(Exception.class)
public ResponseEntity<?> handleUnknownException(Exception e) {
e.printStackTrace();
CustomCommonException exception = new CustomCommonException(CommonErrorCode.UNKNOWN_ERROR);
return exception.toErrorResponse();
}
Expand Down
36 changes: 36 additions & 0 deletions src/main/java/com/laser/ordermanage/common/util/ImageUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.laser.ordermanage.common.util;

import com.laser.ordermanage.common.exception.CommonErrorCode;
import com.laser.ordermanage.common.exception.CustomCommonException;
import org.imgscalr.Scalr;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class ImageUtil {

private ImageUtil() {

}

public static String extractThumbnail(MultipartFile multipartFile, String tempFolderPath) {
try {
// MultipartFile to BufferedImage
BufferedImage bufferedImage = ImageIO.read(multipartFile.getInputStream());

// resize image
BufferedImage resizedImage = Scalr.resize(bufferedImage, Scalr.Method.AUTOMATIC, Scalr.Mode.FIT_EXACT, 500, 500, Scalr.OP_ANTIALIAS);

String filePath = tempFolderPath + FileUtil.getFileNameWithoutExtension(multipartFile) + "-thumbnail.png";

ImageIO.write(resizedImage, "png", new File(filePath));

return filePath;
} catch (IOException e) {
throw new CustomCommonException(CommonErrorCode.UNABLE_TO_EXTRACT_THUMBNAIL);
}
}
}
50 changes: 50 additions & 0 deletions src/main/java/com/laser/ordermanage/common/util/PDFUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.laser.ordermanage.common.util;

import com.laser.ordermanage.common.exception.CommonErrorCode;
import com.laser.ordermanage.common.exception.CustomCommonException;
import org.apache.pdfbox.Loader;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.PDFRenderer;
import org.imgscalr.Scalr;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;

public class PDFUtil {

private PDFUtil() {

}

public static String extractThumbnail(MultipartFile multipartFile, String tempFolderPath) {
try {
// 일시적인 파일 생성
Path tempFile = Files.createTempFile("uploadedDrawing-", ".pdf");
Files.copy(multipartFile.getInputStream(), tempFile, StandardCopyOption.REPLACE_EXISTING);

try (PDDocument document = Loader.loadPDF(tempFile.toFile())) {
PDFRenderer renderer = new PDFRenderer(document);
BufferedImage originalImage = renderer.renderImageWithDPI(0, 300);

// 이미지 크기 조정
BufferedImage resizedImage = Scalr.resize(originalImage, Scalr.Method.AUTOMATIC, Scalr.Mode.FIT_EXACT, 500, 500, Scalr.OP_ANTIALIAS);

String filePath = tempFolderPath + FileUtil.getFileNameWithoutExtension(multipartFile) + "-thumbnail.png";
ImageIO.write(resizedImage, "png", new File(filePath));

// 임시 파일 삭제
Files.deleteIfExists(tempFile);

return filePath;
}
} catch (IOException e) {
throw new CustomCommonException(CommonErrorCode.UNABLE_TO_EXTRACT_THUMBNAIL);
}
}
}
20 changes: 7 additions & 13 deletions src/main/java/com/laser/ordermanage/order/api/DrawingAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@Validated
@RequiredArgsConstructor
@RequestMapping("/drawing")
Expand All @@ -30,7 +32,7 @@ public class DrawingAPI {
* - 썸네일 이미지 파일 AWS S3 에 업로드
*/
@PostMapping("")
public ResponseEntity<?> uploadDrawingFile(@RequestParam @ValidFile(message = "도면 파일은 필수 입력값입니다.") MultipartFile file) {
public ResponseEntity<?> uploadDrawingFile(@RequestParam @ValidFile(message = "도면 파일은 필수 입력값입니다.") MultipartFile file) throws IOException {
// File 확장자 확인
DrawingFileType fileType = DrawingFileType.ofExtension(FileUtil.getExtension(file));

Expand All @@ -39,19 +41,11 @@ public ResponseEntity<?> uploadDrawingFile(@RequestParam @ValidFile(message = "
// 도면 파일 업로드
String drawingFileUrl = drawingService.uploadDrawingFile(file);

String thumbnailUrl;
if (fileType.equals(DrawingFileType.DWG) || fileType.equals(DrawingFileType.DXF)) {
// 썸네일 추출
String tempThumbnailFilePath = drawingService.extractThumbnail(file);

// 썸네일 파일 업로드
thumbnailUrl = drawingService.uploadThumbnailFile(tempThumbnailFilePath);
// 썸네일 추출
String tempThumbnailFilePath = drawingService.extractThumbnail(file, fileType);

} else {
// TODO: pdf, png, jpg, jpeg 파일 썸네일 추출 기능
// 썸네일 파일 업로드
thumbnailUrl = "https://ordermanage-drawing.s3.ap-northeast-2.amazonaws.com/ce429eb7-3319-45ba-b0b9-48bc6c77cf79_exception.png";
}
// 썸네일 파일 업로드
String thumbnailUrl = drawingService.uploadThumbnailFile(tempThumbnailFilePath);

UploadDrawingFileResponse uploadDrawingFileResponse = UploadDrawingFileResponse.builder()
.thumbnailUrl(thumbnailUrl)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
import com.laser.ordermanage.common.cloud.aws.S3Service;
import com.laser.ordermanage.common.exception.CustomCommonException;
import com.laser.ordermanage.common.util.CADUtil;
import com.laser.ordermanage.common.util.ImageUtil;
import com.laser.ordermanage.common.util.PDFUtil;
import com.laser.ordermanage.order.domain.Drawing;
import com.laser.ordermanage.order.domain.Order;
import com.laser.ordermanage.order.domain.type.DrawingFileType;
import com.laser.ordermanage.order.exception.OrderErrorCode;
import com.laser.ordermanage.order.repository.DrawingRepository;
import lombok.RequiredArgsConstructor;
Expand Down Expand Up @@ -34,8 +37,13 @@ public Integer countDrawingByOrder(Order order) {
return drawingRepository.countByOrder(order);
}

public String extractThumbnail(MultipartFile multipartFile) {
return CADUtil.extractThumbnail(multipartFile, tempFolderPath);
public String extractThumbnail(MultipartFile multipartFile, DrawingFileType fileType) {
return switch (fileType) {
case DWG, DXF -> CADUtil.extractThumbnail(multipartFile, tempFolderPath);
case PDF -> PDFUtil.extractThumbnail(multipartFile, tempFolderPath);
// PNG, JPG, JPEG
default -> ImageUtil.extractThumbnail(multipartFile, tempFolderPath);
};
}

public String uploadDrawingFile(MultipartFile multipartFile) {
Expand Down

0 comments on commit 271e417

Please sign in to comment.