@@ -152,17 +152,17 @@ public class FileSimilarityService {
|
||||
return duplicates;
|
||||
}
|
||||
|
||||
public void hasDuplicatesByHash(String path, Long userId, String mimeType) throws Exception {
|
||||
String hash = calculateFileHash(path);
|
||||
List<FileEntity> fileEntityList = fileEntityRepository.findByUserIdAndMimeType(userId, mimeType);
|
||||
|
||||
for (FileEntity file : fileEntityList) {
|
||||
if (calculateFileHash(file.getFilePath()).equals(hash)) {
|
||||
throw new DuplicateImageException("Duplicate", file.getId(),
|
||||
file.getUserId());
|
||||
}
|
||||
}
|
||||
}
|
||||
// public void hasDuplicatesByHash(String path, Long userId, String mimeType) throws Exception {
|
||||
// String hash = calculateFileHash(path);
|
||||
// List<FileEntity> fileEntityList = fileEntityRepository.findByUserIdAndMimeType(userId, mimeType);
|
||||
//
|
||||
// for (FileEntity file : fileEntityList) {
|
||||
// if (calculateFileHash(file.getFilePath()).equals(hash)) {
|
||||
// throw new DuplicateImageException("Duplicate", file.getId(),
|
||||
// file.getUserId());
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
// public Page<SimilarFileDTO> findSimilarFiles(String fileId, SimilarityFilter filter, Pageable pageable,
|
||||
// String authToken) throws Exception {
|
||||
|
||||
@@ -2,6 +2,9 @@ package ru.soune.nocopy.service.file.impl;
|
||||
|
||||
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 jakarta.annotation.PostConstruct;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
@@ -321,8 +324,29 @@ public class FileUploadServiceImpl implements FileUploadService {
|
||||
if (session.getFileType().startsWith("image")) {
|
||||
checkForDuplicatesSynchronously(finalFilePath);
|
||||
} else if (session.getFileType().startsWith("audio") || session.getFileType().startsWith("document")) {
|
||||
fileSimilarityService.hasDuplicatesByHash(finalFilePath, session.getUserId(),
|
||||
session.getFileType());
|
||||
File file = new File(finalFilePath);
|
||||
FileProtector.Type type = "document".equals(session.getFileType()) ?
|
||||
FileProtector.Type.DOC : FileProtector.Type.AUDIO;
|
||||
|
||||
NoCopyCheckResult checkResult = noCopyFileService.checkFile(file, type);
|
||||
|
||||
switch (checkResult) {
|
||||
case DocumentCheckResult.Success success -> {
|
||||
FileProtector.FileInfo info = success.getInfo();
|
||||
throw new DuplicateImageException("Duplicate", info.getId(),
|
||||
Long.valueOf(info.getOwnerId()));
|
||||
}
|
||||
case AudioCheckResult.Success success -> {
|
||||
FileProtector.FileInfo info = success.getInfo();
|
||||
throw new DuplicateImageException("Duplicate", info.getId(),
|
||||
Long.valueOf(info.getOwnerId()));
|
||||
}
|
||||
case DocumentCheckResult.Failed failed ->
|
||||
log.warn("Document check failed: {}", failed.getMessage());
|
||||
case AudioCheckResult.Failed failed ->
|
||||
log.warn("Audio check failed: {}", failed.getMessage());
|
||||
default -> log.warn("Unexpected result type: {}", checkResult.getClass().getSimpleName());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user