SUPPORT-8841: use zt-exec

This commit is contained in:
Alexandr Shalaginov 2025-01-14 14:04:14 +03:00
parent 9ef24f6094
commit 15b200abbc
2 changed files with 26 additions and 21 deletions

View file

@ -22,6 +22,12 @@
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.zeroturnaround</groupId>
<artifactId>zt-exec</artifactId>
<version>1.12</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>

View file

@ -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) {