This commit is contained in:
@@ -117,7 +117,7 @@ public class FileSimilarityService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public FileEntity findDuplicateByHash(String path, String mimeType, Long userId) throws Exception {
|
public FileEntity findDuplicateByHash(String path, String mimeType, Long userId) throws Exception {
|
||||||
String hash = calculateFileHash(path);
|
String hash = calculateFileHash(path, false);
|
||||||
|
|
||||||
User user = userRepository.findById(userId).orElseThrow();
|
User user = userRepository.findById(userId).orElseThrow();
|
||||||
Company company = user.getCompany();
|
Company company = user.getCompany();
|
||||||
@@ -132,15 +132,15 @@ public class FileSimilarityService {
|
|||||||
List<FileEntity> fileEntityList = fileEntityRepository.findByMimeTypeAndUserIds(mimeType, userIds);
|
List<FileEntity> fileEntityList = fileEntityRepository.findByMimeTypeAndUserIds(mimeType, userIds);
|
||||||
|
|
||||||
for (FileEntity file : fileEntityList) {
|
for (FileEntity file : fileEntityList) {
|
||||||
if (file.getProtectedFilePath() == null) continue;
|
// if (file.getProtectedFilePath() == null) continue;
|
||||||
File existingFile = cloudStorageService.readFileFromStorageByPath(file.getFilePath());
|
// File existingFile = cloudStorageService.readFileFromStorageByPath(file.getFilePath());
|
||||||
File newFile = new File(path);
|
// File newFile = new File(path);
|
||||||
|
|
||||||
if (existingFile.length() == newFile.length()) {
|
// if (existingFile.length() == newFile.length()) {
|
||||||
if (calculateFileHash(file.getFilePath()).equals(hash)) {
|
if (calculateFileHash(file.getFilePath(), true).equals(hash)) {
|
||||||
return file;
|
return file;
|
||||||
}
|
}
|
||||||
}
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
@@ -199,10 +199,12 @@ public class FileSimilarityService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private String calculateFileHash(String path) throws Exception {
|
private String calculateFileHash(String path, boolean cloud) throws Exception {
|
||||||
MessageDigest digest = MessageDigest.getInstance("SHA-256");
|
MessageDigest digest = MessageDigest.getInstance("SHA-256");
|
||||||
|
|
||||||
try (InputStream is = cloudStorageService.readFileFromStorage(path)) {
|
File file = cloud ? cloudStorageService.readFileFromStorageByPath(path): new File(path);
|
||||||
|
|
||||||
|
try (InputStream is = new FileInputStream(file)) {
|
||||||
byte[] buffer = new byte[8192];
|
byte[] buffer = new byte[8192];
|
||||||
int read;
|
int read;
|
||||||
while ((read = is.read(buffer)) > 0) {
|
while ((read = is.read(buffer)) > 0) {
|
||||||
|
|||||||
Reference in New Issue
Block a user