From 367dfc0a47ec12e009924107a803b8e3966d35df Mon Sep 17 00:00:00 2001 From: gitchan Date: Tue, 5 Mar 2024 09:46:44 +0900 Subject: [PATCH 1/5] =?UTF-8?q?refactor:=20brf=20file=20=EC=A0=80=EC=9E=A5?= =?UTF-8?q?=20=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 --- .../BrailleTranslateEventListener.java | 35 ++----------------- .../java/sunflower/server/util/FileUtil.java | 27 ++++++++++++++ 2 files changed, 29 insertions(+), 33 deletions(-) diff --git a/server/src/main/java/sunflower/server/application/eventlistener/BrailleTranslateEventListener.java b/server/src/main/java/sunflower/server/application/eventlistener/BrailleTranslateEventListener.java index 2477441..95e87bc 100644 --- a/server/src/main/java/sunflower/server/application/eventlistener/BrailleTranslateEventListener.java +++ b/server/src/main/java/sunflower/server/application/eventlistener/BrailleTranslateEventListener.java @@ -12,12 +12,9 @@ import sunflower.server.client.BrailleTranslationClient; import sunflower.server.entity.Translations; import sunflower.server.repository.TranslationsRepository; +import sunflower.server.util.FileUtil; import java.io.File; -import java.io.FileWriter; -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; import java.nio.file.Paths; import static org.springframework.transaction.annotation.Propagation.REQUIRES_NEW; @@ -29,7 +26,6 @@ public class BrailleTranslateEventListener { private TranslationsRepository translationsRepository; private BrailleTranslationClient brailleTranslationClient; - private ApplicationEventPublisher eventPublisher; @Autowired public BrailleTranslateEventListener( @@ -39,7 +35,6 @@ public BrailleTranslateEventListener( ) { this.translationsRepository = translationsRepository; this.brailleTranslationClient = brailleTranslationClient; - this.eventPublisher = eventPublisher; } @Async @@ -56,34 +51,8 @@ public void downloadLatexFile(final BrailleTranslateEvent event) { } final String brfContent = brailleTranslationClient.translate(latexFile); - final String brfPath = saveBrfFile(brfContent, translations.getOcrPdfId()); + final String brfPath = FileUtil.saveBrfFile(brfContent, translations.getOcrPdfId()); translations.finishTransbraille(brfPath); } - - private String saveBrfFile(final String content, final String ocrPdfId) { - final Path directoryPath = Paths.get("src", "main", "brf"); - if (!Files.exists(directoryPath)) { - try { - Files.createDirectories(directoryPath); - } catch (IOException e) { - throw new RuntimeException("Unable to create directory: " + directoryPath, e); - } - } - - final String directory = "src/main/brf"; - final String fileName = ocrPdfId + ".brf"; - final Path brfPath = Paths.get(directory, fileName); - - final File file = brfPath.toFile(); - try { - final FileWriter writer = new FileWriter(file); - writer.write(content); - writer.close(); - } catch (IOException e) { - throw new RuntimeException(e); - } - - return brfPath.toString(); - } } diff --git a/server/src/main/java/sunflower/server/util/FileUtil.java b/server/src/main/java/sunflower/server/util/FileUtil.java index 28e9738..de80860 100644 --- a/server/src/main/java/sunflower/server/util/FileUtil.java +++ b/server/src/main/java/sunflower/server/util/FileUtil.java @@ -7,6 +7,7 @@ import java.io.ByteArrayInputStream; import java.io.File; +import java.io.FileWriter; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; @@ -73,4 +74,30 @@ public static String saveLatexFile(final String pdfId, byte[] content) { return path; } + + public static String saveBrfFile(final String content, final String ocrPdfId) { + final Path directoryPath = Paths.get("src", "main", "brf"); + if (!Files.exists(directoryPath)) { + try { + Files.createDirectories(directoryPath); + } catch (IOException e) { + throw new RuntimeException("Unable to create directory: " + directoryPath, e); + } + } + + final String directory = "src/main/brf"; + final String fileName = ocrPdfId + ".brf"; + final Path brfPath = Paths.get(directory, fileName); + + final File file = brfPath.toFile(); + try { + final FileWriter writer = new FileWriter(file); + writer.write(content); + writer.close(); + } catch (IOException e) { + throw new RuntimeException(e); + } + + return brfPath.toString(); + } } From 00be21ad6f8b2866cd6520b3801d0409337a7ea8 Mon Sep 17 00:00:00 2001 From: gitchan Date: Tue, 5 Mar 2024 10:17:21 +0900 Subject: [PATCH 2/5] =?UTF-8?q?refactor:=20file=20=EC=B0=BE=EA=B8=B0=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 --- .../server/application/TranslationService.java | 17 ++++------------- .../BrailleTranslateEventListener.java | 8 ++------ .../eventlistener/OcrRegisterEventListener.java | 5 ++--- .../java/sunflower/server/util/FileUtil.java | 17 +++++++++++++---- 4 files changed, 21 insertions(+), 26 deletions(-) diff --git a/server/src/main/java/sunflower/server/application/TranslationService.java b/server/src/main/java/sunflower/server/application/TranslationService.java index 4d99fc6..b8d9a3e 100644 --- a/server/src/main/java/sunflower/server/application/TranslationService.java +++ b/server/src/main/java/sunflower/server/application/TranslationService.java @@ -13,10 +13,6 @@ import sunflower.server.util.FileUtil; import java.io.File; -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Paths; -import java.util.UUID; @Slf4j @RequiredArgsConstructor @@ -29,7 +25,7 @@ public class TranslationService { @Transactional public Long register(final MultipartFile file) { final String originalFileName = file.getOriginalFilename(); - final String fileName = UUID.randomUUID() + "_" + file.getOriginalFilename(); + final String fileName = FileUtil.randomFileName(file); final String pdfPath = FileUtil.savePdfFile(file, fileName); final Translations translations = translationsRepository.save(Translations.of(pdfPath, originalFileName)); @@ -49,13 +45,8 @@ public TranslationStatusDto status(final Long id) { public String findBrfFileById(final Long id) { final Translations translations = translationsRepository.getById(id); - final String brfPath = translations.getBrfPath(); - final File file = Paths.get(brfPath).toFile(); - - try { - return new String(Files.readAllBytes(Paths.get(file.getPath()))); - } catch (IOException e) { - throw new RuntimeException(e); - } + final File file = FileUtil.findFile(translations.getBrfPath()); + + return FileUtil.readFile(file); } } diff --git a/server/src/main/java/sunflower/server/application/eventlistener/BrailleTranslateEventListener.java b/server/src/main/java/sunflower/server/application/eventlistener/BrailleTranslateEventListener.java index 95e87bc..c018e09 100644 --- a/server/src/main/java/sunflower/server/application/eventlistener/BrailleTranslateEventListener.java +++ b/server/src/main/java/sunflower/server/application/eventlistener/BrailleTranslateEventListener.java @@ -3,7 +3,6 @@ import lombok.NoArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.context.ApplicationEventPublisher; import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Component; import org.springframework.transaction.annotation.Transactional; @@ -15,7 +14,6 @@ import sunflower.server.util.FileUtil; import java.io.File; -import java.nio.file.Paths; import static org.springframework.transaction.annotation.Propagation.REQUIRES_NEW; @@ -30,8 +28,7 @@ public class BrailleTranslateEventListener { @Autowired public BrailleTranslateEventListener( final TranslationsRepository translationsRepository, - final BrailleTranslationClient brailleTranslationClient, - final ApplicationEventPublisher eventPublisher + final BrailleTranslationClient brailleTranslationClient ) { this.translationsRepository = translationsRepository; this.brailleTranslationClient = brailleTranslationClient; @@ -43,8 +40,7 @@ public BrailleTranslateEventListener( public void downloadLatexFile(final BrailleTranslateEvent event) { final Translations translations = translationsRepository.getById(event.getId()); - final String latexPath = translations.getLatexPath(); - final File latexFile = Paths.get(latexPath).toFile(); + final File latexFile = FileUtil.findFile(translations.getLatexPath()); if (!latexFile.exists()) { throw new RuntimeException("파일이 존재하지 않습니다!"); 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 5ce225c..3e6b765 100644 --- a/server/src/main/java/sunflower/server/application/eventlistener/OcrRegisterEventListener.java +++ b/server/src/main/java/sunflower/server/application/eventlistener/OcrRegisterEventListener.java @@ -13,9 +13,9 @@ import sunflower.server.client.OcrRegisterClient; import sunflower.server.entity.Translations; import sunflower.server.repository.TranslationsRepository; +import sunflower.server.util.FileUtil; import java.io.File; -import java.nio.file.Paths; import static org.springframework.transaction.annotation.Propagation.REQUIRES_NEW; @@ -47,8 +47,7 @@ public void registerOcr(final OcrRegisterEvent event) { final Long id = event.getTranslations().getId(); final Translations translations = translationsRepository.getById(id); - final String pdfPath = translations.getPdfPath(); - final File file = Paths.get(pdfPath).toFile(); + final File file = FileUtil.findFile(translations.getPdfPath()); translations.startOcr(); final String pdfId = ocrRegisterClient.requestPdfId(file); diff --git a/server/src/main/java/sunflower/server/util/FileUtil.java b/server/src/main/java/sunflower/server/util/FileUtil.java index de80860..ccac6f7 100644 --- a/server/src/main/java/sunflower/server/util/FileUtil.java +++ b/server/src/main/java/sunflower/server/util/FileUtil.java @@ -12,6 +12,7 @@ import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; +import java.util.UUID; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; @@ -39,12 +40,20 @@ public static String savePdfFile(final MultipartFile file, String fileName) { } } - public static FileSystemResource convertToFileSystemResource(final MultipartFile file) { - return (FileSystemResource) file.getResource(); + public static String randomFileName(final MultipartFile file) { + return UUID.randomUUID() + "_" + file.getOriginalFilename(); } - public static FileSystemResource convertToFileSystemResource(final File file) { - return new FileSystemResource(file); + public static File findFile(String path) { + return Paths.get(path).toFile(); + } + + public static String readFile(final File file) { + try { + return new String(Files.readAllBytes(Paths.get(file.getPath()))); + } catch (IOException e) { + throw new RuntimeException(e); + } } public static String saveLatexFile(final String pdfId, byte[] content) { From 6918fa737356b1c13396b26b2f662c4a19f18c85 Mon Sep 17 00:00:00 2001 From: gitchan Date: Tue, 5 Mar 2024 10:31:21 +0900 Subject: [PATCH 3/5] =?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 --- .../application/TranslationService.java | 3 +- .../OcrDownloadEventListener.java | 4 +- .../java/sunflower/server/util/FileUtil.java | 37 +++++++++---------- 3 files changed, 22 insertions(+), 22 deletions(-) diff --git a/server/src/main/java/sunflower/server/application/TranslationService.java b/server/src/main/java/sunflower/server/application/TranslationService.java index b8d9a3e..f3a2274 100644 --- a/server/src/main/java/sunflower/server/application/TranslationService.java +++ b/server/src/main/java/sunflower/server/application/TranslationService.java @@ -13,6 +13,7 @@ import sunflower.server.util.FileUtil; import java.io.File; +import java.nio.file.Paths; @Slf4j @RequiredArgsConstructor @@ -27,7 +28,7 @@ public Long register(final MultipartFile file) { final String originalFileName = file.getOriginalFilename(); final String fileName = FileUtil.randomFileName(file); - final String pdfPath = FileUtil.savePdfFile(file, fileName); + final String pdfPath = FileUtil.saveFile(file, fileName, Paths.get("src", "main", "pdf")); 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 7673deb..04930cc 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,9 @@ import sunflower.server.client.OcrDownloadClient; import sunflower.server.entity.Translations; import sunflower.server.repository.TranslationsRepository; +import sunflower.server.util.FileUtil; import static org.springframework.transaction.annotation.Propagation.REQUIRES_NEW; -import static sunflower.server.util.FileUtil.saveLatexFile; @Slf4j @NoArgsConstructor @@ -45,7 +45,7 @@ public void downloadLatexFile(final OcrDownloadEvent event) { final String pdfId = translations.getOcrPdfId(); final byte[] latex = ocrDownloadClient.download(pdfId); - final String latexPath = saveLatexFile(pdfId, latex); + final String latexPath = FileUtil.saveLatexFile(pdfId, latex); log.info("Latex File 저장! 경로: {}", latexPath); translations.registerLatexPath(latexPath); diff --git a/server/src/main/java/sunflower/server/util/FileUtil.java b/server/src/main/java/sunflower/server/util/FileUtil.java index ccac6f7..2bbf503 100644 --- a/server/src/main/java/sunflower/server/util/FileUtil.java +++ b/server/src/main/java/sunflower/server/util/FileUtil.java @@ -19,8 +19,7 @@ @Slf4j public final class FileUtil { - public static String savePdfFile(final MultipartFile file, String fileName) { - final Path directoryPath = Paths.get("src", "main", "pdf"); + public static String saveFile(final MultipartFile file, final String fileName, final Path directoryPath) { if (!Files.exists(directoryPath)) { try { Files.createDirectories(directoryPath); @@ -29,7 +28,7 @@ public static String savePdfFile(final MultipartFile file, String fileName) { } } - final Path path = Paths.get("src", "main", "pdf", fileName); + final Path path = Paths.get(String.valueOf(directoryPath), fileName); try { Files.copy(file.getInputStream(), path); @@ -40,22 +39,6 @@ public static String savePdfFile(final MultipartFile file, String fileName) { } } - public static String randomFileName(final MultipartFile file) { - return UUID.randomUUID() + "_" + file.getOriginalFilename(); - } - - public static File findFile(String path) { - return Paths.get(path).toFile(); - } - - public static String readFile(final File file) { - try { - return new String(Files.readAllBytes(Paths.get(file.getPath()))); - } catch (IOException e) { - throw new RuntimeException(e); - } - } - public static String saveLatexFile(final String pdfId, byte[] content) { final Path directoryPath = Paths.get("src", "main", "latex"); if (!Files.exists(directoryPath)) { @@ -84,6 +67,22 @@ public static String saveLatexFile(final String pdfId, byte[] content) { return path; } + public static String randomFileName(final MultipartFile file) { + return UUID.randomUUID() + "_" + file.getOriginalFilename(); + } + + public static File findFile(String path) { + return Paths.get(path).toFile(); + } + + public static String readFile(final File file) { + try { + return new String(Files.readAllBytes(Paths.get(file.getPath()))); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + public static String saveBrfFile(final String content, final String ocrPdfId) { final Path directoryPath = Paths.get("src", "main", "brf"); if (!Files.exists(directoryPath)) { From c5aee6404e1ecb8e75eb410d6123302493c53257 Mon Sep 17 00:00:00 2001 From: gitchan Date: Tue, 5 Mar 2024 10:34:53 +0900 Subject: [PATCH 4/5] =?UTF-8?q?refactor:=20=EB=A9=94=EC=84=9C=EB=93=9C=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 --- .../application/eventlistener/OcrDownloadEventListener.java | 4 +++- server/src/main/java/sunflower/server/util/FileUtil.java | 3 +-- 2 files changed, 4 insertions(+), 3 deletions(-) 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 04930cc..03c114a 100644 --- a/server/src/main/java/sunflower/server/application/eventlistener/OcrDownloadEventListener.java +++ b/server/src/main/java/sunflower/server/application/eventlistener/OcrDownloadEventListener.java @@ -15,6 +15,8 @@ import sunflower.server.repository.TranslationsRepository; import sunflower.server.util.FileUtil; +import java.nio.file.Paths; + import static org.springframework.transaction.annotation.Propagation.REQUIRES_NEW; @Slf4j @@ -45,7 +47,7 @@ public void downloadLatexFile(final OcrDownloadEvent event) { final String pdfId = translations.getOcrPdfId(); final byte[] latex = ocrDownloadClient.download(pdfId); - final String latexPath = FileUtil.saveLatexFile(pdfId, latex); + final String latexPath = FileUtil.saveZipFile(pdfId, latex, Paths.get("src", "main", "latex")); log.info("Latex File 저장! 경로: {}", latexPath); translations.registerLatexPath(latexPath); diff --git a/server/src/main/java/sunflower/server/util/FileUtil.java b/server/src/main/java/sunflower/server/util/FileUtil.java index 2bbf503..76f6060 100644 --- a/server/src/main/java/sunflower/server/util/FileUtil.java +++ b/server/src/main/java/sunflower/server/util/FileUtil.java @@ -39,8 +39,7 @@ public static String saveFile(final MultipartFile file, final String fileName, f } } - public static String saveLatexFile(final String pdfId, byte[] content) { - final Path directoryPath = Paths.get("src", "main", "latex"); + public static String saveZipFile(final String pdfId, byte[] content, Path directoryPath) { if (!Files.exists(directoryPath)) { try { Files.createDirectories(directoryPath); From dcb752cb1c63a25939487fe4f940a907f2629c1b Mon Sep 17 00:00:00 2001 From: gitchan Date: Tue, 5 Mar 2024 10:46:04 +0900 Subject: [PATCH 5/5] =?UTF-8?q?refactor:=20file,=20multipartfile,=20zip=20?= =?UTF-8?q?file=20=EC=A0=80=EC=9E=A5=20=EB=A1=9C=EC=A7=81=20=ED=86=B5?= =?UTF-8?q?=EC=9D=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../application/TranslationService.java | 2 +- .../BrailleTranslateEventListener.java | 3 +- .../OcrDownloadEventListener.java | 2 +- .../java/sunflower/server/util/FileUtil.java | 90 ++++++++----------- 4 files changed, 42 insertions(+), 55 deletions(-) diff --git a/server/src/main/java/sunflower/server/application/TranslationService.java b/server/src/main/java/sunflower/server/application/TranslationService.java index f3a2274..5040b2d 100644 --- a/server/src/main/java/sunflower/server/application/TranslationService.java +++ b/server/src/main/java/sunflower/server/application/TranslationService.java @@ -26,7 +26,7 @@ public class TranslationService { @Transactional public Long register(final MultipartFile file) { final String originalFileName = file.getOriginalFilename(); - final String fileName = FileUtil.randomFileName(file); + final String fileName = FileUtil.createRandomFileName(file); final String pdfPath = FileUtil.saveFile(file, fileName, Paths.get("src", "main", "pdf")); final Translations translations = translationsRepository.save(Translations.of(pdfPath, originalFileName)); diff --git a/server/src/main/java/sunflower/server/application/eventlistener/BrailleTranslateEventListener.java b/server/src/main/java/sunflower/server/application/eventlistener/BrailleTranslateEventListener.java index c018e09..3660458 100644 --- a/server/src/main/java/sunflower/server/application/eventlistener/BrailleTranslateEventListener.java +++ b/server/src/main/java/sunflower/server/application/eventlistener/BrailleTranslateEventListener.java @@ -14,6 +14,7 @@ import sunflower.server.util.FileUtil; import java.io.File; +import java.nio.file.Paths; import static org.springframework.transaction.annotation.Propagation.REQUIRES_NEW; @@ -47,7 +48,7 @@ public void downloadLatexFile(final BrailleTranslateEvent event) { } final String brfContent = brailleTranslationClient.translate(latexFile); - final String brfPath = FileUtil.saveBrfFile(brfContent, translations.getOcrPdfId()); + final String brfPath = FileUtil.saveFile(brfContent, translations.getOcrPdfId(), Paths.get("src", "main", "brf")); translations.finishTransbraille(brfPath); } 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 03c114a..8e34778 100644 --- a/server/src/main/java/sunflower/server/application/eventlistener/OcrDownloadEventListener.java +++ b/server/src/main/java/sunflower/server/application/eventlistener/OcrDownloadEventListener.java @@ -47,7 +47,7 @@ public void downloadLatexFile(final OcrDownloadEvent event) { final String pdfId = translations.getOcrPdfId(); final byte[] latex = ocrDownloadClient.download(pdfId); - final String latexPath = FileUtil.saveZipFile(pdfId, latex, Paths.get("src", "main", "latex")); + final String latexPath = FileUtil.saveFile(latex, pdfId, Paths.get("src", "main", "latex")); log.info("Latex File 저장! 경로: {}", latexPath); translations.registerLatexPath(latexPath); diff --git a/server/src/main/java/sunflower/server/util/FileUtil.java b/server/src/main/java/sunflower/server/util/FileUtil.java index 76f6060..ded18a5 100644 --- a/server/src/main/java/sunflower/server/util/FileUtil.java +++ b/server/src/main/java/sunflower/server/util/FileUtil.java @@ -1,8 +1,6 @@ package sunflower.server.util; import lombok.extern.slf4j.Slf4j; -import org.springframework.core.io.FileSystemResource; -import org.springframework.core.io.Resource; import org.springframework.web.multipart.MultipartFile; import java.io.ByteArrayInputStream; @@ -19,7 +17,7 @@ @Slf4j public final class FileUtil { - public static String saveFile(final MultipartFile file, final String fileName, final Path directoryPath) { + public static String saveFile(Object file, String fileName, Path directoryPath) { if (!Files.exists(directoryPath)) { try { Files.createDirectories(directoryPath); @@ -28,45 +26,59 @@ public static String saveFile(final MultipartFile file, final String fileName, f } } - final Path path = Paths.get(String.valueOf(directoryPath), fileName); + Path path; + if (file instanceof MultipartFile) { + path = saveMultipartFile((MultipartFile) file, fileName, directoryPath); + } else if (file instanceof String) { + path = saveStringContent((String) file, fileName, directoryPath); + } else if (file instanceof byte[]) { + path = saveZipFile((byte[]) file, fileName, directoryPath); + } else { + throw new IllegalArgumentException("Unsupported file type"); + } + + return path.toString(); + } + private static Path saveMultipartFile(MultipartFile file, String fileName, Path directoryPath) { try { - Files.copy(file.getInputStream(), path); - Resource resource = new FileSystemResource(path.toFile()); - return resource.getFile().getPath(); + Path filePath = Paths.get(directoryPath.toString(), fileName); + Files.copy(file.getInputStream(), filePath); + return filePath; } catch (IOException e) { throw new RuntimeException(e); } } - public static String saveZipFile(final String pdfId, byte[] content, Path directoryPath) { - if (!Files.exists(directoryPath)) { - try { - Files.createDirectories(directoryPath); - } catch (IOException e) { - throw new RuntimeException("Unable to create directory: " + directoryPath, e); - } + private static Path saveStringContent(String content, String fileName, Path directoryPath) { + Path filePath = Paths.get(directoryPath.toString(), fileName); + try (FileWriter writer = new FileWriter(filePath.toFile())) { + writer.write(content); + return filePath; + } catch (IOException e) { + throw new RuntimeException(e); } + } - String path = "src/main/latex/" + pdfId + ".tex"; - - try (ZipInputStream zipInputStream = new ZipInputStream(new ByteArrayInputStream(content))) { - ZipEntry entry; - while ((entry = zipInputStream.getNextEntry()) != null) { - if (!entry.isDirectory()) { - Files.copy(zipInputStream, Paths.get(path)); - break; + private static Path saveZipFile(byte[] content, String fileName, Path directoryPath) { + try { + String path = "src/main/latex/" + fileName + ".tex"; + try (ZipInputStream zipInputStream = new ZipInputStream(new ByteArrayInputStream(content))) { + ZipEntry entry; + while ((entry = zipInputStream.getNextEntry()) != null) { + if (!entry.isDirectory()) { + Files.copy(zipInputStream, Paths.get(path)); + break; + } } } + return Paths.get(path); } catch (IOException e) { - log.error("Error extracting file from zip: {}", e.getMessage()); throw new RuntimeException(e); } - - return path; } - public static String randomFileName(final MultipartFile file) { + public static String createRandomFileName(final MultipartFile file) { return UUID.randomUUID() + "_" + file.getOriginalFilename(); } @@ -81,30 +93,4 @@ public static String readFile(final File file) { throw new RuntimeException(e); } } - - public static String saveBrfFile(final String content, final String ocrPdfId) { - final Path directoryPath = Paths.get("src", "main", "brf"); - if (!Files.exists(directoryPath)) { - try { - Files.createDirectories(directoryPath); - } catch (IOException e) { - throw new RuntimeException("Unable to create directory: " + directoryPath, e); - } - } - - final String directory = "src/main/brf"; - final String fileName = ocrPdfId + ".brf"; - final Path brfPath = Paths.get(directory, fileName); - - final File file = brfPath.toFile(); - try { - final FileWriter writer = new FileWriter(file); - writer.write(content); - writer.close(); - } catch (IOException e) { - throw new RuntimeException(e); - } - - return brfPath.toString(); - } }