@@ -14,12 +14,19 @@ import ru.soune.nocopy.entity.file.FileEntity;
|
||||
import ru.soune.nocopy.entity.tokenoperation.OperationType;
|
||||
import ru.soune.nocopy.exception.NotValidFieldException;
|
||||
import ru.soune.nocopy.repository.FileEntityRepository;
|
||||
import ru.soune.nocopy.repository.SimilarImageProjection;
|
||||
import ru.soune.nocopy.service.FileSimilarityService;
|
||||
import ru.soune.nocopy.service.ImageHashService;
|
||||
import ru.soune.nocopy.service.file.CheckCounterService;
|
||||
import ru.soune.nocopy.service.file.FileUploadService;
|
||||
import ru.soune.nocopy.service.search.SearchImageService;
|
||||
import ru.soune.nocopy.service.tariff.TariffConstants;
|
||||
import ru.soune.nocopy.service.tariff.TariffInfoService;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.TimeoutException;
|
||||
|
||||
@@ -40,6 +47,12 @@ public class ImageFoundRequestHandler implements RequestHandler {
|
||||
|
||||
private final SearchProperties searchProperties;
|
||||
|
||||
private final FileUploadService fileUploadService;
|
||||
|
||||
private final ImageHashService imageHashService;
|
||||
|
||||
private final FileSimilarityService fileSimilarityService;
|
||||
|
||||
@Override
|
||||
public BaseResponse handle(BaseRequest request) throws Exception {
|
||||
ImageSearchRequest imageSearchRequest = objectMapper.convertValue(request.getMessageBody(),
|
||||
@@ -125,6 +138,35 @@ public class ImageFoundRequestHandler implements RequestHandler {
|
||||
int page = imageSearchRequest.getPage() != null ? imageSearchRequest.getPage() : 1;
|
||||
int pageSize = 5;
|
||||
|
||||
if (imageSearchRequest.getCheckSimilarFirst() && !allUniqueImages.isEmpty()) {
|
||||
YandexSearchResponse.ImageResult image = allUniqueImages.getFirst();
|
||||
String url = image.getUrl();
|
||||
Path tempFile = null;
|
||||
|
||||
try {
|
||||
tempFile = fileUploadService.downloadImageFromUrl(url);
|
||||
String filePath = tempFile.toAbsolutePath().toString();
|
||||
|
||||
Map<String, Long> hash = imageHashService.calculateHash(Paths.get(filePath));
|
||||
|
||||
List<SimilarImageProjection> duplicates = fileSimilarityService.findDuplicatedByHash(hash.get("hi"),
|
||||
hash.get("low"));
|
||||
|
||||
if (duplicates.isEmpty()) {
|
||||
return new BaseResponse(request.getMsgId(), MessageCode.SUCCESS.getCode(),
|
||||
"Not found similar pictures", new YandexSearchResponse());
|
||||
}
|
||||
} finally {
|
||||
if (tempFile != null) {
|
||||
try {
|
||||
Files.deleteIfExists(tempFile);
|
||||
} catch (IOException e) {
|
||||
log.warn("Not delete temp file: {}", tempFile, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
List<YandexSearchResponse.ImageResult> pagedResults = searchImageService.paginateResults(allUniqueImages, page,
|
||||
pageSize * 2);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user