From 9c6f0a698177616f31443537bdb516f8ff0deb06 Mon Sep 17 00:00:00 2001 From: gitchan Date: Sat, 24 Feb 2024 00:44:18 +0900 Subject: [PATCH 1/8] =?UTF-8?q?refactor:=20=EC=82=AC=EC=9A=A9=ED=95=98?= =?UTF-8?q?=EC=A7=80=20=EC=95=8A=EB=8A=94=20=EB=A9=94=EC=84=9C=EB=93=9C=20?= =?UTF-8?q?Deprecated=20=EC=B2=98=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../server/client/ApiOcrDownloadClient.java | 1 + .../server/client/ApiOcrStatusClient.java | 15 ++++++++------- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/server/src/main/java/sunflower/server/client/ApiOcrDownloadClient.java b/server/src/main/java/sunflower/server/client/ApiOcrDownloadClient.java index e9a65b5..799e2a1 100644 --- a/server/src/main/java/sunflower/server/client/ApiOcrDownloadClient.java +++ b/server/src/main/java/sunflower/server/client/ApiOcrDownloadClient.java @@ -57,6 +57,7 @@ public File download(final String pdfId) { log.info("Response Status Code: {}", response.getStatusCode()); log.info("Response Body: {}", response.getBody()); + // TODO: 파일 저장과 클라이언트 호출 분리 final File file = new File("src/main/latex/" + pdfId + ".zip"); try (FileOutputStream fos = new FileOutputStream(file)) { 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; - } } From 946af194030e67ba568da9eb622b2668e270aafd Mon Sep 17 00:00:00 2001 From: gitchan Date: Sat, 24 Feb 2024 01:08:53 +0900 Subject: [PATCH 2/8] =?UTF-8?q?refactor:=20Resource=20=EC=82=AC=EC=9A=A9?= =?UTF-8?q?=20=EB=B6=80=EB=B6=84=20=EA=B0=9C=EC=84=A0=20(=EA=B2=BD?= =?UTF-8?q?=EB=A1=9C=EC=97=90=20file:=EA=B0=80=20=ED=8F=AC=ED=95=A8?= =?UTF-8?q?=EB=90=98=EB=8A=94=20=EB=AC=B8=EC=A0=9C)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../sunflower/server/application/TranslationService.java | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/server/src/main/java/sunflower/server/application/TranslationService.java b/server/src/main/java/sunflower/server/application/TranslationService.java index 4c83db8..63f755a 100644 --- a/server/src/main/java/sunflower/server/application/TranslationService.java +++ b/server/src/main/java/sunflower/server/application/TranslationService.java @@ -3,8 +3,8 @@ import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.context.ApplicationEventPublisher; +import org.springframework.core.io.FileSystemResource; 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; @@ -25,12 +25,11 @@ 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:", ""); + final String pdfURI = saveFile(file); log.info("Saved pdf File in Server. File URI: {}", pdfURI); final Translations translations = translationsRepository.save(Translations.of(pdfURI, file.getOriginalFilename())); @@ -48,8 +47,8 @@ private String saveFile(final MultipartFile file) { try { Files.copy(file.getInputStream(), path); - final Resource resource = resourceLoader.getResource("file:" + path); // TODO: なんで「ファイル」だって - return resource.getURI().toString(); + Resource resource = new FileSystemResource(path.toFile()); + return resource.getURI().getPath(); } catch (IOException e) { throw new RuntimeException(e); } From 5c08d957d51e57c581ecafc93df6e56331661f26 Mon Sep 17 00:00:00 2001 From: gitchan Date: Sat, 24 Feb 2024 01:18:10 +0900 Subject: [PATCH 3/8] =?UTF-8?q?refactor:=20file=20name=20=EC=84=A4?= =?UTF-8?q?=EC=A0=95=20=EC=9C=84=EC=B9=98=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../server/application/TranslationService.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/server/src/main/java/sunflower/server/application/TranslationService.java b/server/src/main/java/sunflower/server/application/TranslationService.java index 63f755a..9124512 100644 --- a/server/src/main/java/sunflower/server/application/TranslationService.java +++ b/server/src/main/java/sunflower/server/application/TranslationService.java @@ -29,10 +29,11 @@ public class TranslationService { @Transactional public Long register(final MultipartFile file) { - final String pdfURI = saveFile(file); - log.info("Saved pdf File in Server. File URI: {}", pdfURI); + final String pdfURI = saveFile(file, UUID.randomUUID() + "_" + file.getOriginalFilename()); - final Translations translations = translationsRepository.save(Translations.of(pdfURI, file.getOriginalFilename())); + String fileName = UUID.randomUUID() + "_" + file.getOriginalFilename(); + final Translations translations = translationsRepository.save(Translations.of(pdfURI, fileName)); + log.info("Saved pdf File in Server. File URI: {}", pdfURI); eventPublisher.publishEvent(new OcrRegisterEvent(this, translations)); log.info("pdf file 저장 이벤트를 발행했습니다!"); @@ -41,8 +42,7 @@ public Long register(final MultipartFile file) { return translations.getId(); } - private String saveFile(final MultipartFile file) { - String fileName = UUID.randomUUID() + "_" + file.getOriginalFilename(); + private String saveFile(final MultipartFile file, String fileName) { final Path path = Paths.get("src", "main", "pdf", fileName); try { From 89cda0e49a686e4f0904fc5858efe21e39d9cc63 Mon Sep 17 00:00:00 2001 From: gitchan Date: Sat, 24 Feb 2024 01:21:52 +0900 Subject: [PATCH 4/8] =?UTF-8?q?refactor:=20=ED=8C=8C=EC=9D=BC=20=EC=A0=80?= =?UTF-8?q?=EC=9E=A5=20=EA=B4=80=EB=A0=A8=20=EB=A1=9C=EC=A7=81=20Util=20?= =?UTF-8?q?=ED=81=B4=EB=9E=98=EC=8A=A4=EB=A1=9C=20=EB=B6=84=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../application/TranslationService.java | 30 ++++------------ .../server/client/ApiOcrRegisterClient.java | 6 ++-- .../sunflower/server/util/FileSaveUtil.java | 34 +++++++++++++++++++ 3 files changed, 43 insertions(+), 27 deletions(-) create mode 100644 server/src/main/java/sunflower/server/util/FileSaveUtil.java diff --git a/server/src/main/java/sunflower/server/application/TranslationService.java b/server/src/main/java/sunflower/server/application/TranslationService.java index 9124512..608ec52 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.FileSystemResource; -import org.springframework.core.io.Resource; 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.FileSaveUtil; -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; import java.util.UUID; @Slf4j @@ -29,10 +24,11 @@ public class TranslationService { @Transactional public Long register(final MultipartFile file) { - final String pdfURI = saveFile(file, UUID.randomUUID() + "_" + file.getOriginalFilename()); + final String originalFileName = file.getOriginalFilename(); + final String fileName = UUID.randomUUID() + "_" + file.getOriginalFilename(); - String fileName = UUID.randomUUID() + "_" + file.getOriginalFilename(); - final Translations translations = translationsRepository.save(Translations.of(pdfURI, fileName)); + final String pdfURI = FileSaveUtil.savePdfFile(file, fileName); + final Translations translations = translationsRepository.save(Translations.of(pdfURI, originalFileName)); log.info("Saved pdf File in Server. File URI: {}", pdfURI); eventPublisher.publishEvent(new OcrRegisterEvent(this, translations)); @@ -42,22 +38,8 @@ public Long register(final MultipartFile file) { return translations.getId(); } - private String saveFile(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); - } - } - @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/client/ApiOcrRegisterClient.java b/server/src/main/java/sunflower/server/client/ApiOcrRegisterClient.java index 877d966..31028cb 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.FileSaveUtil; 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", FileSaveUtil.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", FileSaveUtil.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/util/FileSaveUtil.java b/server/src/main/java/sunflower/server/util/FileSaveUtil.java new file mode 100644 index 0000000..9157bf8 --- /dev/null +++ b/server/src/main/java/sunflower/server/util/FileSaveUtil.java @@ -0,0 +1,34 @@ +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.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; + +public final class FileSaveUtil { + + 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); + } +} From 175dd4d04c78be4980f487e62ec1f3f6a3d5658c Mon Sep 17 00:00:00 2001 From: gitchan Date: Sat, 24 Feb 2024 01:47:30 +0900 Subject: [PATCH 5/8] =?UTF-8?q?refactor:=20File=20=EC=A0=80=EC=9E=A5=20?= =?UTF-8?q?=EB=A1=9C=EC=A7=81=20=EB=B6=84=EB=A6=AC!?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DeprecatedTranslationService.java | 7 +++---- .../OcrDownloadEventListener.java | 9 ++++++--- .../server/client/ApiOcrDownloadClient.java | 18 ++---------------- .../server/client/OcrDownloadClient.java | 4 +--- .../sunflower/server/entity/Translations.java | 4 ++++ .../sunflower/server/util/FileSaveUtil.java | 12 ++++++++++++ .../server/client/MockOcrDownloadClient.java | 2 +- 7 files changed, 29 insertions(+), 27 deletions(-) diff --git a/server/src/main/java/sunflower/server/application/DeprecatedTranslationService.java b/server/src/main/java/sunflower/server/application/DeprecatedTranslationService.java index 61a15c9..ee1e061 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.FileSaveUtil; @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); + FileSaveUtil.saveLatexFile(pdfId, latex); return null; } 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..1e0dc33 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.FileSaveUtil.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[] latexFile = ocrDownloadClient.download(pdfId); + final String latexFilePath = saveLatexFile(pdfId, latexFile); + translations.registerLatexURI(latexFilePath); + + // TODO: publish event } } diff --git a/server/src/main/java/sunflower/server/client/ApiOcrDownloadClient.java b/server/src/main/java/sunflower/server/client/ApiOcrDownloadClient.java index 799e2a1..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,17 +53,7 @@ public File download(final String pdfId) { log.info("Response Status Code: {}", response.getStatusCode()); log.info("Response Body: {}", response.getBody()); - // TODO: 파일 저장과 클라이언트 호출 분리 - 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/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..653f14b 100644 --- a/server/src/main/java/sunflower/server/entity/Translations.java +++ b/server/src/main/java/sunflower/server/entity/Translations.java @@ -64,4 +64,8 @@ public void changeOcrStatus(final OcrStatusDto dto) { this.ocrStatus = dto.getStatus(); this.ocrPercentDone = dto.getPercentDone(); } + + public void registerLatexURI(final String ocrLatexFileURI) { + this.ocrLatexFileURI = ocrLatexFileURI; + } } diff --git a/server/src/main/java/sunflower/server/util/FileSaveUtil.java b/server/src/main/java/sunflower/server/util/FileSaveUtil.java index 9157bf8..1d02e2f 100644 --- a/server/src/main/java/sunflower/server/util/FileSaveUtil.java +++ b/server/src/main/java/sunflower/server/util/FileSaveUtil.java @@ -5,6 +5,7 @@ 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; @@ -31,4 +32,15 @@ public static FileSystemResource convertToFileSystemResource(final MultipartFile 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..4adfe1e 100644 --- a/server/src/test/java/sunflower/server/client/MockOcrDownloadClient.java +++ b/server/src/test/java/sunflower/server/client/MockOcrDownloadClient.java @@ -10,7 +10,7 @@ public class MockOcrDownloadClient implements OcrDownloadClient { @Override - public File download(final String pdfId) { + public byte[] download(final String pdfId) { return new File(""); } } From 034c9e964621a6d521970e6ef7647f7b08bc4ed1 Mon Sep 17 00:00:00 2001 From: gitchan Date: Sat, 24 Feb 2024 01:52:39 +0900 Subject: [PATCH 6/8] =?UTF-8?q?refactor:=20=ED=95=84=EB=93=9C=20=EC=9D=B4?= =?UTF-8?q?=EB=A6=84=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../application/TranslationService.java | 6 ++--- .../OcrDownloadEventListener.java | 6 ++--- .../OcrRegisterEventListener.java | 2 +- .../sunflower/server/entity/Translations.java | 23 ++++++++++--------- 4 files changed, 19 insertions(+), 18 deletions(-) diff --git a/server/src/main/java/sunflower/server/application/TranslationService.java b/server/src/main/java/sunflower/server/application/TranslationService.java index 608ec52..3e58dab 100644 --- a/server/src/main/java/sunflower/server/application/TranslationService.java +++ b/server/src/main/java/sunflower/server/application/TranslationService.java @@ -27,9 +27,9 @@ public Long register(final MultipartFile file) { final String originalFileName = file.getOriginalFilename(); final String fileName = UUID.randomUUID() + "_" + file.getOriginalFilename(); - final String pdfURI = FileSaveUtil.savePdfFile(file, fileName); - final Translations translations = translationsRepository.save(Translations.of(pdfURI, originalFileName)); - log.info("Saved pdf File in Server. File URI: {}", pdfURI); + final String pdfPath = FileSaveUtil.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 저장 이벤트를 발행했습니다!"); 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 1e0dc33..f05074a 100644 --- a/server/src/main/java/sunflower/server/application/eventlistener/OcrDownloadEventListener.java +++ b/server/src/main/java/sunflower/server/application/eventlistener/OcrDownloadEventListener.java @@ -43,9 +43,9 @@ public void downloadLatexFile(final OcrDownloadEvent event) { final Translations translations = translationsRepository.getById(event.getId()); final String pdfId = translations.getOcrPdfId(); - final byte[] latexFile = ocrDownloadClient.download(pdfId); - final String latexFilePath = saveLatexFile(pdfId, latexFile); - translations.registerLatexURI(latexFilePath); + 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/entity/Translations.java b/server/src/main/java/sunflower/server/entity/Translations.java index 653f14b..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,8 +57,8 @@ 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) { @@ -65,7 +66,7 @@ public void changeOcrStatus(final OcrStatusDto dto) { this.ocrPercentDone = dto.getPercentDone(); } - public void registerLatexURI(final String ocrLatexFileURI) { - this.ocrLatexFileURI = ocrLatexFileURI; + public void registerLatexPath(final String latexPath) { + this.latexPath = latexPath; } } From 52ab4d60f1f922af1bf73beb84eac70013e35dd9 Mon Sep 17 00:00:00 2001 From: gitchan Date: Sat, 24 Feb 2024 01:54:02 +0900 Subject: [PATCH 7/8] =?UTF-8?q?style:=20=ED=81=B4=EB=9E=98=EC=8A=A4=20?= =?UTF-8?q?=EC=9D=B4=EB=A6=84=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../server/application/DeprecatedTranslationService.java | 4 ++-- .../sunflower/server/application/TranslationService.java | 4 ++-- .../application/eventlistener/OcrDownloadEventListener.java | 2 +- .../java/sunflower/server/client/ApiOcrRegisterClient.java | 6 +++--- .../server/util/{FileSaveUtil.java => FileUtil.java} | 2 +- 5 files changed, 9 insertions(+), 9 deletions(-) rename server/src/main/java/sunflower/server/util/{FileSaveUtil.java => FileUtil.java} (97%) diff --git a/server/src/main/java/sunflower/server/application/DeprecatedTranslationService.java b/server/src/main/java/sunflower/server/application/DeprecatedTranslationService.java index ee1e061..d27ba13 100644 --- a/server/src/main/java/sunflower/server/application/DeprecatedTranslationService.java +++ b/server/src/main/java/sunflower/server/application/DeprecatedTranslationService.java @@ -7,7 +7,7 @@ import sunflower.server.client.OcrDownloadClient; import sunflower.server.client.OcrRegisterClient; import sunflower.server.client.OcrStatusClient; -import sunflower.server.util.FileSaveUtil; +import sunflower.server.util.FileUtil; @Slf4j @RequiredArgsConstructor @@ -30,7 +30,7 @@ public Long translate(final MultipartFile file) { log.info("Mathpix API의 OCR 작업이 완료되었습니다. File: {}, pdf id: {}", fileName, pdfId); final byte[] latex = ocrDownloadClient.download(pdfId); - FileSaveUtil.saveLatexFile(pdfId, latex); + 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 3e58dab..802cb08 100644 --- a/server/src/main/java/sunflower/server/application/TranslationService.java +++ b/server/src/main/java/sunflower/server/application/TranslationService.java @@ -10,7 +10,7 @@ import sunflower.server.application.event.OcrRegisterEvent; import sunflower.server.entity.Translations; import sunflower.server.repository.TranslationsRepository; -import sunflower.server.util.FileSaveUtil; +import sunflower.server.util.FileUtil; import java.util.UUID; @@ -27,7 +27,7 @@ public Long register(final MultipartFile file) { final String originalFileName = file.getOriginalFilename(); final String fileName = UUID.randomUUID() + "_" + file.getOriginalFilename(); - final String pdfPath = FileSaveUtil.savePdfFile(file, fileName); + 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); 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 f05074a..a5bad54 100644 --- a/server/src/main/java/sunflower/server/application/eventlistener/OcrDownloadEventListener.java +++ b/server/src/main/java/sunflower/server/application/eventlistener/OcrDownloadEventListener.java @@ -14,7 +14,7 @@ import sunflower.server.repository.TranslationsRepository; import static org.springframework.transaction.annotation.Propagation.REQUIRES_NEW; -import static sunflower.server.util.FileSaveUtil.saveLatexFile; +import static sunflower.server.util.FileUtil.saveLatexFile; @Slf4j @NoArgsConstructor diff --git a/server/src/main/java/sunflower/server/client/ApiOcrRegisterClient.java b/server/src/main/java/sunflower/server/client/ApiOcrRegisterClient.java index 31028cb..5398943 100644 --- a/server/src/main/java/sunflower/server/client/ApiOcrRegisterClient.java +++ b/server/src/main/java/sunflower/server/client/ApiOcrRegisterClient.java @@ -15,7 +15,7 @@ import org.springframework.util.MultiValueMap; import org.springframework.web.client.RestTemplate; import org.springframework.web.multipart.MultipartFile; -import sunflower.server.util.FileSaveUtil; +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", FileSaveUtil.convertToFileSystemResource(file)); + 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", FileSaveUtil.convertToFileSystemResource(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/util/FileSaveUtil.java b/server/src/main/java/sunflower/server/util/FileUtil.java similarity index 97% rename from server/src/main/java/sunflower/server/util/FileSaveUtil.java rename to server/src/main/java/sunflower/server/util/FileUtil.java index 1d02e2f..cb38777 100644 --- a/server/src/main/java/sunflower/server/util/FileSaveUtil.java +++ b/server/src/main/java/sunflower/server/util/FileUtil.java @@ -11,7 +11,7 @@ import java.nio.file.Path; import java.nio.file.Paths; -public final class FileSaveUtil { +public final class FileUtil { public static String savePdfFile(final MultipartFile file, String fileName) { final Path path = Paths.get("src", "main", "pdf", fileName); From f52ff502ab6ed8d48ce1b9b04d46a6164903d710 Mon Sep 17 00:00:00 2001 From: gitchan Date: Sat, 24 Feb 2024 01:57:40 +0900 Subject: [PATCH 8/8] =?UTF-8?q?fix:=20=EC=BB=B4=ED=8C=8C=EC=9D=BC=20?= =?UTF-8?q?=EC=97=90=EB=9F=AC=20=ED=95=B4=EA=B2=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/sunflower/server/client/MockOcrDownloadClient.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/server/src/test/java/sunflower/server/client/MockOcrDownloadClient.java b/server/src/test/java/sunflower/server/client/MockOcrDownloadClient.java index 4adfe1e..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 byte[] download(final String pdfId) { - return new File(""); + return null; } }