Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ public enum DocumentType {
RESUME, // 이력서
CERTIFICATE, // 재직증명서
CONSENT, // 위임장
SELF_INTRO, // 자기소개서
SELF_INTRO , // 자기소개서
REPORT; // 일일업무보고서, 일일업무일지

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import Sprout_Squad.EyeOn.global.converter.ImgConverter;
import Sprout_Squad.EyeOn.global.external.exception.GetLabelFailedException;
import Sprout_Squad.EyeOn.global.external.exception.TypeDetectedFiledException;
import Sprout_Squad.EyeOn.global.external.exception.UnsupportedFileTypeException;
import Sprout_Squad.EyeOn.global.external.service.FlaskService;
import Sprout_Squad.EyeOn.global.external.service.PdfService;
import Sprout_Squad.EyeOn.global.external.service.S3Service;
Expand All @@ -26,7 +27,6 @@
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Service
@RequiredArgsConstructor
Expand Down Expand Up @@ -92,12 +92,25 @@ public UploadFormRes uploadForm(UserPrincipal userPrincipal, MultipartFile file)
// 사용자가 존재하지 않을 경우 -> UserNotFoundException
User user = userRepository.getUserById(userPrincipal.getId());

/**
* pdf일 경우, pdf to img 로직 필요
*/

String fileName = s3Service.generateFileName(file);
String fileUrl = s3Service.uploadFile(fileName, file);
String extension = pdfService.getFileExtension(file.getOriginalFilename()).toLowerCase();

String fileUrl;
String fileName;

if (extension.equals("pdf")) {
// PDF -> 이미지 변환
byte[] pdfBytes = file.getBytes();
fileUrl = pdfService.convertPdfToImage(pdfBytes);
fileName = s3Service.extractKeyFromUrl(fileUrl); // 변환된 이미지의 S3 key
} else if (extension.equals("jpg") || extension.equals("jpeg") || extension.equals("png")) {
fileName = s3Service.generateFileName(file);
fileUrl = s3Service.uploadFile(fileName, file);
} else {
throw new UnsupportedFileTypeException();
}

System.out.println("fileName : " + fileName);
System.out.println("fileUrl : " + fileUrl);

// 플라스크 서버와 통신하여 파일 유형 받아옴
FormType formType = getFormType(file, fileName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import lombok.AllArgsConstructor;
import lombok.Getter;

import static Sprout_Squad.EyeOn.global.constant.StaticValue.BAD_REQUEST;
import static Sprout_Squad.EyeOn.global.constant.StaticValue.INTERNAL_SERVER_ERROR;

@Getter
Expand All @@ -19,6 +20,10 @@ public enum ExternalErrorCode implements BaseResponseCode {
// pdf box
FONT_NOT_FOUND_ERROR_500("FONT_NOT_FOUND_ERROR_500", INTERNAL_SERVER_ERROR, "폰트 로드 중 에러가 발생했습니다."),
FILE_CREATE_FAILED_500("FILE_CREATE_FAILED_500", INTERNAL_SERVER_ERROR, "파일을 생성하는 중 에러가 발생했습니다."),
UNSUPPORTED_FILE_TYPE_400("UNSUPPORTED_FILE_TYPE_400", BAD_REQUEST, "지원하지 않는 파일 형식입니다."),

// s3
S3_URL_INVALID_500("S3_URL_INVALID_500", INTERNAL_SERVER_ERROR, "S3 URL이 유효하지 않습니다."),

// flask
TYPE_DETECTED_FAILED_500("TYPE_DETECTED_FAILED_500", INTERNAL_SERVER_ERROR,"문서 유형 감지 중 에러가 발생했습니다."),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package Sprout_Squad.EyeOn.global.external.exception;

import Sprout_Squad.EyeOn.global.exception.BaseException;

public class S3UrlInvalidException extends BaseException {
public S3UrlInvalidException() { super(ExternalErrorCode.S3_URL_INVALID_500); }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package Sprout_Squad.EyeOn.global.external.exception;

import Sprout_Squad.EyeOn.global.exception.BaseException;

public class UnsupportedFileTypeException extends BaseException {
public UnsupportedFileTypeException() { super(ExternalErrorCode.UNSUPPORTED_FILE_TYPE_400); }
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ public class FlaskService {
private final ObjectMapper objectMapper = new ObjectMapper();
private String baseUrl = "http://3.39.215.178:5050";

/**
*
*/

/**
* 문서 필드 분석 (라벨링)
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
import Sprout_Squad.EyeOn.global.external.exception.FileNotCreatedException;
import Sprout_Squad.EyeOn.global.external.exception.FontNotFoundException;
import lombok.RequiredArgsConstructor;
import org.apache.pdfbox.rendering.PDFRenderer;
import org.apache.pdfbox.pdmodel.*;
import org.apache.pdfbox.pdmodel.font.PDType0Font;
import org.apache.pdfbox.pdmodel.common.PDRectangle;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.springframework.stereotype.Service;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.time.LocalDate;
import java.util.UUID;
Expand Down Expand Up @@ -68,6 +71,31 @@ public String textToPdf(String content) {
}
}

/**
* pdf를 이미지로 변환
*/
public String convertPdfToImage(byte[] pdfBytes) throws IOException {
try(
PDDocument document = PDDocument.load(pdfBytes);
ByteArrayOutputStream imageOutputStream = new ByteArrayOutputStream();
){
PDFRenderer renderer = new PDFRenderer(document);

// 첫 페이지를 이미지로 변환
BufferedImage bim = renderer.renderImageWithDPI(0, 300);

ImageIO.write(bim, "jpg", imageOutputStream);
byte[] imageBytes = imageOutputStream.toByteArray();

String fileName = generateImageFileName();
String s3Key = "pdf-preview/" + fileName;
return s3Service.uploadImageBytes(s3Key, imageBytes);
} catch (IOException e) {
throw new FileNotCreatedException();
}

}

/**
* pdf 파일명 생성
*/
Expand All @@ -77,6 +105,15 @@ private String generatePdfFileName() {
return today + "/" + uuid + ".pdf";
}

/**
* 이미지 파일명 생성
*/
private String generateImageFileName() {
String today = LocalDate.now().toString();
String uuid = UUID.randomUUID().toString();
return today + "/" + uuid + ".jpg";
}

/**
* 확장자 명 추출
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package Sprout_Squad.EyeOn.global.external.service;

import Sprout_Squad.EyeOn.global.external.exception.S3UrlInvalidException;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
Expand All @@ -9,6 +10,8 @@
import software.amazon.awssdk.services.s3.model.*;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.time.LocalDate;
import java.util.UUID;

Expand Down Expand Up @@ -39,6 +42,22 @@ public String uploadFile(String fileName, MultipartFile file) throws IOException
return "https://" + bucket + ".s3." + region + ".amazonaws.com/" + fileName;
}

/**
* base64로 인코딩된 image를 업로드
*/
public String uploadImageBytes(String fileName, byte[] bytes) {
PutObjectRequest putObjectRequest = PutObjectRequest.builder()
.bucket(bucket)
.key(fileName)
.contentType("image/jpeg")
.build();

s3Client.putObject(putObjectRequest, RequestBody.fromBytes(bytes));

return "https://" + bucket + ".s3." + region + ".amazonaws.com/" + fileName;
}


/**
* json 업로드
*/
Expand Down Expand Up @@ -103,8 +122,16 @@ public long getSize(String fileUrl) {
* S3 url에서 Key 값 추출
*/
public String extractKeyFromUrl(String fileUrl) {
int index = fileUrl.indexOf(".amazonaws.com/") + ".amazonaws.com/".length();
return fileUrl.substring(index);
try {
URI uri = new URI(fileUrl);
String path = uri.getPath();
if (path == null || path.length() <= 1) {
throw new S3UrlInvalidException();
}
return path.substring(1);
} catch (URISyntaxException e) {
throw new S3UrlInvalidException();
}
}

/**
Expand Down
9 changes: 6 additions & 3 deletions src/main/resources/application-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@ spring:
driver-class-name: com.mysql.cj.jdbc.Driver
jpa:
hibernate:
ddl-auto: create
ddl-auto: update
properties:
hibernate:
format_sql: true
show-sql: true
format_sql: false
show-sql: false
dialect: org.hibernate.dialect.MySQL8Dialect
jdbc:
time_zone: UTC

cloud:
aws:
credentials:
Expand Down
1 change: 0 additions & 1 deletion src/main/resources/application-prod.yml

This file was deleted.