-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* fix: 저장하는 path 변경 * feat: TranslationEvent 발행 * feat: Translations 객체 초기화 조건 설정 * fix: Latex zip 파일 압축 해제 (#60) * chore: submodules 최신화 * feat: brf file 경로 저장 * fix: 테스트용 mock 객체 생성
- Loading branch information
Showing
13 changed files
with
190 additions
and
27 deletions.
There are no files selected for viewing
15 changes: 15 additions & 0 deletions
15
server/src/main/java/sunflower/server/application/event/BrailleTranslateEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package sunflower.server.application.event; | ||
|
||
import lombok.Getter; | ||
import org.springframework.context.ApplicationEvent; | ||
|
||
@Getter | ||
public class BrailleTranslateEvent extends ApplicationEvent { | ||
|
||
private final Long id; | ||
|
||
public BrailleTranslateEvent(final Object source, final Long id) { | ||
super(source); | ||
this.id = id; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
...c/main/java/sunflower/server/application/eventlistener/BrailleTranslateEventListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
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.BrailleTranslationClient; | ||
import sunflower.server.entity.Translations; | ||
import sunflower.server.repository.TranslationsRepository; | ||
|
||
import java.io.File; | ||
import java.io.FileWriter; | ||
import java.io.IOException; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
|
||
import static org.springframework.transaction.annotation.Propagation.REQUIRES_NEW; | ||
|
||
@Slf4j | ||
@NoArgsConstructor | ||
@Component | ||
public class BrailleTranslateEventListener { | ||
|
||
private TranslationsRepository translationsRepository; | ||
private BrailleTranslationClient brailleTranslationClient; | ||
private ApplicationEventPublisher eventPublisher; | ||
|
||
@Autowired | ||
public BrailleTranslateEventListener( | ||
final TranslationsRepository translationsRepository, | ||
final BrailleTranslationClient brailleTranslationClient, | ||
final ApplicationEventPublisher eventPublisher | ||
) { | ||
this.translationsRepository = translationsRepository; | ||
this.brailleTranslationClient = brailleTranslationClient; | ||
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 String brfContent = brailleTranslationClient.translate(latexFile); | ||
final String brfPath = saveBrfFile(brfContent, translations.getOcrPdfId()); | ||
|
||
translations.registerBrfPath(brfPath); | ||
} | ||
|
||
private String saveBrfFile(final String content, final String ocrPdfId) { | ||
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,5 +4,5 @@ | |
|
||
public interface BrailleTranslationClient { | ||
|
||
File translate(final Long id); | ||
String translate(File file); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Submodule security
updated
from f4d19e to 5334c6
16 changes: 16 additions & 0 deletions
16
server/src/test/java/sunflower/server/client/MockBrailleTranslationClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package sunflower.server.client; | ||
|
||
import org.springframework.context.annotation.Profile; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.io.File; | ||
|
||
@Component | ||
@Profile("test") | ||
public class MockBrailleTranslationClient implements BrailleTranslationClient { | ||
|
||
@Override | ||
public String translate(final File file) { | ||
return null; | ||
} | ||
} |