@@ -103,16 +103,35 @@ public class FileSimilarityService {
|
||||
return duplicates;
|
||||
}
|
||||
|
||||
public void hasDuplicatesByHash(String path, String mimeType) throws Exception {
|
||||
public void hasDuplicatesByHash(String path, String mimeType, Long userId) throws Exception {
|
||||
FileEntity duplicateByHash = findDuplicateByHash(path, mimeType, userId);
|
||||
if (duplicateByHash != null) {
|
||||
throw new DuplicateImageException("Duplicate", duplicateByHash.getId(),
|
||||
duplicateByHash.getUserId());
|
||||
}
|
||||
}
|
||||
|
||||
public FileEntity findDuplicateByHash(String path, String mimeType, Long userId) throws Exception {
|
||||
String hash = calculateFileHash(path);
|
||||
List<FileEntity> fileEntityList = fileEntityRepository.findByMimeType(mimeType);
|
||||
User user = userRepository.findById(userId).orElseThrow();
|
||||
Company company = user.getCompany();
|
||||
List<Long> userIds = new ArrayList<>();
|
||||
|
||||
if (company != null) {
|
||||
userIds.addAll(company.getUsers().stream().map(User::getId).toList());
|
||||
} else {
|
||||
userIds.add(userId);
|
||||
}
|
||||
|
||||
List<FileEntity> fileEntityList = fileEntityRepository.findByMimeTypeAndUserIds(mimeType, userIds);
|
||||
|
||||
for (FileEntity file : fileEntityList) {
|
||||
if (calculateFileHash(file.getFilePath()).equals(hash)) {
|
||||
throw new DuplicateImageException("Duplicate", file.getId(),
|
||||
file.getUserId());
|
||||
return file;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private final UserRepository userRepository;
|
||||
|
||||
Reference in New Issue
Block a user