NCBACK-25 add protection for audio and use hash method from library
Test Workflow / test (push) Successful in 2s
Test Workflow / test (push) Successful in 2s
This commit is contained in:
@@ -32,15 +32,15 @@ public class FileSimilarityService {
|
||||
var imageHashEntity = hashRepository.findById(fileId)
|
||||
.orElseThrow(() -> new RuntimeException("Hash not found"));
|
||||
|
||||
Integer hash64Hi = imageHashEntity.getHash64Hi();
|
||||
Integer hash64Lo = imageHashEntity.getHash64Lo();
|
||||
Long hash64Hi = imageHashEntity.getHash64Hi();
|
||||
Long hash64Lo = imageHashEntity.getHash64Lo();
|
||||
|
||||
List<SimilarImageProjection> candidates = repository.findCandidates(fileId);
|
||||
|
||||
return candidates.stream()
|
||||
.map(c -> {
|
||||
Integer cHi = c.getHash64Hi();
|
||||
Integer cLo = c.getHash64Lo();
|
||||
Long cHi = c.getHash64Hi();
|
||||
Long cLo = c.getHash64Lo();
|
||||
|
||||
int hamming = fileUtil.hamming64(hash64Hi, hash64Lo, cHi, cLo);
|
||||
|
||||
@@ -73,13 +73,13 @@ public class FileSimilarityService {
|
||||
var imageHashEntity = hashRepository.findById(fileId)
|
||||
.orElseThrow(() -> new RuntimeException("Hash not found"));
|
||||
|
||||
Integer hash64Hi = imageHashEntity.getHash64Hi();
|
||||
Integer hash64Lo = imageHashEntity.getHash64Lo();
|
||||
Long hash64Hi = imageHashEntity.getHash64Hi();
|
||||
Long hash64Lo = imageHashEntity.getHash64Lo();
|
||||
|
||||
return candidates.stream()
|
||||
.map(c -> {
|
||||
Integer cHi = c.getHash64Hi();
|
||||
Integer cLo = c.getHash64Lo();
|
||||
Long cHi = c.getHash64Hi();
|
||||
Long cLo = c.getHash64Lo();
|
||||
|
||||
int hamming = fileUtil.hamming64(hash64Hi, hash64Lo, cHi, cLo);
|
||||
|
||||
@@ -112,7 +112,7 @@ public class FileSimilarityService {
|
||||
.toList();
|
||||
}
|
||||
|
||||
public List<SimilarImageProjection> findDuplicatedByHash(Integer hash64Hi, Integer hash64Lo) {
|
||||
public List<SimilarImageProjection> findDuplicatedByHash(Long hash64Hi, Long hash64Lo) {
|
||||
List<SimilarImageProjection> duplicates = repository.findExactDuplicates(hash64Hi,hash64Lo);
|
||||
|
||||
if (duplicates.isEmpty()) {
|
||||
@@ -126,8 +126,8 @@ public class FileSimilarityService {
|
||||
var imageHashEntity = hashRepository.findById(fileId)
|
||||
.orElseThrow(() -> new RuntimeException("Hash not found"));
|
||||
|
||||
Integer hash64Hi = imageHashEntity.getHash64Hi();
|
||||
Integer hash64Lo = imageHashEntity.getHash64Lo();
|
||||
Long hash64Hi = imageHashEntity.getHash64Hi();
|
||||
Long hash64Lo = imageHashEntity.getHash64Lo();
|
||||
|
||||
List<SimilarImageProjection> candidates = repository.findCandidates(fileId);
|
||||
|
||||
@@ -154,9 +154,9 @@ public class FileSimilarityService {
|
||||
}
|
||||
|
||||
private SimilarFileDTO createSimilarFileResponse(SimilarImageProjection similarImageProjection,
|
||||
Integer hash64Hi, Integer hash64Lo) {
|
||||
Integer imageProjectionHash64Hi = similarImageProjection.getHash64Hi();
|
||||
Integer similarImageProjectionHash64Lo = similarImageProjection.getHash64Lo();
|
||||
Long hash64Hi, Long hash64Lo) {
|
||||
Long imageProjectionHash64Hi = similarImageProjection.getHash64Hi();
|
||||
Long similarImageProjectionHash64Lo = similarImageProjection.getHash64Lo();
|
||||
|
||||
int hamming = fileUtil.hamming64(hash64Hi, hash64Lo, imageProjectionHash64Hi, similarImageProjectionHash64Lo);
|
||||
|
||||
|
||||
@@ -22,19 +22,19 @@ public class ImageHashService {
|
||||
|
||||
private final ImageHashRepository repository;
|
||||
|
||||
public Map<String, Integer> calculateHash(Path imagePath) throws IOException {
|
||||
public Map<String, Long> calculateHash(Path imagePath) throws IOException {
|
||||
File file = imagePath.toFile();
|
||||
PHash pHash = PerceptualHashHelper.INSTANCE.generateDCTPerceptualHash(file);
|
||||
Long firstPart = pHash.getFirstPart();
|
||||
Long secondPart = pHash.getSecondPart();
|
||||
Map<String, Integer> hash = Map.of(
|
||||
"hi", Math.toIntExact(firstPart),
|
||||
"low", Math.toIntExact(secondPart));
|
||||
Map<String, Long> hash = Map.of(
|
||||
"hi", firstPart,
|
||||
"low", secondPart);
|
||||
|
||||
return hash;
|
||||
}
|
||||
|
||||
public void create(FileEntity file, Map<String, Integer> stringIntegerMap) {
|
||||
public void create(FileEntity file, Map<String, Long> stringIntegerMap) {
|
||||
ImageHashEntity entity = ImageHashEntity.builder()
|
||||
.file(file)
|
||||
.hash64Hi(stringIntegerMap.get("hi"))
|
||||
|
||||
@@ -49,7 +49,7 @@ public class FileEntityService {
|
||||
throw new IOException("File not found on disk: " + filePath);
|
||||
}
|
||||
|
||||
Map<String, Integer> imageHash = Map.of();
|
||||
Map<String, Long> imageHash = Map.of();
|
||||
|
||||
if (session.getFileType().startsWith("image")) {
|
||||
imageHash = imageHashService.calculateHash(filePath);
|
||||
|
||||
@@ -283,12 +283,16 @@ public class FileUploadServiceImpl implements FileUploadService {
|
||||
|
||||
FileEntity saved = fileEntityRepository.save(fileEntity);
|
||||
|
||||
Map<String, Integer> hash = imageHashService.calculateHash(filePath);
|
||||
if (session.getFileType().equals("image")) {
|
||||
Map<String, Long> hash = imageHashService.calculateHash(filePath);
|
||||
|
||||
imageHashService.create(saved, hash);
|
||||
imageHashService.create(saved, hash);
|
||||
}
|
||||
|
||||
cleanupSessionFiles(session);
|
||||
|
||||
noCopyFileService.addFile(fileUtil.createFileInfo(fileEntity));
|
||||
|
||||
log.info("File processing completed for session: {}", session.getUploadId());
|
||||
|
||||
} catch (Exception e) {
|
||||
@@ -380,7 +384,8 @@ public class FileUploadServiceImpl implements FileUploadService {
|
||||
private void checkForDuplicatesSynchronously(String filePath)
|
||||
throws IOException {
|
||||
Path path = Paths.get(filePath);
|
||||
Map<String, Integer> hash = imageHashService.calculateHash(path);
|
||||
Map<String, Long> hash = imageHashService.calculateHash(path);
|
||||
|
||||
List<SimilarImageProjection> duplicates = fileSimilarityService.findDuplicatedByHash(
|
||||
hash.get("hi"), hash.get("low"));
|
||||
|
||||
|
||||
@@ -44,8 +44,8 @@ public class ImageLocalSearchImpl implements ImageLocalSearch {
|
||||
@Override
|
||||
public @Nullable FileProtector.FileInfo findByPHash(@NotNull PHash pHash) {
|
||||
ImageHashEntity imageHashEntity =
|
||||
imageHashRepository.findByHash64HiAndHash64Lo(Math.toIntExact(pHash.getFirstPart()),
|
||||
Math.toIntExact(pHash.getSecondPart()));
|
||||
imageHashRepository.findByHash64HiAndHash64Lo(pHash.getFirstPart(),
|
||||
pHash.getSecondPart());
|
||||
FileEntity file = fileEntityRepository.findByFileId(imageHashEntity.getFileId());
|
||||
|
||||
return new FileProtector.FileInfo(FileProtector.Type.IMAGE, imageHashEntity.getFileId(),
|
||||
|
||||
@@ -10,12 +10,18 @@ import org.springframework.stereotype.Component;
|
||||
import ru.soune.nocopy.service.file.FileEntityService;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
|
||||
@Slf4j
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
public class ProtectionFileProviderImpl implements FileProtector.FileProvider {
|
||||
|
||||
private static final Path SIGNATURE_FILE_PATH = Paths.get("/data/uploads/signature");
|
||||
|
||||
private final FileEntityService fileEntityService;
|
||||
|
||||
@Nullable
|
||||
@@ -38,9 +44,41 @@ public class ProtectionFileProviderImpl implements FileProtector.FileProvider {
|
||||
|
||||
@Override
|
||||
public @Nullable File getSignature() {
|
||||
log.info(" !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! GET SIGNATURE FILE !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
|
||||
File signatureFile = SIGNATURE_FILE_PATH.toFile();
|
||||
|
||||
if (signatureFile.exists() && signatureFile.isFile() && signatureFile.length() > 0) {
|
||||
return signatureFile;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull OperationResult writeSignature(@NotNull byte[] bytes) {
|
||||
log.info(" !!!!!!!!!!!!!!!!!!!!!!!!!!! WRITING SIGNATURE FILE !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
|
||||
log.info(" !!!!!!!!!!!!!!!!!!!!!!!!!!! WRITING SIGNATURE FILE !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
|
||||
log.info(" !!!!!!!!!!!!!!!!!!!!!!!!!!! WRITING SIGNATURE FILE !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
|
||||
log.info(" !!!!!!!!!!!!!!!!!!!!!!!!!!! WRITING SIGNATURE FILE !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
|
||||
log.info(" !!!!!!!!!!!!!!!!!!!!!!!!!!! WRITING SIGNATURE FILE !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
|
||||
log.info(" !!!!!!!!!!!!!!!!!!!!!!!!!!! WRITING SIGNATURE FILE !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
|
||||
log.info(" !!!!!!!!!!!!!!!!!!!!!!!!!!! WRITING SIGNATURE FILE !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
|
||||
log.info(" !!!!!!!!!!!!!!!!!!!!!!!!!!! WRITING SIGNATURE FILE !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
|
||||
try {
|
||||
Path directory = SIGNATURE_FILE_PATH.getParent();
|
||||
|
||||
if (directory != null && !Files.exists(directory)) {
|
||||
Files.createDirectories(directory);
|
||||
}
|
||||
|
||||
Files.write(SIGNATURE_FILE_PATH, bytes);
|
||||
|
||||
return OperationResult.Companion.success();
|
||||
} catch (IOException e) {
|
||||
return OperationResult.Companion.failure("Not saved signature file");
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public com.vrt.fileprotection.OperationResult writeAudioFile(@NotNull String id, @NotNull byte[] data, @Nullable String fileExt) {
|
||||
@@ -76,9 +114,4 @@ public class ProtectionFileProviderImpl implements FileProtector.FileProvider {
|
||||
return OperationResult.Companion.failure("Failed to create protected file");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull OperationResult writeSignature(@NotNull byte[] bytes) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user