@@ -63,8 +63,7 @@ public class ImageFoundRequestHandler implements RequestHandler {
|
|||||||
|
|
||||||
String yandexReverseSearchResponse;
|
String yandexReverseSearchResponse;
|
||||||
try {
|
try {
|
||||||
yandexReverseSearchResponse = yandexSearchService.searchReverseByPublicUrlWithTimeout(fileEntity,
|
yandexReverseSearchResponse = yandexSearchService.searchReverseByPublicUrlWithTimeout(fileEntity);
|
||||||
45);
|
|
||||||
} catch (TimeoutException e) {
|
} catch (TimeoutException e) {
|
||||||
log.warn("Search timeout for file {}, returning empty results", fileId);
|
log.warn("Search timeout for file {}, returning empty results", fileId);
|
||||||
return new BaseResponse(request.getMsgId(), MessageCode.SUCCESS.getCode(),
|
return new BaseResponse(request.getMsgId(), MessageCode.SUCCESS.getCode(),
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ import ru.soune.nocopy.exception.NotValidFieldException;
|
|||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.net.HttpURLConnection;
|
import java.net.HttpURLConnection;
|
||||||
|
import java.net.SocketTimeoutException;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
@@ -33,10 +34,10 @@ public class YandexSearchService {
|
|||||||
|
|
||||||
public YandexSearchService() {
|
public YandexSearchService() {
|
||||||
this.httpClient = new OkHttpClient.Builder()
|
this.httpClient = new OkHttpClient.Builder()
|
||||||
.connectTimeout(30, TimeUnit.SECONDS)
|
.connectTimeout(5, TimeUnit.SECONDS)
|
||||||
.readTimeout(120, TimeUnit.SECONDS)
|
.writeTimeout(5, TimeUnit.SECONDS)
|
||||||
.writeTimeout(30, TimeUnit.SECONDS)
|
.readTimeout(15, TimeUnit.SECONDS)
|
||||||
.callTimeout(180, TimeUnit.SECONDS)
|
.callTimeout(20, TimeUnit.SECONDS)
|
||||||
.retryOnConnectionFailure(true)
|
.retryOnConnectionFailure(true)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
@@ -102,44 +103,44 @@ public class YandexSearchService {
|
|||||||
return callReverseImageApiByUrl(publicUrl);
|
return callReverseImageApiByUrl(publicUrl);
|
||||||
}
|
}
|
||||||
|
|
||||||
private String callReverseImageApiByUrl(String imageUrl) throws IOException {
|
// private String callReverseImageApiByUrl(String imageUrl) throws IOException {
|
||||||
if (searchApiKey == null || searchApiKey.isEmpty()) {
|
// if (searchApiKey == null || searchApiKey.isEmpty()) {
|
||||||
throw new IllegalStateException("SearchAPI key not configured. Set searchapi.api-key property");
|
// throw new IllegalStateException("SearchAPI key not configured. Set searchapi.api-key property");
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
HttpUrl url = HttpUrl.parse("https://searchapi.io/api/v1/search")
|
// HttpUrl url = HttpUrl.parse("https://searchapi.io/api/v1/search")
|
||||||
.newBuilder()
|
// .newBuilder()
|
||||||
.addQueryParameter("engine", "yandex_reverse_image")
|
// .addQueryParameter("engine", "yandex_reverse_image")
|
||||||
.addQueryParameter("api_key", searchApiKey)
|
// .addQueryParameter("api_key", searchApiKey)
|
||||||
.addQueryParameter("url", imageUrl)
|
// .addQueryParameter("url", imageUrl)
|
||||||
.addQueryParameter("wait", "true")
|
// .addQueryParameter("wait", "true")
|
||||||
.addQueryParameter("timeout", "30000")
|
// .addQueryParameter("timeout", "30000")
|
||||||
.build();
|
// .build();
|
||||||
|
//
|
||||||
log.info("Calling Yandex Reverse Image API: {}", url);
|
// log.info("Calling Yandex Reverse Image API: {}", url);
|
||||||
|
//
|
||||||
Request request = new Request.Builder()
|
// Request request = new Request.Builder()
|
||||||
.url(url)
|
// .url(url)
|
||||||
.header("Accept", "application/json")
|
// .header("Accept", "application/json")
|
||||||
.header("User-Agent", "Mozilla/5.0")
|
// .header("User-Agent", "Mozilla/5.0")
|
||||||
.build();
|
// .build();
|
||||||
|
//
|
||||||
try (Response response = httpClient.newCall(request).execute()) {
|
// try (Response response = httpClient.newCall(request).execute()) {
|
||||||
log.info("Response code: {}", response.code());
|
// log.info("Response code: {}", response.code());
|
||||||
log.info("Response headers: {}", response.headers());
|
// log.info("Response headers: {}", response.headers());
|
||||||
|
//
|
||||||
if (!response.isSuccessful()) {
|
// if (!response.isSuccessful()) {
|
||||||
String errorBody = response.body() != null ? response.body().string() : "No error body";
|
// String errorBody = response.body() != null ? response.body().string() : "No error body";
|
||||||
log.error("API request failed: {} - {}", response.code(), errorBody);
|
// log.error("API request failed: {} - {}", response.code(), errorBody);
|
||||||
throw new IOException("API request failed: " + response.code() + " - " + response.message());
|
// throw new IOException("API request failed: " + response.code() + " - " + response.message());
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
String responseBody = response.body().string();
|
// String responseBody = response.body().string();
|
||||||
log.info("Response body length: {} characters", responseBody.length());
|
// log.info("Response body length: {} characters", responseBody.length());
|
||||||
|
//
|
||||||
return responseBody;
|
// return responseBody;
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
|
||||||
|
|
||||||
private boolean isImageFile(FileEntity fileEntity) {
|
private boolean isImageFile(FileEntity fileEntity) {
|
||||||
@@ -207,79 +208,62 @@ public class YandexSearchService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public String searchReverseByPublicUrlWithTimeout(FileEntity fileEntity, int timeoutSeconds)
|
public String searchReverseByPublicUrlWithTimeout(FileEntity fileEntity)
|
||||||
throws IOException, TimeoutException {
|
throws IOException, TimeoutException {
|
||||||
|
|
||||||
if (!isImageFile(fileEntity)) {
|
if (!isImageFile(fileEntity)) {
|
||||||
log.error("File not image: {}", fileEntity.getMimeType());
|
throw new NotValidFieldException(
|
||||||
throw new NotValidFieldException("File not image", new BaseResponse(20007,
|
"File not image",
|
||||||
MessageCode.INVALID_FIELD.getCode(), MessageCode.INVALID_FIELD.getDescription(),
|
new BaseResponse(
|
||||||
Map.of("file_type", fileEntity.getMimeType())));
|
20007,
|
||||||
|
MessageCode.INVALID_FIELD.getCode(),
|
||||||
|
MessageCode.INVALID_FIELD.getDescription(),
|
||||||
|
Map.of("file_type", fileEntity.getMimeType())
|
||||||
|
)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
String publicUrl = String.format("%s/api/files/public/%s", appBaseUrl, fileEntity.getId());
|
String publicUrl = String.format("%s/api/files/public/%s", appBaseUrl, fileEntity.getId());
|
||||||
|
|
||||||
log.info("Searching reverse for image: {}", publicUrl);
|
log.info("Searching reverse for image: {}", publicUrl);
|
||||||
|
|
||||||
ExecutorService executor = Executors.newSingleThreadExecutor();
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
Future<String> future = executor.submit(() ->
|
return callReverseImageApiByUrl(publicUrl);
|
||||||
callReverseImageApiByUrlWithTimeout(publicUrl, timeoutSeconds)
|
} catch (SocketTimeoutException e) {
|
||||||
);
|
log.error("Yandex search timeout after {}", fileEntity.getId());
|
||||||
|
throw new TimeoutException("Search timeout");
|
||||||
return future.get(timeoutSeconds, TimeUnit.SECONDS);
|
|
||||||
|
|
||||||
} catch (TimeoutException e) {
|
|
||||||
log.error("Yandex search timeout after {} seconds for file: {}", timeoutSeconds, fileEntity.getId());
|
|
||||||
throw e;
|
|
||||||
} catch (InterruptedException e) {
|
|
||||||
Thread.currentThread().interrupt();
|
|
||||||
throw new IOException("Search interrupted", e);
|
|
||||||
} catch (ExecutionException e) {
|
|
||||||
Throwable cause = e.getCause();
|
|
||||||
if (cause instanceof IOException) {
|
|
||||||
throw (IOException) cause;
|
|
||||||
}
|
|
||||||
throw new IOException("Search failed", cause);
|
|
||||||
} finally {
|
|
||||||
executor.shutdownNow();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private String callReverseImageApiByUrlWithTimeout(String imageUrl, int timeoutSeconds) throws IOException {
|
private String callReverseImageApiByUrl(String imageUrl) throws IOException {
|
||||||
if (searchApiKey == null || searchApiKey.isEmpty()) {
|
|
||||||
|
if (searchApiKey == null || searchApiKey.isBlank()) {
|
||||||
throw new IllegalStateException("SearchAPI key not configured");
|
throw new IllegalStateException("SearchAPI key not configured");
|
||||||
}
|
}
|
||||||
|
|
||||||
int apiTimeout = Math.max(1, timeoutSeconds - 2) * 1000;
|
|
||||||
|
|
||||||
HttpUrl url = HttpUrl.parse("https://searchapi.io/api/v1/search")
|
HttpUrl url = HttpUrl.parse("https://searchapi.io/api/v1/search")
|
||||||
.newBuilder()
|
.newBuilder()
|
||||||
.addQueryParameter("engine", "yandex_reverse_image")
|
.addQueryParameter("engine", "yandex_reverse_image")
|
||||||
.addQueryParameter("api_key", searchApiKey)
|
.addQueryParameter("api_key", searchApiKey)
|
||||||
.addQueryParameter("url", imageUrl)
|
.addQueryParameter("url", imageUrl)
|
||||||
.addQueryParameter("wait", "true")
|
.addQueryParameter("wait", "true")
|
||||||
.addQueryParameter("timeout", String.valueOf(apiTimeout))
|
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
log.info("Calling Yandex API with timeout {}ms: {}", apiTimeout, imageUrl);
|
|
||||||
|
|
||||||
Request request = new Request.Builder()
|
Request request = new Request.Builder()
|
||||||
.url(url)
|
.url(url)
|
||||||
.header("Accept", "application/json")
|
.header("Accept", "application/json")
|
||||||
.header("User-Agent", "Mozilla/5.0")
|
.header("User-Agent", "Mozilla/5.0")
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
long startTime = System.currentTimeMillis();
|
long start = System.currentTimeMillis();
|
||||||
|
|
||||||
try (Response response = httpClient.newCall(request).execute()) {
|
try (Response response = httpClient.newCall(request).execute()) {
|
||||||
long duration = System.currentTimeMillis() - startTime;
|
|
||||||
|
|
||||||
log.info("Response code: {}, duration: {}ms", response.code(), duration);
|
long duration = System.currentTimeMillis() - start;
|
||||||
|
log.info("Yandex response code={}, duration={}ms", response.code(), duration);
|
||||||
|
|
||||||
if (!response.isSuccessful()) {
|
if (!response.isSuccessful()) {
|
||||||
String errorBody = response.body() != null ? response.body().string() : "No error body";
|
throw new IOException("API error: " + response.code());
|
||||||
log.error("API request failed: {} - {}", response.code(), errorBody);
|
|
||||||
throw new IOException("API request failed: " + response.code());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ResponseBody body = response.body();
|
ResponseBody body = response.body();
|
||||||
@@ -287,12 +271,96 @@ public class YandexSearchService {
|
|||||||
throw new IOException("Empty response body");
|
throw new IOException("Empty response body");
|
||||||
}
|
}
|
||||||
|
|
||||||
String responseBody = body.string();
|
return body.string();
|
||||||
log.info("Response body length: {} characters", responseBody.length());
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return responseBody;
|
// public String searchReverseByPublicUrlWithTimeout(FileEntity fileEntity, int timeoutSeconds)
|
||||||
}
|
// throws IOException, TimeoutException {
|
||||||
}
|
// if (!isImageFile(fileEntity)) {
|
||||||
|
// log.error("File not image: {}", fileEntity.getMimeType());
|
||||||
|
// throw new NotValidFieldException("File not image", new BaseResponse(20007,
|
||||||
|
// MessageCode.INVALID_FIELD.getCode(), MessageCode.INVALID_FIELD.getDescription(),
|
||||||
|
// Map.of("file_type", fileEntity.getMimeType())));
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// String publicUrl = String.format("%s/api/files/public/%s", appBaseUrl, fileEntity.getId());
|
||||||
|
// log.info("Searching reverse for image: {}", publicUrl);
|
||||||
|
//
|
||||||
|
// ExecutorService executor = Executors.newSingleThreadExecutor();
|
||||||
|
//
|
||||||
|
// try {
|
||||||
|
// Future<String> future = executor.submit(() ->
|
||||||
|
// callReverseImageApiByUrlWithTimeout(publicUrl, timeoutSeconds)
|
||||||
|
// );
|
||||||
|
//
|
||||||
|
// return future.get(timeoutSeconds, TimeUnit.SECONDS);
|
||||||
|
//
|
||||||
|
// } catch (TimeoutException e) {
|
||||||
|
// log.error("Yandex search timeout after {} seconds for file: {}", timeoutSeconds, fileEntity.getId());
|
||||||
|
// throw e;
|
||||||
|
// } catch (InterruptedException e) {
|
||||||
|
// Thread.currentThread().interrupt();
|
||||||
|
// throw new IOException("Search interrupted", e);
|
||||||
|
// } catch (ExecutionException e) {
|
||||||
|
// Throwable cause = e.getCause();
|
||||||
|
// if (cause instanceof IOException) {
|
||||||
|
// throw (IOException) cause;
|
||||||
|
// }
|
||||||
|
// throw new IOException("Search failed", cause);
|
||||||
|
// } finally {
|
||||||
|
// executor.shutdownNow();
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
// private String callReverseImageApiByUrlWithTimeout(String imageUrl, int timeoutSeconds) throws IOException {
|
||||||
|
// if (searchApiKey == null || searchApiKey.isEmpty()) {
|
||||||
|
// throw new IllegalStateException("SearchAPI key not configured");
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// int apiTimeout = Math.max(1, timeoutSeconds - 2) * 1000;
|
||||||
|
//
|
||||||
|
// HttpUrl url = HttpUrl.parse("https://searchapi.io/api/v1/search")
|
||||||
|
// .newBuilder()
|
||||||
|
// .addQueryParameter("engine", "yandex_reverse_image")
|
||||||
|
// .addQueryParameter("api_key", searchApiKey)
|
||||||
|
// .addQueryParameter("url", imageUrl)
|
||||||
|
// .addQueryParameter("wait", "true")
|
||||||
|
// .addQueryParameter("timeout", String.valueOf(apiTimeout))
|
||||||
|
// .build();
|
||||||
|
//
|
||||||
|
// log.info("Calling Yandex API with timeout {}ms: {}", apiTimeout, imageUrl);
|
||||||
|
//
|
||||||
|
// Request request = new Request.Builder()
|
||||||
|
// .url(url)
|
||||||
|
// .header("Accept", "application/json")
|
||||||
|
// .header("User-Agent", "Mozilla/5.0")
|
||||||
|
// .build();
|
||||||
|
//
|
||||||
|
// long startTime = System.currentTimeMillis();
|
||||||
|
//
|
||||||
|
// try (Response response = httpClient.newCall(request).execute()) {
|
||||||
|
// long duration = System.currentTimeMillis() - startTime;
|
||||||
|
//
|
||||||
|
// log.info("Response code: {}, duration: {}ms", response.code(), duration);
|
||||||
|
//
|
||||||
|
// if (!response.isSuccessful()) {
|
||||||
|
// String errorBody = response.body() != null ? response.body().string() : "No error body";
|
||||||
|
// log.error("API request failed: {} - {}", response.code(), errorBody);
|
||||||
|
// throw new IOException("API request failed: " + response.code());
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// ResponseBody body = response.body();
|
||||||
|
// if (body == null) {
|
||||||
|
// throw new IOException("Empty response body");
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// String responseBody = body.string();
|
||||||
|
// log.info("Response body length: {} characters", responseBody.length());
|
||||||
|
//
|
||||||
|
// return responseBody;
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
private String readAll(BufferedReader reader) throws IOException {
|
private String readAll(BufferedReader reader) throws IOException {
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
|
|||||||
Reference in New Issue
Block a user