@@ -198,9 +198,18 @@ public class ApiController {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private List<SimilarFileDTO> processNonImageFile(FileEntity fileEntity, String authToken) {
|
private List<SimilarFileDTO> processNonImageFile(FileEntity fileEntity, String authToken) throws Exception {
|
||||||
List<SimilarFileDTO> results = new ArrayList<>();
|
List<SimilarFileDTO> results = new ArrayList<>();
|
||||||
|
|
||||||
|
FileEntity duplicateByHash = fileSimilarityService.findDuplicateByHash(fileEntity.getProtectedFilePath(),
|
||||||
|
fileEntity.getMimeType(), fileEntity.getUserId());
|
||||||
|
|
||||||
|
if (duplicateByHash != null) {
|
||||||
|
results.add(fileSimilarityService.buildDTO(duplicateByHash));
|
||||||
|
|
||||||
|
return results;
|
||||||
|
}
|
||||||
|
|
||||||
File file = new File(fileEntity.getFilePath());
|
File file = new File(fileEntity.getFilePath());
|
||||||
FileProtector.Type type = "document".equals(fileEntity.getMimeType()) ?
|
FileProtector.Type type = "document".equals(fileEntity.getMimeType()) ?
|
||||||
FileProtector.Type.DOC : FileProtector.Type.AUDIO;
|
FileProtector.Type.DOC : FileProtector.Type.AUDIO;
|
||||||
@@ -465,45 +474,6 @@ public class ApiController {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// @GetMapping("/protect/{fileId}")
|
|
||||||
// public ResponseEntity<?> protect( @PathVariable(required = false) String fileId) {
|
|
||||||
// Optional<FileEntity> optionalFileEntity = fileEntityRepository.findById(fileId);
|
|
||||||
//
|
|
||||||
// if (!optionalFileEntity.isPresent()) {
|
|
||||||
// return ResponseEntity.notFound().build();
|
|
||||||
// }
|
|
||||||
// FileEntity fileEntity = optionalFileEntity.get();
|
|
||||||
// FileProtector.FileInfo fileInfo = fileUtil.createFileInfo(fileEntity);
|
|
||||||
//
|
|
||||||
// noCopyFileService.addFile(fileInfo);
|
|
||||||
//
|
|
||||||
// fileEntity.setProtectionStatus(ProtectionStatus.PROCESSING);
|
|
||||||
// fileEntityRepository.save(fileEntity);
|
|
||||||
//
|
|
||||||
// return ResponseEntity.ok().build();
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// @GetMapping("/check/{fileId}/{type}")
|
|
||||||
// public ResponseEntity<?> check(@PathVariable(required = false) String fileId,
|
|
||||||
// @PathVariable(required = false) String type) {
|
|
||||||
// Optional<FileEntity> optionalFileEntity = fileEntityRepository.findById(fileId);
|
|
||||||
//
|
|
||||||
// if (!optionalFileEntity.isPresent()) {
|
|
||||||
// return ResponseEntity.notFound().build();
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// FileEntity fileEntity = optionalFileEntity.get();
|
|
||||||
//
|
|
||||||
// Path path = Paths.get(fileEntity.getProtectedFilePath());
|
|
||||||
//
|
|
||||||
// File file = path.toFile();
|
|
||||||
//
|
|
||||||
// NoCopyCheckResult noCopyCheckResult = noCopyFileService.checkFile(file,
|
|
||||||
// FileProtector.Type.valueOf(type.toUpperCase()));
|
|
||||||
//
|
|
||||||
// return ResponseEntity.ok().body(noCopyCheckResult);
|
|
||||||
// }
|
|
||||||
|
|
||||||
@GetMapping("/check/file_stats")
|
@GetMapping("/check/file_stats")
|
||||||
public ResponseEntity<?> checkFileProtectStats(@RequestHeader("Authorization") String tokenHeader) {
|
public ResponseEntity<?> checkFileProtectStats(@RequestHeader("Authorization") String tokenHeader) {
|
||||||
String token = tokenHeader.replace("Bearer ", "");
|
String token = tokenHeader.replace("Bearer ", "");
|
||||||
|
|||||||
@@ -59,6 +59,9 @@ public interface FileEntityRepository extends JpaRepository<FileEntity, String>
|
|||||||
@Query("SELECT f FROM FileEntity f WHERE f.mimeType = :mimeType")
|
@Query("SELECT f FROM FileEntity f WHERE f.mimeType = :mimeType")
|
||||||
List<FileEntity> findByMimeType(String mimeType);
|
List<FileEntity> findByMimeType(String mimeType);
|
||||||
|
|
||||||
|
@Query("SELECT f FROM FileEntity f WHERE f.mimeType = :mimeType AND f.userId IN :userIds")
|
||||||
|
List<FileEntity> findByMimeTypeAndUserIds(String mimeType, List<Long> userIds);
|
||||||
|
|
||||||
@Query("SELECT MAX(f.supportId) FROM FileEntity f")
|
@Query("SELECT MAX(f.supportId) FROM FileEntity f")
|
||||||
Integer findMaxSupportId();
|
Integer findMaxSupportId();
|
||||||
|
|
||||||
|
|||||||
@@ -103,16 +103,35 @@ public class FileSimilarityService {
|
|||||||
return duplicates;
|
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);
|
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) {
|
for (FileEntity file : fileEntityList) {
|
||||||
if (calculateFileHash(file.getFilePath()).equals(hash)) {
|
if (calculateFileHash(file.getFilePath()).equals(hash)) {
|
||||||
throw new DuplicateImageException("Duplicate", file.getId(),
|
return file;
|
||||||
file.getUserId());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
private final UserRepository userRepository;
|
private final UserRepository userRepository;
|
||||||
|
|||||||
@@ -319,7 +319,8 @@ public class FileUploadServiceImpl implements FileUploadService {
|
|||||||
if (session.getFileType().startsWith("image")) {
|
if (session.getFileType().startsWith("image")) {
|
||||||
checkForDuplicatesSynchronously(finalFilePath);
|
checkForDuplicatesSynchronously(finalFilePath);
|
||||||
} else if (session.getFileType().startsWith("audio") || session.getFileType().startsWith("document")) {
|
} else if (session.getFileType().startsWith("audio") || session.getFileType().startsWith("document")) {
|
||||||
fileSimilarityService.hasDuplicatesByHash(finalFilePath ,session.getFileType());
|
fileSimilarityService.hasDuplicatesByHash(finalFilePath ,session.getFileType(),
|
||||||
|
session.getUserId());
|
||||||
|
|
||||||
File file = new File(finalFilePath);
|
File file = new File(finalFilePath);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user