@@ -1,7 +1,10 @@
|
||||
package ru.soune.nocopy.service;
|
||||
|
||||
import com.vrt.fileprotection.image.phash.PHash;
|
||||
import com.vrt.fileprotection.image.phash.PerceptualHashHelper;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
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;
|
||||
@@ -31,6 +34,9 @@ public class FileSimilarityService {
|
||||
|
||||
private final AuthTokenRepository authTokenRepository;
|
||||
|
||||
@Value("${server.baseurl}")
|
||||
private String baseUrl;
|
||||
|
||||
public List<SimilarFileDTO> findSimilarFiles(String fileId) {
|
||||
var imageHashEntity = hashRepository.findById(fileId)
|
||||
.orElseThrow(() -> new RuntimeException("Hash not found"));
|
||||
@@ -167,11 +173,11 @@ public class FileSimilarityService {
|
||||
Long hash64Hi, Long hash64Lo) {
|
||||
Long imageProjectionHash64Hi = similarImageProjection.getHash64Hi();
|
||||
Long similarImageProjectionHash64Lo = similarImageProjection.getHash64Lo();
|
||||
|
||||
int hamming = fileUtil.hamming64(hash64Hi, hash64Lo, imageProjectionHash64Hi, similarImageProjectionHash64Lo);
|
||||
int hamming = PerceptualHashHelper.INSTANCE.hammingDistance(new PHash(hash64Hi, hash64Lo),
|
||||
new PHash(imageProjectionHash64Hi, similarImageProjectionHash64Lo));
|
||||
|
||||
String level;
|
||||
if (hamming <= 5) {
|
||||
if (hamming <= 6) {
|
||||
level = "DUPLICATE";
|
||||
} else if (hamming <= 12) {
|
||||
level = "SIMILAR";
|
||||
@@ -186,6 +192,10 @@ public class FileSimilarityService {
|
||||
.fileSize(similarImageProjection.getFileSize())
|
||||
.hammingDistance(hamming)
|
||||
.similarityLevel(level)
|
||||
.supportId(similarImageProjection.getSupportId())
|
||||
.uploadDate(similarImageProjection.getUploadDate())
|
||||
.status(similarImageProjection.getProtectionStatus())
|
||||
.url(baseUrl + "/api/files/public/" + similarImageProjection.getId())
|
||||
.build();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
package ru.soune.nocopy.service.file;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.core.io.UrlResource;
|
||||
import org.springframework.stereotype.Service;
|
||||
import ru.soune.nocopy.configuration.file.FileStorageConfig;
|
||||
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
|
||||
@Service
|
||||
@Slf4j
|
||||
public class FileStorageService {
|
||||
|
||||
@Autowired
|
||||
private FileStorageConfig storageConfig;
|
||||
|
||||
public Resource loadFileAsResource(String filePath) throws IOException {
|
||||
Path path = Paths.get(storageConfig.getBasePath()).resolve(filePath).normalize();
|
||||
Resource resource = new UrlResource(path.toUri());
|
||||
|
||||
if (!resource.exists()) {
|
||||
throw new FileNotFoundException("File not found: " + filePath);
|
||||
}
|
||||
|
||||
if (!resource.isReadable()) {
|
||||
throw new IOException("File is not readable: " + filePath);
|
||||
}
|
||||
|
||||
return resource;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user