This commit is contained in:
@@ -4,8 +4,8 @@ import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.DeserializationFeature;
|
||||
import jakarta.annotation.PostConstruct;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import okhttp3.*;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Service;
|
||||
import ru.soune.nocopy.dto.BaseResponse;
|
||||
@@ -13,7 +13,6 @@ import ru.soune.nocopy.dto.MessageCode;
|
||||
import ru.soune.nocopy.dto.file.YandexSearchResponse;
|
||||
import ru.soune.nocopy.entity.file.FileEntity;
|
||||
import ru.soune.nocopy.exception.NotValidFieldException;
|
||||
import ru.soune.nocopy.repository.FileEntityRepository;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.HttpURLConnection;
|
||||
@@ -22,15 +21,28 @@ import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.Base64;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@Slf4j
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class YandexSearchService {
|
||||
private final FileEntityRepository fileEntityRepository;
|
||||
|
||||
private final ObjectMapper objectMapper;
|
||||
|
||||
private final OkHttpClient httpClient;
|
||||
|
||||
public YandexSearchService() {
|
||||
this.httpClient = new OkHttpClient.Builder()
|
||||
.connectTimeout(30, TimeUnit.SECONDS)
|
||||
.readTimeout(120, TimeUnit.SECONDS)
|
||||
.writeTimeout(30, TimeUnit.SECONDS)
|
||||
.callTimeout(180, TimeUnit.SECONDS)
|
||||
.retryOnConnectionFailure(true)
|
||||
.build();
|
||||
|
||||
this.objectMapper = new ObjectMapper();
|
||||
}
|
||||
|
||||
@Value("${yandex.api-key}")
|
||||
private String apiKey;
|
||||
|
||||
@@ -40,6 +52,12 @@ public class YandexSearchService {
|
||||
@Value("${yandex.search-url}")
|
||||
private String searchUrl;
|
||||
|
||||
@Value("${searchapi.api-key:}")
|
||||
private String searchApiKey;
|
||||
|
||||
@Value("${server.baseurl}")
|
||||
private String appBaseUrl;
|
||||
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
||||
@@ -68,6 +86,63 @@ public class YandexSearchService {
|
||||
return callYandexApi(fileBytes);
|
||||
}
|
||||
|
||||
|
||||
public String searchReverseByPublicUrl(FileEntity fileEntity) throws IOException {
|
||||
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);
|
||||
|
||||
return callReverseImageApiByUrl(publicUrl);
|
||||
}
|
||||
|
||||
|
||||
private String callReverseImageApiByUrl(String imageUrl) throws IOException {
|
||||
if (searchApiKey == null || searchApiKey.isEmpty()) {
|
||||
throw new IllegalStateException("SearchAPI key not configured. Set searchapi.api-key property");
|
||||
}
|
||||
|
||||
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", "30000")
|
||||
.build();
|
||||
|
||||
log.info("Calling Yandex Reverse Image API: {}", url);
|
||||
|
||||
Request request = new Request.Builder()
|
||||
.url(url)
|
||||
.header("Accept", "application/json")
|
||||
.header("User-Agent", "Mozilla/5.0")
|
||||
.build();
|
||||
|
||||
try (Response response = httpClient.newCall(request).execute()) {
|
||||
log.info("Response code: {}", response.code());
|
||||
log.info("Response headers: {}", response.headers());
|
||||
|
||||
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() + " - " + response.message());
|
||||
}
|
||||
|
||||
String responseBody = response.body().string();
|
||||
log.info("Response body length: {} characters", responseBody.length());
|
||||
|
||||
return responseBody;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private boolean isImageFile(FileEntity fileEntity) {
|
||||
String mimeType = fileEntity.getMimeType();
|
||||
return mimeType != null && mimeType.startsWith("image");
|
||||
|
||||
Reference in New Issue
Block a user