SUPPORT-8836:refactor

This commit is contained in:
adel.kalimullin 2024-12-27 12:41:45 +03:00
parent 4fdc195b91
commit 384a97d629
3 changed files with 7 additions and 44 deletions

View file

@ -9,7 +9,6 @@ import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import ru.micord.av.service.model.ScanResult;
import ru.micord.av.service.service.FileScanService; import ru.micord.av.service.service.FileScanService;
/** /**
@ -32,11 +31,10 @@ public class FileScanController {
return ResponseEntity.badRequest().build(); return ResponseEntity.badRequest().build();
} }
try { try {
ScanResult result = fileScanService.scanFile(file); int result = fileScanService.scanFile(file);
LOGGER.info("Scan result for file {}: status - {}, verdicts - {}", LOGGER.info("Scan result for file {}: exitCode - {}",
file.getOriginalFilename(), file.getOriginalFilename(),
result.status(), result);
String.join(", ", result.verdicts()));
return ResponseEntity.ok(result); return ResponseEntity.ok(result);
} }
catch (Exception e) { catch (Exception e) {

View file

@ -1,17 +0,0 @@
package ru.micord.av.service.model;
import java.util.Map;
/**
* @author Adel Kalimullin
*/
public record ScanResult(String completed, String created, Integer progress,
Map<String, Scan> scan_result, String status, String[] verdicts) {
public record Scan(String started, String stopped, Threat[] threats, String verdict) {
public static final String VERDICT_CLEAN = "clean";
public static final String VERDICT_INFECTED = "infected";
public record Threat(String name, String object) {
}
}
}

View file

@ -12,7 +12,6 @@ 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 ru.micord.av.service.exception.AvException; import ru.micord.av.service.exception.AvException;
import ru.micord.av.service.model.ScanResult;
/** /**
* @author Adel Kalimullin * @author Adel Kalimullin
@ -25,7 +24,7 @@ public class FileScanService {
@Value("${file.upload.directory}") @Value("${file.upload.directory}")
private String uploadDirectory; private String uploadDirectory;
public ScanResult scanFile(MultipartFile file) { public int scanFile(MultipartFile file) {
File tempFile = null; File tempFile = null;
try { try {
tempFile = saveFile(file); tempFile = saveFile(file);
@ -56,32 +55,15 @@ public class FileScanService {
return tempFile.toFile(); return tempFile.toFile();
} }
private ScanResult runKeslScan(File file) throws IOException, InterruptedException { private int runKeslScan(File file) throws IOException, InterruptedException {
ProcessBuilder processBuilder = new ProcessBuilder(KESL_CONTROL, KESL_SCAN, ProcessBuilder processBuilder = new ProcessBuilder(KESL_CONTROL, KESL_SCAN,
file.getAbsolutePath() file.getAbsolutePath()
); );
processBuilder.redirectErrorStream(true); processBuilder.redirectErrorStream(true);
Process process = processBuilder.start(); Process process = processBuilder.start();
int exitCode = process.waitFor(); int exitCode = process.waitFor();
if (exitCode == 0) { if (exitCode == 0 || exitCode == 72) {
return new ScanResult( return exitCode;
null,
null,
100,
null,
"completed",
new String[] {ScanResult.Scan.VERDICT_CLEAN}
);
}
else if (exitCode == 72) {
return new ScanResult(
null,
null,
100,
null,
"completed",
new String[] {ScanResult.Scan.VERDICT_INFECTED}
);
} }
else { else {
throw new AvException("KESL error, exit code: " + exitCode); throw new AvException("KESL error, exit code: " + exitCode);