2026-01-20 03:47:51 +07:00
|
|
|
package ru.soune.nocopy.service.search;
|
2025-12-22 14:07:44 +07:00
|
|
|
|
|
|
|
|
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 org.springframework.beans.factory.annotation.Value;
|
|
|
|
|
import org.springframework.stereotype.Service;
|
2025-12-22 14:42:27 +07:00
|
|
|
import ru.soune.nocopy.dto.BaseResponse;
|
|
|
|
|
import ru.soune.nocopy.dto.MessageCode;
|
2025-12-22 14:07:44 +07:00
|
|
|
import ru.soune.nocopy.dto.file.YandexSearchResponse;
|
|
|
|
|
import ru.soune.nocopy.entity.file.FileEntity;
|
2025-12-22 14:42:27 +07:00
|
|
|
import ru.soune.nocopy.exception.NotValidFieldException;
|
2025-12-22 14:07:44 +07:00
|
|
|
import ru.soune.nocopy.repository.FileEntityRepository;
|
|
|
|
|
|
|
|
|
|
import java.io.*;
|
|
|
|
|
import java.net.HttpURLConnection;
|
|
|
|
|
import java.net.URL;
|
|
|
|
|
import java.nio.file.Files;
|
|
|
|
|
import java.nio.file.Path;
|
|
|
|
|
import java.util.Base64;
|
2025-12-22 14:42:27 +07:00
|
|
|
import java.util.Map;
|
2025-12-22 14:07:44 +07:00
|
|
|
|
|
|
|
|
@Slf4j
|
|
|
|
|
@Service
|
|
|
|
|
@RequiredArgsConstructor
|
|
|
|
|
public class YandexSearchService {
|
|
|
|
|
private final FileEntityRepository fileEntityRepository;
|
2026-01-10 00:54:49 +07:00
|
|
|
|
2025-12-22 14:07:44 +07:00
|
|
|
private final ObjectMapper objectMapper;
|
|
|
|
|
|
2026-01-10 00:54:49 +07:00
|
|
|
@Value("${yandex.api-key}")
|
2025-12-22 14:07:44 +07:00
|
|
|
private String apiKey;
|
|
|
|
|
|
2026-01-10 00:54:49 +07:00
|
|
|
@Value("${yandex.folder-id}")
|
2025-12-22 14:07:44 +07:00
|
|
|
private String folderId;
|
|
|
|
|
|
2026-01-10 00:54:49 +07:00
|
|
|
@Value("${yandex.search-url}")
|
2025-12-22 14:07:44 +07:00
|
|
|
private String searchUrl;
|
2026-01-10 01:06:32 +07:00
|
|
|
|
2025-12-22 14:07:44 +07:00
|
|
|
@PostConstruct
|
|
|
|
|
public void init() {
|
|
|
|
|
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
|
|
|
|
objectMapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public YandexSearchResponse searchByFileEntity(String fileId) throws IOException {
|
|
|
|
|
FileEntity fileEntity = fileEntityRepository.findById(fileId)
|
|
|
|
|
.orElseThrow(() -> {
|
2025-12-22 14:42:27 +07:00
|
|
|
throw new NotValidFieldException("File not found", new BaseResponse(20007,
|
|
|
|
|
MessageCode.FILE_NOT_FOUND.getCode(), MessageCode.FILE_NOT_FOUND.getDescription(),
|
|
|
|
|
Map.of("fileId",fileId)));
|
2025-12-22 14:07:44 +07:00
|
|
|
});
|
2026-01-09 23:41:19 +07:00
|
|
|
byte[] fileBytes;
|
2025-12-22 14:07:44 +07:00
|
|
|
|
|
|
|
|
if (!isImageFile(fileEntity)) {
|
|
|
|
|
log.error("File not image: {}", fileEntity.getMimeType());
|
2025-12-22 14:42:27 +07:00
|
|
|
throw new NotValidFieldException("File not image", new BaseResponse(20007,
|
|
|
|
|
MessageCode.INVALID_FIELD.getCode(), MessageCode.INVALID_FIELD.getDescription(),
|
|
|
|
|
Map.of("file_type", fileEntity.getMimeType())));
|
2025-12-22 14:07:44 +07:00
|
|
|
}
|
|
|
|
|
|
2026-01-09 23:41:19 +07:00
|
|
|
try {
|
|
|
|
|
fileBytes = readFileFromDisk(fileEntity);
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
throw new NotValidFieldException("File not found or cannot read file", new BaseResponse(20007,
|
|
|
|
|
MessageCode.FILE_NOT_FOUND.getCode(), MessageCode.FILE_NOT_FOUND.getDescription(),
|
|
|
|
|
Map.of("fileId", fileId,
|
|
|
|
|
"filePath", fileEntity.getFilePath())));
|
|
|
|
|
}
|
2025-12-22 14:07:44 +07:00
|
|
|
|
|
|
|
|
return callYandexApi(fileBytes);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private boolean isImageFile(FileEntity fileEntity) {
|
|
|
|
|
String mimeType = fileEntity.getMimeType();
|
|
|
|
|
return mimeType != null && mimeType.startsWith("image");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private byte[] readFileFromDisk(FileEntity fileEntity) throws IOException {
|
|
|
|
|
Path filePath = Path.of(fileEntity.getFilePath());
|
|
|
|
|
|
|
|
|
|
if (!Files.exists(filePath)) {
|
|
|
|
|
throw new IOException("File not found: " + fileEntity.getFilePath());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!Files.isReadable(filePath)) {
|
|
|
|
|
throw new IOException("Cannot read file: " + fileEntity.getFilePath());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Files.readAllBytes(filePath);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private YandexSearchResponse callYandexApi(byte[] imageBytes) throws IOException {
|
|
|
|
|
String imageBase64 = Base64.getEncoder().encodeToString(imageBytes);
|
|
|
|
|
String jsonRequest = String.format("{\"folderId\":\"%s\",\"data\":\"%s\",\"page\":0,\"showSimilarImages\":true}",
|
|
|
|
|
folderId, imageBase64);
|
|
|
|
|
|
|
|
|
|
URL url = new URL(searchUrl);
|
|
|
|
|
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
connection.setRequestMethod("POST");
|
|
|
|
|
connection.setRequestProperty("Authorization", "Api-Key " + apiKey);
|
|
|
|
|
connection.setRequestProperty("Content-Type", "application/json");
|
|
|
|
|
connection.setRequestProperty("Accept", "application/json");
|
|
|
|
|
connection.setConnectTimeout(30000);
|
|
|
|
|
connection.setReadTimeout(30000);
|
|
|
|
|
connection.setDoOutput(true);
|
|
|
|
|
|
|
|
|
|
try (OutputStream os = connection.getOutputStream()) {
|
|
|
|
|
byte[] input = jsonRequest.getBytes("utf-8");
|
|
|
|
|
os.write(input, 0, input.length);
|
|
|
|
|
os.flush();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int responseCode = connection.getResponseCode();
|
|
|
|
|
|
|
|
|
|
String responseBody;
|
|
|
|
|
if (responseCode == HttpURLConnection.HTTP_OK) {
|
|
|
|
|
try (BufferedReader br = new BufferedReader(
|
|
|
|
|
new InputStreamReader(connection.getInputStream(), "utf-8"))) {
|
|
|
|
|
responseBody = readAll(br);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
try (BufferedReader br = new BufferedReader(
|
|
|
|
|
new InputStreamReader(connection.getErrorStream(), "utf-8"))) {
|
|
|
|
|
responseBody = readAll(br);
|
|
|
|
|
}
|
2025-12-22 14:42:27 +07:00
|
|
|
throw new IOException("Error Yandex API: " + responseCode + " - " + responseBody);
|
2025-12-22 14:07:44 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return parseJsonResponse(responseBody);
|
|
|
|
|
|
|
|
|
|
} finally {
|
|
|
|
|
connection.disconnect();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private String readAll(BufferedReader reader) throws IOException {
|
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
|
String line;
|
|
|
|
|
while ((line = reader.readLine()) != null) {
|
|
|
|
|
sb.append(line);
|
|
|
|
|
}
|
|
|
|
|
return sb.toString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private YandexSearchResponse parseJsonResponse(String json) {
|
|
|
|
|
YandexSearchResponse response = null;
|
|
|
|
|
try {
|
|
|
|
|
response = objectMapper.readValue(json, YandexSearchResponse.class);
|
|
|
|
|
} catch (JsonProcessingException e) {
|
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return response;
|
|
|
|
|
}
|
|
|
|
|
}
|