@@ -35,6 +35,8 @@ public class FileSimilarityService {
|
||||
|
||||
private final AuthTokenRepository authTokenRepository;
|
||||
|
||||
private final FileEntityRepository fileEntityRepository;
|
||||
|
||||
@Value("${server.baseurl}")
|
||||
private String baseUrl;
|
||||
|
||||
@@ -138,7 +140,18 @@ public class FileSimilarityService {
|
||||
return duplicates;
|
||||
}
|
||||
|
||||
private final FileEntityRepository fileEntityRepository;
|
||||
public boolean 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)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public Page<SimilarFileDTO> findSimilarFiles(String fileId, SimilarityFilter filter, Pageable pageable,
|
||||
String authToken) throws Exception {
|
||||
@@ -165,12 +178,12 @@ public class FileSimilarityService {
|
||||
.sorted(Comparator.comparingInt(SimilarFileDTO::getHammingDistance))
|
||||
.collect(Collectors.toList());
|
||||
} else {
|
||||
String hash = calculateFileHash(fileEntity);
|
||||
String hash = calculateFileHash(fileEntity.getFilePath());
|
||||
Long userId = fileEntity.getUserId();
|
||||
List<FileEntity> fileEntityList = fileEntityRepository.findByUserIdAndMimeType(userId, mimeType);
|
||||
|
||||
for (FileEntity file : fileEntityList) {
|
||||
if (calculateFileHash(file).equals(hash)) {
|
||||
if (calculateFileHash(file.getFilePath()).equals(hash)) {
|
||||
allResults.add(buildDTO(file));
|
||||
}
|
||||
}
|
||||
@@ -189,28 +202,15 @@ public class FileSimilarityService {
|
||||
}
|
||||
|
||||
|
||||
private String calculateFileHash(FileEntity fileEntity) throws Exception {
|
||||
private String calculateFileHash(String path) throws Exception {
|
||||
MessageDigest digest = MessageDigest.getInstance("SHA-256");
|
||||
|
||||
// if (fileEntity.getStatus() == FileStatus.CLOUD) {
|
||||
// File tempFile = cloudStorageService.readFileFromStorageByPath(fileEntity.getFilePath());
|
||||
// try (InputStream is = new FileInputStream(tempFile)) {
|
||||
// byte[] buffer = new byte[8192];
|
||||
// int read;
|
||||
// while ((read = is.read(buffer)) > 0) {
|
||||
// digest.update(buffer, 0, read);
|
||||
// }
|
||||
// } finally {
|
||||
// tempFile.delete();
|
||||
// }
|
||||
// } else {
|
||||
try (InputStream is = new FileInputStream(fileEntity.getFilePath())) {
|
||||
byte[] buffer = new byte[8192];
|
||||
int read;
|
||||
while ((read = is.read(buffer)) > 0) {
|
||||
digest.update(buffer, 0, read);
|
||||
}
|
||||
// }
|
||||
try (InputStream is = new FileInputStream(path)) {
|
||||
byte[] buffer = new byte[8192];
|
||||
int read;
|
||||
while ((read = is.read(buffer)) > 0) {
|
||||
digest.update(buffer, 0, read);
|
||||
}
|
||||
}
|
||||
|
||||
byte[] hashBytes = digest.digest();
|
||||
|
||||
Reference in New Issue
Block a user