2025-12-22 14:07:44 +07:00
|
|
|
package ru.soune.nocopy.handler;
|
|
|
|
|
|
|
|
|
|
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-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-05 00:37:59 +07:00
|
|
|
import ru.soune.nocopy.service.search.SearchApiToYandexResponseMapper;
|
2026-01-20 03:47:51 +07:00
|
|
|
import ru.soune.nocopy.service.search.YandexSearchService;
|
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-01-28 23:25:16 +07:00
|
|
|
import java.util.Map;
|
|
|
|
|
|
2025-12-22 14:07:44 +07:00
|
|
|
@Slf4j
|
|
|
|
|
@Component
|
|
|
|
|
@RequiredArgsConstructor
|
|
|
|
|
public class ImageFoundRequestHandler implements RequestHandler {
|
|
|
|
|
|
|
|
|
|
private final ObjectMapper objectMapper;
|
|
|
|
|
|
|
|
|
|
private final YandexSearchService yandexSearchService;
|
|
|
|
|
|
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;
|
|
|
|
|
|
2026-02-05 00:37:59 +07:00
|
|
|
private final SearchApiToYandexResponseMapper mapper = new SearchApiToYandexResponseMapper();
|
|
|
|
|
|
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-05 00:37:59 +07:00
|
|
|
// YandexSearchResponse response = yandexSearchService.searchByFileEntity(fileEntity);
|
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-05 20:21:27 +07:00
|
|
|
String yandexReverseSearchResponse = yandexSearchService.searchReverseByPublicUrl(fileEntity);
|
2026-02-10 11:58:32 +07:00
|
|
|
int page = imageSearchRequest.getPage() != null ? imageSearchRequest.getPage() : 1;
|
|
|
|
|
int pageSize = imageSearchRequest.getPageSize() != null ? imageSearchRequest.getPageSize() : 10;
|
2026-02-05 00:37:59 +07:00
|
|
|
|
2026-02-05 17:25:50 +07:00
|
|
|
tariffInfoService.writeOffTokens(fileEntity.getUserId(), TariffConstants.TOKEN_VALUE_FOR_SEARCH);
|
|
|
|
|
|
2026-02-05 18:55:54 +07:00
|
|
|
checkCounterService.incrementCheckCount(fileEntity.getUserId(), fileEntity.getMimeType());
|
|
|
|
|
|
2025-12-22 14:07:44 +07:00
|
|
|
return new BaseResponse(request.getMsgId(), MessageCode.SUCCESS.getCode(),
|
2026-02-10 12:16:31 +07:00
|
|
|
MessageCode.SUCCESS.getDescription(), mapper.mapToYandexResponse(yandexReverseSearchResponse, page,
|
2026-02-10 11:58:32 +07:00
|
|
|
pageSize));
|
2025-12-22 14:07:44 +07:00
|
|
|
}
|
|
|
|
|
}
|