Skip to content

Commit

Permalink
refactor: File 처리 로직 분리 (#56)
Browse files Browse the repository at this point in the history
* refactor: 사용하지 않는 메서드 Deprecated 처리

* refactor: Resource 사용 부분 개선 (경로에 file:가 포함되는 문제)

* refactor: file name 설정 위치 변경

* refactor: 파일 저장 관련 로직 Util 클래스로 분리

* refactor: File 저장 로직 분리!

* refactor: 필드 이름 변경

* style: 클래스 이름 변경

* fix: 컴파일 에러 해결
  • Loading branch information
gitchannn authored Feb 23, 2024
1 parent cb1252e commit c2cc0a3
Show file tree
Hide file tree
Showing 11 changed files with 93 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,15 @@
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;
import sunflower.server.application.dto.TranslationStatusDto;
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
Expand All @@ -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 저장 이벤트를 발행했습니다!");
Expand All @@ -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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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();
Expand All @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -87,7 +87,7 @@ private HttpHeaders createRequestHeader() {
@Deprecated
private MultiValueMap<String, Object> createRequestBody(final MultipartFile file) {
MultiValueMap<String, Object> requestBody = new LinkedMultiValueMap<>();
requestBody.add("file", file.getResource());
requestBody.add("file", FileUtil.convertToFileSystemResource(file));

Map<String, Object> bodyMap = new HashMap<>();
bodyMap.put("conversion_formats", Map.of("docx", true, "tex.zip", true));
Expand Down Expand Up @@ -136,7 +136,7 @@ public String requestPdfId(final File file) {

private MultiValueMap<String, Object> createRequestBody(final File file) {
MultiValueMap<String, Object> requestBody = new LinkedMultiValueMap<>();
requestBody.add("file", new FileSystemResource(file));
requestBody.add("file", FileUtil.convertToFileSystemResource(file));

Map<String, Object> bodyMap = new HashMap<>();
bodyMap.put("conversion_formats", Map.of("docx", true, "tex.zip", true));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -85,6 +92,7 @@ public boolean isDone(final String pdfId) {
return true;
}

@Deprecated
private ResponseEntity<String> fetchResponse(final String requestURI, final HttpEntity<MultiValueMap<String, Object>> requestEntity) {
final Instant startTime = Instant.now();
while (true) {
Expand Down Expand Up @@ -112,11 +120,4 @@ private ResponseEntity<String> 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;
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
23 changes: 14 additions & 9 deletions server/src/main/java/sunflower/server/entity/Translations.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
}
}
46 changes: 46 additions & 0 deletions server/src/main/java/sunflower/server/util/FileUtil.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

0 comments on commit c2cc0a3

Please sign in to comment.