Skip to content

Commit

Permalink
fix: Latex zip 파일 압축 해제 (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
gitchannn authored Feb 27, 2024
1 parent f9700b9 commit dbf27d4
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
import org.springframework.context.ApplicationEvent;

@Getter
public class TranslateEvent extends ApplicationEvent {
public class BrailleTranslateEvent extends ApplicationEvent {

private final Long id;

public TranslateEvent(final Object source, final Long id) {
public BrailleTranslateEvent(final Object source, final Long id) {
super(source);
this.id = id;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package sunflower.server.application.eventlistener;

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;
import org.springframework.transaction.event.TransactionalEventListener;
import sunflower.server.application.event.BrailleTranslateEvent;
import sunflower.server.client.ApiBrailleTranslationClient;
import sunflower.server.entity.Translations;
import sunflower.server.repository.TranslationsRepository;

import java.io.File;
import java.nio.file.Paths;

import static org.springframework.transaction.annotation.Propagation.REQUIRES_NEW;

@Slf4j
@NoArgsConstructor
@Component
public class BrailleTranslateEventListener {

private TranslationsRepository translationsRepository;
private ApiBrailleTranslationClient apiBrailleTranslationClient;
private ApplicationEventPublisher eventPublisher;

@Autowired
public BrailleTranslateEventListener(
final TranslationsRepository translationsRepository,
final ApiBrailleTranslationClient apiBrailleTranslationClient,
final ApplicationEventPublisher eventPublisher
) {
this.translationsRepository = translationsRepository;
this.apiBrailleTranslationClient = apiBrailleTranslationClient;
this.eventPublisher = eventPublisher;
}

@Async
@TransactionalEventListener
@Transactional(propagation = REQUIRES_NEW)
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();

if (!latexFile.exists()) {
throw new RuntimeException("파일이 존재하지 않습니다!");
}

final File brfFile = apiBrailleTranslationClient.translate(latexFile);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.event.TransactionalEventListener;
import sunflower.server.application.event.BrailleTranslateEvent;
import sunflower.server.application.event.OcrDownloadEvent;
import sunflower.server.application.event.TranslateEvent;
import sunflower.server.client.OcrDownloadClient;
import sunflower.server.entity.Translations;
import sunflower.server.repository.TranslationsRepository;
Expand Down Expand Up @@ -46,8 +46,9 @@ public void downloadLatexFile(final OcrDownloadEvent event) {

final byte[] latex = ocrDownloadClient.download(pdfId);
final String latexPath = saveLatexFile(pdfId, latex);
log.info("Latex File 저장! 경로: {}", latexPath);
translations.registerLatexPath(latexPath);

eventPublisher.publishEvent(new TranslateEvent(this, event.getId()));
eventPublisher.publishEvent(new BrailleTranslateEvent(this, event.getId()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,64 @@
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.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.io.File;

import static org.springframework.http.MediaType.MULTIPART_FORM_DATA;

@Slf4j
@Profile("!test")
@Component
public class ApiBrailleTranslationClient implements BrailleTranslationClient {

private final String appURI;
private final String key;
private final String keyName;
private final RestTemplate restTemplate;

public ApiBrailleTranslationClient(
@Value("${braille-translation.url}") String appURI,
@Value("${braille-translation.key-name}") String key,
@Value("${braille-translation.uri}") String appURI,
@Value("${braille-translation.key-name}") String keyName,
RestTemplate restTemplate
) {
this.appURI = appURI;
this.key = key;
this.keyName = keyName;
this.restTemplate = restTemplate;
}

@Override
public File translate(final Long id) {
MultiValueMap<String, Object> requestBody = new LinkedMultiValueMap<>();
public File translate(final File file) {
final HttpHeaders requestHeader = createRequestHeader();
MultiValueMap<String, Object> requestBody = createRequestBody(file);
final HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(requestBody, requestHeader);

log.info("Request URI: {}", appURI);
log.info("Request Headers: {}", requestHeader);
log.info("Request Parameters: {}", requestBody);

final ResponseEntity<String> response = restTemplate.postForEntity(appURI, requestEntity, String.class);

log.info("Response Body: {}", response.getBody());

return null;
}

private HttpHeaders createRequestHeader() {
HttpHeaders requestHeader = new HttpHeaders();
requestHeader.setContentType(MULTIPART_FORM_DATA);
return requestHeader;
}

private MultiValueMap<String, Object> createRequestBody(final File file) {
MultiValueMap<String, Object> requestBody = new LinkedMultiValueMap<>();
requestBody.add(keyName, new FileSystemResource(file));
return requestBody;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
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 @@ -15,7 +16,6 @@
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", FileUtil.convertToFileSystemResource(file));
requestBody.add("file", file.getResource());

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", FileUtil.convertToFileSystemResource(file));
requestBody.add("file", new FileSystemResource(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
@@ -1,8 +1,10 @@
package sunflower.server.client;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

public interface BrailleTranslationClient {

File translate(final Long id);
File translate(File file) throws IOException;
}
28 changes: 21 additions & 7 deletions server/src/main/java/sunflower/server/util/FileUtil.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
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;
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;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

@Slf4j
public final class FileUtil {

private static final String BASE_PATH = "src/main/latex/";

public static String savePdfFile(final MultipartFile file, String fileName) {
final Path path = Paths.get("src", "main", "pdf", fileName);

Expand All @@ -34,13 +40,21 @@ public static FileSystemResource convertToFileSystemResource(final File 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);
String path = BASE_PATH + 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;
}
}
} catch (IOException e) {
throw new RuntimeException("파일을 읽는데 실패함!");
log.error("Error extracting file from zip: {}", e.getMessage());
throw new RuntimeException(e);
}
return file.getPath();

return path;
}
}

0 comments on commit dbf27d4

Please sign in to comment.