dev add violation notion

This commit is contained in:
vladp
2026-03-20 17:37:00 +07:00
parent 2bf5e99fbb
commit 29cdbb887f
9 changed files with 354 additions and 174 deletions
@@ -8,6 +8,7 @@ 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;
@@ -23,40 +24,15 @@ import java.util.concurrent.*;
@Slf4j
@Service
public class SearchImageService {
private final ObjectMapper objectMapper;
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(60, TimeUnit.SECONDS)
.writeTimeout(60, TimeUnit.SECONDS)
.readTimeout(60, TimeUnit.SECONDS)
.callTimeout(60, 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();
private final SearchProperties searchProperties;
public SearchImageService(SearchProperties searchProperties) {
this.searchProperties = searchProperties;
this.objectMapper = new ObjectMapper();
}
@@ -113,17 +89,34 @@ public class SearchImageService {
return mimeType != null && mimeType.startsWith("image");
}
private String callReverseImageApiByUrl(String imageUrl, String engine, String searchType) throws IOException {
log.info("=== START SEARCH API CALL ===");
log.info("Engine: {}, SearchType: {}", engine, searchType);
log.info("ImageUrl: {}", imageUrl);
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()) {
log.error("SearchAPI key not configured");
throw new IllegalStateException("SearchAPI key not configured");
}
OkHttpClient client = engine.contains("yandex_reverse_image") ? yandexHttpClient : googleHttpClient;
boolean useProxy = searchProperties.getProxy().isEnabled();
OkHttpClient client = createHttpClient(useProxy);
HttpUrl url = HttpUrl.parse("https://www.searchapi.io/api/v1/search")
.newBuilder()
@@ -134,8 +127,6 @@ public class SearchImageService {
.addQueryParameter("t_", String.valueOf(System.currentTimeMillis()))
.build();
log.info("Request URL: {}", url);
Request request = new Request.Builder()
.url(url)
.header("Accept", "application/json")
@@ -149,27 +140,17 @@ public class SearchImageService {
log.info("SearchAPI response code={}, duration={}ms, engine={}",
response.code(), duration, engine);
if (!response.isSuccessful()) {
String errorBody = response.body() != null ? response.body().string() : "null";
throw new IOException("API error " + response.code() + ": " + errorBody);
}
ResponseBody body = response.body();
if (body == null) {
log.error("Response body is null");
throw new IOException("Empty response body");
}
String responseBody = body.string();
log.info("=== RESPONSE BODY LENGTH: {} ===", responseBody.length());
log.info("=== RESPONSE BODY START: {} ===",
responseBody.length() > 200 ? responseBody.substring(0, 200) : responseBody);
if (!response.isSuccessful()) {
log.error("Response not successful: {}", response.code());
throw new IOException("API error " + response.code() + ": " + responseBody);
}
return responseBody;
} catch (Exception e) {
log.error("Exception in search API call: {}", e.getMessage(), e);
throw e;
return body.string();
}
}