2025-12-22 14:07:44 +07:00
|
|
|
package ru.soune.nocopy.handler;
|
|
|
|
|
|
2026-02-12 11:58:05 +07:00
|
|
|
import com.fasterxml.jackson.databind.JsonNode;
|
2025-12-22 14:07:44 +07:00
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
import ru.soune.nocopy.dto.BaseRequest;
|
|
|
|
|
import ru.soune.nocopy.dto.BaseResponse;
|
|
|
|
|
import ru.soune.nocopy.dto.MessageCode;
|
|
|
|
|
import ru.soune.nocopy.dto.file.ImageSearchRequest;
|
2026-02-11 17:41:48 +07:00
|
|
|
import ru.soune.nocopy.dto.file.YandexSearchResponse;
|
2026-01-28 23:25:16 +07:00
|
|
|
import ru.soune.nocopy.entity.file.FileEntity;
|
|
|
|
|
import ru.soune.nocopy.exception.NotValidFieldException;
|
|
|
|
|
import ru.soune.nocopy.repository.FileEntityRepository;
|
|
|
|
|
import ru.soune.nocopy.service.file.CheckCounterService;
|
2026-02-11 17:41:48 +07:00
|
|
|
import ru.soune.nocopy.service.search.SearchImageService;
|
2026-01-27 08:09:54 +07:00
|
|
|
import ru.soune.nocopy.service.search.GoogleVisionSearchService;
|
2026-02-05 17:25:50 +07:00
|
|
|
import ru.soune.nocopy.service.tariff.TariffConstants;
|
|
|
|
|
import ru.soune.nocopy.service.tariff.TariffInfoService;
|
2025-12-22 14:07:44 +07:00
|
|
|
|
2026-02-10 13:26:44 +07:00
|
|
|
import java.io.IOException;
|
2026-02-12 11:58:05 +07:00
|
|
|
import java.util.*;
|
2026-02-10 13:26:44 +07:00
|
|
|
import java.util.concurrent.TimeoutException;
|
2026-01-28 23:25:16 +07:00
|
|
|
|
2025-12-22 14:07:44 +07:00
|
|
|
@Slf4j
|
|
|
|
|
@Component
|
|
|
|
|
@RequiredArgsConstructor
|
|
|
|
|
public class ImageFoundRequestHandler implements RequestHandler {
|
|
|
|
|
|
|
|
|
|
private final ObjectMapper objectMapper;
|
|
|
|
|
|
2026-02-11 17:41:48 +07:00
|
|
|
private final SearchImageService searchImageService;
|
2025-12-22 14:07:44 +07:00
|
|
|
|
2026-01-27 05:41:47 +07:00
|
|
|
private final GoogleVisionSearchService googleVisionSearchService;
|
|
|
|
|
|
2026-01-28 23:25:16 +07:00
|
|
|
private final CheckCounterService checkCounterService;
|
|
|
|
|
|
|
|
|
|
private final FileEntityRepository fileEntityRepository;
|
|
|
|
|
|
2026-02-05 17:25:50 +07:00
|
|
|
private final TariffInfoService tariffInfoService;
|
|
|
|
|
|
2025-12-22 14:07:44 +07:00
|
|
|
@Override
|
|
|
|
|
public BaseResponse handle(BaseRequest request) throws Exception {
|
2025-12-22 14:42:27 +07:00
|
|
|
ImageSearchRequest imageSearchRequest = objectMapper.convertValue(request.getMessageBody(),
|
|
|
|
|
ImageSearchRequest.class);
|
2025-12-22 14:07:44 +07:00
|
|
|
|
|
|
|
|
String fileId = imageSearchRequest.getFileId();
|
|
|
|
|
|
2026-01-28 23:25:16 +07:00
|
|
|
FileEntity fileEntity = fileEntityRepository.findById(fileId)
|
|
|
|
|
.orElseThrow(() -> {
|
|
|
|
|
throw new NotValidFieldException("File not found", new BaseResponse(20007,
|
|
|
|
|
MessageCode.FILE_NOT_FOUND.getCode(), MessageCode.FILE_NOT_FOUND.getDescription(),
|
|
|
|
|
Map.of("fileId",fileId)));
|
|
|
|
|
});
|
|
|
|
|
|
2026-02-19 16:01:10 +07:00
|
|
|
// YandexSearchResponse response = yandexSearchService.searchByFileEntity(fileId);
|
2025-12-22 14:07:44 +07:00
|
|
|
|
2026-01-27 05:41:47 +07:00
|
|
|
//TODO uncommited when add billing
|
|
|
|
|
// GoogleVisionSearchResponse googleVisionSearchResponse = googleVisionSearchService.searchByFileEntity(fileId);
|
2026-02-19 14:15:59 +07:00
|
|
|
//TODO отдавать любой полученный результат,даже если по таймауту падают
|
2026-02-19 16:01:10 +07:00
|
|
|
|
|
|
|
|
String searchResponseYandex = null;
|
2026-02-22 00:42:13 +07:00
|
|
|
String searchResponseGoogle = null;
|
2026-02-19 16:01:10 +07:00
|
|
|
List<YandexSearchResponse.ImageResult> allYandexImages = new ArrayList<>();
|
|
|
|
|
List<YandexSearchResponse.ImageResult> allGoogleImages = new ArrayList<>();
|
|
|
|
|
List<YandexSearchResponse.ImageResult> allUniqueImages;
|
|
|
|
|
boolean hasTimeout = false;
|
|
|
|
|
|
2026-02-22 00:42:13 +07:00
|
|
|
try {
|
|
|
|
|
searchResponseGoogle = searchImageService.searchReverseByPublicUrl(fileEntity, "google_lens",
|
|
|
|
|
"exact_matches");
|
|
|
|
|
log.info("searchResponseGoogle OK");
|
|
|
|
|
allGoogleImages = searchImageService.getAllImagesWithoutPagination(
|
|
|
|
|
searchResponseGoogle, "exact_matches");
|
|
|
|
|
} catch (TimeoutException e) {
|
|
|
|
|
log.warn("Google search timeout for file {}", fileId);
|
|
|
|
|
hasTimeout = true;
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
log.error("Google search failed for file {}", fileId, e);
|
|
|
|
|
}
|
2026-02-05 17:25:50 +07:00
|
|
|
|
2026-02-22 02:49:21 +07:00
|
|
|
try {
|
|
|
|
|
searchResponseYandex = searchImageService.searchReverseByPublicUrl(fileEntity, "yandex_reverse_image",
|
|
|
|
|
"visual_matches");
|
|
|
|
|
log.info("searchResponseYandex OK");
|
|
|
|
|
allYandexImages = searchImageService.getAllImagesWithoutPagination(
|
|
|
|
|
searchResponseYandex, "visual_matches");
|
|
|
|
|
} catch (TimeoutException e) {
|
|
|
|
|
log.warn("Yandex search timeout for file {}", fileId);
|
|
|
|
|
hasTimeout = true;
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
log.error("Yandex search failed for file {}", fileId, e);
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-19 16:01:10 +07:00
|
|
|
allUniqueImages = searchImageService.removeDuplicateUrls(allYandexImages, allGoogleImages);
|
|
|
|
|
log.info("allUniqueImages OK: {} images", allUniqueImages.size());
|
|
|
|
|
|
|
|
|
|
if (!allUniqueImages.isEmpty()) {
|
|
|
|
|
tariffInfoService.writeOffTokens(fileEntity.getUserId(), TariffConstants.TOKEN_VALUE_FOR_SEARCH);
|
|
|
|
|
checkCounterService.incrementCheckCount(fileEntity.getUserId(), fileEntity.getMimeType());
|
|
|
|
|
}
|
2026-02-05 18:55:54 +07:00
|
|
|
|
2026-02-12 11:58:05 +07:00
|
|
|
int page = imageSearchRequest.getPage() != null ? imageSearchRequest.getPage() : 1;
|
|
|
|
|
int pageSize = 5;
|
|
|
|
|
|
2026-02-12 12:07:41 +07:00
|
|
|
List<YandexSearchResponse.ImageResult> pagedResults = searchImageService.paginateResults(allUniqueImages, page,
|
2026-02-12 11:58:05 +07:00
|
|
|
pageSize * 2);
|
|
|
|
|
|
|
|
|
|
YandexSearchResponse finalResponse = new YandexSearchResponse();
|
|
|
|
|
finalResponse.setImages(pagedResults);
|
|
|
|
|
finalResponse.setPage(page);
|
|
|
|
|
finalResponse.setPageSize(pageSize * 2);
|
|
|
|
|
finalResponse.setTotalResults(allUniqueImages.size());
|
|
|
|
|
finalResponse.setTotalPages((int) Math.ceil((double) allUniqueImages.size() / (pageSize * 2)));
|
2026-02-11 18:47:58 +07:00
|
|
|
|
2026-02-19 16:01:10 +07:00
|
|
|
String message = hasTimeout ? "Search completed (partial)" : MessageCode.SUCCESS.getDescription();
|
|
|
|
|
|
2025-12-22 14:07:44 +07:00
|
|
|
return new BaseResponse(request.getMsgId(), MessageCode.SUCCESS.getCode(),
|
2026-02-19 16:01:10 +07:00
|
|
|
message, finalResponse);
|
2025-12-22 14:07:44 +07:00
|
|
|
}
|
2026-02-19 16:01:10 +07:00
|
|
|
|
|
|
|
|
// @Override
|
|
|
|
|
// public BaseResponse handle(BaseRequest request) throws Exception {
|
|
|
|
|
// ImageSearchRequest imageSearchRequest = objectMapper.convertValue(request.getMessageBody(),
|
|
|
|
|
// ImageSearchRequest.class);
|
|
|
|
|
//
|
|
|
|
|
// String fileId = imageSearchRequest.getFileId();
|
|
|
|
|
//
|
|
|
|
|
// FileEntity fileEntity = fileEntityRepository.findById(fileId)
|
|
|
|
|
// .orElseThrow(() -> {
|
|
|
|
|
// throw new NotValidFieldException("File not found", new BaseResponse(20007,
|
|
|
|
|
// MessageCode.FILE_NOT_FOUND.getCode(), MessageCode.FILE_NOT_FOUND.getDescription(),
|
|
|
|
|
// Map.of("fileId",fileId)));
|
|
|
|
|
// });
|
|
|
|
|
//
|
|
|
|
|
//// YandexSearchResponse response = yandexSearchService.searchByFileEntity(fileEntity);
|
|
|
|
|
//
|
|
|
|
|
// //TODO uncommited when add billing
|
|
|
|
|
//// GoogleVisionSearchResponse googleVisionSearchResponse = googleVisionSearchService.searchByFileEntity(fileId);
|
|
|
|
|
// //TODO отдавать любой полученный результат,даже если по таймауту падают
|
|
|
|
|
//
|
|
|
|
|
// String searchResponseYandex;
|
|
|
|
|
// String searchResponseGoogle;
|
|
|
|
|
// List<YandexSearchResponse.ImageResult> allUniqueImages;
|
|
|
|
|
// try {
|
|
|
|
|
// searchResponseYandex = searchImageService.searchReverseByPublicUrl(fileEntity, "yandex_reverse_image",
|
|
|
|
|
// "visual_matches");
|
|
|
|
|
// log.info("searchResponseYandex OK");
|
|
|
|
|
// List<YandexSearchResponse.ImageResult> allYandexImages = searchImageService.getAllImagesWithoutPagination(
|
|
|
|
|
// searchResponseYandex, "visual_matches");
|
|
|
|
|
// searchResponseGoogle = searchImageService.searchReverseByPublicUrl(fileEntity, "google_lens",
|
|
|
|
|
// "exact_matches");
|
|
|
|
|
//
|
|
|
|
|
// log.info("searchResponseGoogle OK");
|
|
|
|
|
// List<YandexSearchResponse.ImageResult> allGoogleImages = searchImageService.getAllImagesWithoutPagination(
|
|
|
|
|
// searchResponseGoogle, "exact_matches");
|
|
|
|
|
// allUniqueImages = searchImageService.removeDuplicateUrls(allYandexImages,
|
|
|
|
|
// allGoogleImages);
|
|
|
|
|
// log.info("allUniqueImages OK");
|
|
|
|
|
//
|
|
|
|
|
// } catch (TimeoutException e) {
|
|
|
|
|
// log.warn("Search timeout for file {}, returning empty results", fileId);
|
|
|
|
|
// return new BaseResponse(request.getMsgId(), MessageCode.SUCCESS.getCode(),
|
|
|
|
|
// "Search completed (partial)", Map.of(
|
|
|
|
|
// "results", List.of(),
|
|
|
|
|
// "total", 0,
|
|
|
|
|
// "searchStatus", "timeout"
|
|
|
|
|
// ));
|
|
|
|
|
// } catch (IOException e) {
|
|
|
|
|
// log.error("Search failed for file {}", fileId, e);
|
|
|
|
|
// return new BaseResponse(request.getMsgId(), 20007,
|
|
|
|
|
// "Search service temporarily unavailable",
|
|
|
|
|
// Map.of("fileId", fileId));
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// tariffInfoService.writeOffTokens(fileEntity.getUserId(), TariffConstants.TOKEN_VALUE_FOR_SEARCH);
|
|
|
|
|
//
|
|
|
|
|
// checkCounterService.incrementCheckCount(fileEntity.getUserId(), fileEntity.getMimeType());
|
|
|
|
|
//
|
|
|
|
|
// int page = imageSearchRequest.getPage() != null ? imageSearchRequest.getPage() : 1;
|
|
|
|
|
// int pageSize = 5;
|
|
|
|
|
//
|
|
|
|
|
// List<YandexSearchResponse.ImageResult> pagedResults = searchImageService.paginateResults(allUniqueImages, page,
|
|
|
|
|
// pageSize * 2);
|
|
|
|
|
//
|
|
|
|
|
// YandexSearchResponse finalResponse = new YandexSearchResponse();
|
|
|
|
|
// finalResponse.setImages(pagedResults);
|
|
|
|
|
// finalResponse.setPage(page);
|
|
|
|
|
// finalResponse.setPageSize(pageSize * 2);
|
|
|
|
|
// finalResponse.setTotalResults(allUniqueImages.size());
|
|
|
|
|
// finalResponse.setTotalPages((int) Math.ceil((double) allUniqueImages.size() / (pageSize * 2)));
|
|
|
|
|
//
|
|
|
|
|
// return new BaseResponse(request.getMsgId(), MessageCode.SUCCESS.getCode(),
|
|
|
|
|
// MessageCode.SUCCESS.getDescription(), finalResponse);
|
|
|
|
|
// }
|
2025-12-22 14:07:44 +07:00
|
|
|
}
|