@@ -54,14 +54,8 @@ public class ImageFoundRequestHandler implements RequestHandler {
|
||||
Map.of("fileId",fileId)));
|
||||
});
|
||||
|
||||
// YandexSearchResponse response = yandexSearchService.searchByFileEntity(fileId);
|
||||
|
||||
//TODO uncommited when add billing
|
||||
// GoogleVisionSearchResponse googleVisionSearchResponse = googleVisionSearchService.searchByFileEntity(fileId);
|
||||
//TODO отдавать любой полученный результат,даже если по таймауту падают
|
||||
|
||||
String searchResponseYandex = null;
|
||||
String searchResponseGoogle = null;
|
||||
String searchResponseYandex;
|
||||
String searchResponseGoogle;
|
||||
List<YandexSearchResponse.ImageResult> allYandexImages = new ArrayList<>();
|
||||
List<YandexSearchResponse.ImageResult> allGoogleImages = new ArrayList<>();
|
||||
List<YandexSearchResponse.ImageResult> allUniqueImages;
|
||||
@@ -119,79 +113,4 @@ public class ImageFoundRequestHandler implements RequestHandler {
|
||||
return new BaseResponse(request.getMsgId(), MessageCode.SUCCESS.getCode(),
|
||||
message, finalResponse);
|
||||
}
|
||||
|
||||
// @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);
|
||||
// }
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ import org.springframework.stereotype.Repository;
|
||||
import ru.soune.nocopy.entity.file.FileEntity;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
@Repository
|
||||
public interface ImageSimilarityRepository
|
||||
@@ -60,6 +61,31 @@ public interface ImageSimilarityRepository
|
||||
@Param("userId") Long userId
|
||||
);
|
||||
|
||||
@Query(value = """
|
||||
SELECT
|
||||
f.id AS id,
|
||||
f.original_file_name AS originalFileName,
|
||||
f.file_size AS fileSize,
|
||||
f.user_id AS userId,
|
||||
f.support_id AS supportId,
|
||||
f.created_at AS uploadDate,
|
||||
f.protection_status AS protectionStatus,
|
||||
f.file_extension AS extension,
|
||||
h.hash64_hi AS hash64Hi,
|
||||
h.hash64_lo AS hash64Lo
|
||||
FROM image_hashes ref
|
||||
JOIN image_hashes h
|
||||
ON ref.file_id <> h.file_id
|
||||
JOIN file_entities f
|
||||
ON f.id = h.file_id
|
||||
WHERE ref.file_id = :fileId AND f.user_id IN :userIds
|
||||
""",
|
||||
nativeQuery = true)
|
||||
List<SimilarImageProjection> findCandidatesFromUserFiles(
|
||||
@Param("fileId") String fileId,
|
||||
@Param("userId") Set<Long> userIds
|
||||
);
|
||||
|
||||
|
||||
@Query(value = """
|
||||
SELECT
|
||||
|
||||
@@ -1,32 +1,25 @@
|
||||
package ru.soune.nocopy.service;
|
||||
|
||||
import com.vrt.NoCopyFileService;
|
||||
import com.vrt.fileprotection.FileProtector;
|
||||
import com.vrt.fileprotection.NoCopyCheckResult;
|
||||
import com.vrt.fileprotection.audio.AudioCheckResult;
|
||||
import com.vrt.fileprotection.documents.DocumentCheckResult;
|
||||
import com.vrt.fileprotection.image.phash.PHash;
|
||||
import com.vrt.fileprotection.image.phash.PerceptualHashHelper;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.hibernate.validator.internal.util.stereotypes.Lazy;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.PageImpl;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.stereotype.Service;
|
||||
import ru.soune.nocopy.dto.file.SimilarFileDTO;
|
||||
import ru.soune.nocopy.dto.file.SimilarityFilter;
|
||||
import ru.soune.nocopy.entity.company.Company;
|
||||
import ru.soune.nocopy.entity.file.FileEntity;
|
||||
import ru.soune.nocopy.entity.file.ImageHashEntity;
|
||||
import ru.soune.nocopy.entity.user.User;
|
||||
import ru.soune.nocopy.exception.DuplicateImageException;
|
||||
import ru.soune.nocopy.repository.*;
|
||||
import ru.soune.nocopy.util.FileUtil;
|
||||
|
||||
import java.io.FileInputStream;
|
||||
import java.io.InputStream;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.security.MessageDigest;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
@@ -122,6 +115,8 @@ public class FileSimilarityService {
|
||||
}
|
||||
}
|
||||
|
||||
private final UserRepository userRepository;
|
||||
|
||||
public Page<SimilarFileDTO> findSimilarFiles(
|
||||
String fileId,
|
||||
List<String> similarityLevels,
|
||||
@@ -137,7 +132,14 @@ public class FileSimilarityService {
|
||||
candidates = repository.findCandidates(fileId);
|
||||
} else {
|
||||
Long userId = authTokenRepository.findUserIdByToken(authToken);
|
||||
candidates = repository.findCandidatesFromUserFiles(fileId, userId);
|
||||
User user = userRepository.findById(userId).orElseThrow(() -> new RuntimeException("User not found"));
|
||||
Company company = user.getCompany();
|
||||
if (company != null) {
|
||||
Set<Long> userIds = company.getUsers().stream().map(User::getId).collect(Collectors.toSet());
|
||||
candidates = repository.findCandidatesFromUserFiles(fileId, userIds);
|
||||
} else {
|
||||
candidates = repository.findCandidatesFromUserFiles(fileId, userId);
|
||||
}
|
||||
}
|
||||
|
||||
List<String> levels = (similarityLevels != null && !similarityLevels.isEmpty())
|
||||
|
||||
Reference in New Issue
Block a user