diff --git a/pom.xml b/pom.xml index 07262c6..376be50 100644 --- a/pom.xml +++ b/pom.xml @@ -22,6 +22,12 @@ spring-boot-starter-web + + org.zeroturnaround + zt-exec + 1.12 + + org.springframework.boot spring-boot-starter-test diff --git a/src/main/java/ru/micord/av/service/service/FileScanService.java b/src/main/java/ru/micord/av/service/service/FileScanService.java index dbb2fcc..250d769 100644 --- a/src/main/java/ru/micord/av/service/service/FileScanService.java +++ b/src/main/java/ru/micord/av/service/service/FileScanService.java @@ -2,16 +2,19 @@ package ru.micord.av.service.service; import java.io.File; import java.io.IOException; -import java.io.InputStream; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; +import java.util.concurrent.TimeoutException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; +import org.zeroturnaround.exec.ProcessExecutor; +import org.zeroturnaround.exec.ProcessResult; +import org.zeroturnaround.exec.stream.slf4j.Slf4jStream; import ru.micord.av.service.exception.AvException; /** @@ -33,7 +36,7 @@ public class FileScanService { tempFile = saveFile(file); return runKeslScan(tempFile, file.getOriginalFilename()); } - catch (IOException | InterruptedException e) { + catch (IOException | InterruptedException | TimeoutException e) { throw new AvException("Error scanning file: " + file.getOriginalFilename(), e); } finally { @@ -57,27 +60,23 @@ public class FileScanService { return tempFile; } - private int runKeslScan(File file, String originalFileName) throws IOException, InterruptedException { - ProcessBuilder processBuilder = new ProcessBuilder(KESL_CONTROL, KESL_SCAN, - file.getAbsolutePath() - ); - processBuilder.redirectErrorStream(true); - Process process = processBuilder.start(); + private int runKeslScan(File file, String originalFileName) + throws IOException, InterruptedException, TimeoutException { + ProcessResult processResult = new ProcessExecutor() + .command(KESL_CONTROL, KESL_SCAN, file.getAbsolutePath()) + .redirectOutput(Slf4jStream.of(getClass()).asDebug()) + .readOutput(true) + .execute(); + String processOutput = processResult.outputUTF8(); + int exitCode = processResult.getExitValue(); - try (InputStream inputStream = process.getInputStream()) { - String result = new String(inputStream.readAllBytes()); - - int exitCode = process.waitFor(); - - if (exitCode != 0 && exitCode != 72) { - throw new AvException( - "KESL error scanning file: " + originalFileName + ", exit code: " + exitCode); - } - LOGGER.debug("File {} scanned with result: {}", originalFileName, result); - checkScanResult(result, originalFileName); - - return exitCode; + if (exitCode != 0 && exitCode != 72) { + throw new AvException( + "KESL error scanning file: " + originalFileName + ", exit code: " + exitCode); } + checkScanResult(processOutput, originalFileName); + + return exitCode; } private void checkScanResult(String result, String originalFileName) {