diff --git a/server/src/main/java/sunflower/server/application/DeprecatedTranslationService.java b/server/src/main/java/sunflower/server/application/DeprecatedTranslationService.java index 61a15c9..d27ba13 100644 --- a/server/src/main/java/sunflower/server/application/DeprecatedTranslationService.java +++ b/server/src/main/java/sunflower/server/application/DeprecatedTranslationService.java @@ -7,8 +7,7 @@ import sunflower.server.client.OcrDownloadClient; import sunflower.server.client.OcrRegisterClient; import sunflower.server.client.OcrStatusClient; - -import java.io.File; +import sunflower.server.util.FileUtil; @Slf4j @RequiredArgsConstructor @@ -30,8 +29,8 @@ public Long translate(final MultipartFile file) { final boolean isDone = ocrStatusClient.isDone(pdfId); log.info("Mathpix API의 OCR 작업이 완료되었습니다. File: {}, pdf id: {}", fileName, pdfId); - final File latexFile = ocrDownloadClient.download(pdfId); - log.info("Latex 파일을 다운로드했습니다. File Name: {}", latexFile.getName()); + final byte[] latex = ocrDownloadClient.download(pdfId); + FileUtil.saveLatexFile(pdfId, latex); return null; } diff --git a/server/src/main/java/sunflower/server/application/TranslationService.java b/server/src/main/java/sunflower/server/application/TranslationService.java index 4c83db8..802cb08 100644 --- a/server/src/main/java/sunflower/server/application/TranslationService.java +++ b/server/src/main/java/sunflower/server/application/TranslationService.java @@ -3,8 +3,6 @@ import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.context.ApplicationEventPublisher; -import org.springframework.core.io.Resource; -import org.springframework.core.io.ResourceLoader; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.multipart.MultipartFile; @@ -12,11 +10,8 @@ import sunflower.server.application.event.OcrRegisterEvent; import sunflower.server.entity.Translations; import sunflower.server.repository.TranslationsRepository; +import sunflower.server.util.FileUtil; -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; import java.util.UUID; @Slf4j @@ -25,15 +20,16 @@ public class TranslationService { private final TranslationsRepository translationsRepository; - private final ResourceLoader resourceLoader; private final ApplicationEventPublisher eventPublisher; @Transactional public Long register(final MultipartFile file) { - final String pdfURI = saveFile(file).replace("file:", ""); - log.info("Saved pdf File in Server. File URI: {}", pdfURI); + final String originalFileName = file.getOriginalFilename(); + final String fileName = UUID.randomUUID() + "_" + file.getOriginalFilename(); - final Translations translations = translationsRepository.save(Translations.of(pdfURI, file.getOriginalFilename())); + final String pdfPath = FileUtil.savePdfFile(file, fileName); + final Translations translations = translationsRepository.save(Translations.of(pdfPath, originalFileName)); + log.info("Saved pdf File in Server. File URI: {}", pdfPath); eventPublisher.publishEvent(new OcrRegisterEvent(this, translations)); log.info("pdf file 저장 이벤트를 발행했습니다!"); @@ -42,23 +38,8 @@ public Long register(final MultipartFile file) { return translations.getId(); } - private String saveFile(final MultipartFile file) { - String fileName = UUID.randomUUID() + "_" + file.getOriginalFilename(); - final Path path = Paths.get("src", "main", "pdf", fileName); - - try { - Files.copy(file.getInputStream(), path); - final Resource resource = resourceLoader.getResource("file:" + path); // TODO: なんで「ファイル」だって - return resource.getURI().toString(); - } catch (IOException e) { - throw new RuntimeException(e); - } - } - @Transactional public TranslationStatusDto status(final Long id) { - final Translations translations = translationsRepository.getById(id); - - return TranslationStatusDto.from(translations); + return TranslationStatusDto.from(translationsRepository.getById(id)); } } diff --git a/server/src/main/java/sunflower/server/application/eventlistener/OcrDownloadEventListener.java b/server/src/main/java/sunflower/server/application/eventlistener/OcrDownloadEventListener.java index 6b2f185..a5bad54 100644 --- a/server/src/main/java/sunflower/server/application/eventlistener/OcrDownloadEventListener.java +++ b/server/src/main/java/sunflower/server/application/eventlistener/OcrDownloadEventListener.java @@ -13,9 +13,8 @@ import sunflower.server.entity.Translations; import sunflower.server.repository.TranslationsRepository; -import java.io.File; - import static org.springframework.transaction.annotation.Propagation.REQUIRES_NEW; +import static sunflower.server.util.FileUtil.saveLatexFile; @Slf4j @NoArgsConstructor @@ -44,6 +43,10 @@ public void downloadLatexFile(final OcrDownloadEvent event) { final Translations translations = translationsRepository.getById(event.getId()); final String pdfId = translations.getOcrPdfId(); - final File latexFile = ocrDownloadClient.download(pdfId); + final byte[] latex = ocrDownloadClient.download(pdfId); + final String latexPath = saveLatexFile(pdfId, latex); + translations.registerLatexPath(latexPath); + + // TODO: publish event } } diff --git a/server/src/main/java/sunflower/server/application/eventlistener/OcrRegisterEventListener.java b/server/src/main/java/sunflower/server/application/eventlistener/OcrRegisterEventListener.java index cbf19b7..38f921c 100644 --- a/server/src/main/java/sunflower/server/application/eventlistener/OcrRegisterEventListener.java +++ b/server/src/main/java/sunflower/server/application/eventlistener/OcrRegisterEventListener.java @@ -47,7 +47,7 @@ public void registerOcr(final OcrRegisterEvent event) { final Long id = event.getTranslations().getId(); final Translations translations = translationsRepository.getById(id); - final String pdfURI = translations.getPdfURI(); + final String pdfURI = translations.getPdfPath(); final File file = Paths.get(pdfURI).toFile(); translations.startOcr(); diff --git a/server/src/main/java/sunflower/server/client/ApiOcrDownloadClient.java b/server/src/main/java/sunflower/server/client/ApiOcrDownloadClient.java index e9a65b5..0f62891 100644 --- a/server/src/main/java/sunflower/server/client/ApiOcrDownloadClient.java +++ b/server/src/main/java/sunflower/server/client/ApiOcrDownloadClient.java @@ -11,10 +11,6 @@ import org.springframework.util.MultiValueMap; import org.springframework.web.client.RestTemplate; -import java.io.File; -import java.io.FileOutputStream; -import java.io.IOException; - @Slf4j @Profile("!test") @Component @@ -38,7 +34,7 @@ public ApiOcrDownloadClient( } @Override - public File download(final String pdfId) { + public byte[] download(final String pdfId) { final String requestURI = String.format(appURI, pdfId); HttpHeaders requestHeader = createRequestHeader(); @@ -57,16 +53,7 @@ public File download(final String pdfId) { log.info("Response Status Code: {}", response.getStatusCode()); log.info("Response Body: {}", response.getBody()); - final File file = new File("src/main/latex/" + pdfId + ".zip"); - - try (FileOutputStream fos = new FileOutputStream(file)) { - fos.write(response.getBody()); - } catch (IOException e) { - log.error("[ERROR] Error occurred while saving file: {}", e.getMessage()); - throw new RuntimeException("파일을 읽는데 실패함!"); - } - - return file; + return response.getBody(); } private HttpHeaders createRequestHeader() { diff --git a/server/src/main/java/sunflower/server/client/ApiOcrRegisterClient.java b/server/src/main/java/sunflower/server/client/ApiOcrRegisterClient.java index 877d966..5398943 100644 --- a/server/src/main/java/sunflower/server/client/ApiOcrRegisterClient.java +++ b/server/src/main/java/sunflower/server/client/ApiOcrRegisterClient.java @@ -6,7 +6,6 @@ import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Profile; -import org.springframework.core.io.FileSystemResource; import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; @@ -16,6 +15,7 @@ import org.springframework.util.MultiValueMap; import org.springframework.web.client.RestTemplate; import org.springframework.web.multipart.MultipartFile; +import sunflower.server.util.FileUtil; import java.io.File; import java.util.Arrays; @@ -87,7 +87,7 @@ private HttpHeaders createRequestHeader() { @Deprecated private MultiValueMap createRequestBody(final MultipartFile file) { MultiValueMap requestBody = new LinkedMultiValueMap<>(); - requestBody.add("file", file.getResource()); + requestBody.add("file", FileUtil.convertToFileSystemResource(file)); Map bodyMap = new HashMap<>(); bodyMap.put("conversion_formats", Map.of("docx", true, "tex.zip", true)); @@ -136,7 +136,7 @@ public String requestPdfId(final File file) { private MultiValueMap createRequestBody(final File file) { MultiValueMap requestBody = new LinkedMultiValueMap<>(); - requestBody.add("file", new FileSystemResource(file)); + requestBody.add("file", FileUtil.convertToFileSystemResource(file)); Map bodyMap = new HashMap<>(); bodyMap.put("conversion_formats", Map.of("docx", true, "tex.zip", true)); diff --git a/server/src/main/java/sunflower/server/client/ApiOcrStatusClient.java b/server/src/main/java/sunflower/server/client/ApiOcrStatusClient.java index b655eca..94719dd 100644 --- a/server/src/main/java/sunflower/server/client/ApiOcrStatusClient.java +++ b/server/src/main/java/sunflower/server/client/ApiOcrStatusClient.java @@ -66,6 +66,13 @@ public OcrStatusDto checkStatus(final String pdfId) { } } + private HttpHeaders createRequestHeader() { + HttpHeaders requestHeader = new HttpHeaders(); + requestHeader.set("app_id", appId); + requestHeader.set("app_key", appKey); + return requestHeader; + } + @Deprecated @Override public boolean isDone(final String pdfId) { @@ -85,6 +92,7 @@ public boolean isDone(final String pdfId) { return true; } + @Deprecated private ResponseEntity fetchResponse(final String requestURI, final HttpEntity> requestEntity) { final Instant startTime = Instant.now(); while (true) { @@ -112,11 +120,4 @@ private ResponseEntity fetchResponse(final String requestURI, final Http } } } - - private HttpHeaders createRequestHeader() { - HttpHeaders requestHeader = new HttpHeaders(); - requestHeader.set("app_id", appId); - requestHeader.set("app_key", appKey); - return requestHeader; - } } diff --git a/server/src/main/java/sunflower/server/client/OcrDownloadClient.java b/server/src/main/java/sunflower/server/client/OcrDownloadClient.java index 78a7dd2..f663dac 100644 --- a/server/src/main/java/sunflower/server/client/OcrDownloadClient.java +++ b/server/src/main/java/sunflower/server/client/OcrDownloadClient.java @@ -1,8 +1,6 @@ package sunflower.server.client; -import java.io.File; - public interface OcrDownloadClient { - File download(final String pdfId); + byte[] download(final String pdfId); } diff --git a/server/src/main/java/sunflower/server/entity/Translations.java b/server/src/main/java/sunflower/server/entity/Translations.java index d05fbb5..2abc7ac 100644 --- a/server/src/main/java/sunflower/server/entity/Translations.java +++ b/server/src/main/java/sunflower/server/entity/Translations.java @@ -25,26 +25,27 @@ public class Translations { private Long id; private String inputFileName; - private String pdfURI; private String ocrPdfId; @Enumerated(STRING) private OcrStatus ocrStatus; private Integer ocrPercentDone; - private String ocrLatexFileURI; private Integer translationPercentDone; - private String brfFileURI; - public static Translations of(final String pdfURI, final String inputFileName) { + private String pdfPath; + private String latexPath; + private String brfPath; + + public static Translations of(final String pdfPath, final String inputFileName) { final Translations translations = new Translations(); - translations.changePdfURI(pdfURI); + translations.changePdfPath(pdfPath); translations.changeInputFileName(inputFileName); return translations; } - private void changePdfURI(final String pdfURI) { - this.pdfURI = pdfURI; + private void changePdfPath(final String pdfPath) { + this.pdfPath = pdfPath; } private void changeInputFileName(final String inputFileName) { @@ -56,12 +57,16 @@ public void startOcr() { this.ocrStatus = OcrStatus.SPLIT; } - public void registerPdfId(final String pdfId) { - this.ocrPdfId = pdfId; + public void registerPdfId(final String ocrPdfId) { + this.ocrPdfId = ocrPdfId; } public void changeOcrStatus(final OcrStatusDto dto) { this.ocrStatus = dto.getStatus(); this.ocrPercentDone = dto.getPercentDone(); } + + public void registerLatexPath(final String latexPath) { + this.latexPath = latexPath; + } } diff --git a/server/src/main/java/sunflower/server/util/FileUtil.java b/server/src/main/java/sunflower/server/util/FileUtil.java new file mode 100644 index 0000000..cb38777 --- /dev/null +++ b/server/src/main/java/sunflower/server/util/FileUtil.java @@ -0,0 +1,46 @@ +package sunflower.server.util; + +import org.springframework.core.io.FileSystemResource; +import org.springframework.core.io.Resource; +import org.springframework.web.multipart.MultipartFile; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; + +public final class FileUtil { + + public static String savePdfFile(final MultipartFile file, String fileName) { + final Path path = Paths.get("src", "main", "pdf", fileName); + + try { + Files.copy(file.getInputStream(), path); + Resource resource = new FileSystemResource(path.toFile()); + return resource.getURI().getPath(); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + public static FileSystemResource convertToFileSystemResource(final MultipartFile file) { + return (FileSystemResource) file.getResource(); + } + + public static FileSystemResource convertToFileSystemResource(final File file) { + return new FileSystemResource(file); + } + + public static String saveLatexFile(final String pdfId, byte[] content) { + final File file = new File("src/main/latex/" + pdfId + ".zip"); + + try (FileOutputStream fos = new FileOutputStream(file)) { + fos.write(content); + } catch (IOException e) { + throw new RuntimeException("파일을 읽는데 실패함!"); + } + return file.getPath(); + } +} diff --git a/server/src/test/java/sunflower/server/client/MockOcrDownloadClient.java b/server/src/test/java/sunflower/server/client/MockOcrDownloadClient.java index 1dc06ff..c435fcc 100644 --- a/server/src/test/java/sunflower/server/client/MockOcrDownloadClient.java +++ b/server/src/test/java/sunflower/server/client/MockOcrDownloadClient.java @@ -3,14 +3,12 @@ import org.springframework.context.annotation.Profile; import org.springframework.stereotype.Component; -import java.io.File; - @Component @Profile("test") public class MockOcrDownloadClient implements OcrDownloadClient { @Override - public File download(final String pdfId) { - return new File(""); + public byte[] download(final String pdfId) { + return null; } }