SUPPORT-8836:refactor

This commit is contained in:
adel.kalimullin 2024-12-27 12:53:21 +03:00
parent a94e2d535d
commit fb36447ecb
3 changed files with 7 additions and 33 deletions

View file

@ -1,17 +0,0 @@
package ru.micord.ervu.av.response;
import java.util.Map;
/**
* @author r.latypov
*/
public record AvResponse(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

@ -5,7 +5,6 @@ import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.concurrent.CompletableFuture;
import com.google.gson.Gson;
@ -30,7 +29,6 @@ import ru.micord.ervu.av.exception.RetryableException;
import ru.micord.ervu.av.kafka.dto.DownloadRequest;
import ru.micord.ervu.av.kafka.dto.DownloadResponse;
import ru.micord.ervu.av.kafka.dto.FileStatus;
import ru.micord.ervu.av.response.AvResponse;
import ru.micord.ervu.av.s3.S3Service;
/**
@ -38,6 +36,7 @@ import ru.micord.ervu.av.s3.S3Service;
*/
@Service
public class FileUploadService {
private static final int INJECTED_CODE = 72;
private static final Logger LOGGER = LoggerFactory.getLogger(FileUploadService.class);
@Value("${av.check.enabled}")
private boolean avCheckEnabled;
@ -84,16 +83,11 @@ public class FileUploadService {
String downloadUrl = fileUrl.fileUrl();
fIleManager.downloadFile(downloadUrl, filePath);
boolean isAvError = false;
boolean clean = true;
boolean infected = false;
int exitCode = 0;
if (avCheckEnabled) {
try {
AvResponse avResponse = receiveScanReportRetryable.checkFile(filePath);
clean = Arrays.stream(avResponse.verdicts())
.anyMatch(verdict -> verdict.equalsIgnoreCase(AvResponse.Scan.VERDICT_CLEAN));
infected = Arrays.stream(avResponse.verdicts())
.anyMatch(verdict -> verdict.equalsIgnoreCase(AvResponse.Scan.VERDICT_INFECTED));
exitCode = receiveScanReportRetryable.checkFile(filePath);
}
catch (FileUploadException | RetryableException e) {
LOGGER.error(e.getMessage(), e);
@ -101,7 +95,7 @@ public class FileUploadService {
}
}
if (isAvError || infected || !clean) {
if (isAvError || exitCode == INJECTED_CODE) {
downloadRequest.fileInfo().setFileUrl(null);
FileStatus fileStatus = isAvError ? FileStatus.FILE_STATUS_11 : FileStatus.FILE_STATUS_02;
downloadRequest.fileInfo().setFileStatus(fileStatus);

View file

@ -6,7 +6,6 @@ import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.Path;
import com.google.gson.Gson;
import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
@ -24,7 +23,6 @@ import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Service;
import ru.micord.ervu.av.exception.FileUploadException;
import ru.micord.ervu.av.exception.RetryableException;
import ru.micord.ervu.av.response.AvResponse;
/**
* @author r.latypov
@ -38,12 +36,11 @@ public class ReceiveScanReportRetryable {
@Retryable(retryFor = {RetryableException.class},
maxAttemptsExpression = "${av.retry.max.attempts.count}",
backoff = @Backoff(delayExpression = "${av.retry.delay.milliseconds}"))
public AvResponse checkFile(Path filePath) throws RetryableException, FileUploadException {
public int checkFile(Path filePath) throws RetryableException, FileUploadException {
File file = filePath.toFile();
try (CloseableHttpClient client = HttpClients.createDefault()) {
URI uri = new URIBuilder(avRestAddress)
.addParameter("wait", "1")
.build();
HttpPost post = new HttpPost(uri);
HttpEntity entity = MultipartEntityBuilder.create()
@ -53,10 +50,10 @@ public class ReceiveScanReportRetryable {
try (CloseableHttpResponse postResponse = client.execute(post)) {
int postStatusCode = postResponse.getStatusLine().getStatusCode();
String postResponseJson = EntityUtils.toString(postResponse.getEntity());
String exitCode = EntityUtils.toString(postResponse.getEntity());
if (postStatusCode == HttpStatus.OK.value()) {
return new Gson().fromJson(postResponseJson, AvResponse.class);
return Integer.parseInt(exitCode);
}
else {
throw new FileUploadException(