2026-01-13 22:40:52 +07:00
|
|
|
package ru.soune.nocopy.service;
|
|
|
|
|
|
2026-01-27 20:27:39 +07:00
|
|
|
import com.vrt.fileprotection.image.phash.PHash;
|
|
|
|
|
import com.vrt.fileprotection.image.phash.PerceptualHashHelper;
|
2026-01-13 22:40:52 +07:00
|
|
|
import lombok.RequiredArgsConstructor;
|
2026-01-25 20:45:29 +07:00
|
|
|
import lombok.extern.slf4j.Slf4j;
|
2026-01-27 20:27:39 +07:00
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
2026-01-16 14:30:45 +07:00
|
|
|
import org.springframework.data.domain.Page;
|
|
|
|
|
import org.springframework.data.domain.PageImpl;
|
|
|
|
|
import org.springframework.data.domain.Pageable;
|
2026-01-13 22:40:52 +07:00
|
|
|
import org.springframework.stereotype.Service;
|
2026-01-22 19:55:09 +07:00
|
|
|
import ru.soune.nocopy.dto.file.SimilarFileDTO;
|
2026-01-16 14:30:45 +07:00
|
|
|
import ru.soune.nocopy.dto.file.SimilarityFilter;
|
2026-01-25 22:05:19 +07:00
|
|
|
import ru.soune.nocopy.entity.file.ImageHashEntity;
|
2026-01-26 21:03:45 +07:00
|
|
|
import ru.soune.nocopy.repository.AuthTokenRepository;
|
2026-01-13 22:40:52 +07:00
|
|
|
import ru.soune.nocopy.repository.ImageHashRepository;
|
|
|
|
|
import ru.soune.nocopy.repository.ImageSimilarityRepository;
|
|
|
|
|
import ru.soune.nocopy.repository.SimilarImageProjection;
|
2026-01-22 19:55:09 +07:00
|
|
|
import ru.soune.nocopy.util.FileUtil;
|
2026-01-13 22:40:52 +07:00
|
|
|
|
2026-01-25 22:05:19 +07:00
|
|
|
import java.util.*;
|
2026-01-16 14:30:45 +07:00
|
|
|
import java.util.stream.Collectors;
|
2026-01-13 22:40:52 +07:00
|
|
|
|
|
|
|
|
@Service
|
|
|
|
|
@RequiredArgsConstructor
|
2026-01-25 20:45:29 +07:00
|
|
|
@Slf4j
|
2026-01-13 22:40:52 +07:00
|
|
|
public class FileSimilarityService {
|
2026-01-22 19:55:09 +07:00
|
|
|
|
2026-01-13 22:40:52 +07:00
|
|
|
private final ImageSimilarityRepository repository;
|
2026-01-16 14:30:45 +07:00
|
|
|
|
2026-01-13 22:40:52 +07:00
|
|
|
private final ImageHashRepository hashRepository;
|
|
|
|
|
|
2026-01-22 19:55:09 +07:00
|
|
|
private final FileUtil fileUtil;
|
|
|
|
|
|
2026-01-26 21:03:45 +07:00
|
|
|
private final AuthTokenRepository authTokenRepository;
|
|
|
|
|
|
2026-01-27 20:27:39 +07:00
|
|
|
@Value("${server.baseurl}")
|
|
|
|
|
private String baseUrl;
|
|
|
|
|
|
2026-01-22 19:55:09 +07:00
|
|
|
public List<SimilarFileDTO> findSimilarFiles(String fileId) {
|
2026-01-13 22:40:52 +07:00
|
|
|
var imageHashEntity = hashRepository.findById(fileId)
|
|
|
|
|
.orElseThrow(() -> new RuntimeException("Hash not found"));
|
|
|
|
|
|
2026-01-23 13:22:02 +07:00
|
|
|
Long hash64Hi = imageHashEntity.getHash64Hi();
|
|
|
|
|
Long hash64Lo = imageHashEntity.getHash64Lo();
|
2026-01-13 22:40:52 +07:00
|
|
|
|
2026-01-22 19:55:09 +07:00
|
|
|
List<SimilarImageProjection> candidates = repository.findCandidates(fileId);
|
2026-01-13 22:40:52 +07:00
|
|
|
|
|
|
|
|
return candidates.stream()
|
|
|
|
|
.map(c -> {
|
2026-01-23 13:22:02 +07:00
|
|
|
Long cHi = c.getHash64Hi();
|
|
|
|
|
Long cLo = c.getHash64Lo();
|
2026-01-13 22:40:52 +07:00
|
|
|
|
2026-01-22 19:55:09 +07:00
|
|
|
int hamming = fileUtil.hamming64(hash64Hi, hash64Lo, cHi, cLo);
|
2026-01-13 22:40:52 +07:00
|
|
|
|
|
|
|
|
String level;
|
|
|
|
|
if (hamming <= 5) {
|
|
|
|
|
level = "DUPLICATE";
|
|
|
|
|
} else if (hamming <= 12) {
|
|
|
|
|
level = "SIMILAR";
|
|
|
|
|
} else {
|
|
|
|
|
level = "DIFFERENT";
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-22 19:55:09 +07:00
|
|
|
return SimilarFileDTO.builder()
|
2026-01-24 10:59:02 +07:00
|
|
|
.fileId(c.getId())
|
2026-01-25 20:45:29 +07:00
|
|
|
.ownerId(c.getUserId())
|
2026-01-13 22:40:52 +07:00
|
|
|
.originalFileName(c.getOriginalFileName())
|
|
|
|
|
.fileSize(c.getFileSize())
|
|
|
|
|
.hammingDistance(hamming)
|
|
|
|
|
.similarityLevel(level)
|
|
|
|
|
.build();
|
|
|
|
|
})
|
|
|
|
|
.sorted((a, b) ->
|
|
|
|
|
Integer.compare(a.getHammingDistance(), b.getHammingDistance()))
|
|
|
|
|
.toList();
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-22 19:55:09 +07:00
|
|
|
public List<SimilarFileDTO> findDuplicateByHammingDistance(String fileId, int hammingDistance,
|
|
|
|
|
int duplicate, int similar) {
|
|
|
|
|
List<SimilarImageProjection> candidates = repository.findCandidates(fileId);
|
|
|
|
|
|
2026-01-25 22:05:19 +07:00
|
|
|
Optional<ImageHashEntity> hashOptional = hashRepository.findById(fileId);
|
|
|
|
|
|
|
|
|
|
if (hashOptional.isEmpty()) {
|
|
|
|
|
return Collections.emptyList();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var imageHashEntity = hashOptional.get();
|
2026-01-22 19:55:09 +07:00
|
|
|
|
2026-01-23 13:22:02 +07:00
|
|
|
Long hash64Hi = imageHashEntity.getHash64Hi();
|
|
|
|
|
Long hash64Lo = imageHashEntity.getHash64Lo();
|
2026-01-22 19:55:09 +07:00
|
|
|
|
|
|
|
|
return candidates.stream()
|
|
|
|
|
.map(c -> {
|
2026-01-23 13:22:02 +07:00
|
|
|
Long cHi = c.getHash64Hi();
|
|
|
|
|
Long cLo = c.getHash64Lo();
|
2026-01-22 19:55:09 +07:00
|
|
|
|
|
|
|
|
int hamming = fileUtil.hamming64(hash64Hi, hash64Lo, cHi, cLo);
|
|
|
|
|
|
|
|
|
|
return new AbstractMap.SimpleEntry<>(c, hamming);
|
|
|
|
|
})
|
|
|
|
|
.filter(entry -> entry.getValue() <= hammingDistance)
|
|
|
|
|
.sorted((a, b) -> Integer.compare(a.getValue(), b.getValue()))
|
|
|
|
|
.map(entry -> {
|
|
|
|
|
SimilarImageProjection similarImageProjection = entry.getKey();
|
|
|
|
|
int hamming = entry.getValue();
|
|
|
|
|
|
|
|
|
|
String level;
|
|
|
|
|
if (hamming <= duplicate) {
|
|
|
|
|
level = "DUPLICATE";
|
|
|
|
|
} else if (hamming <= similar) {
|
|
|
|
|
level = "SIMILAR";
|
|
|
|
|
} else {
|
|
|
|
|
level = "DIFFERENT";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return SimilarFileDTO.builder()
|
2026-01-25 18:59:55 +07:00
|
|
|
.fileId(similarImageProjection.getId())
|
2026-01-22 19:55:09 +07:00
|
|
|
.originalFileName(similarImageProjection.getOriginalFileName())
|
|
|
|
|
.fileSize(similarImageProjection.getFileSize())
|
|
|
|
|
.hammingDistance(hamming)
|
|
|
|
|
.similarityLevel(level)
|
|
|
|
|
.ownerId(similarImageProjection.getUserId())
|
|
|
|
|
.build();
|
|
|
|
|
})
|
|
|
|
|
.toList();
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-23 13:22:02 +07:00
|
|
|
public List<SimilarImageProjection> findDuplicatedByHash(Long hash64Hi, Long hash64Lo) {
|
2026-01-19 15:49:27 +07:00
|
|
|
List<SimilarImageProjection> duplicates = repository.findExactDuplicates(hash64Hi,hash64Lo);
|
|
|
|
|
|
|
|
|
|
if (duplicates.isEmpty()) {
|
|
|
|
|
return new ArrayList<>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return duplicates;
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-26 21:03:45 +07:00
|
|
|
public Page<SimilarFileDTO> findSimilarFiles(String fileId, SimilarityFilter filter, Pageable pageable, String authToken) {
|
2026-01-16 14:30:45 +07:00
|
|
|
var imageHashEntity = hashRepository.findById(fileId)
|
|
|
|
|
.orElseThrow(() -> new RuntimeException("Hash not found"));
|
|
|
|
|
|
2026-01-23 13:22:02 +07:00
|
|
|
Long hash64Hi = imageHashEntity.getHash64Hi();
|
|
|
|
|
Long hash64Lo = imageHashEntity.getHash64Lo();
|
2026-01-16 14:30:45 +07:00
|
|
|
|
2026-01-26 21:03:45 +07:00
|
|
|
List<SimilarImageProjection> candidates = authToken.equals("all") ? repository.findCandidates(fileId):
|
|
|
|
|
repository.findCandidatesFromUserFiles(fileId, authTokenRepository.findUserIdByToken(authToken));
|
2026-01-16 14:30:45 +07:00
|
|
|
|
|
|
|
|
List<String> similarityLevels = (filter != null && filter.getSimilarityLevels() != null)
|
|
|
|
|
? filter.getSimilarityLevels()
|
|
|
|
|
: List.of("DUPLICATE", "SIMILAR", "DIFFERENT");
|
|
|
|
|
|
2026-01-22 19:55:09 +07:00
|
|
|
List<SimilarFileDTO> allResults = candidates.stream()
|
2026-01-16 14:30:45 +07:00
|
|
|
.map(c -> createSimilarFileResponse(c, hash64Hi, hash64Lo))
|
|
|
|
|
.filter(response -> similarityLevels.contains(response.getSimilarityLevel()))
|
2026-01-22 19:55:09 +07:00
|
|
|
.sorted(Comparator.comparingInt(SimilarFileDTO::getHammingDistance))
|
2026-01-16 14:30:45 +07:00
|
|
|
.collect(Collectors.toList());
|
|
|
|
|
|
|
|
|
|
int total = allResults.size();
|
|
|
|
|
int page = (pageable != null) ? pageable.getPageNumber() : 0;
|
|
|
|
|
int size = (pageable != null) ? pageable.getPageSize() : 20;
|
|
|
|
|
|
|
|
|
|
int fromIndex = Math.min(page * size, total);
|
|
|
|
|
int toIndex = Math.min(fromIndex + size, total);
|
|
|
|
|
|
2026-01-22 19:55:09 +07:00
|
|
|
List<SimilarFileDTO> pageContent = allResults.subList(fromIndex, toIndex);
|
2026-01-16 14:30:45 +07:00
|
|
|
|
|
|
|
|
return new PageImpl<>(pageContent, pageable, total);
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-22 19:55:09 +07:00
|
|
|
private SimilarFileDTO createSimilarFileResponse(SimilarImageProjection similarImageProjection,
|
2026-01-23 13:22:02 +07:00
|
|
|
Long hash64Hi, Long hash64Lo) {
|
|
|
|
|
Long imageProjectionHash64Hi = similarImageProjection.getHash64Hi();
|
|
|
|
|
Long similarImageProjectionHash64Lo = similarImageProjection.getHash64Lo();
|
2026-01-27 20:27:39 +07:00
|
|
|
int hamming = PerceptualHashHelper.INSTANCE.hammingDistance(new PHash(hash64Hi, hash64Lo),
|
|
|
|
|
new PHash(imageProjectionHash64Hi, similarImageProjectionHash64Lo));
|
2026-01-16 14:30:45 +07:00
|
|
|
|
|
|
|
|
String level;
|
2026-01-27 20:27:39 +07:00
|
|
|
if (hamming <= 6) {
|
2026-01-16 14:30:45 +07:00
|
|
|
level = "DUPLICATE";
|
|
|
|
|
} else if (hamming <= 12) {
|
|
|
|
|
level = "SIMILAR";
|
|
|
|
|
} else {
|
|
|
|
|
level = "DIFFERENT";
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-22 19:55:09 +07:00
|
|
|
return SimilarFileDTO.builder()
|
2026-01-24 10:59:02 +07:00
|
|
|
.fileId(similarImageProjection.getId())
|
2026-01-25 20:45:29 +07:00
|
|
|
.ownerId(similarImageProjection.getUserId())
|
2026-01-16 14:30:45 +07:00
|
|
|
.originalFileName(similarImageProjection.getOriginalFileName())
|
|
|
|
|
.fileSize(similarImageProjection.getFileSize())
|
|
|
|
|
.hammingDistance(hamming)
|
|
|
|
|
.similarityLevel(level)
|
2026-01-27 20:27:39 +07:00
|
|
|
.supportId(similarImageProjection.getSupportId())
|
|
|
|
|
.uploadDate(similarImageProjection.getUploadDate())
|
|
|
|
|
.status(similarImageProjection.getProtectionStatus())
|
2026-02-05 10:47:40 +07:00
|
|
|
.url(baseUrl + "/api/files/protected/" + similarImageProjection.getId())
|
2026-01-16 14:30:45 +07:00
|
|
|
.build();
|
|
|
|
|
}
|
2026-01-13 22:40:52 +07:00
|
|
|
}
|