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> <artifactId>spring-boot-starter-web</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.zeroturnaround</groupId>
<artifactId>zt-exec</artifactId>
<version>1.12</version>
</dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId> <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.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.util.concurrent.TimeoutException;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile; 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; import ru.micord.av.service.exception.AvException;
/** /**
@ -33,7 +36,7 @@ public class FileScanService {
tempFile = saveFile(file); tempFile = saveFile(file);
return runKeslScan(tempFile, file.getOriginalFilename()); return runKeslScan(tempFile, file.getOriginalFilename());
} }
catch (IOException | InterruptedException e) { catch (IOException | InterruptedException | TimeoutException e) {
throw new AvException("Error scanning file: " + file.getOriginalFilename(), e); throw new AvException("Error scanning file: " + file.getOriginalFilename(), e);
} }
finally { finally {
@ -57,27 +60,23 @@ public class FileScanService {
return tempFile; return tempFile;
} }
private int runKeslScan(File file, String originalFileName) throws IOException, InterruptedException { private int runKeslScan(File file, String originalFileName)
ProcessBuilder processBuilder = new ProcessBuilder(KESL_CONTROL, KESL_SCAN, throws IOException, InterruptedException, TimeoutException {
file.getAbsolutePath() ProcessResult processResult = new ProcessExecutor()
); .command(KESL_CONTROL, KESL_SCAN, file.getAbsolutePath())
processBuilder.redirectErrorStream(true); .redirectOutput(Slf4jStream.of(getClass()).asDebug())
Process process = processBuilder.start(); .readOutput(true)
.execute();
String processOutput = processResult.outputUTF8();
int exitCode = processResult.getExitValue();
try (InputStream inputStream = process.getInputStream()) { if (exitCode != 0 && exitCode != 72) {
String result = new String(inputStream.readAllBytes()); throw new AvException(
"KESL error scanning file: " + originalFileName + ", exit code: " + exitCode);
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;
} }
checkScanResult(processOutput, originalFileName);
return exitCode;
} }
private void checkScanResult(String result, String originalFileName) { private void checkScanResult(String result, String originalFileName) {