@@ -1,5 +1,10 @@
|
||||
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;
|
||||
@@ -12,7 +17,6 @@ import org.springframework.stereotype.Service;
|
||||
import ru.soune.nocopy.dto.file.SimilarFileDTO;
|
||||
import ru.soune.nocopy.dto.file.SimilarityFilter;
|
||||
import ru.soune.nocopy.entity.file.FileEntity;
|
||||
import ru.soune.nocopy.entity.file.FileStatus;
|
||||
import ru.soune.nocopy.entity.file.ImageHashEntity;
|
||||
import ru.soune.nocopy.exception.DuplicateImageException;
|
||||
import ru.soune.nocopy.repository.*;
|
||||
@@ -20,6 +24,8 @@ 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;
|
||||
@@ -154,6 +160,8 @@ public class FileSimilarityService {
|
||||
}
|
||||
}
|
||||
|
||||
private NoCopyFileService noCopyFileService;
|
||||
|
||||
public Page<SimilarFileDTO> findSimilarFiles(String fileId, SimilarityFilter filter, Pageable pageable,
|
||||
String authToken) throws Exception {
|
||||
FileEntity fileEntity = fileEntityRepository.findByFileId(fileId);
|
||||
@@ -179,13 +187,73 @@ public class FileSimilarityService {
|
||||
.sorted(Comparator.comparingInt(SimilarFileDTO::getHammingDistance))
|
||||
.collect(Collectors.toList());
|
||||
} else {
|
||||
String hash = calculateFileHash(fileEntity.getFilePath());
|
||||
// Long userId = fileEntity.getUserId();
|
||||
// List<FileEntity> fileEntityList = fileEntityRepository.findByUserIdAndMimeType(userId, mimeType);
|
||||
// NoCopyCheckResult noCopyCheckResult;
|
||||
//
|
||||
// for (FileEntity file : fileEntityList) {
|
||||
// Path path = Paths.get(file.getFilePath());
|
||||
// if (mimeType.equals("document")) {
|
||||
// noCopyCheckResult = (DocumentCheckResult) noCopyFileService.checkFile(
|
||||
// path.toFile(), FileProtector.Type.valueOf(file.getMimeType().toUpperCase()));
|
||||
// } else {
|
||||
// noCopyCheckResult = (AudioCheckResult) noCopyFileService.checkFile(
|
||||
// path.toFile(), FileProtector.Type.valueOf(file.getMimeType().toUpperCase()));
|
||||
// }
|
||||
//
|
||||
// switch (noCopyCheckResult) {
|
||||
// case DocumentCheckResult.Success success -> {
|
||||
// FileProtector.FileInfo info = success.getInfo();
|
||||
// allResults.add(buildDTO(file));
|
||||
// }
|
||||
// case DocumentCheckResult.Failed failed -> {
|
||||
// String message = failed.getMessage();
|
||||
// }
|
||||
// default -> throw new IllegalStateException("Unexpected result");
|
||||
// }
|
||||
// }
|
||||
|
||||
Long userId = fileEntity.getUserId();
|
||||
List<FileEntity> fileEntityList = fileEntityRepository.findByUserIdAndMimeType(userId, mimeType);
|
||||
List<String> errors = new ArrayList<>();
|
||||
|
||||
for (FileEntity file : fileEntityList) {
|
||||
if (calculateFileHash(file.getFilePath()).equals(hash) && !file.getStatus().equals(FileStatus.TEMP)) {
|
||||
allResults.add(buildDTO(file));
|
||||
try {
|
||||
Path path = Paths.get(file.getFilePath());
|
||||
FileProtector.Type fileType = FileProtector.Type.valueOf(file.getMimeType().toUpperCase());
|
||||
|
||||
NoCopyCheckResult noCopyCheckResult = noCopyFileService.checkFile(path.toFile(), fileType);
|
||||
|
||||
switch (noCopyCheckResult) {
|
||||
case DocumentCheckResult.Success success -> {
|
||||
FileProtector.FileInfo info = success.getInfo();
|
||||
allResults.add(buildDTO(file));
|
||||
log.debug("File processed successfully: {}", file.getFilePath());
|
||||
}
|
||||
case AudioCheckResult.Success success -> {
|
||||
FileProtector.FileInfo info = success.getInfo();
|
||||
allResults.add(buildDTO(file));
|
||||
log.debug("File processed successfully: {}", file.getFilePath());
|
||||
}
|
||||
case DocumentCheckResult.Failed failed -> {
|
||||
String errorMessage = String.format("Document check failed for %s: %s",
|
||||
file.getFilePath(), failed.getMessage());
|
||||
log.warn(errorMessage);
|
||||
errors.add(errorMessage);
|
||||
}
|
||||
case AudioCheckResult.Failed failed -> {
|
||||
String errorMessage = String.format("Audio check failed for %s: %s",
|
||||
file.getFilePath(), failed.getMessage());
|
||||
log.warn(errorMessage);
|
||||
errors.add(errorMessage);
|
||||
}
|
||||
default -> throw new IllegalStateException("Unexpected result type: " + noCopyCheckResult.getClass().getSimpleName());
|
||||
}
|
||||
} catch (Exception e) {
|
||||
String errorMessage = String.format("Error processing file %s: %s",
|
||||
file.getFilePath(), e.getMessage());
|
||||
log.error(errorMessage, e);
|
||||
errors.add(errorMessage);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user