This commit is contained in:
@@ -124,12 +124,12 @@ public class FileController {
|
|||||||
|
|
||||||
String filePath = type.equals("thumbnail") ? fileEntity.getThumbnailPath(): fileEntity.getFilePath();
|
String filePath = type.equals("thumbnail") ? fileEntity.getThumbnailPath(): fileEntity.getFilePath();
|
||||||
|
|
||||||
File file = cloudStorageService.readFileFromStorageByPath(filePath);
|
// InputStream file = cloudStorageService.readFileFromStorage(filePath);
|
||||||
|
|
||||||
if (file == null || !file.exists()) {
|
// if (file == null || !file.exists()) {
|
||||||
log.error("File not found in storage: {}", fileEntity.getFilePath());
|
// log.error("File not found in storage: {}", fileEntity.getFilePath());
|
||||||
return ResponseEntity.notFound().build();
|
// return ResponseEntity.notFound().build();
|
||||||
}
|
// }
|
||||||
|
|
||||||
if (fileEntity.getProtectionStatus() == ProtectionStatus.NOT_PROTECTED ||
|
if (fileEntity.getProtectionStatus() == ProtectionStatus.NOT_PROTECTED ||
|
||||||
fileEntity.getProtectionStatus() == ProtectionStatus.PROCESSING) {
|
fileEntity.getProtectionStatus() == ProtectionStatus.PROCESSING) {
|
||||||
@@ -142,7 +142,7 @@ public class FileController {
|
|||||||
String encodedFileName = URLEncoder.encode(fileName, StandardCharsets.UTF_8)
|
String encodedFileName = URLEncoder.encode(fileName, StandardCharsets.UTF_8)
|
||||||
.replace("+", "%20");
|
.replace("+", "%20");
|
||||||
|
|
||||||
InputStreamResource resource = new InputStreamResource(new FileInputStream(file));
|
InputStreamResource resource = new InputStreamResource(cloudStorageService.readFileFromStorage(filePath));
|
||||||
|
|
||||||
return ResponseEntity.ok()
|
return ResponseEntity.ok()
|
||||||
.contentType(new MediaType(fileEntity.getMimeType(), fileEntity.getFileExtension()))
|
.contentType(new MediaType(fileEntity.getMimeType(), fileEntity.getFileExtension()))
|
||||||
|
|||||||
@@ -202,13 +202,16 @@ public class FileSimilarityService {
|
|||||||
private String calculateFileHash(String path, boolean cloud) throws Exception {
|
private String calculateFileHash(String path, boolean cloud) throws Exception {
|
||||||
MessageDigest digest = MessageDigest.getInstance("SHA-256");
|
MessageDigest digest = MessageDigest.getInstance("SHA-256");
|
||||||
|
|
||||||
File file = cloud ? cloudStorageService.readFileFromStorageByPath(path): new File(path);
|
if (cloud) {
|
||||||
|
byte[] data = cloudStorageService.readFileFromStorageBytes(path);
|
||||||
try (InputStream is = new FileInputStream(file)) {
|
digest.update(data);
|
||||||
byte[] buffer = new byte[8192];
|
} else {
|
||||||
int read;
|
try (InputStream is = new FileInputStream(path)) {
|
||||||
while ((read = is.read(buffer)) > 0) {
|
byte[] buffer = new byte[8192];
|
||||||
digest.update(buffer, 0, read);
|
int read;
|
||||||
|
while ((read = is.read(buffer)) > 0) {
|
||||||
|
digest.update(buffer, 0, read);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ import ru.soune.nocopy.service.file.cloud.CloudStorageService;
|
|||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
@@ -25,22 +26,48 @@ public class ImageHashService {
|
|||||||
|
|
||||||
private final CloudStorageService cloudStorageService;
|
private final CloudStorageService cloudStorageService;
|
||||||
|
|
||||||
public Map<String, Long> calculateHash(Path imagePath) throws IOException {
|
// public Map<String, Long> calculateHash(Path imagePath) throws IOException {
|
||||||
// File file = cloudStorageService.readFileFromStorageByPath(imagePath.toString());
|
//// File file = cloudStorageService.readFileFromStorageByPath(imagePath.toString());
|
||||||
File file = imagePath.toFile();
|
// File file = imagePath.toFile();
|
||||||
if (file == null) {
|
// if (file == null) {
|
||||||
file = cloudStorageService.readFileFromStorageByPath(imagePath.toString());
|
// file = cloudStorageService.readFileFromStorageByPath(imagePath.toString());
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
|
// PHash pHash = PerceptualHashHelper.INSTANCE.generateDCTPerceptualHash(file);
|
||||||
|
//
|
||||||
|
// Long firstPart = pHash.getFirstPart();
|
||||||
|
// Long secondPart = pHash.getSecondPart();
|
||||||
|
// Map<String, Long> hash = Map.of(
|
||||||
|
// "hi", firstPart,
|
||||||
|
// "low", secondPart);
|
||||||
|
//
|
||||||
|
// return hash;
|
||||||
|
// }
|
||||||
|
|
||||||
PHash pHash = PerceptualHashHelper.INSTANCE.generateDCTPerceptualHash(file);
|
public Map<String, Long> calculateHash(Path imagePath) throws IOException {
|
||||||
|
PHash pHash;
|
||||||
|
|
||||||
|
if (imagePath.toFile().exists()) {
|
||||||
|
pHash = PerceptualHashHelper.INSTANCE.generateDCTPerceptualHash(imagePath.toFile());
|
||||||
|
} else {
|
||||||
|
byte[] data = cloudStorageService.readFileFromStorageBytes(imagePath.toString());
|
||||||
|
|
||||||
|
Path tempFile = Files.createTempFile("perceptual_hash_", ".tmp");
|
||||||
|
try {
|
||||||
|
Files.write(tempFile, data);
|
||||||
|
pHash = PerceptualHashHelper.INSTANCE.generateDCTPerceptualHash(tempFile.toFile());
|
||||||
|
} finally {
|
||||||
|
Files.deleteIfExists(tempFile);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Long firstPart = pHash.getFirstPart();
|
Long firstPart = pHash.getFirstPart();
|
||||||
Long secondPart = pHash.getSecondPart();
|
Long secondPart = pHash.getSecondPart();
|
||||||
Map<String, Long> hash = Map.of(
|
|
||||||
"hi", firstPart,
|
|
||||||
"low", secondPart);
|
|
||||||
|
|
||||||
return hash;
|
return Map.of(
|
||||||
|
"hi", firstPart,
|
||||||
|
"low", secondPart
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void create(FileEntity file, Map<String, Long> stringIntegerMap) {
|
public void create(FileEntity file, Map<String, Long> stringIntegerMap) {
|
||||||
|
|||||||
@@ -19,8 +19,6 @@ import ru.soune.nocopy.exception.FileEntityNotFoundException;
|
|||||||
import ru.soune.nocopy.repository.FileEntityRepository;
|
import ru.soune.nocopy.repository.FileEntityRepository;
|
||||||
import ru.soune.nocopy.repository.FileMonitoringRepository;
|
import ru.soune.nocopy.repository.FileMonitoringRepository;
|
||||||
import ru.soune.nocopy.repository.UserRepository;
|
import ru.soune.nocopy.repository.UserRepository;
|
||||||
import ru.soune.nocopy.service.FileSimilarityService;
|
|
||||||
import ru.soune.nocopy.service.ImageHashService;
|
|
||||||
import ru.soune.nocopy.service.file.cloud.CloudStorageService;
|
import ru.soune.nocopy.service.file.cloud.CloudStorageService;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|||||||
@@ -9,12 +9,14 @@ import ru.soune.nocopy.repository.FileEntityRepository;
|
|||||||
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
|
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
|
||||||
import software.amazon.awssdk.auth.credentials.AwsCredentials;
|
import software.amazon.awssdk.auth.credentials.AwsCredentials;
|
||||||
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
|
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
|
||||||
|
import software.amazon.awssdk.core.ResponseBytes;
|
||||||
import software.amazon.awssdk.core.sync.RequestBody;
|
import software.amazon.awssdk.core.sync.RequestBody;
|
||||||
import software.amazon.awssdk.core.sync.ResponseTransformer;
|
import software.amazon.awssdk.core.sync.ResponseTransformer;
|
||||||
import software.amazon.awssdk.http.apache.ApacheHttpClient;
|
import software.amazon.awssdk.http.apache.ApacheHttpClient;
|
||||||
import software.amazon.awssdk.regions.Region;
|
import software.amazon.awssdk.regions.Region;
|
||||||
import software.amazon.awssdk.services.s3.S3Client;
|
import software.amazon.awssdk.services.s3.S3Client;
|
||||||
import software.amazon.awssdk.services.s3.model.GetObjectRequest;
|
import software.amazon.awssdk.services.s3.model.GetObjectRequest;
|
||||||
|
import software.amazon.awssdk.services.s3.model.GetObjectResponse;
|
||||||
import software.amazon.awssdk.services.s3.model.PutObjectRequest;
|
import software.amazon.awssdk.services.s3.model.PutObjectRequest;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
@@ -23,6 +25,7 @@ import java.io.InputStream;
|
|||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Paths;
|
import java.nio.file.Paths;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@@ -86,6 +89,29 @@ public class CloudStorageService {
|
|||||||
log.error("Failed to read file: {}", filePath, e);
|
log.error("Failed to read file: {}", filePath, e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
//
|
||||||
|
// public File readFileFromStorageByPath(String filePath) throws IOException {
|
||||||
|
// GetObjectRequest objectRequest = GetObjectRequest.builder()
|
||||||
|
// .bucket(bucket)
|
||||||
|
// .key("files" + filePath)
|
||||||
|
// .build();
|
||||||
|
// AwsCredentials credentials = AwsBasicCredentials.create(key, secretKey);
|
||||||
|
//
|
||||||
|
// S3Client s3Client = S3Client.builder()
|
||||||
|
// .httpClient(ApacheHttpClient.create())
|
||||||
|
// .region(Region.of(region))
|
||||||
|
// .endpointOverride(URI.create(s3endpoint))
|
||||||
|
// .credentialsProvider(StaticCredentialsProvider.create(credentials))
|
||||||
|
// .build();
|
||||||
|
//
|
||||||
|
// String tempDir = System.getProperty("java.io.tmpdir");
|
||||||
|
// String fileName = filePath.replace("/", "_");
|
||||||
|
// File tempFile = new File(tempDir, fileName);
|
||||||
|
//
|
||||||
|
// s3Client.getObject(objectRequest, tempFile.toPath());
|
||||||
|
//
|
||||||
|
// return tempFile;
|
||||||
|
// }
|
||||||
|
|
||||||
public File readFileFromStorageByPath(String filePath) throws IOException {
|
public File readFileFromStorageByPath(String filePath) throws IOException {
|
||||||
GetObjectRequest objectRequest = GetObjectRequest.builder()
|
GetObjectRequest objectRequest = GetObjectRequest.builder()
|
||||||
@@ -102,14 +128,43 @@ public class CloudStorageService {
|
|||||||
.build();
|
.build();
|
||||||
|
|
||||||
String tempDir = System.getProperty("java.io.tmpdir");
|
String tempDir = System.getProperty("java.io.tmpdir");
|
||||||
String fileName = filePath.replace("/", "_");
|
|
||||||
|
String fileName = System.currentTimeMillis() + "_" + UUID.randomUUID() + "_" +
|
||||||
|
filePath.replace("/", "_");
|
||||||
File tempFile = new File(tempDir, fileName);
|
File tempFile = new File(tempDir, fileName);
|
||||||
|
|
||||||
s3Client.getObject(objectRequest, tempFile.toPath());
|
s3Client.getObject(objectRequest, tempFile.toPath());
|
||||||
|
|
||||||
|
tempFile.deleteOnExit();
|
||||||
|
|
||||||
return tempFile;
|
return tempFile;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public byte[] readFileFromStorageBytes(String filePath) {
|
||||||
|
AwsCredentials credentials = AwsBasicCredentials.create(key, secretKey);
|
||||||
|
|
||||||
|
S3Client s3Client = S3Client.builder()
|
||||||
|
.httpClient(ApacheHttpClient.create())
|
||||||
|
.region(Region.of(region))
|
||||||
|
.endpointOverride(URI.create(s3endpoint))
|
||||||
|
.credentialsProvider(StaticCredentialsProvider.create(credentials))
|
||||||
|
.build();
|
||||||
|
|
||||||
|
GetObjectRequest objectRequest = GetObjectRequest.builder()
|
||||||
|
.bucket(bucket)
|
||||||
|
.key("files" + filePath)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
try {
|
||||||
|
ResponseBytes<GetObjectResponse> responseBytes = s3Client.getObjectAsBytes(objectRequest);
|
||||||
|
return responseBytes.asByteArray();
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("Failed to read file from S3: {}", filePath, e);
|
||||||
|
throw new RuntimeException("Failed to read file from S3", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public InputStream readFileFromStorage(String filePath) {
|
public InputStream readFileFromStorage(String filePath) {
|
||||||
GetObjectRequest objectRequest = GetObjectRequest.builder()
|
GetObjectRequest objectRequest = GetObjectRequest.builder()
|
||||||
.bucket(bucket)
|
.bucket(bucket)
|
||||||
|
|||||||
Reference in New Issue
Block a user