@@ -4,6 +4,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Component;
|
||||
import ru.soune.nocopy.configuration.search.SearchProperties;
|
||||
import ru.soune.nocopy.dto.BaseRequest;
|
||||
import ru.soune.nocopy.dto.BaseResponse;
|
||||
import ru.soune.nocopy.dto.MessageCode;
|
||||
@@ -36,6 +37,8 @@ public class ImageFoundRequestHandler implements RequestHandler {
|
||||
|
||||
private final TariffInfoService tariffInfoService;
|
||||
|
||||
private final SearchProperties searchProperties;
|
||||
|
||||
@Override
|
||||
public BaseResponse handle(BaseRequest request) throws Exception {
|
||||
ImageSearchRequest imageSearchRequest = objectMapper.convertValue(request.getMessageBody(),
|
||||
@@ -47,47 +50,72 @@ public class ImageFoundRequestHandler implements RequestHandler {
|
||||
.orElseThrow(() -> {
|
||||
throw new NotValidFieldException("File not found", new BaseResponse(20007,
|
||||
MessageCode.FILE_NOT_FOUND.getCode(), MessageCode.FILE_NOT_FOUND.getDescription(),
|
||||
Map.of("fileId",fileId)));
|
||||
});
|
||||
Map.of("fileId", fileId)));});
|
||||
|
||||
String searchResponseYandex;
|
||||
// String searchResponseGoogle;
|
||||
List<YandexSearchResponse.ImageResult> allYandexImages = new ArrayList<>();
|
||||
// List<YandexSearchResponse.ImageResult> allGoogleImages = new ArrayList<>();
|
||||
List<YandexSearchResponse.ImageResult> allUniqueImages;
|
||||
List<YandexSearchResponse.ImageResult> allYandexImages = new ArrayList<>();
|
||||
List<YandexSearchResponse.ImageResult> allGoogleImages = new ArrayList<>();
|
||||
boolean hasTimeout = false;
|
||||
//
|
||||
// try {
|
||||
// searchResponseGoogle = searchImageService.searchReverseByPublicUrl(fileEntity, "google_lens",
|
||||
// "exact_matches");
|
||||
// log.info("searchResponseGoogle OK,{}", searchResponseGoogle);
|
||||
// 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);
|
||||
// }
|
||||
|
||||
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);
|
||||
}
|
||||
boolean useGoogle = searchProperties.getEngines().getOrDefault("google",
|
||||
new SearchProperties.EngineConfig()).isEnabled();
|
||||
boolean useYandex = searchProperties.getEngines().getOrDefault("yandex",
|
||||
new SearchProperties.EngineConfig()).isEnabled();
|
||||
|
||||
// allUniqueImages = searchImageService.removeDuplicateUrls(allYandexImages, allGoogleImages);
|
||||
// allUniqueImages = allGoogleImages;
|
||||
allUniqueImages = allYandexImages;
|
||||
log.info("allUniqueImages OK: {} images", allUniqueImages.size());
|
||||
log.info("Search settings: useGoogle={}, useYandex={}", useGoogle, useYandex);
|
||||
|
||||
if (useGoogle) {
|
||||
try {
|
||||
String 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);
|
||||
}
|
||||
} else {
|
||||
log.info("Google search is disabled by settings");
|
||||
}
|
||||
|
||||
if (useYandex) {
|
||||
try {
|
||||
String 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);
|
||||
}
|
||||
} else {
|
||||
log.info("Yandex search is disabled by settings");
|
||||
}
|
||||
|
||||
if (allYandexImages.isEmpty() && allGoogleImages.isEmpty()) {
|
||||
log.warn("No results from any search engine");
|
||||
String errorMessage = useGoogle || useYandex ? "No results found" : "All search engines are disabled";
|
||||
|
||||
return new BaseResponse(request.getMsgId(), MessageCode.SUCCESS.getCode(),
|
||||
errorMessage, new YandexSearchResponse());
|
||||
}
|
||||
|
||||
List<YandexSearchResponse.ImageResult> allUniqueImages;
|
||||
if (useGoogle && useYandex) {
|
||||
allUniqueImages = searchImageService.removeDuplicateUrls(allYandexImages, allGoogleImages);
|
||||
log.info("Merged results from both engines: {} unique images", allUniqueImages.size());
|
||||
} else if (useGoogle) {
|
||||
allUniqueImages = allGoogleImages;
|
||||
log.info("Results only from Google: {} images", allUniqueImages.size());
|
||||
} else {
|
||||
allUniqueImages = allYandexImages;
|
||||
log.info("Results only from Yandex: {} images", allUniqueImages.size());
|
||||
}
|
||||
|
||||
tariffInfoService.writeOffTokens(fileEntity.getUserId(), TariffConstants.TOKEN_VALUE_FOR_SEARCH);
|
||||
checkCounterService.incrementCheckCount(fileEntity.getUserId(), fileEntity.getMimeType());
|
||||
|
||||
Reference in New Issue
Block a user