Revert "dev add violation notion"
Test Workflow / test (push) Successful in 3s

This reverts commit 4ff5c5ec13.
This commit is contained in:
vladp
2026-03-20 16:02:11 +07:00
parent a9b7b7ca6e
commit 726da25ddf
9 changed files with 153 additions and 350 deletions
@@ -1,18 +1,21 @@
package ru.soune.nocopy.service.search;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.vrt.fileprotection.image.ImageCheckResult;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import ru.soune.nocopy.configuration.search.SearchProperties;
import ru.soune.nocopy.dto.file.YandexSearchResponse;
import ru.soune.nocopy.entity.file.FileEntity;
import ru.soune.nocopy.entity.search.GlobalSearchResult;
import ru.soune.nocopy.entity.search.GlobalSearchTask;
import ru.soune.nocopy.entity.search.SearchStatus;
import ru.soune.nocopy.entity.violation.Violation;
import ru.soune.nocopy.repository.GlobalSearchResultRepository;
import ru.soune.nocopy.repository.GlobalSearchTaskRepository;
import ru.soune.nocopy.repository.ViolationRepository;
import ru.soune.nocopy.service.tariff.TariffConstants;
import ru.soune.nocopy.service.tariff.TariffInfoService;
import ru.soune.nocopy.service.violation.ViolationService;
@@ -33,13 +36,15 @@ public class GlobalSearchAsyncProcessor {
private final SearchImageService searchImageService;
private final ObjectMapper objectMapper;
private final TariffInfoService tariffInfoService;
private final GlobalSearchTaskRepository globalSearchTaskRepository;
private final ViolationService violationService;
private final ViolationRepository violationRepository;
private final SearchProperties searchProperties;
private final ViolationService violationService;
@Async
@Transactional
@@ -80,85 +85,52 @@ public class GlobalSearchAsyncProcessor {
result = globalSearchResultRepository.save(result);
boolean useGoogle = searchProperties.getEngines().getOrDefault("google",
new SearchProperties.EngineConfig()).isEnabled();
boolean useYandex = searchProperties.getEngines().getOrDefault("yandex",
new SearchProperties.EngineConfig()).isEnabled();
log.info("Global search settings for file {}: useGoogle={}, useYandex={}",
file.getId(), useGoogle, useYandex);
List<YandexSearchResponse.ImageResult> googleImages = new ArrayList<>();
// List<YandexSearchResponse.ImageResult> googleImages = new ArrayList<>();
List<YandexSearchResponse.ImageResult> yandexImages = new ArrayList<>();
List<YandexSearchResponse.ImageResult> allResults = new ArrayList<>();
List<YandexSearchResponse.ImageResult> allUniqueImages = new ArrayList<>();
boolean hasTimeout = false;
if (useGoogle) {
try {
String searchResponseGoogle = searchImageService.searchReverseByPublicUrl(
file, "google_lens", "exact_matches");
// try {
// String searchResponseGoogle = searchImageService.searchReverseByPublicUrl(
// file, "google_lens", "exact_matches");
//
// googleImages = searchImageService.getAllImagesWithoutPagination(searchResponseGoogle,
// "exact_matches");
//
// allUniqueImages.addAll(googleImages);
// log.info("Google search OK for file {}", file.getId());
//
// } catch (TimeoutException e) {
// log.warn("Google search timeout for file {}", file.getId());
// hasTimeout = true;
// } catch (IOException e) {
// log.error("Google search failed for file {}", file.getId(), e);
// }
googleImages = searchImageService.getAllImagesWithoutPagination(searchResponseGoogle,
"exact_matches");
try {
String searchResponseYandex = searchImageService.searchReverseByPublicUrl(
file, "yandex_reverse_image", "visual_matches");
allResults.addAll(googleImages);
log.info("Google search OK for file {}, found {} results", file.getId(), googleImages.size());
yandexImages =
searchImageService.getAllImagesWithoutPagination(searchResponseYandex, "visual_matches");
} catch (TimeoutException e) {
log.warn("Google search timeout for file {}", file.getId());
hasTimeout = true;
} catch (IOException e) {
log.error("Google search failed for file {}", file.getId(), e);
}
} else {
log.info("Google search is disabled by settings for file {}", file.getId());
allUniqueImages.addAll(yandexImages);
log.info("Yandex search OK for file {}", file.getId());
} catch (TimeoutException e) {
log.warn("Yandex search timeout for file {}", file.getId());
hasTimeout = true;
} catch (IOException e) {
log.error("Yandex search failed for file {}", file.getId(), e);
}
if (useYandex) {
try {
String searchResponseYandex = searchImageService.searchReverseByPublicUrl(
file, "yandex_reverse_image", "visual_matches");
// allUniqueImages = searchImageService.removeDuplicateUrls(
// allUniqueImages.stream()
// .filter(img -> img.getUrl() != null)
// .collect(Collectors.toList()),
// new ArrayList<>());
yandexImages = searchImageService.getAllImagesWithoutPagination(searchResponseYandex, "visual_matches");
allResults.addAll(yandexImages);
log.info("Yandex search OK for file {}, found {} results", file.getId(), yandexImages.size());
} catch (TimeoutException e) {
log.warn("Yandex search timeout for file {}", file.getId());
hasTimeout = true;
} catch (IOException e) {
log.error("Yandex search failed for file {}", file.getId(), e);
}
} else {
log.info("Yandex search is disabled by settings for file {}", file.getId());
}
List<YandexSearchResponse.ImageResult> allUniqueImages;
if (useGoogle && useYandex) {
allUniqueImages = searchImageService.removeDuplicateUrls(
yandexImages.stream()
.filter(img -> img.getUrl() != null)
.collect(Collectors.toList()),
googleImages.stream()
.filter(img -> img.getUrl() != null)
.collect(Collectors.toList())
);
log.info("Merged results from both engines: {} unique images", allUniqueImages.size());
} else if (useGoogle) {
allUniqueImages = googleImages.stream()
.filter(img -> img.getUrl() != null)
.collect(Collectors.toList());
log.info("Results only from Google: {} images", allUniqueImages.size());
} else if (useYandex) {
allUniqueImages = yandexImages.stream()
.filter(img -> img.getUrl() != null)
.collect(Collectors.toList());
log.info("Results only from Yandex: {} images", allUniqueImages.size());
} else {
allUniqueImages = new ArrayList<>();
log.warn("All search engines are disabled for file {}", file.getId());
}
allUniqueImages = yandexImages;
for (YandexSearchResponse.ImageResult imageResult : allUniqueImages) {
violationService.processViolation(imageResult, file, result);
@@ -166,15 +138,10 @@ public class GlobalSearchAsyncProcessor {
if (allUniqueImages.isEmpty() && hasTimeout) {
result.setFileStatus(SearchStatus.TIMEOUT.name());
} else if (allUniqueImages.isEmpty() && (!useGoogle && !useYandex)) {
result.setFileStatus(SearchStatus.FAILED.name());
log.warn("No results for file {} because all engines are disabled", file.getId());
} else {
result.setFileStatus(SearchStatus.SUCCESS.name());
if (!allUniqueImages.isEmpty()) {
tariffInfoService.writeOffTokens(userId, TariffConstants.TOKEN_VALUE_FOR_SEARCH);
}
tariffInfoService.writeOffTokens(userId, TariffConstants.TOKEN_VALUE_FOR_SEARCH);
}
return result;
@@ -8,7 +8,6 @@ import lombok.extern.slf4j.Slf4j;
import okhttp3.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import ru.soune.nocopy.configuration.search.SearchProperties;
import ru.soune.nocopy.dto.BaseResponse;
import ru.soune.nocopy.dto.MessageCode;
import ru.soune.nocopy.dto.file.YandexSearchResponse;
@@ -24,15 +23,40 @@ import java.util.concurrent.*;
@Slf4j
@Service
public class SearchImageService {
private final ObjectMapper objectMapper;
private final SearchProperties searchProperties;
private final OkHttpClient yandexHttpClient;
private final OkHttpClient googleHttpClient;
public SearchImageService() {
Proxy proxy = new Proxy(Proxy.Type.HTTP,
new InetSocketAddress("193.46.217.94", 3128));
this.yandexHttpClient = new OkHttpClient.Builder()
.proxy(proxy)
.connectTimeout(35, TimeUnit.SECONDS)
.writeTimeout(35, TimeUnit.SECONDS)
.readTimeout(35, TimeUnit.SECONDS)
.callTimeout(35, TimeUnit.SECONDS)
.followRedirects(true)
.followSslRedirects(true)
.retryOnConnectionFailure(true)
.build();
this.googleHttpClient = new OkHttpClient.Builder()
// .proxy(proxy)
.connectTimeout(35, TimeUnit.SECONDS)
.writeTimeout(35, TimeUnit.SECONDS)
.readTimeout(35, TimeUnit.SECONDS)
.callTimeout(35, TimeUnit.SECONDS)
.followRedirects(true)
.followSslRedirects(true)
.retryOnConnectionFailure(true)
.build();
public SearchImageService(SearchProperties searchProperties) {
this.searchProperties = searchProperties;
this.objectMapper = new ObjectMapper();
}
@@ -89,34 +113,12 @@ public class SearchImageService {
return mimeType != null && mimeType.startsWith("image");
}
private OkHttpClient createHttpClient(boolean useProxy) {
OkHttpClient.Builder builder = new OkHttpClient.Builder()
.connectTimeout(35, TimeUnit.SECONDS)
.writeTimeout(35, TimeUnit.SECONDS)
.readTimeout(35, TimeUnit.SECONDS)
.callTimeout(35, TimeUnit.SECONDS)
.followRedirects(true)
.followSslRedirects(true)
.retryOnConnectionFailure(true);
if (useProxy && searchProperties.getProxy().isEnabled()) {
Proxy proxy = new Proxy(Proxy.Type.HTTP,
new InetSocketAddress(searchProperties.getProxy().getHost(),
searchProperties.getProxy().getPort()));
builder.proxy(proxy);
}
return builder.build();
}
private String callReverseImageApiByUrl(String imageUrl, String engine, String searchType) throws IOException {
if (searchApiKey == null || searchApiKey.isBlank()) {
throw new IllegalStateException("SearchAPI key not configured");
}
boolean useProxy = searchProperties.getProxy().isEnabled();
OkHttpClient client = createHttpClient(useProxy);
OkHttpClient client = engine.contains("yandex_reverse_image") ? yandexHttpClient : googleHttpClient;
HttpUrl url = HttpUrl.parse("https://www.searchapi.io/api/v1/search")
.newBuilder()