Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a982eef9c6 |
@@ -21,9 +21,6 @@ configurations {
|
|||||||
|
|
||||||
repositories {
|
repositories {
|
||||||
mavenCentral()
|
mavenCentral()
|
||||||
flatDir {
|
|
||||||
dirs 'libs'
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
@@ -35,11 +32,6 @@ dependencies {
|
|||||||
implementation 'commons-validator:commons-validator:1.7'
|
implementation 'commons-validator:commons-validator:1.7'
|
||||||
implementation group: 'org.springframework.boot', name: 'spring-boot-starter-mail', version: '4.0.1'
|
implementation group: 'org.springframework.boot', name: 'spring-boot-starter-mail', version: '4.0.1'
|
||||||
|
|
||||||
implementation group: 'com.google.cloud', name: 'google-cloud-vision', version: '3.55.0'
|
|
||||||
implementation group: 'com.google.api-client', name: 'google-api-client', version: '2.7.2'
|
|
||||||
|
|
||||||
implementation 'org.flywaydb:flyway-core:9.22.0'
|
|
||||||
|
|
||||||
implementation 'com.fasterxml.jackson.dataformat:jackson-dataformat-xml:2.17.0'
|
implementation 'com.fasterxml.jackson.dataformat:jackson-dataformat-xml:2.17.0'
|
||||||
implementation 'tools.jackson.core:jackson-core:3.0.3'
|
implementation 'tools.jackson.core:jackson-core:3.0.3'
|
||||||
implementation 'jakarta.xml.bind:jakarta.xml.bind-api:4.0.0'
|
implementation 'jakarta.xml.bind:jakarta.xml.bind-api:4.0.0'
|
||||||
@@ -58,9 +50,6 @@ dependencies {
|
|||||||
testImplementation 'org.springframework.boot:spring-boot-starter-test'
|
testImplementation 'org.springframework.boot:spring-boot-starter-test'
|
||||||
testImplementation 'org.mockito:mockito-core:5.3.1'
|
testImplementation 'org.mockito:mockito-core:5.3.1'
|
||||||
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
|
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
|
||||||
|
|
||||||
implementation name: 'testlib-fat-0.2.1-all'
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
tasks.named('test') {
|
tasks.named('test') {
|
||||||
|
|||||||
+2
-3
@@ -77,12 +77,11 @@ services:
|
|||||||
resources:
|
resources:
|
||||||
limits:
|
limits:
|
||||||
cpus: '1.5'
|
cpus: '1.5'
|
||||||
memory: 3G
|
memory: 1G
|
||||||
reservations:
|
reservations:
|
||||||
cpus: '0.5'
|
cpus: '0.5'
|
||||||
memory: 2G
|
memory: 512M
|
||||||
environment:
|
environment:
|
||||||
JAVA_TOOL_OPTIONS: "-Xmx2g -Xms1g -XX:MaxMetaspaceSize=512m -XX:+UseG1GC -XX:+HeapDumpOnOutOfMemoryError -XX:+UseContainerSupport -XX:InitialRAMPercentage=50 -XX:MaxRAMPercentage=75"
|
|
||||||
FILE_STORAGE_PATH: /data/uploads
|
FILE_STORAGE_PATH: /data/uploads
|
||||||
MAX_FILE_SIZE: 10737418240
|
MAX_FILE_SIZE: 10737418240
|
||||||
# FILE_CHUNK_SIZE: 1048576
|
# FILE_CHUNK_SIZE: 1048576
|
||||||
|
|||||||
@@ -17,7 +17,6 @@ pipeline {
|
|||||||
stages {
|
stages {
|
||||||
stage('Git pull') {
|
stage('Git pull') {
|
||||||
steps {
|
steps {
|
||||||
cleanWs()
|
|
||||||
script {
|
script {
|
||||||
checkout([
|
checkout([
|
||||||
$class: 'GitSCM',
|
$class: 'GitSCM',
|
||||||
@@ -31,6 +30,7 @@ pipeline {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
stage('Deploy with docker-compose') {
|
stage('Deploy with docker-compose') {
|
||||||
steps {
|
steps {
|
||||||
script {
|
script {
|
||||||
@@ -44,76 +44,68 @@ pipeline {
|
|||||||
string(credentialsId: 'DB_PASSWORD', variable: 'DB_PASSWORD')
|
string(credentialsId: 'DB_PASSWORD', variable: 'DB_PASSWORD')
|
||||||
]) {
|
]) {
|
||||||
sh """
|
sh """
|
||||||
echo "Deploying branch: ${params.BRANCH}"
|
echo "Deploying branch: ${params.BRANCH}"
|
||||||
|
|
||||||
echo "Copying files to server..."
|
echo "Copying files to server..."
|
||||||
sshpass -p '$SSH_PASS' ssh -o StrictHostKeyChecking=no $SSH_USER@$SERVER "mkdir -p /opt/deployments/${params.BRANCH}"
|
sshpass -p '$SSH_PASS' ssh -o StrictHostKeyChecking=no $SSH_USER@$SERVER "mkdir -p /opt/deployments/${params.BRANCH}"
|
||||||
|
sshpass -p '$SSH_PASS' scp -r -o StrictHostKeyChecking=no ./* $SSH_USER@$SERVER:/opt/deployments/${params.BRANCH}/
|
||||||
|
|
||||||
sshpass -p '$SSH_PASS' rsync -av --delete \\
|
sshpass -p '$SSH_PASS' ssh -o StrictHostKeyChecking=no $SSH_USER@$SERVER "
|
||||||
--exclude=.git \\
|
cd /opt/deployments/${params.BRANCH}
|
||||||
--exclude=.gradle \\
|
|
||||||
--exclude=build \\
|
|
||||||
--exclude=**/build \\
|
|
||||||
--exclude=.idea \\
|
|
||||||
--exclude=out \\
|
|
||||||
. $SSH_USER@$SERVER:/opt/deployments/${params.BRANCH}/
|
|
||||||
|
|
||||||
sshpass -p '$SSH_PASS' ssh -o StrictHostKeyChecking=no $SSH_USER@$SERVER "
|
echo '1. Остановка старого приложения...'
|
||||||
cd /opt/deployments/${params.BRANCH}
|
docker stop app-backend 2>/dev/null || echo 'Контейнер не найден'
|
||||||
|
docker rm app-backend 2>/dev/null || echo 'Контейнер не найден'
|
||||||
|
|
||||||
echo '1. Остановка старого приложения...'
|
echo '2. Удаление старых образов...'
|
||||||
docker stop app-backend 2>/dev/null || echo 'Контейнер не найден'
|
docker images --filter 'reference=*app*' -q | xargs -r docker rmi -f 2>/dev/null || echo 'Нет образов для удаления'
|
||||||
docker rm app-backend 2>/dev/null || echo 'Контейнер не найден'
|
|
||||||
|
|
||||||
echo '2. Удаление старых образов...'
|
echo '3. Создание сети если нужно...'
|
||||||
docker images --filter 'reference=*app*' -q | xargs -r docker rmi -f 2>/dev/null || echo 'Нет образов для удаления'
|
docker network create app-network 2>/dev/null || echo 'Сеть уже существует'
|
||||||
|
|
||||||
echo '3. Создание сети если нужно...'
|
echo '4. Запуск инфраструктуры...'
|
||||||
docker network create app-network 2>/dev/null || echo 'Сеть уже существует'
|
docker-compose up -d db storage
|
||||||
|
|
||||||
echo '4. Запуск инфраструктуры...'
|
echo '5. Ожидание PostgreSQL...'
|
||||||
docker-compose up -d db storage
|
sleep 10
|
||||||
|
|
||||||
echo '5. Ожидание PostgreSQL...'
|
echo '6. Сборка нового образа приложения...'
|
||||||
sleep 10
|
docker build --no-cache -t app-backend:latest .
|
||||||
|
|
||||||
echo '6. Сборка нового образа приложения...'
|
echo '7. Запуск приложения...'
|
||||||
docker build --no-cache -t app-backend:latest .
|
docker run -d \\
|
||||||
|
--name app-backend \\
|
||||||
|
--network app-network \\
|
||||||
|
--network-alias app \\
|
||||||
|
-p 80:8080 \\
|
||||||
|
-v uploads_data:/data/uploads:rw \\
|
||||||
|
-e POSTGRES_DB=no_copy_ \\
|
||||||
|
-e POSTGRES_USER=$DB_USER \\
|
||||||
|
-e POSTGRES_PASSWORD=$DB_PASSWORD \\
|
||||||
|
-e POSTGRES_PORT=5432 \\
|
||||||
|
-e POSTGRES_HOST=db \\
|
||||||
|
--restart unless-stopped \\
|
||||||
|
app-backend:latest
|
||||||
|
|
||||||
echo '7. Запуск приложения...'
|
echo '8. Запуск мониторинга...'
|
||||||
docker run -d \\
|
docker-compose up -d grafana prometheus loki tempo alloy
|
||||||
--name app-backend \\
|
|
||||||
--network app-network \\
|
|
||||||
--network-alias app \\
|
|
||||||
-p 80:8080 \\
|
|
||||||
-v uploads_data:/data/uploads:rw \\
|
|
||||||
-e POSTGRES_DB=no_copy_ \\
|
|
||||||
-e POSTGRES_USER=$DB_USER \\
|
|
||||||
-e POSTGRES_PASSWORD=$DB_PASSWORD \\
|
|
||||||
-e POSTGRES_PORT=5432 \\
|
|
||||||
-e POSTGRES_HOST=db \\
|
|
||||||
--restart unless-stopped \\
|
|
||||||
app-backend:latest
|
|
||||||
|
|
||||||
echo '8. Запуск мониторинга...'
|
echo '9. Проверка...'
|
||||||
docker-compose up -d grafana prometheus loki tempo alloy
|
sleep 5
|
||||||
|
|
||||||
echo '9. Проверка...'
|
echo 'Статус контейнеров:'
|
||||||
sleep 5
|
docker ps --format 'table {{.Names}}\\t{{.Image}}\\t{{.Status}}'
|
||||||
|
|
||||||
echo 'Статус контейнеров:'
|
echo '10. Проверка health...'
|
||||||
docker ps --format 'table {{.Names}}\\t{{.Image}}\\t{{.Status}}'
|
if curl -s -f http://localhost:80/health > /dev/null 2>&1; then
|
||||||
|
echo 'Приложение работает'
|
||||||
echo '10. Проверка health...'
|
echo 'URL: http://${params.SERVER}:80'
|
||||||
if curl -s -f http://localhost:80/health > /dev/null 2>&1; then
|
else
|
||||||
echo 'Приложение работает'
|
echo 'Проверка health не удалась'
|
||||||
echo 'URL: http://${params.SERVER}:80'
|
docker logs app-backend --tail=20
|
||||||
else
|
fi
|
||||||
echo 'Проверка health не удалась'
|
"
|
||||||
docker logs app-backend --tail=20
|
"""
|
||||||
fi
|
|
||||||
"
|
|
||||||
"""
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Binary file not shown.
@@ -1,26 +0,0 @@
|
|||||||
plugins {
|
|
||||||
kotlin("jvm") version "2.1.10"
|
|
||||||
}
|
|
||||||
|
|
||||||
group = "ru.soune"
|
|
||||||
version = "1.0.0"
|
|
||||||
|
|
||||||
repositories {
|
|
||||||
mavenCentral()
|
|
||||||
}
|
|
||||||
|
|
||||||
dependencies {
|
|
||||||
|
|
||||||
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.10.2")
|
|
||||||
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core-jvm:1.10.2")
|
|
||||||
|
|
||||||
implementation("io.insert-koin:koin-core:4.1.1")
|
|
||||||
implementation("io.insert-koin:koin-core-jvm:4.1.1")
|
|
||||||
}
|
|
||||||
|
|
||||||
tasks.test {
|
|
||||||
useJUnitPlatform()
|
|
||||||
}
|
|
||||||
kotlin {
|
|
||||||
jvmToolchain(21)
|
|
||||||
}
|
|
||||||
@@ -1,5 +0,0 @@
|
|||||||
package ru.soune
|
|
||||||
|
|
||||||
fun main() {
|
|
||||||
println("Hello World!")
|
|
||||||
}
|
|
||||||
@@ -1,6 +1 @@
|
|||||||
plugins {
|
|
||||||
id 'org.gradle.toolchains.foojay-resolver-convention' version '0.8.0'
|
|
||||||
}
|
|
||||||
rootProject.name = 'no-copy'
|
rootProject.name = 'no-copy'
|
||||||
include 'referral'
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,49 +1,17 @@
|
|||||||
package ru.soune.nocopy.configuration;
|
package ru.soune.nocopy.configuration;
|
||||||
|
|
||||||
import com.vrt.AudioFilePathProvider;
|
|
||||||
import com.vrt.fileprotection.FileProtector;
|
|
||||||
import com.vrt.fileprotection.audio.AudioLocalSearch;
|
|
||||||
import com.vrt.fileprotection.image.ImageLocalSearch;
|
|
||||||
import com.vrt.fileprotection.image.ImageUniqueCheck;
|
|
||||||
import com.vrt.fileprotection.image.phash.PerceptualHashHelper;
|
|
||||||
import lombok.AllArgsConstructor;
|
|
||||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
||||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||||
|
|
||||||
import java.util.Collections;
|
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
@EnableAutoConfiguration
|
@EnableAutoConfiguration
|
||||||
@AllArgsConstructor
|
|
||||||
public class ApplicationConfig {
|
public class ApplicationConfig {
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
PasswordEncoder passwordEncoder() {
|
PasswordEncoder passwordEncoder() {
|
||||||
return new BCryptPasswordEncoder();
|
return new BCryptPasswordEncoder();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
|
||||||
public com.vrt.
|
|
||||||
NoCopyFileService noCopyFileService(
|
|
||||||
FileProtector.FileProvider fileProvider,
|
|
||||||
FileProtector.ProcessingListener processingListener,
|
|
||||||
ImageUniqueCheck imageUniqueCheck,
|
|
||||||
ImageLocalSearch imageLocalSearch,
|
|
||||||
AudioLocalSearch audioLocalSearch,
|
|
||||||
AudioFilePathProvider audioFilePathProvider) {
|
|
||||||
|
|
||||||
return new com.vrt.NoCopyFileService(
|
|
||||||
Collections.emptyList(),
|
|
||||||
fileProvider,
|
|
||||||
processingListener,
|
|
||||||
imageUniqueCheck,
|
|
||||||
imageLocalSearch,
|
|
||||||
audioLocalSearch,
|
|
||||||
audioFilePathProvider
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -19,7 +19,8 @@ public class HandlerConfig {
|
|||||||
LogoutRequestHandler logoutHandler,
|
LogoutRequestHandler logoutHandler,
|
||||||
ImageFoundRequestHandler imageFoundRequestHandler,
|
ImageFoundRequestHandler imageFoundRequestHandler,
|
||||||
VerifyRegisterUserHandler verifyRegisterUser,
|
VerifyRegisterUserHandler verifyRegisterUser,
|
||||||
AuthRequestHandler authRequestHandler
|
AuthRequestHandler authRequestHandler,
|
||||||
|
ResetPasswordHandler resetPasswordHandler
|
||||||
) {
|
) {
|
||||||
Map<Integer, RequestHandler> map = new HashMap<>();
|
Map<Integer, RequestHandler> map = new HashMap<>();
|
||||||
map.put(20001, login);
|
map.put(20001, login);
|
||||||
@@ -30,6 +31,7 @@ public class HandlerConfig {
|
|||||||
map.put(20007, imageFoundRequestHandler);
|
map.put(20007, imageFoundRequestHandler);
|
||||||
map.put(20008, authRequestHandler);
|
map.put(20008, authRequestHandler);
|
||||||
map.put(20009, verifyRegisterUser);
|
map.put(20009, verifyRegisterUser);
|
||||||
|
map.put(20010, resetPasswordHandler);
|
||||||
|
|
||||||
return map;
|
return map;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,22 +0,0 @@
|
|||||||
package ru.soune.nocopy.configuration;
|
|
||||||
|
|
||||||
import org.springframework.context.annotation.Bean;
|
|
||||||
import org.springframework.context.annotation.Configuration;
|
|
||||||
import org.springframework.http.client.SimpleClientHttpRequestFactory;
|
|
||||||
import org.springframework.web.client.RestTemplate;
|
|
||||||
|
|
||||||
@Configuration
|
|
||||||
public class RestTemplateConfig {
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
public RestTemplate restTemplate() {
|
|
||||||
RestTemplate restTemplate = new RestTemplate();
|
|
||||||
|
|
||||||
SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
|
|
||||||
requestFactory.setConnectTimeout(30000);
|
|
||||||
requestFactory.setReadTimeout(60000);
|
|
||||||
restTemplate.setRequestFactory(requestFactory);
|
|
||||||
|
|
||||||
return restTemplate;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,19 +0,0 @@
|
|||||||
package ru.soune.nocopy.configuration.file;
|
|
||||||
|
|
||||||
import lombok.AllArgsConstructor;
|
|
||||||
import org.springframework.boot.context.event.ApplicationReadyEvent;
|
|
||||||
import org.springframework.context.event.EventListener;
|
|
||||||
import org.springframework.stereotype.Component;
|
|
||||||
import ru.soune.nocopy.service.file.FileProcessingOrchestrator;
|
|
||||||
|
|
||||||
@Component
|
|
||||||
@AllArgsConstructor
|
|
||||||
public class NoCopyInitializer {
|
|
||||||
|
|
||||||
private final FileProcessingOrchestrator orchestrator;
|
|
||||||
|
|
||||||
@EventListener(ApplicationReadyEvent.class)
|
|
||||||
public void initializeOnStartup() {
|
|
||||||
orchestrator.initializeProcessingQueue();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,15 +1,9 @@
|
|||||||
package ru.soune.nocopy.controller;
|
package ru.soune.nocopy.controller;
|
||||||
|
|
||||||
import com.vrt.NoCopyFileService;
|
|
||||||
import com.vrt.fileprotection.FileProtector;
|
|
||||||
import com.vrt.fileprotection.NoCopyCheckResult;
|
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.core.io.Resource;
|
import org.springframework.core.io.Resource;
|
||||||
import org.springframework.core.io.UrlResource;
|
import org.springframework.core.io.UrlResource;
|
||||||
import org.springframework.data.domain.Page;
|
|
||||||
import org.springframework.data.domain.Pageable;
|
|
||||||
import org.springframework.data.web.PageableDefault;
|
|
||||||
import org.springframework.http.HttpHeaders;
|
import org.springframework.http.HttpHeaders;
|
||||||
import org.springframework.http.MediaType;
|
import org.springframework.http.MediaType;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
@@ -20,22 +14,19 @@ import org.springframework.web.multipart.MultipartFile;
|
|||||||
import ru.soune.nocopy.dto.BaseRequest;
|
import ru.soune.nocopy.dto.BaseRequest;
|
||||||
import ru.soune.nocopy.dto.BaseResponse;
|
import ru.soune.nocopy.dto.BaseResponse;
|
||||||
import ru.soune.nocopy.dto.MessageCode;
|
import ru.soune.nocopy.dto.MessageCode;
|
||||||
import ru.soune.nocopy.dto.file.*;
|
|
||||||
import ru.soune.nocopy.dto.register.RegAnswer;
|
import ru.soune.nocopy.dto.register.RegAnswer;
|
||||||
import ru.soune.nocopy.entity.file.FileEntity;
|
import ru.soune.nocopy.dto.file.ChunkUploadResponse;
|
||||||
|
import ru.soune.nocopy.dto.file.CompleteUploadResponse;
|
||||||
|
import ru.soune.nocopy.dto.file.FileEntityResponse;
|
||||||
|
import ru.soune.nocopy.dto.file.UploadProgress;
|
||||||
import ru.soune.nocopy.entity.file.FileStatus;
|
import ru.soune.nocopy.entity.file.FileStatus;
|
||||||
import ru.soune.nocopy.entity.file.ProtectionStatus;
|
|
||||||
import ru.soune.nocopy.entity.file.UploadStatus;
|
import ru.soune.nocopy.entity.file.UploadStatus;
|
||||||
import ru.soune.nocopy.exception.*;
|
import ru.soune.nocopy.exception.*;
|
||||||
import ru.soune.nocopy.handler.*;
|
import ru.soune.nocopy.handler.*;
|
||||||
import ru.soune.nocopy.repository.FileEntityRepository;
|
|
||||||
import ru.soune.nocopy.service.FileSimilarityService;
|
|
||||||
import ru.soune.nocopy.service.register.AuthService;
|
import ru.soune.nocopy.service.register.AuthService;
|
||||||
import ru.soune.nocopy.service.file.FileEntityService;
|
import ru.soune.nocopy.service.file.FileEntityService;
|
||||||
import ru.soune.nocopy.service.file.FileUploadService;
|
import ru.soune.nocopy.service.file.FileUploadService;
|
||||||
import ru.soune.nocopy.util.FileUtil;
|
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
@@ -43,7 +34,6 @@ import java.nio.file.Paths;
|
|||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Optional;
|
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@@ -59,14 +49,6 @@ public class ApiController {
|
|||||||
|
|
||||||
private final AuthService authService;
|
private final AuthService authService;
|
||||||
|
|
||||||
private final FileSimilarityService fileSimilarityService;
|
|
||||||
|
|
||||||
private final FileEntityRepository fileEntityRepository;
|
|
||||||
|
|
||||||
private final NoCopyFileService noCopyFileService;
|
|
||||||
|
|
||||||
private final FileUtil fileUtil;
|
|
||||||
|
|
||||||
@PostMapping("/v{version}/data")
|
@PostMapping("/v{version}/data")
|
||||||
public ResponseEntity<?> handlePostRequest(@RequestBody BaseRequest request,
|
public ResponseEntity<?> handlePostRequest(@RequestBody BaseRequest request,
|
||||||
@PathVariable("version") int version) {
|
@PathVariable("version") int version) {
|
||||||
@@ -113,82 +95,57 @@ public class ApiController {
|
|||||||
@PathVariable("version") int version,
|
@PathVariable("version") int version,
|
||||||
@RequestParam(value = "upload_id", required = false) String uploadId,
|
@RequestParam(value = "upload_id", required = false) String uploadId,
|
||||||
@RequestParam(value = "chunk_number", required = false) Integer chunkNumber,
|
@RequestParam(value = "chunk_number", required = false) Integer chunkNumber,
|
||||||
@RequestParam(value = "chunk", required = false) MultipartFile chunk,
|
@RequestParam(value = "chunk", required = false) MultipartFile chunk) {
|
||||||
@RequestParam(value = "findSimilar", required = false, defaultValue = "0") Integer findSimilar) {
|
|
||||||
try {
|
try {
|
||||||
if (chunk == null || chunk.isEmpty()) {
|
if (chunk == null || chunk.isEmpty()) {
|
||||||
return buildErrorResponse(uploadId, chunkNumber, "Chunk file null or empty");
|
return ResponseEntity.ok().body(new BaseResponse(20004, MessageCode.FILE_UPLOAD_ERROR.getCode(),
|
||||||
|
"Chunk file null or empty", ChunkUploadResponse.builder()
|
||||||
|
.uploadId(uploadId)
|
||||||
|
.chunkNumber(chunkNumber)
|
||||||
|
.build()));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (uploadId == null || uploadId.isBlank()) {
|
if (uploadId == null || uploadId.isBlank()) {
|
||||||
return buildErrorResponse(uploadId, chunkNumber, "Upload ID is required");
|
return ResponseEntity.ok().body(new BaseResponse(20004, MessageCode.FILE_UPLOAD_ERROR.getCode(),
|
||||||
|
"Upload ID is required", ChunkUploadResponse.builder()
|
||||||
|
.uploadId(uploadId)
|
||||||
|
.chunkNumber(chunkNumber)
|
||||||
|
.build()));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (chunkNumber == null || chunkNumber < 0) {
|
if (chunkNumber == null || chunkNumber < 0) {
|
||||||
return buildErrorResponse(uploadId, chunkNumber, "Valid chunk number is required");
|
return ResponseEntity.ok().body(new BaseResponse(20004, MessageCode.FILE_UPLOAD_ERROR.getCode(),
|
||||||
|
"Valid chunk number is required", ChunkUploadResponse.builder()
|
||||||
|
.uploadId(uploadId)
|
||||||
|
.chunkNumber(chunkNumber)
|
||||||
|
.build()));
|
||||||
}
|
}
|
||||||
|
|
||||||
UploadProgressResponse uploadProgressResponse = fileUploadService.uploadChunk(uploadId, chunkNumber,
|
fileUploadService.uploadChunk(uploadId, chunkNumber, chunk);
|
||||||
chunk, findSimilar);
|
|
||||||
|
|
||||||
return buildSuccessResponse(uploadId, chunkNumber, chunk,
|
ChunkUploadResponse responseBody = ChunkUploadResponse.builder()
|
||||||
fileEntityService.findFileIdByPath(uploadProgressResponse.getFilePath()));
|
.uploadId(uploadId)
|
||||||
} catch (DuplicateImageException e) {
|
.chunkNumber(chunkNumber)
|
||||||
Map<String, Object> duplicateData = new HashMap<>();
|
.chunkSize(chunk.getSize())
|
||||||
duplicateData.put("duplicateFileId", e.duplicateFileId());
|
.message("Chunk uploaded successfully")
|
||||||
duplicateData.put("userId", e.userId());
|
.build();
|
||||||
duplicateData.put("message", e.getMessage());
|
|
||||||
|
|
||||||
if (uploadId != null) {
|
return ResponseEntity.ok().body(new BaseResponse(20004, MessageCode.SUCCESS.getCode(),
|
||||||
duplicateData.put("uploadId", uploadId);
|
"Chunk uploaded successfully", responseBody));
|
||||||
}
|
|
||||||
|
|
||||||
return ResponseEntity.ok().body(new BaseResponse(
|
|
||||||
20004,
|
|
||||||
MessageCode.DUPLICATE_FILE_UPLOAD.getCode(),
|
|
||||||
MessageCode.DUPLICATE_FILE_UPLOAD.getDescription(),
|
|
||||||
duplicateData
|
|
||||||
));
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("Error uploading chunk", e);
|
log.error("Error uploading chunk", e);
|
||||||
return buildErrorResponse(uploadId, chunkNumber, "Failed to upload chunk: " + e.getMessage());
|
|
||||||
|
ChunkUploadResponse responseBody = ChunkUploadResponse.builder()
|
||||||
|
.uploadId(uploadId)
|
||||||
|
.chunkNumber(chunkNumber)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
return ResponseEntity.ok().body(new BaseResponse(
|
||||||
|
20004, MessageCode.FILE_UPLOAD_ERROR.getCode(),
|
||||||
|
"Failed to upload chunk: " + e.getMessage(), responseBody));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/v{version}/files/{fileId}/similar")
|
|
||||||
public ResponseEntity<BaseResponse> findSimilarFiles(
|
|
||||||
@PathVariable("version") int version,
|
|
||||||
@PathVariable String fileId,
|
|
||||||
@RequestParam(value = "auth_token",required = false) String authToken,
|
|
||||||
@RequestParam(required = false) List<String> similarityLevels,
|
|
||||||
@PageableDefault(size = 20, sort = "hammingDistance") Pageable pageable) {
|
|
||||||
SimilarityFilter filter = SimilarityFilter.builder()
|
|
||||||
.similarityLevels(similarityLevels)
|
|
||||||
.build();
|
|
||||||
Page<SimilarFileDTO> similarFiles = fileSimilarityService.findSimilarFiles(fileId, filter, pageable, authToken);
|
|
||||||
|
|
||||||
String messageDesc;
|
|
||||||
MessageCode success;
|
|
||||||
|
|
||||||
if (similarFiles.isEmpty()) {
|
|
||||||
messageDesc = MessageCode.FILE_NOT_FOUND.getDescription();
|
|
||||||
success = MessageCode.SUCCESS;
|
|
||||||
} else {
|
|
||||||
messageDesc = MessageCode.SIMILAR_FILES_FOUND.getDescription();
|
|
||||||
success = MessageCode.SUCCESS;
|
|
||||||
}
|
|
||||||
|
|
||||||
Map<String, Object> responseData = new HashMap<>();
|
|
||||||
responseData.put("content", similarFiles.getContent());
|
|
||||||
responseData.put("page", similarFiles.getNumber());
|
|
||||||
responseData.put("size", similarFiles.getSize());
|
|
||||||
responseData.put("totalElements", similarFiles.getTotalElements());
|
|
||||||
responseData.put("totalPages", similarFiles.getTotalPages());
|
|
||||||
responseData.put("hasNext", similarFiles.hasNext());
|
|
||||||
responseData.put("hasPrevious", similarFiles.hasPrevious());
|
|
||||||
|
|
||||||
return ResponseEntity.ok()
|
|
||||||
.body(new BaseResponse(20004, success.getCode(), messageDesc, responseData));
|
|
||||||
}
|
|
||||||
|
|
||||||
@GetMapping("/v{version}/files/progress/{uploadId}")
|
@GetMapping("/v{version}/files/progress/{uploadId}")
|
||||||
public ResponseEntity<BaseResponse> getUploadProgress(
|
public ResponseEntity<BaseResponse> getUploadProgress(
|
||||||
@PathVariable("version") int version,
|
@PathVariable("version") int version,
|
||||||
@@ -212,6 +169,7 @@ public class ApiController {
|
|||||||
|
|
||||||
return ResponseEntity.ok().body(new BaseResponse(
|
return ResponseEntity.ok().body(new BaseResponse(
|
||||||
20004, MessageCode.SUCCESS.getCode(), MessageCode.SUCCESS.getDescription(), responseBody));
|
20004, MessageCode.SUCCESS.getCode(), MessageCode.SUCCESS.getDescription(), responseBody));
|
||||||
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("Error getting progress for upload: {}", uploadId, e);
|
log.error("Error getting progress for upload: {}", uploadId, e);
|
||||||
|
|
||||||
@@ -379,119 +337,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 = type.toLowerCase().equals("image") ? Paths.get(fileEntity.getFilePath()) :
|
|
||||||
Paths.get(fileEntity.getProtectedFilePath());
|
|
||||||
|
|
||||||
File file = path.toFile();
|
|
||||||
|
|
||||||
NoCopyCheckResult noCopyCheckResult = noCopyFileService.checkFile(file,
|
|
||||||
FileProtector.Type.valueOf(type.toUpperCase()));
|
|
||||||
|
|
||||||
return ResponseEntity.ok().body(noCopyCheckResult);
|
|
||||||
}
|
|
||||||
|
|
||||||
private ResponseEntity<BaseResponse> checkForDuplicates(String uploadId) throws IOException {
|
|
||||||
Optional<FileEntity> uploadedFile = fileEntityRepository.findByUploadSessionId(uploadId);
|
|
||||||
|
|
||||||
if (uploadedFile.isEmpty() || !uploadedFile.get().getMimeType().equals("image")) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
FileEntity fileEntity = uploadedFile.get();
|
|
||||||
List<SimilarFileDTO> similarFiles = fileSimilarityService.findSimilarFiles(fileEntity.getId());
|
|
||||||
|
|
||||||
if (hasDuplicate(similarFiles)) {
|
|
||||||
return handleDuplicate(fileEntity, similarFiles);
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
private boolean hasDuplicate(List<SimilarFileDTO> similarFiles) {
|
|
||||||
return similarFiles.stream().anyMatch(f -> f.getHammingDistance() <= 5);
|
|
||||||
}
|
|
||||||
|
|
||||||
private ResponseEntity<BaseResponse> handleDuplicate(FileEntity fileEntity, List<SimilarFileDTO> similarFiles)
|
|
||||||
throws IOException {
|
|
||||||
fileEntityService.deleteFromDisk(fileEntity);
|
|
||||||
|
|
||||||
fileEntityService.softDeleteFileWithHash(fileEntity);
|
|
||||||
|
|
||||||
Optional<FileEntity> originalFile = fileEntityRepository.findById(similarFiles.get(0).getFileId());
|
|
||||||
|
|
||||||
if (originalFile.isPresent()) {
|
|
||||||
Map<String, String> duplicateInfo = Map.of(
|
|
||||||
"duplicate_file_id", originalFile.get().getId(),
|
|
||||||
"owner_user_id", String.valueOf(originalFile.get().getUserId()));
|
|
||||||
|
|
||||||
return ResponseEntity.ok().body(new BaseResponse(
|
|
||||||
20004,
|
|
||||||
MessageCode.DUPLICATE_FILE_UPLOAD.getCode(),
|
|
||||||
"Failed to upload chunk, duplicate",
|
|
||||||
duplicateInfo));
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
private ResponseEntity<BaseResponse> buildSuccessResponse(String uploadId, Integer chunkNumber, MultipartFile chunk,
|
|
||||||
String fileId) {
|
|
||||||
ChunkUploadResponse responseBody = ChunkUploadResponse.builder()
|
|
||||||
.uploadId(uploadId)
|
|
||||||
.chunkNumber(chunkNumber)
|
|
||||||
.chunkSize(chunk.getSize())
|
|
||||||
.fileId(fileId)
|
|
||||||
.message("Chunk uploaded successfully")
|
|
||||||
.build();
|
|
||||||
|
|
||||||
return ResponseEntity.ok().body(new BaseResponse(
|
|
||||||
20000,
|
|
||||||
MessageCode.SUCCESS.getCode(),
|
|
||||||
"Chunk uploaded successfully",
|
|
||||||
responseBody));
|
|
||||||
}
|
|
||||||
|
|
||||||
private ResponseEntity<BaseResponse> buildErrorResponse(String uploadId, Integer chunkNumber, String errorMessage) {
|
|
||||||
ChunkUploadResponse responseBody = ChunkUploadResponse.builder()
|
|
||||||
.uploadId(uploadId)
|
|
||||||
.chunkNumber(chunkNumber)
|
|
||||||
.build();
|
|
||||||
|
|
||||||
return ResponseEntity.ok().body(new BaseResponse(
|
|
||||||
20004,
|
|
||||||
MessageCode.FILE_UPLOAD_ERROR.getCode(),
|
|
||||||
errorMessage,
|
|
||||||
responseBody));
|
|
||||||
}
|
|
||||||
|
|
||||||
private ResponseEntity<BaseResponse> createValidationErrorResponse(BindingResult bindingResult, Integer msgId) {
|
private ResponseEntity<BaseResponse> createValidationErrorResponse(BindingResult bindingResult, Integer msgId) {
|
||||||
List<Map<String, String>> fieldErrors = bindingResult.getFieldErrors()
|
List<Map<String, String>> fieldErrors = bindingResult.getFieldErrors()
|
||||||
.stream()
|
.stream()
|
||||||
@@ -527,4 +372,16 @@ public class ApiController {
|
|||||||
}
|
}
|
||||||
return contentType;
|
return contentType;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private boolean isPreviewSupported(String mimeType) {
|
||||||
|
if (mimeType == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return mimeType.startsWith("image") ||
|
||||||
|
mimeType.startsWith("text") ||
|
||||||
|
mimeType.equals("pdf") ||
|
||||||
|
mimeType.startsWith("video") ||
|
||||||
|
mimeType.startsWith("audio");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -3,9 +3,6 @@ package ru.soune.nocopy.controller;
|
|||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("check/api")
|
@RequestMapping("check/api")
|
||||||
public class HealtCheckController {
|
public class HealtCheckController {
|
||||||
@@ -14,22 +11,4 @@ public class HealtCheckController {
|
|||||||
public HttpStatus healtCheck() {
|
public HttpStatus healtCheck() {
|
||||||
return HttpStatus.OK;
|
return HttpStatus.OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/api/debug/memory")
|
|
||||||
public Map<String, String> getMemoryInfo() {
|
|
||||||
Runtime runtime = Runtime.getRuntime();
|
|
||||||
long mb = 1024 * 1024;
|
|
||||||
|
|
||||||
Map<String, String> info = new HashMap<>();
|
|
||||||
info.put("maxMemory (MB)", String.valueOf(runtime.maxMemory() / mb));
|
|
||||||
info.put("totalMemory (MB)", String.valueOf(runtime.totalMemory() / mb));
|
|
||||||
info.put("freeMemory (MB)", String.valueOf(runtime.freeMemory() / mb));
|
|
||||||
info.put("usedMemory (MB)", String.valueOf((runtime.totalMemory() - runtime.freeMemory()) / mb));
|
|
||||||
info.put("availableProcessors", String.valueOf(runtime.availableProcessors()));
|
|
||||||
|
|
||||||
info.put("JAVA_TOOL_OPTIONS", System.getenv("JAVA_TOOL_OPTIONS"));
|
|
||||||
info.put("JAVA_OPTS", System.getenv("JAVA_OPTS"));
|
|
||||||
|
|
||||||
return info;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,11 @@
|
|||||||
|
package ru.soune.nocopy.controller;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("v1/api/content")
|
||||||
|
@AllArgsConstructor
|
||||||
|
public class UserContentController {
|
||||||
|
}
|
||||||
@@ -2,29 +2,20 @@ package ru.soune.nocopy.controller;
|
|||||||
|
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
import ru.soune.nocopy.dto.BaseResponse;
|
|
||||||
import ru.soune.nocopy.dto.MessageCode;
|
|
||||||
import ru.soune.nocopy.dto.register.ChangePasswordRequest;
|
import ru.soune.nocopy.dto.register.ChangePasswordRequest;
|
||||||
import ru.soune.nocopy.dto.register.RegAnswer;
|
|
||||||
import ru.soune.nocopy.dto.register.RegRequest;
|
|
||||||
import ru.soune.nocopy.dto.user.UserDTO;
|
import ru.soune.nocopy.dto.user.UserDTO;
|
||||||
import ru.soune.nocopy.dto.user.UserRequest;
|
import ru.soune.nocopy.dto.user.UserRequest;
|
||||||
import ru.soune.nocopy.entity.user.AuthToken;
|
import ru.soune.nocopy.entity.user.AuthToken;
|
||||||
import ru.soune.nocopy.entity.user.User;
|
import ru.soune.nocopy.entity.user.User;
|
||||||
import ru.soune.nocopy.exception.InvalidUserEmail;
|
import ru.soune.nocopy.exception.InvalidUserEmail;
|
||||||
import ru.soune.nocopy.exception.NotFoundAuthToken;
|
import ru.soune.nocopy.exception.NotFoundAuthToken;
|
||||||
import ru.soune.nocopy.exception.NotValidFieldException;
|
|
||||||
import ru.soune.nocopy.mapper.UserMapper;
|
import ru.soune.nocopy.mapper.UserMapper;
|
||||||
import ru.soune.nocopy.repository.AuthTokenRepository;
|
import ru.soune.nocopy.repository.AuthTokenRepository;
|
||||||
import ru.soune.nocopy.repository.UserRepository;
|
import ru.soune.nocopy.repository.UserRepository;
|
||||||
import ru.soune.nocopy.service.register.AuthService;
|
|
||||||
import ru.soune.nocopy.service.user.UserService;
|
import ru.soune.nocopy.service.user.UserService;
|
||||||
|
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@@ -40,10 +31,6 @@ public class UserController {
|
|||||||
|
|
||||||
private final UserMapper userMapper;
|
private final UserMapper userMapper;
|
||||||
|
|
||||||
private final PasswordEncoder passwordEncoder;
|
|
||||||
|
|
||||||
private final AuthService authService;
|
|
||||||
|
|
||||||
@GetMapping("/all")
|
@GetMapping("/all")
|
||||||
public ResponseEntity<List<UserDTO>> getAllUsers() {
|
public ResponseEntity<List<UserDTO>> getAllUsers() {
|
||||||
List<UserDTO> allUsers = userRepository.findAll().stream()
|
List<UserDTO> allUsers = userRepository.findAll().stream()
|
||||||
@@ -121,50 +108,4 @@ public class UserController {
|
|||||||
|
|
||||||
return ResponseEntity.ok(userService.updateUser(userRequest, user));
|
return ResponseEntity.ok(userService.updateUser(userRequest, user));
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping("/create-user")
|
|
||||||
public ResponseEntity<User> addUser(@RequestBody RegRequest registerRequest) {
|
|
||||||
|
|
||||||
if (userRepository.existsByEmail(registerRequest.getEmail()) ||
|
|
||||||
userRepository.existsByPhone(registerRequest.getPhone())) {
|
|
||||||
RegAnswer regAnswer = new RegAnswer();
|
|
||||||
regAnswer.setFieldErrors(Arrays.asList(Map.of("email", registerRequest.getEmail())));
|
|
||||||
regAnswer.setFieldErrors(Arrays.asList(Map.of("phone", registerRequest.getPhone())));
|
|
||||||
|
|
||||||
throw new NotValidFieldException("User already exists with email:" + registerRequest.getEmail() + " or phone: " +
|
|
||||||
registerRequest.getPhone(), new BaseResponse(2,
|
|
||||||
MessageCode.REG_EMAIL_OR_PHONE_EXISTS.getCode(),
|
|
||||||
MessageCode.REG_EMAIL_OR_PHONE_EXISTS.getDescription(), regAnswer));
|
|
||||||
}
|
|
||||||
|
|
||||||
User user = new User();
|
|
||||||
user.setFullName(registerRequest.getFullName());
|
|
||||||
user.setEmail(registerRequest.getEmail());
|
|
||||||
user.setPassword(passwordEncoder.encode(registerRequest.getPassword()));
|
|
||||||
user.setActive(true);
|
|
||||||
user.setEmailVerified(true);
|
|
||||||
|
|
||||||
if (registerRequest.getCompanyName() != null) {
|
|
||||||
user.setCompany(registerRequest.getCompanyName());
|
|
||||||
}
|
|
||||||
|
|
||||||
if (registerRequest.getPhone() != null) {
|
|
||||||
user.setPhone(registerRequest.getPhone());
|
|
||||||
}
|
|
||||||
|
|
||||||
User savedUser = userRepository.save(user);
|
|
||||||
|
|
||||||
AuthToken authToken = authService.generateAuthToken(savedUser);
|
|
||||||
|
|
||||||
authTokenRepository.save(authToken);
|
|
||||||
|
|
||||||
return ResponseEntity.ok().body(savedUser);
|
|
||||||
}
|
|
||||||
|
|
||||||
@DeleteMapping("/delete-user/{userId}")
|
|
||||||
public ResponseEntity<?> addUser(@PathVariable Long userId) {
|
|
||||||
userRepository.deleteById(userId);
|
|
||||||
return ResponseEntity.ok().body(Map.of("userId", userId, "deleted", "true"));
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,7 +9,6 @@ public enum MessageCode {
|
|||||||
TOKEN_IS_ALIVE(2, "Token is alive"),
|
TOKEN_IS_ALIVE(2, "Token is alive"),
|
||||||
INVALID_ACTION(2, "Invalid action"),
|
INVALID_ACTION(2, "Invalid action"),
|
||||||
FILE_UPLOAD_ERROR(2, "File upload error"),
|
FILE_UPLOAD_ERROR(2, "File upload error"),
|
||||||
DUPLICATE_FILE_UPLOAD(2, "Duplicate file upload"),
|
|
||||||
FILE_DOWNLOAD_ERROR(2, "File download error"),
|
FILE_DOWNLOAD_ERROR(2, "File download error"),
|
||||||
USER_NOT_VERIFIED(2, "User not verified"),
|
USER_NOT_VERIFIED(2, "User not verified"),
|
||||||
USER_NOT_FOUND(2, "User not found"),
|
USER_NOT_FOUND(2, "User not found"),
|
||||||
@@ -26,8 +25,7 @@ public enum MessageCode {
|
|||||||
AUTH_TOKEN_NOT_FOUND(4, "Token not found"),
|
AUTH_TOKEN_NOT_FOUND(4, "Token not found"),
|
||||||
FILE_NOT_FOUND(4, "File not found"),
|
FILE_NOT_FOUND(4, "File not found"),
|
||||||
AUTH_PASSWORD_NOT_MATCHES(2, "Password does not match"),
|
AUTH_PASSWORD_NOT_MATCHES(2, "Password does not match"),
|
||||||
SEND_EMAIL_EXCEPTION(2, "Send email exception"),
|
USER_NOT_ACTIVE(2, "User not active");
|
||||||
SIMILAR_FILES_FOUND(0, "Similar files found");
|
|
||||||
|
|
||||||
private final Integer code;
|
private final Integer code;
|
||||||
|
|
||||||
|
|||||||
@@ -20,9 +20,6 @@ public class ChunkUploadResponse {
|
|||||||
@JsonProperty("chunk_size")
|
@JsonProperty("chunk_size")
|
||||||
private Long chunkSize;
|
private Long chunkSize;
|
||||||
|
|
||||||
@JsonProperty("file_id")
|
|
||||||
private String fileId;
|
|
||||||
|
|
||||||
@JsonProperty("message")
|
@JsonProperty("message")
|
||||||
private String message;
|
private String message;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,9 +17,6 @@ public class FileEntityRequest {
|
|||||||
@JsonProperty("file_id")
|
@JsonProperty("file_id")
|
||||||
private String fileId;
|
private String fileId;
|
||||||
|
|
||||||
@JsonProperty("full_delete")
|
|
||||||
private Integer fullDelete;
|
|
||||||
|
|
||||||
@JsonProperty("upload_session_id")
|
@JsonProperty("upload_session_id")
|
||||||
private String uploadSessionId;
|
private String uploadSessionId;
|
||||||
|
|
||||||
|
|||||||
@@ -31,5 +31,4 @@ public class FileEntityResponse {
|
|||||||
private String downloadUrl;
|
private String downloadUrl;
|
||||||
private boolean existsOnDisk;
|
private boolean existsOnDisk;
|
||||||
private Integer supportId;
|
private Integer supportId;
|
||||||
private String protectStatus;
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -58,16 +58,4 @@ public class FileInfoUserResponse {
|
|||||||
|
|
||||||
@JsonProperty("audios_violations")
|
@JsonProperty("audios_violations")
|
||||||
private Integer audiosViolations;
|
private Integer audiosViolations;
|
||||||
|
|
||||||
@JsonProperty("protected_files_count")
|
|
||||||
private Long protectedFilesCount;
|
|
||||||
|
|
||||||
@JsonProperty("protected_audio_files_count")
|
|
||||||
private Long protectedAudioFilesCount;
|
|
||||||
|
|
||||||
@JsonProperty("protected_video_files_count")
|
|
||||||
private Long protectedVideoFilesCount;
|
|
||||||
|
|
||||||
@JsonProperty("protected_image_files_count")
|
|
||||||
private Long protectedImageFilesCount;
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,68 +0,0 @@
|
|||||||
package ru.soune.nocopy.dto.file;
|
|
||||||
|
|
||||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
|
||||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
|
||||||
import lombok.Data;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
@Data
|
|
||||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
|
||||||
public class GoogleVisionSearchResponse {
|
|
||||||
|
|
||||||
@JsonProperty("bestGuessLabels")
|
|
||||||
private List<BestGuessLabel> bestGuessLabels;
|
|
||||||
|
|
||||||
@JsonProperty("fullMatchingImages")
|
|
||||||
private List<ImageResult> fullMatchingImages;
|
|
||||||
|
|
||||||
@JsonProperty("visuallySimilarImages")
|
|
||||||
private List<ImageResult> visuallySimilarImages;
|
|
||||||
|
|
||||||
@JsonProperty("pagesWithMatchingImages")
|
|
||||||
private List<PageResult> pagesWithMatchingImages;
|
|
||||||
|
|
||||||
@JsonProperty("partialMatchingImages")
|
|
||||||
private List<ImageResult> partialMatchingImages;
|
|
||||||
|
|
||||||
@Data
|
|
||||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
|
||||||
public static class BestGuessLabel {
|
|
||||||
@JsonProperty("label")
|
|
||||||
private String label;
|
|
||||||
|
|
||||||
@JsonProperty("languageCode")
|
|
||||||
private String languageCode;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Data
|
|
||||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
|
||||||
public static class ImageResult {
|
|
||||||
@JsonProperty("url")
|
|
||||||
private String url;
|
|
||||||
|
|
||||||
@JsonProperty("score")
|
|
||||||
private Float score;
|
|
||||||
|
|
||||||
@JsonProperty("height")
|
|
||||||
private Integer height;
|
|
||||||
|
|
||||||
@JsonProperty("width")
|
|
||||||
private Integer width;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Data
|
|
||||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
|
||||||
public static class PageResult {
|
|
||||||
@JsonProperty("url")
|
|
||||||
private String url;
|
|
||||||
|
|
||||||
@JsonProperty("pageTitle")
|
|
||||||
private String pageTitle;
|
|
||||||
|
|
||||||
@JsonProperty("fullMatchingImages")
|
|
||||||
private List<ImageResult> fullMatchingImages;
|
|
||||||
|
|
||||||
@JsonProperty("partialMatchingImages")
|
|
||||||
private List<ImageResult> partialMatchingImages;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,15 +0,0 @@
|
|||||||
package ru.soune.nocopy.dto.file;
|
|
||||||
|
|
||||||
import lombok.Builder;
|
|
||||||
import lombok.Value;
|
|
||||||
|
|
||||||
@Value
|
|
||||||
@Builder
|
|
||||||
public class SimilarFileDTO {
|
|
||||||
String fileId;
|
|
||||||
String originalFileName;
|
|
||||||
Long fileSize;
|
|
||||||
Integer hammingDistance;
|
|
||||||
String similarityLevel;
|
|
||||||
Long ownerId;
|
|
||||||
}
|
|
||||||
@@ -1,18 +0,0 @@
|
|||||||
package ru.soune.nocopy.dto.file;
|
|
||||||
|
|
||||||
import lombok.AllArgsConstructor;
|
|
||||||
import lombok.Builder;
|
|
||||||
import lombok.Data;
|
|
||||||
import lombok.NoArgsConstructor;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
@Data
|
|
||||||
@Builder
|
|
||||||
@NoArgsConstructor
|
|
||||||
@AllArgsConstructor
|
|
||||||
public class SimilarityFilter {
|
|
||||||
private List<String> similarityLevels;
|
|
||||||
private Integer page;
|
|
||||||
private Integer size;
|
|
||||||
}
|
|
||||||
@@ -17,9 +17,5 @@ public class RegAnswer {
|
|||||||
|
|
||||||
private boolean isVerified;
|
private boolean isVerified;
|
||||||
|
|
||||||
private Long userId;
|
|
||||||
|
|
||||||
private String email;
|
|
||||||
|
|
||||||
private List<Map<String, String>> fieldErrors;
|
private List<Map<String, String>> fieldErrors;
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
package ru.soune.nocopy.dto.register;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@AllArgsConstructor
|
||||||
|
@NoArgsConstructor
|
||||||
|
public class ResetPasswordRequest {
|
||||||
|
|
||||||
|
@JsonProperty("email")
|
||||||
|
private String email;
|
||||||
|
|
||||||
|
@JsonProperty("verifyToken")
|
||||||
|
private String verifyToken;
|
||||||
|
|
||||||
|
@JsonProperty("action")
|
||||||
|
private String action;
|
||||||
|
|
||||||
|
@JsonProperty("password")
|
||||||
|
private String password;
|
||||||
|
}
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
package ru.soune.nocopy.dto.register;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public class ResetPasswordResponse {
|
||||||
|
@JsonProperty("user_id")
|
||||||
|
private Long userId;
|
||||||
|
|
||||||
|
@JsonProperty("authToken")
|
||||||
|
private String authToken;
|
||||||
|
}
|
||||||
@@ -1,6 +1,5 @@
|
|||||||
package ru.soune.nocopy.entity.file;
|
package ru.soune.nocopy.entity.file;
|
||||||
|
|
||||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
|
||||||
import jakarta.persistence.*;
|
import jakarta.persistence.*;
|
||||||
import lombok.*;
|
import lombok.*;
|
||||||
import org.hibernate.annotations.GenerationTime;
|
import org.hibernate.annotations.GenerationTime;
|
||||||
@@ -66,42 +65,14 @@ public class FileEntity {
|
|||||||
@Column(name = "updated_at")
|
@Column(name = "updated_at")
|
||||||
private LocalDateTime updatedAt;
|
private LocalDateTime updatedAt;
|
||||||
|
|
||||||
@Column(name = "protected_file_path")
|
|
||||||
private String protectedFilePath;
|
|
||||||
|
|
||||||
@Column(name = "protection_status")
|
|
||||||
@Enumerated(EnumType.STRING)
|
|
||||||
private ProtectionStatus protectionStatus;
|
|
||||||
|
|
||||||
@Column(name = "protected_at")
|
|
||||||
private LocalDateTime protectedAt;
|
|
||||||
|
|
||||||
@Column(name = "signature")
|
|
||||||
@JsonIgnore
|
|
||||||
private String signature;
|
|
||||||
|
|
||||||
@OneToOne(mappedBy = "file", cascade = CascadeType.ALL, orphanRemoval = true)
|
|
||||||
@JsonIgnore
|
|
||||||
private ImageHashEntity imageHash;
|
|
||||||
|
|
||||||
@PrePersist
|
@PrePersist
|
||||||
public void prePersist() {
|
public void prePersist() {
|
||||||
if (this.status == null) {
|
if (this.status == null) {
|
||||||
this.status = FileStatus.ACTIVE;
|
this.status = FileStatus.ACTIVE;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.protectionStatus == null) {
|
|
||||||
this.protectionStatus = ProtectionStatus.NOT_PROTECTED;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (this.createdAt == null) {
|
if (this.createdAt == null) {
|
||||||
this.createdAt = LocalDateTime.now();
|
this.createdAt = LocalDateTime.now();
|
||||||
}
|
}
|
||||||
this.updatedAt = LocalDateTime.now();
|
this.updatedAt = LocalDateTime.now();
|
||||||
}
|
}
|
||||||
|
|
||||||
@PreUpdate
|
|
||||||
public void preUpdate() {
|
|
||||||
this.updatedAt = LocalDateTime.now();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,11 +7,10 @@ import java.util.List;
|
|||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
public enum FileType {
|
public enum FileType {
|
||||||
// IMAGE("image", Arrays.asList("jpg", "jpeg", "png", "gif", "bmp", "webp", "jfif")),
|
IMAGE("image", Arrays.asList("jpg", "jpeg", "png", "gif", "bmp", "webp", "jfif")),
|
||||||
IMAGE("image", Arrays.asList("jpg", "jpeg", "png", "gif", "bmp")),
|
|
||||||
VIDEO("video", Arrays.asList("mp4", "avi", "mov", "wmv", "flv", "mkv", "webm", "m4v", "mpg", "mpeg",
|
VIDEO("video", Arrays.asList("mp4", "avi", "mov", "wmv", "flv", "mkv", "webm", "m4v", "mpg", "mpeg",
|
||||||
"3gp", "3g2", "f4v", "m2ts", "mts", "vob", "ogv", "divx")),
|
"3gp", "3g2", "f4v", "m2ts", "mts", "vob", "ogv", "divx")),
|
||||||
AUDIO("audio", List.of("wav"));
|
AUDIO("audio", Arrays.asList("mp3", "wav", "flac"));
|
||||||
|
|
||||||
private final String displayName;
|
private final String displayName;
|
||||||
private final List<String> allowedExtensions;
|
private final List<String> allowedExtensions;
|
||||||
|
|||||||
@@ -69,7 +69,6 @@ public class FileUploadSession {
|
|||||||
private String extension;
|
private String extension;
|
||||||
|
|
||||||
@Column(name = "retry_count")
|
@Column(name = "retry_count")
|
||||||
@Builder.Default
|
|
||||||
private Integer retryCount = 0;
|
private Integer retryCount = 0;
|
||||||
|
|
||||||
@Column(name = "completed_at")
|
@Column(name = "completed_at")
|
||||||
@@ -82,7 +81,6 @@ public class FileUploadSession {
|
|||||||
)
|
)
|
||||||
@MapKeyColumn(name = "chunk_number")
|
@MapKeyColumn(name = "chunk_number")
|
||||||
@Column(name = "chunk_path")
|
@Column(name = "chunk_path")
|
||||||
@Builder.Default
|
|
||||||
private Map<Integer, String> chunkPaths = new HashMap<>();
|
private Map<Integer, String> chunkPaths = new HashMap<>();
|
||||||
|
|
||||||
@PrePersist
|
@PrePersist
|
||||||
|
|||||||
@@ -1,43 +0,0 @@
|
|||||||
package ru.soune.nocopy.entity.file;
|
|
||||||
|
|
||||||
import jakarta.persistence.*;
|
|
||||||
import lombok.AllArgsConstructor;
|
|
||||||
import lombok.Builder;
|
|
||||||
import lombok.Data;
|
|
||||||
import lombok.NoArgsConstructor;
|
|
||||||
|
|
||||||
import java.time.LocalDateTime;
|
|
||||||
|
|
||||||
@Entity
|
|
||||||
@Table(name = "image_hashes", indexes = {
|
|
||||||
@Index(name = "idx_image_hashes_hash64", columnList = "hash64_hi, hash64_lo"),
|
|
||||||
@Index(name = "idx_image_hashes_hash64_hi", columnList = "hash64_hi"),
|
|
||||||
@Index(name = "idx_image_hashes_hash64_lo", columnList = "hash64_lo")
|
|
||||||
})
|
|
||||||
@Data
|
|
||||||
@NoArgsConstructor
|
|
||||||
@AllArgsConstructor
|
|
||||||
@Builder
|
|
||||||
public class ImageHashEntity {
|
|
||||||
|
|
||||||
@Id
|
|
||||||
@Column(name = "file_id")
|
|
||||||
private String fileId;
|
|
||||||
|
|
||||||
@OneToOne(fetch = FetchType.LAZY)
|
|
||||||
@MapsId
|
|
||||||
@JoinColumn(name = "file_id")
|
|
||||||
private FileEntity file;
|
|
||||||
|
|
||||||
@Column(name = "hash64_hi")
|
|
||||||
private Long hash64Hi;
|
|
||||||
|
|
||||||
@Column(name = "hash64_lo")
|
|
||||||
private Long hash64Lo;
|
|
||||||
|
|
||||||
@Column(name = "hash_algorithm", nullable = false)
|
|
||||||
private String hashAlgorithm;
|
|
||||||
|
|
||||||
@Column(name = "created_at")
|
|
||||||
private LocalDateTime createdAt;
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,51 @@
|
|||||||
|
package ru.soune.nocopy.entity.file;
|
||||||
|
|
||||||
|
import jakarta.persistence.*;
|
||||||
|
import lombok.*;
|
||||||
|
import org.springframework.data.annotation.CreatedDate;
|
||||||
|
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
|
||||||
|
import ru.soune.nocopy.entity.user.User;
|
||||||
|
import ru.soune.nocopy.entity.user.UserContent;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
@Table(name = "image_protection")
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
@NoArgsConstructor
|
||||||
|
@AllArgsConstructor
|
||||||
|
@ToString
|
||||||
|
@EntityListeners(AuditingEntityListener.class)
|
||||||
|
public class ImageProtection {
|
||||||
|
|
||||||
|
@Id
|
||||||
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
|
private Long protectionId;
|
||||||
|
|
||||||
|
@ManyToOne(fetch = FetchType.LAZY)
|
||||||
|
@JoinColumn(name = "user_id", nullable = false)
|
||||||
|
@ToString.Exclude
|
||||||
|
private User user;
|
||||||
|
|
||||||
|
@ManyToOne(fetch = FetchType.LAZY)
|
||||||
|
@JoinColumn(name = "content_id", nullable = false)
|
||||||
|
@ToString.Exclude
|
||||||
|
private UserContent content;
|
||||||
|
|
||||||
|
@Column(name = "protection_method", nullable = false, length = 50)
|
||||||
|
private String protectionMethod;
|
||||||
|
|
||||||
|
@Column(name = "protection_level", nullable = false)
|
||||||
|
private Integer protectionLevel;
|
||||||
|
|
||||||
|
@Column(name = "is_active", nullable = false)
|
||||||
|
private Boolean isActive = true;
|
||||||
|
|
||||||
|
@CreatedDate
|
||||||
|
@Column(name = "applied_at", nullable = false, updatable = false)
|
||||||
|
private LocalDateTime appliedAt;
|
||||||
|
|
||||||
|
@Column(name = "metadata", columnDefinition = "JSON")
|
||||||
|
private String metadata;
|
||||||
|
}
|
||||||
@@ -1,9 +0,0 @@
|
|||||||
package ru.soune.nocopy.entity.file;
|
|
||||||
|
|
||||||
public enum ProtectionStatus {
|
|
||||||
NOT_PROTECTED,
|
|
||||||
PROCESSING,
|
|
||||||
PROTECTED,
|
|
||||||
FAILED,
|
|
||||||
FAILED_SAVE
|
|
||||||
}
|
|
||||||
@@ -4,8 +4,10 @@ import com.fasterxml.jackson.annotation.JsonIgnore;
|
|||||||
import jakarta.persistence.*;
|
import jakarta.persistence.*;
|
||||||
import jakarta.validation.constraints.Size;
|
import jakarta.validation.constraints.Size;
|
||||||
import lombok.*;
|
import lombok.*;
|
||||||
|
import org.hibernate.annotations.ColumnDefault;
|
||||||
import org.springframework.data.annotation.CreatedDate;
|
import org.springframework.data.annotation.CreatedDate;
|
||||||
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
|
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
|
||||||
|
import ru.soune.nocopy.entity.file.ImageProtection;
|
||||||
import ru.soune.nocopy.entity.file.Violation;
|
import ru.soune.nocopy.entity.file.Violation;
|
||||||
|
|
||||||
import java.time.LocalDate;
|
import java.time.LocalDate;
|
||||||
@@ -75,6 +77,11 @@ public class User {
|
|||||||
@ToString.Exclude
|
@ToString.Exclude
|
||||||
private List<AuthToken> tokens = new ArrayList<>();
|
private List<AuthToken> tokens = new ArrayList<>();
|
||||||
|
|
||||||
|
@OneToOne(mappedBy = "user", cascade = CascadeType.ALL, orphanRemoval = true)
|
||||||
|
@JsonIgnore
|
||||||
|
@ToString.Exclude
|
||||||
|
private UserNotActive userNotActive;
|
||||||
|
|
||||||
@OneToMany(mappedBy = "user", cascade = CascadeType.ALL, orphanRemoval = true)
|
@OneToMany(mappedBy = "user", cascade = CascadeType.ALL, orphanRemoval = true)
|
||||||
@JsonIgnore
|
@JsonIgnore
|
||||||
@ToString.Exclude
|
@ToString.Exclude
|
||||||
@@ -83,4 +90,14 @@ public class User {
|
|||||||
@OneToMany(mappedBy = "user", cascade = CascadeType.ALL, orphanRemoval = true)
|
@OneToMany(mappedBy = "user", cascade = CascadeType.ALL, orphanRemoval = true)
|
||||||
@JsonIgnore
|
@JsonIgnore
|
||||||
@ToString.Exclude
|
@ToString.Exclude
|
||||||
private List<Violation> violations = new ArrayList<>();}
|
private List<Violation> violations = new ArrayList<>();
|
||||||
|
|
||||||
|
@OneToMany(mappedBy = "user", cascade = CascadeType.ALL, orphanRemoval = true)
|
||||||
|
@JsonIgnore
|
||||||
|
@ToString.Exclude
|
||||||
|
private List<ImageProtection> imageProtections = new ArrayList<>();
|
||||||
|
|
||||||
|
@Column(name = "failed_login_attempts")
|
||||||
|
@ColumnDefault("0")
|
||||||
|
private Integer failedLoginAttempts = 0;
|
||||||
|
}
|
||||||
|
|||||||
@@ -0,0 +1,32 @@
|
|||||||
|
package ru.soune.nocopy.entity.user;
|
||||||
|
|
||||||
|
import jakarta.persistence.*;
|
||||||
|
import lombok.*;
|
||||||
|
import org.springframework.data.annotation.CreatedDate;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
@Table(name = "user_not_active")
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
@NoArgsConstructor
|
||||||
|
@AllArgsConstructor
|
||||||
|
@ToString
|
||||||
|
@Builder
|
||||||
|
public class UserNotActive {
|
||||||
|
|
||||||
|
@Id
|
||||||
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
|
private Long id;
|
||||||
|
|
||||||
|
@OneToOne(fetch = FetchType.LAZY)
|
||||||
|
private User user;
|
||||||
|
|
||||||
|
@CreatedDate
|
||||||
|
@Column(name = "created_at", updatable = false)
|
||||||
|
private LocalDateTime createdAt;
|
||||||
|
|
||||||
|
@Column(name = "blocked_until")
|
||||||
|
private LocalDateTime blockedUntil;
|
||||||
|
}
|
||||||
@@ -1,20 +0,0 @@
|
|||||||
package ru.soune.nocopy.exception;
|
|
||||||
|
|
||||||
public class DuplicateImageException extends RuntimeException {
|
|
||||||
private final String duplicateFileId;
|
|
||||||
private final Long userId;
|
|
||||||
|
|
||||||
public DuplicateImageException(String message, String duplicateFileId, Long userId) {
|
|
||||||
super(message);
|
|
||||||
this.duplicateFileId = duplicateFileId;
|
|
||||||
this.userId = userId;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String duplicateFileId() {
|
|
||||||
return duplicateFileId;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Long userId() {
|
|
||||||
return userId;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -6,12 +6,12 @@ import lombok.extern.slf4j.Slf4j;
|
|||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
import ru.soune.nocopy.dto.*;
|
import ru.soune.nocopy.dto.*;
|
||||||
import ru.soune.nocopy.dto.file.*;
|
import ru.soune.nocopy.dto.file.*;
|
||||||
|
import ru.soune.nocopy.entity.user.AuthToken;
|
||||||
import ru.soune.nocopy.entity.file.FileEntity;
|
import ru.soune.nocopy.entity.file.FileEntity;
|
||||||
import ru.soune.nocopy.entity.file.FileStatus;
|
import ru.soune.nocopy.entity.file.FileStatus;
|
||||||
import ru.soune.nocopy.exception.FileEntityNotFoundException;
|
import ru.soune.nocopy.exception.FileEntityNotFoundException;
|
||||||
import ru.soune.nocopy.exception.NotFoundAuthToken;
|
import ru.soune.nocopy.exception.NotFoundAuthToken;
|
||||||
import ru.soune.nocopy.repository.FileEntityRepository;
|
import ru.soune.nocopy.repository.FileEntityRepository;
|
||||||
import ru.soune.nocopy.repository.ImageHashRepository;
|
|
||||||
import ru.soune.nocopy.service.register.AuthService;
|
import ru.soune.nocopy.service.register.AuthService;
|
||||||
import ru.soune.nocopy.service.file.FileEntityService;
|
import ru.soune.nocopy.service.file.FileEntityService;
|
||||||
import ru.soune.nocopy.service.file.FileStatsService;
|
import ru.soune.nocopy.service.file.FileStatsService;
|
||||||
@@ -34,8 +34,6 @@ public class FileEntityHandler implements RequestHandler {
|
|||||||
|
|
||||||
private final FileEntityRepository fileEntityRepository;
|
private final FileEntityRepository fileEntityRepository;
|
||||||
|
|
||||||
private final ImageHashRepository imageHashRepository;
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public BaseResponse handle(BaseRequest request) {
|
public BaseResponse handle(BaseRequest request) {
|
||||||
try {
|
try {
|
||||||
@@ -275,21 +273,15 @@ public class FileEntityHandler implements RequestHandler {
|
|||||||
|
|
||||||
FileEntity fileEntity = fileEntityRepository.findById(fileId)
|
FileEntity fileEntity = fileEntityRepository.findById(fileId)
|
||||||
.orElseThrow(() -> new FileEntityNotFoundException(fileId));
|
.orElseThrow(() -> new FileEntityNotFoundException(fileId));
|
||||||
int fullDelete = fileRequest.getFullDelete() == null ? 0 : fileRequest.getFullDelete();
|
|
||||||
|
|
||||||
if (fileEntity.getStatus().equals(FileStatus.DELETED) || fullDelete == 1) {
|
if (fileEntity.getStatus().equals(FileStatus.DELETED)) {
|
||||||
fileEntityService.deleteFromDisk(fileEntity);
|
fileEntityService.deleteFromDisk(fileEntity);
|
||||||
response = DeleteFileResponse.builder()
|
response = DeleteFileResponse.builder()
|
||||||
.fileId(fileRequest.getFileId())
|
.fileId(fileRequest.getFileId())
|
||||||
.message("File deleted from disk")
|
.message("File deleted from disk")
|
||||||
.build();
|
.build();
|
||||||
} else {
|
} else {
|
||||||
fileEntityService.softDeleteFileWithHash(fileEntity);
|
fileEntityService.markAsDeleted(fileEntity);
|
||||||
//
|
|
||||||
// imageHashRepository.deleteByFileId(fileEntity.getId());
|
|
||||||
//
|
|
||||||
// fileEntityService.markAsDeleted(fileEntity);
|
|
||||||
|
|
||||||
response = DeleteFileResponse.builder()
|
response = DeleteFileResponse.builder()
|
||||||
.fileId(fileRequest.getFileId())
|
.fileId(fileRequest.getFileId())
|
||||||
.message("File marked as deleted")
|
.message("File marked as deleted")
|
||||||
|
|||||||
@@ -177,13 +177,40 @@ public class FileUploadHandler implements RequestHandler {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private BaseResponse handleRetryUpload(BaseRequest request, FileUploadRequest fileRequest) {
|
||||||
|
try {
|
||||||
|
fileUploadService.retryFailedUpload(fileRequest.getUploadId());
|
||||||
|
ru.soune.nocopy.dto.file.UploadProgressResponse progress =
|
||||||
|
fileUploadService.getUploadProgress(fileRequest.getUploadId());
|
||||||
|
|
||||||
|
RetryUploadResponse response = RetryUploadResponse.builder()
|
||||||
|
.uploadId(progress.getUploadId())
|
||||||
|
.message("Upload retry initiated")
|
||||||
|
.status(progress.getStatus().toString())
|
||||||
|
.build();
|
||||||
|
|
||||||
|
return new BaseResponse(request.getMsgId(), MessageCode.SUCCESS.getCode(), "Upload retry initiated",
|
||||||
|
response);
|
||||||
|
|
||||||
|
} catch (UploadSessionNotFoundException e) {
|
||||||
|
return new BaseResponse(request.getMsgId(), MessageCode.FILE_NOT_FOUND.getCode(),
|
||||||
|
"Upload session not found", null);
|
||||||
|
} catch (FileUploadException e) {
|
||||||
|
return new BaseResponse(request.getMsgId(), MessageCode.FILE_UPLOAD_ERROR.getCode(), e.getMessage(),
|
||||||
|
null);
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("Error retrying upload", e);
|
||||||
|
return new BaseResponse(request.getMsgId(), MessageCode.FILE_UPLOAD_ERROR.getCode(),
|
||||||
|
"Failed to retry upload", null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private BaseResponse handleGetChunkStatus(BaseRequest request, FileUploadRequest fileRequest) {
|
private BaseResponse handleGetChunkStatus(BaseRequest request, FileUploadRequest fileRequest) {
|
||||||
try {
|
try {
|
||||||
FileUploadSession session = fileUploadSessionRepository.findById(fileRequest.getUploadId())
|
FileUploadSession session = fileUploadSessionRepository.findById(fileRequest.getUploadId())
|
||||||
.orElseThrow(() -> new UploadSessionNotFoundException(fileRequest.getUploadId()));
|
.orElseThrow(() -> new UploadSessionNotFoundException(fileRequest.getUploadId()));
|
||||||
|
|
||||||
Map<String, Boolean> chunkStatus = new HashMap<>();
|
Map<String, Boolean> chunkStatus = new HashMap<>();
|
||||||
|
|
||||||
for (int i = 0; i < session.getTotalChunks(); i++) {
|
for (int i = 0; i < session.getTotalChunks(); i++) {
|
||||||
chunkStatus.put("chunk_" + i, session.getChunkPaths().containsKey(i));
|
chunkStatus.put("chunk_" + i, session.getChunkPaths().containsKey(i));
|
||||||
}
|
}
|
||||||
@@ -198,6 +225,7 @@ public class FileUploadHandler implements RequestHandler {
|
|||||||
|
|
||||||
return new BaseResponse(request.getMsgId(), MessageCode.SUCCESS.getCode(),
|
return new BaseResponse(request.getMsgId(), MessageCode.SUCCESS.getCode(),
|
||||||
MessageCode.SUCCESS.getDescription(), response);
|
MessageCode.SUCCESS.getDescription(), response);
|
||||||
|
|
||||||
} catch (UploadSessionNotFoundException e) {
|
} catch (UploadSessionNotFoundException e) {
|
||||||
return new BaseResponse(request.getMsgId(), MessageCode.FILE_NOT_FOUND.getCode(),
|
return new BaseResponse(request.getMsgId(), MessageCode.FILE_NOT_FOUND.getCode(),
|
||||||
"Upload session not found", null);
|
"Upload session not found", null);
|
||||||
|
|||||||
@@ -5,16 +5,15 @@ import org.springframework.context.support.DefaultMessageSourceResolvable;
|
|||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.web.bind.MethodArgumentNotValidException;
|
import org.springframework.web.bind.MethodArgumentNotValidException;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||||
import ru.soune.nocopy.dto.BaseResponse;
|
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||||
import ru.soune.nocopy.dto.MessageCode;
|
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||||
import ru.soune.nocopy.exception.*;
|
import ru.soune.nocopy.exception.*;
|
||||||
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
@RestControllerAdvice
|
@RestControllerAdvice
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
@ControllerAdvice
|
|
||||||
public class GlobalExceptionHandler {
|
public class GlobalExceptionHandler {
|
||||||
|
|
||||||
@ExceptionHandler(MethodArgumentNotValidException.class)
|
@ExceptionHandler(MethodArgumentNotValidException.class)
|
||||||
@@ -82,12 +81,4 @@ public class GlobalExceptionHandler {
|
|||||||
"message" ,ex.getMessage()
|
"message" ,ex.getMessage()
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ExceptionHandler(DuplicateImageException.class)
|
|
||||||
@ResponseBody
|
|
||||||
public ResponseEntity<BaseResponse> handleDuplicateImage(DuplicateImageException e) {
|
|
||||||
return ResponseEntity.ok().body(new BaseResponse(
|
|
||||||
20004, MessageCode.DUPLICATE_FILE_UPLOAD.getCode(),
|
|
||||||
"Duplicate image detected", e.getMessage()));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,7 +10,6 @@ import ru.soune.nocopy.dto.MessageCode;
|
|||||||
import ru.soune.nocopy.dto.file.ImageSearchRequest;
|
import ru.soune.nocopy.dto.file.ImageSearchRequest;
|
||||||
import ru.soune.nocopy.dto.file.YandexSearchResponse;
|
import ru.soune.nocopy.dto.file.YandexSearchResponse;
|
||||||
import ru.soune.nocopy.service.search.YandexSearchService;
|
import ru.soune.nocopy.service.search.YandexSearchService;
|
||||||
import ru.soune.nocopy.service.search.GoogleVisionSearchService;
|
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@Component
|
@Component
|
||||||
@@ -21,8 +20,6 @@ public class ImageFoundRequestHandler implements RequestHandler {
|
|||||||
|
|
||||||
private final YandexSearchService yandexSearchService;
|
private final YandexSearchService yandexSearchService;
|
||||||
|
|
||||||
private final GoogleVisionSearchService googleVisionSearchService;
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public BaseResponse handle(BaseRequest request) throws Exception {
|
public BaseResponse handle(BaseRequest request) throws Exception {
|
||||||
ImageSearchRequest imageSearchRequest = objectMapper.convertValue(request.getMessageBody(),
|
ImageSearchRequest imageSearchRequest = objectMapper.convertValue(request.getMessageBody(),
|
||||||
@@ -32,9 +29,6 @@ public class ImageFoundRequestHandler implements RequestHandler {
|
|||||||
|
|
||||||
YandexSearchResponse response = yandexSearchService.searchByFileEntity(fileId);
|
YandexSearchResponse response = yandexSearchService.searchByFileEntity(fileId);
|
||||||
|
|
||||||
//TODO uncommited when add billing
|
|
||||||
// GoogleVisionSearchResponse googleVisionSearchResponse = googleVisionSearchService.searchByFileEntity(fileId);
|
|
||||||
|
|
||||||
return new BaseResponse(request.getMsgId(), MessageCode.SUCCESS.getCode(),
|
return new BaseResponse(request.getMsgId(), MessageCode.SUCCESS.getCode(),
|
||||||
MessageCode.SUCCESS.getDescription(), response);
|
MessageCode.SUCCESS.getDescription(), response);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -61,24 +61,14 @@ public class RegRequestHandler implements RequestHandler {
|
|||||||
AuthToken authToken = authService.register(regRequest);
|
AuthToken authToken = authService.register(regRequest);
|
||||||
EmailVerificationToken emailToken = emailService.createEmailVerificationToken(authToken);
|
EmailVerificationToken emailToken = emailService.createEmailVerificationToken(authToken);
|
||||||
|
|
||||||
try {
|
emailService.sendVerificationEmail(authToken.getUser(), emailToken.getToken());
|
||||||
emailService.sendVerificationEmail(authToken.getUser(), emailToken.getToken());
|
|
||||||
} catch (Exception e) {
|
|
||||||
return new BaseResponse(request.getMsgId(), MessageCode.SEND_EMAIL_EXCEPTION.getCode(),
|
|
||||||
MessageCode.SEND_EMAIL_EXCEPTION.getDescription(), Map.of(
|
|
||||||
"message", "not send",
|
|
||||||
"email", regRequest.getEmail()
|
|
||||||
));
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
String token = authToken.getToken();
|
String token = authToken.getToken();
|
||||||
authService.useUserAuthToken(token);
|
authService.useUserAuthToken(token);
|
||||||
|
|
||||||
RegAnswer regAnswer = new RegAnswer();
|
RegAnswer regAnswer = new RegAnswer();
|
||||||
|
regAnswer.setToken(token);
|
||||||
regAnswer.setVerified(false);
|
regAnswer.setVerified(false);
|
||||||
regAnswer.setActive(false);
|
regAnswer.setActive(false);
|
||||||
regAnswer.setUserId(authToken.getUser().getId());
|
|
||||||
|
|
||||||
return new BaseResponse(request.getMsgId(), MessageCode.SUCCESS.getCode(),
|
return new BaseResponse(request.getMsgId(), MessageCode.SUCCESS.getCode(),
|
||||||
MessageCode.SUCCESS.getDescription(), regAnswer);
|
MessageCode.SUCCESS.getDescription(), regAnswer);
|
||||||
|
|||||||
@@ -0,0 +1,112 @@
|
|||||||
|
package ru.soune.nocopy.handler;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
import ru.soune.nocopy.dto.BaseRequest;
|
||||||
|
import ru.soune.nocopy.dto.BaseResponse;
|
||||||
|
import ru.soune.nocopy.dto.MessageCode;
|
||||||
|
import ru.soune.nocopy.dto.file.ActionResponse;
|
||||||
|
import ru.soune.nocopy.dto.register.ResetPasswordRequest;
|
||||||
|
import ru.soune.nocopy.dto.register.ResetPasswordResponse;
|
||||||
|
import ru.soune.nocopy.entity.user.AuthToken;
|
||||||
|
import ru.soune.nocopy.entity.user.EmailVerificationToken;
|
||||||
|
import ru.soune.nocopy.entity.user.User;
|
||||||
|
import ru.soune.nocopy.exception.NotValidFieldException;
|
||||||
|
import ru.soune.nocopy.exception.UserNotFoundException;
|
||||||
|
import ru.soune.nocopy.repository.AuthTokenRepository;
|
||||||
|
import ru.soune.nocopy.repository.EmailVerificationTokenRepository;
|
||||||
|
import ru.soune.nocopy.repository.UserRepository;
|
||||||
|
import ru.soune.nocopy.service.mail.EmailService;
|
||||||
|
import ru.soune.nocopy.service.register.AuthService;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
@Slf4j
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public class ResetPasswordHandler implements RequestHandler{
|
||||||
|
|
||||||
|
private final ObjectMapper objectMapper;
|
||||||
|
|
||||||
|
private final EmailService emailService;
|
||||||
|
|
||||||
|
private final AuthService authService;
|
||||||
|
|
||||||
|
private final UserRepository userRepository;
|
||||||
|
|
||||||
|
private final EmailVerificationTokenRepository emailVerificationTokenRepository;
|
||||||
|
|
||||||
|
private final AuthTokenRepository authTokenRepository;
|
||||||
|
|
||||||
|
private final PasswordEncoder passwordEncoder;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BaseResponse handle(BaseRequest request) throws Exception {
|
||||||
|
ResetPasswordRequest resetPasswordRequest = objectMapper.convertValue(request.getMessageBody(),
|
||||||
|
ResetPasswordRequest.class);
|
||||||
|
String action = resetPasswordRequest.getAction();
|
||||||
|
User user = userRepository.findByEmail(resetPasswordRequest.getEmail());
|
||||||
|
AuthToken authToken;
|
||||||
|
|
||||||
|
if (user == null) {
|
||||||
|
throw new UserNotFoundException("User not found with email: " + resetPasswordRequest.getEmail());
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (action) {
|
||||||
|
case "resetPassword":
|
||||||
|
authToken = authService.generateAuthToken(user);
|
||||||
|
|
||||||
|
EmailVerificationToken emailToken = emailService.createEmailVerificationToken(authToken);
|
||||||
|
|
||||||
|
emailService.sendResetPasswordEmail(authToken.getUser(), emailToken.getToken());
|
||||||
|
|
||||||
|
return successResponse(request, authToken, user, MessageCode.SUCCESS.getDescription());
|
||||||
|
case "confirmVerification":
|
||||||
|
String verifyToken = resetPasswordRequest.getVerifyToken();
|
||||||
|
|
||||||
|
EmailVerificationToken verificationToken =
|
||||||
|
emailVerificationTokenRepository.findByUserIdAndToken(user.getId(), verifyToken);
|
||||||
|
|
||||||
|
if (verificationToken == null) {
|
||||||
|
throw new NotValidFieldException("Verification token not found",
|
||||||
|
new BaseResponse(request.getMsgId(), MessageCode.INVALID_TOKEN.getCode(),
|
||||||
|
MessageCode.INVALID_TOKEN.getDescription(), Map.of("verifyToken", verifyToken)));
|
||||||
|
}
|
||||||
|
List<AuthToken> authTokens = authTokenRepository.findByUserId(user.getId());
|
||||||
|
|
||||||
|
authToken = authTokens.isEmpty() ? authService.generateAuthToken(user): authTokens.getFirst();
|
||||||
|
|
||||||
|
user.setPassword(passwordEncoder.encode(resetPasswordRequest.getPassword()));
|
||||||
|
user.setActive(true);
|
||||||
|
|
||||||
|
emailVerificationTokenRepository.delete(verificationToken);
|
||||||
|
|
||||||
|
User saveUser = userRepository.save(user);
|
||||||
|
|
||||||
|
return successResponse(request, authToken, saveUser, MessageCode.SUCCESS.getDescription());
|
||||||
|
default:
|
||||||
|
ActionResponse response = ActionResponse.builder()
|
||||||
|
.action(action)
|
||||||
|
.availableActions(Arrays.asList("confirmVerification", "resetPassword"))
|
||||||
|
.build();
|
||||||
|
|
||||||
|
return new BaseResponse(request.getMsgId(), MessageCode.INVALID_ACTION.getCode(),
|
||||||
|
"Invalid action: " + action, response);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private BaseResponse successResponse(BaseRequest request, AuthToken authToken, User user, String message) {
|
||||||
|
ResetPasswordResponse resetPasswordResponse = new ResetPasswordResponse();
|
||||||
|
resetPasswordResponse.setAuthToken(authToken.getToken());
|
||||||
|
resetPasswordResponse.setUserId(user.getId());
|
||||||
|
|
||||||
|
return new BaseResponse(request.getMsgId(), MessageCode.SUCCESS.getCode(), message, resetPasswordResponse);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,7 +1,6 @@
|
|||||||
package ru.soune.nocopy.handler;
|
package ru.soune.nocopy.handler;
|
||||||
|
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import jakarta.mail.MessagingException;
|
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
@@ -22,7 +21,7 @@ import ru.soune.nocopy.service.mail.EmailService;
|
|||||||
import ru.soune.nocopy.service.register.AuthService;
|
import ru.soune.nocopy.service.register.AuthService;
|
||||||
import ru.soune.nocopy.service.user.UserService;
|
import ru.soune.nocopy.service.user.UserService;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
@@ -45,7 +44,7 @@ public class VerifyRegisterUserHandler implements RequestHandler {
|
|||||||
private final AuthService authService;
|
private final AuthService authService;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public BaseResponse handle(BaseRequest request) throws MessagingException, IOException {
|
public BaseResponse handle(BaseRequest request) {
|
||||||
VerifyUserRequest verifyUserRequest = objectMapper.convertValue(request.getMessageBody(), VerifyUserRequest.class);
|
VerifyUserRequest verifyUserRequest = objectMapper.convertValue(request.getMessageBody(), VerifyUserRequest.class);
|
||||||
|
|
||||||
Long userId = verifyUserRequest.getUserId();
|
Long userId = verifyUserRequest.getUserId();
|
||||||
@@ -53,9 +52,9 @@ public class VerifyRegisterUserHandler implements RequestHandler {
|
|||||||
User user = userRepository.findById(userId)
|
User user = userRepository.findById(userId)
|
||||||
.orElseThrow(() -> new UserNotFoundException("User not found with id: " + userId));
|
.orElseThrow(() -> new UserNotFoundException("User not found with id: " + userId));
|
||||||
|
|
||||||
Optional<AuthToken> userAuthTokens = authTokenRepository.findByUserId(userId);
|
List<AuthToken> authTokens = authTokenRepository.findByUserId(user.getId());
|
||||||
|
|
||||||
AuthToken authToken = userAuthTokens.isEmpty() ? authService.generateAuthToken(user): userAuthTokens.get();
|
AuthToken authToken = authTokens.isEmpty() ? authService.generateAuthToken(user): authTokens.getFirst();
|
||||||
|
|
||||||
if (verifyUserRequest.getResend() != null && verifyUserRequest.getResend() == 1) {
|
if (verifyUserRequest.getResend() != null && verifyUserRequest.getResend() == 1) {
|
||||||
return handleResend(request, user, authToken);
|
return handleResend(request, user, authToken);
|
||||||
@@ -64,7 +63,7 @@ public class VerifyRegisterUserHandler implements RequestHandler {
|
|||||||
return handleVerify(request, user, authToken, verifyUserRequest.getVerifyToken());
|
return handleVerify(request, user, authToken, verifyUserRequest.getVerifyToken());
|
||||||
}
|
}
|
||||||
|
|
||||||
private BaseResponse handleResend(BaseRequest request, User user, AuthToken authToken) throws MessagingException, IOException {
|
private BaseResponse handleResend(BaseRequest request, User user, AuthToken authToken) {
|
||||||
|
|
||||||
EmailVerificationToken token = emailVerificationTokenRepository.findByUserId(user.getId());
|
EmailVerificationToken token = emailVerificationTokenRepository.findByUserId(user.getId());
|
||||||
|
|
||||||
@@ -72,8 +71,7 @@ public class VerifyRegisterUserHandler implements RequestHandler {
|
|||||||
emailService.sendVerificationEmail(user, token.getToken());
|
emailService.sendVerificationEmail(user, token.getToken());
|
||||||
|
|
||||||
log.info("Verification token resent for user {}", user.getEmail());
|
log.info("Verification token resent for user {}", user.getEmail());
|
||||||
return successResponse(request, authToken, "Verification code has been resent", false,
|
return successResponse(request, authToken, "Verification code has been resent", false, false);
|
||||||
false, user.getEmail());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (token != null) {
|
if (token != null) {
|
||||||
@@ -87,7 +85,7 @@ public class VerifyRegisterUserHandler implements RequestHandler {
|
|||||||
log.info("New verification token generated for user {}", user.getEmail());
|
log.info("New verification token generated for user {}", user.getEmail());
|
||||||
|
|
||||||
return successResponse(request, authToken, "New verification code has been sent",
|
return successResponse(request, authToken, "New verification code has been sent",
|
||||||
false, false, user.getEmail());
|
false, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -102,20 +100,19 @@ public class VerifyRegisterUserHandler implements RequestHandler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
User updateUser = userService.activateAndVerifyUser(user.getId());
|
User updateUser = userService.activateAndVerifyUser(user.getId());
|
||||||
|
|
||||||
emailVerificationTokenRepository.delete(token);
|
emailVerificationTokenRepository.delete(token);
|
||||||
|
|
||||||
return successResponse(request, authToken, MessageCode.SUCCESS.getDescription(), updateUser.isActive(),
|
return successResponse(request, authToken, MessageCode.SUCCESS.getDescription(), updateUser.isActive(),
|
||||||
updateUser.isEmailVerified(), updateUser.getEmail());
|
updateUser.isEmailVerified());
|
||||||
}
|
}
|
||||||
|
|
||||||
private BaseResponse successResponse(BaseRequest request, AuthToken authToken, String message, boolean isActive,
|
private BaseResponse successResponse(BaseRequest request, AuthToken authToken, String message, boolean isActive,
|
||||||
boolean isVerified, String email) {
|
boolean isVerified) {
|
||||||
|
|
||||||
RegAnswer regAnswer = new RegAnswer();
|
RegAnswer regAnswer = new RegAnswer();
|
||||||
regAnswer.setToken(authToken.getToken());
|
regAnswer.setToken(authToken.getToken());
|
||||||
regAnswer.setVerified(isVerified);
|
regAnswer.setVerified(isVerified);
|
||||||
regAnswer.setActive(isActive);
|
regAnswer.setActive(isActive);
|
||||||
regAnswer.setEmail(email);
|
|
||||||
|
|
||||||
return new BaseResponse(request.getMsgId(), MessageCode.SUCCESS.getCode(), message, regAnswer);
|
return new BaseResponse(request.getMsgId(), MessageCode.SUCCESS.getCode(), message, regAnswer);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -138,6 +138,15 @@ public class RegRequestValidator implements Validator {
|
|||||||
errors.rejectValue("password", "password.contains.spaces",
|
errors.rejectValue("password", "password.contains.spaces",
|
||||||
"Password cannot contain spaces");
|
"Password cannot contain spaces");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// recomment if need complexity
|
||||||
|
// checkPasswordComplexity(password, errors);
|
||||||
|
|
||||||
|
// reccoment if need check simply standart password
|
||||||
|
// checkCommonPasswords(password, errors);
|
||||||
|
|
||||||
|
// reccoment if need check simply standart password
|
||||||
|
// checkForSequences(password, errors);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void checkPasswordComplexity(String password, Errors errors) {
|
private void checkPasswordComplexity(String password, Errors errors) {
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package ru.soune.nocopy.repository;
|
package ru.soune.nocopy.repository;
|
||||||
|
|
||||||
import org.springframework.data.jpa.repository.JpaRepository;
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
import org.springframework.data.jpa.repository.Query;
|
|
||||||
import org.springframework.stereotype.Repository;
|
import org.springframework.stereotype.Repository;
|
||||||
import ru.soune.nocopy.entity.user.AuthToken;
|
import ru.soune.nocopy.entity.user.AuthToken;
|
||||||
|
|
||||||
@@ -14,11 +13,6 @@ public interface AuthTokenRepository extends JpaRepository<AuthToken, Long> {
|
|||||||
List<AuthToken> findByExpiresAtBefore(LocalDateTime expiresAtBefore);
|
List<AuthToken> findByExpiresAtBefore(LocalDateTime expiresAtBefore);
|
||||||
Optional<AuthToken> findByLastUsedAtBefore(LocalDateTime lastUsedAt);
|
Optional<AuthToken> findByLastUsedAtBefore(LocalDateTime lastUsedAt);
|
||||||
Optional<AuthToken> findByToken(String token);
|
Optional<AuthToken> findByToken(String token);
|
||||||
@Query(value = """
|
|
||||||
SELECT a.user_id FROM auth_tokens a WHERE a.token = :token
|
|
||||||
""",
|
|
||||||
nativeQuery = true)
|
|
||||||
Long findUserIdByToken(String token);
|
|
||||||
Optional<AuthToken> findByUserIdAndIsActive(Long userId, boolean isActive);
|
Optional<AuthToken> findByUserIdAndIsActive(Long userId, boolean isActive);
|
||||||
Optional<AuthToken> findByUserId(Long userId);
|
List<AuthToken> findByUserId(Long userId);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,15 +6,14 @@ import org.springframework.data.repository.query.Param;
|
|||||||
import org.springframework.stereotype.Repository;
|
import org.springframework.stereotype.Repository;
|
||||||
import ru.soune.nocopy.entity.file.FileEntity;
|
import ru.soune.nocopy.entity.file.FileEntity;
|
||||||
import ru.soune.nocopy.entity.file.FileStatus;
|
import ru.soune.nocopy.entity.file.FileStatus;
|
||||||
import ru.soune.nocopy.entity.file.ProtectionStatus;
|
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
@Repository
|
@Repository
|
||||||
public interface FileEntityRepository extends JpaRepository<FileEntity, String> {
|
public interface FileEntityRepository extends JpaRepository<FileEntity, String> {
|
||||||
List<FileEntity> findByUserId(Long userId);
|
|
||||||
|
|
||||||
|
List<FileEntity> findByUserId(Long userId);
|
||||||
Optional<FileEntity> findByUserIdAndChecksum(Long userId, String imageHash);
|
Optional<FileEntity> findByUserIdAndChecksum(Long userId, String imageHash);
|
||||||
|
|
||||||
List<FileEntity> findByUserIdAndStatus(Long userId, FileStatus status);
|
List<FileEntity> findByUserIdAndStatus(Long userId, FileStatus status);
|
||||||
@@ -23,31 +22,13 @@ public interface FileEntityRepository extends JpaRepository<FileEntity, String>
|
|||||||
|
|
||||||
Optional<FileEntity> findByUploadSessionId(String uploadSessionId);
|
Optional<FileEntity> findByUploadSessionId(String uploadSessionId);
|
||||||
|
|
||||||
List<FileEntity> findByProtectionStatus(ProtectionStatus protectionStatus);
|
|
||||||
|
|
||||||
boolean existsByFilePath(String filePath);
|
boolean existsByFilePath(String filePath);
|
||||||
|
|
||||||
@Query("SELECT SUM(f.fileSize) FROM FileEntity f WHERE f.userId = :userId AND f.status = 'ACTIVE'")
|
@Query("SELECT SUM(f.fileSize) FROM FileEntity f WHERE f.userId = :userId AND f.status = 'ACTIVE'")
|
||||||
Long getTotalSizeByUserId(@Param("userId") Long userId);
|
Long getTotalSizeByUserId(@Param("userId") Long userId);
|
||||||
|
|
||||||
@Query("SELECT f FROM FileEntity f WHERE f.protectionStatus = null AND f.status = 'ACTIVE' OR " +
|
|
||||||
"f.protectionStatus = 'NOT_PROTECTED' AND f.status = 'ACTIVE'")
|
|
||||||
List<FileEntity> findAllActiveFilesAndNotProtected();
|
|
||||||
|
|
||||||
@Query("SELECT f FROM FileEntity f WHERE f.userId = :userId AND f.originalFileName LIKE %:keyword%")
|
@Query("SELECT f FROM FileEntity f WHERE f.userId = :userId AND f.originalFileName LIKE %:keyword%")
|
||||||
List<FileEntity> searchByFileName(@Param("userId") Long userId, @Param("keyword") String keyword);
|
List<FileEntity> searchByFileName(@Param("userId") Long userId, @Param("keyword") String keyword);
|
||||||
|
|
||||||
@Query("SELECT f FROM FileEntity f WHERE f.status = :status")
|
|
||||||
List<FileEntity> searchFileEntityByStatus(@Param("status") FileStatus status);
|
|
||||||
|
|
||||||
@Query("SELECT f FROM FileEntity f WHERE f.id = :fileId")
|
|
||||||
FileEntity findByFileId(@Param("fileId") String fileId);
|
|
||||||
|
|
||||||
@Query("SELECT f FROM FileEntity f WHERE f.signature = :signature")
|
|
||||||
FileEntity findBySignature(@Param("signature") String signature);
|
|
||||||
|
|
||||||
@Query("SELECT f.id FROM FileEntity f WHERE f.filePath = :filePath")
|
|
||||||
String findFileIdByFilePath(@Param("filePath") String filePath);
|
|
||||||
|
|
||||||
long countByUserId(Long userId);
|
long countByUserId(Long userId);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,14 +0,0 @@
|
|||||||
package ru.soune.nocopy.repository;
|
|
||||||
|
|
||||||
import org.springframework.data.jpa.repository.JpaRepository;
|
|
||||||
import org.springframework.stereotype.Repository;
|
|
||||||
import ru.soune.nocopy.entity.file.ImageHashEntity;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
@Repository
|
|
||||||
public interface ImageHashRepository extends JpaRepository<ImageHashEntity, String> {
|
|
||||||
void deleteByFileId(String fileId);
|
|
||||||
List<ImageHashEntity> findByFileId(String fileId);
|
|
||||||
ImageHashEntity findByHash64HiAndHash64Lo(Long hash64Hi, Long hash64Lo);
|
|
||||||
}
|
|
||||||
@@ -1,75 +0,0 @@
|
|||||||
package ru.soune.nocopy.repository;
|
|
||||||
|
|
||||||
import org.springframework.data.jpa.repository.JpaRepository;
|
|
||||||
import org.springframework.data.jpa.repository.Query;
|
|
||||||
import org.springframework.data.repository.query.Param;
|
|
||||||
import org.springframework.stereotype.Repository;
|
|
||||||
import ru.soune.nocopy.entity.file.FileEntity;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
@Repository
|
|
||||||
public interface ImageSimilarityRepository
|
|
||||||
extends JpaRepository<FileEntity, String> {
|
|
||||||
@Query(value = """
|
|
||||||
SELECT
|
|
||||||
f.id AS id,
|
|
||||||
f.original_file_name AS originalFileName,
|
|
||||||
f.file_size AS fileSize,
|
|
||||||
f.user_id AS userId,
|
|
||||||
h.hash64_hi AS hash64Hi,
|
|
||||||
h.hash64_lo AS hash64Lo
|
|
||||||
FROM image_hashes ref
|
|
||||||
JOIN image_hashes h
|
|
||||||
ON ref.file_id <> h.file_id
|
|
||||||
JOIN file_entities f
|
|
||||||
ON f.id = h.file_id
|
|
||||||
WHERE ref.file_id = :fileId
|
|
||||||
""",
|
|
||||||
nativeQuery = true)
|
|
||||||
List<SimilarImageProjection> findCandidates(
|
|
||||||
@Param("fileId") String fileId
|
|
||||||
);
|
|
||||||
|
|
||||||
@Query(value = """
|
|
||||||
SELECT
|
|
||||||
f.id AS id,
|
|
||||||
f.original_file_name AS originalFileName,
|
|
||||||
f.file_size AS fileSize,
|
|
||||||
f.user_id AS userId,
|
|
||||||
h.hash64_hi AS hash64Hi,
|
|
||||||
h.hash64_lo AS hash64Lo
|
|
||||||
FROM image_hashes ref
|
|
||||||
JOIN image_hashes h
|
|
||||||
ON ref.file_id <> h.file_id
|
|
||||||
JOIN file_entities f
|
|
||||||
ON f.id = h.file_id
|
|
||||||
WHERE ref.file_id = :fileId AND f.user_id = :userId
|
|
||||||
""",
|
|
||||||
nativeQuery = true)
|
|
||||||
List<SimilarImageProjection> findCandidatesFromUserFiles(
|
|
||||||
@Param("fileId") String fileId,
|
|
||||||
@Param("userId") Long userId
|
|
||||||
);
|
|
||||||
|
|
||||||
|
|
||||||
@Query(value = """
|
|
||||||
SELECT
|
|
||||||
h.hash64_hi AS hash64Hi,
|
|
||||||
h.hash64_lo AS hash64Lo,
|
|
||||||
h.file_id AS fileId,
|
|
||||||
f.user_id AS userId,
|
|
||||||
f.original_file_name AS originalFileName,
|
|
||||||
f.file_size AS fileSize,
|
|
||||||
f.stored_file_name AS similarFileId
|
|
||||||
FROM image_hashes h
|
|
||||||
JOIN file_entities f ON f.id = h.file_id
|
|
||||||
WHERE h.hash64_hi = :hash64Hi
|
|
||||||
AND h.hash64_lo = :hash64Lo
|
|
||||||
""",
|
|
||||||
nativeQuery = true)
|
|
||||||
List<SimilarImageProjection> findExactDuplicates(
|
|
||||||
@Param("hash64Hi") Long hash64_hi,
|
|
||||||
@Param("hash64Lo") Long hash64_lo);
|
|
||||||
}
|
|
||||||
|
|
||||||
@@ -1,10 +0,0 @@
|
|||||||
package ru.soune.nocopy.repository;
|
|
||||||
|
|
||||||
public interface SimilarImageProjection {
|
|
||||||
Long getHash64Hi();
|
|
||||||
Long getHash64Lo();
|
|
||||||
String getId();
|
|
||||||
Long getUserId();
|
|
||||||
String getOriginalFileName();
|
|
||||||
Long getFileSize();
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
package ru.soune.nocopy.repository;
|
||||||
|
|
||||||
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
import org.springframework.stereotype.Repository;
|
||||||
|
import ru.soune.nocopy.entity.user.UserNotActive;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
@Repository
|
||||||
|
public interface UserNotActiveRepository extends JpaRepository<UserNotActive, Long> {
|
||||||
|
Optional<UserNotActive> findByUserId(Long userId);
|
||||||
|
List<UserNotActive> findByBlockedUntilBefore(LocalDateTime now);
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,191 +0,0 @@
|
|||||||
package ru.soune.nocopy.service;
|
|
||||||
|
|
||||||
import lombok.RequiredArgsConstructor;
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
|
||||||
import org.springframework.data.domain.Page;
|
|
||||||
import org.springframework.data.domain.PageImpl;
|
|
||||||
import org.springframework.data.domain.Pageable;
|
|
||||||
import org.springframework.stereotype.Service;
|
|
||||||
import ru.soune.nocopy.dto.file.SimilarFileDTO;
|
|
||||||
import ru.soune.nocopy.dto.file.SimilarityFilter;
|
|
||||||
import ru.soune.nocopy.entity.file.ImageHashEntity;
|
|
||||||
import ru.soune.nocopy.repository.AuthTokenRepository;
|
|
||||||
import ru.soune.nocopy.repository.ImageHashRepository;
|
|
||||||
import ru.soune.nocopy.repository.ImageSimilarityRepository;
|
|
||||||
import ru.soune.nocopy.repository.SimilarImageProjection;
|
|
||||||
import ru.soune.nocopy.util.FileUtil;
|
|
||||||
|
|
||||||
import java.util.*;
|
|
||||||
import java.util.stream.Collectors;
|
|
||||||
|
|
||||||
@Service
|
|
||||||
@RequiredArgsConstructor
|
|
||||||
@Slf4j
|
|
||||||
public class FileSimilarityService {
|
|
||||||
|
|
||||||
private final ImageSimilarityRepository repository;
|
|
||||||
|
|
||||||
private final ImageHashRepository hashRepository;
|
|
||||||
|
|
||||||
private final FileUtil fileUtil;
|
|
||||||
|
|
||||||
private final AuthTokenRepository authTokenRepository;
|
|
||||||
|
|
||||||
public List<SimilarFileDTO> findSimilarFiles(String fileId) {
|
|
||||||
var imageHashEntity = hashRepository.findById(fileId)
|
|
||||||
.orElseThrow(() -> new RuntimeException("Hash not found"));
|
|
||||||
|
|
||||||
Long hash64Hi = imageHashEntity.getHash64Hi();
|
|
||||||
Long hash64Lo = imageHashEntity.getHash64Lo();
|
|
||||||
|
|
||||||
List<SimilarImageProjection> candidates = repository.findCandidates(fileId);
|
|
||||||
|
|
||||||
return candidates.stream()
|
|
||||||
.map(c -> {
|
|
||||||
Long cHi = c.getHash64Hi();
|
|
||||||
Long cLo = c.getHash64Lo();
|
|
||||||
|
|
||||||
int hamming = fileUtil.hamming64(hash64Hi, hash64Lo, cHi, cLo);
|
|
||||||
|
|
||||||
String level;
|
|
||||||
if (hamming <= 5) {
|
|
||||||
level = "DUPLICATE";
|
|
||||||
} else if (hamming <= 12) {
|
|
||||||
level = "SIMILAR";
|
|
||||||
} else {
|
|
||||||
level = "DIFFERENT";
|
|
||||||
}
|
|
||||||
|
|
||||||
return SimilarFileDTO.builder()
|
|
||||||
.fileId(c.getId())
|
|
||||||
.ownerId(c.getUserId())
|
|
||||||
.originalFileName(c.getOriginalFileName())
|
|
||||||
.fileSize(c.getFileSize())
|
|
||||||
.hammingDistance(hamming)
|
|
||||||
.similarityLevel(level)
|
|
||||||
.build();
|
|
||||||
})
|
|
||||||
.sorted((a, b) ->
|
|
||||||
Integer.compare(a.getHammingDistance(), b.getHammingDistance()))
|
|
||||||
.toList();
|
|
||||||
}
|
|
||||||
|
|
||||||
public List<SimilarFileDTO> findDuplicateByHammingDistance(String fileId, int hammingDistance,
|
|
||||||
int duplicate, int similar) {
|
|
||||||
List<SimilarImageProjection> candidates = repository.findCandidates(fileId);
|
|
||||||
|
|
||||||
Optional<ImageHashEntity> hashOptional = hashRepository.findById(fileId);
|
|
||||||
|
|
||||||
if (hashOptional.isEmpty()) {
|
|
||||||
return Collections.emptyList();
|
|
||||||
}
|
|
||||||
|
|
||||||
var imageHashEntity = hashOptional.get();
|
|
||||||
|
|
||||||
Long hash64Hi = imageHashEntity.getHash64Hi();
|
|
||||||
Long hash64Lo = imageHashEntity.getHash64Lo();
|
|
||||||
|
|
||||||
return candidates.stream()
|
|
||||||
.map(c -> {
|
|
||||||
Long cHi = c.getHash64Hi();
|
|
||||||
Long cLo = c.getHash64Lo();
|
|
||||||
|
|
||||||
int hamming = fileUtil.hamming64(hash64Hi, hash64Lo, cHi, cLo);
|
|
||||||
|
|
||||||
return new AbstractMap.SimpleEntry<>(c, hamming);
|
|
||||||
})
|
|
||||||
.filter(entry -> entry.getValue() <= hammingDistance)
|
|
||||||
.sorted((a, b) -> Integer.compare(a.getValue(), b.getValue()))
|
|
||||||
.map(entry -> {
|
|
||||||
SimilarImageProjection similarImageProjection = entry.getKey();
|
|
||||||
int hamming = entry.getValue();
|
|
||||||
|
|
||||||
String level;
|
|
||||||
if (hamming <= duplicate) {
|
|
||||||
level = "DUPLICATE";
|
|
||||||
} else if (hamming <= similar) {
|
|
||||||
level = "SIMILAR";
|
|
||||||
} else {
|
|
||||||
level = "DIFFERENT";
|
|
||||||
}
|
|
||||||
|
|
||||||
return SimilarFileDTO.builder()
|
|
||||||
.fileId(similarImageProjection.getId())
|
|
||||||
.originalFileName(similarImageProjection.getOriginalFileName())
|
|
||||||
.fileSize(similarImageProjection.getFileSize())
|
|
||||||
.hammingDistance(hamming)
|
|
||||||
.similarityLevel(level)
|
|
||||||
.ownerId(similarImageProjection.getUserId())
|
|
||||||
.build();
|
|
||||||
})
|
|
||||||
.toList();
|
|
||||||
}
|
|
||||||
|
|
||||||
public List<SimilarImageProjection> findDuplicatedByHash(Long hash64Hi, Long hash64Lo) {
|
|
||||||
List<SimilarImageProjection> duplicates = repository.findExactDuplicates(hash64Hi,hash64Lo);
|
|
||||||
|
|
||||||
if (duplicates.isEmpty()) {
|
|
||||||
return new ArrayList<>();
|
|
||||||
}
|
|
||||||
|
|
||||||
return duplicates;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Page<SimilarFileDTO> findSimilarFiles(String fileId, SimilarityFilter filter, Pageable pageable, String authToken) {
|
|
||||||
var imageHashEntity = hashRepository.findById(fileId)
|
|
||||||
.orElseThrow(() -> new RuntimeException("Hash not found"));
|
|
||||||
|
|
||||||
Long hash64Hi = imageHashEntity.getHash64Hi();
|
|
||||||
Long hash64Lo = imageHashEntity.getHash64Lo();
|
|
||||||
|
|
||||||
List<SimilarImageProjection> candidates = authToken.equals("all") ? repository.findCandidates(fileId):
|
|
||||||
repository.findCandidatesFromUserFiles(fileId, authTokenRepository.findUserIdByToken(authToken));
|
|
||||||
|
|
||||||
List<String> similarityLevels = (filter != null && filter.getSimilarityLevels() != null)
|
|
||||||
? filter.getSimilarityLevels()
|
|
||||||
: List.of("DUPLICATE", "SIMILAR", "DIFFERENT");
|
|
||||||
|
|
||||||
List<SimilarFileDTO> allResults = candidates.stream()
|
|
||||||
.map(c -> createSimilarFileResponse(c, hash64Hi, hash64Lo))
|
|
||||||
.filter(response -> similarityLevels.contains(response.getSimilarityLevel()))
|
|
||||||
.sorted(Comparator.comparingInt(SimilarFileDTO::getHammingDistance))
|
|
||||||
.collect(Collectors.toList());
|
|
||||||
|
|
||||||
int total = allResults.size();
|
|
||||||
int page = (pageable != null) ? pageable.getPageNumber() : 0;
|
|
||||||
int size = (pageable != null) ? pageable.getPageSize() : 20;
|
|
||||||
|
|
||||||
int fromIndex = Math.min(page * size, total);
|
|
||||||
int toIndex = Math.min(fromIndex + size, total);
|
|
||||||
|
|
||||||
List<SimilarFileDTO> pageContent = allResults.subList(fromIndex, toIndex);
|
|
||||||
|
|
||||||
return new PageImpl<>(pageContent, pageable, total);
|
|
||||||
}
|
|
||||||
|
|
||||||
private SimilarFileDTO createSimilarFileResponse(SimilarImageProjection similarImageProjection,
|
|
||||||
Long hash64Hi, Long hash64Lo) {
|
|
||||||
Long imageProjectionHash64Hi = similarImageProjection.getHash64Hi();
|
|
||||||
Long similarImageProjectionHash64Lo = similarImageProjection.getHash64Lo();
|
|
||||||
|
|
||||||
int hamming = fileUtil.hamming64(hash64Hi, hash64Lo, imageProjectionHash64Hi, similarImageProjectionHash64Lo);
|
|
||||||
|
|
||||||
String level;
|
|
||||||
if (hamming <= 5) {
|
|
||||||
level = "DUPLICATE";
|
|
||||||
} else if (hamming <= 12) {
|
|
||||||
level = "SIMILAR";
|
|
||||||
} else {
|
|
||||||
level = "DIFFERENT";
|
|
||||||
}
|
|
||||||
|
|
||||||
return SimilarFileDTO.builder()
|
|
||||||
.fileId(similarImageProjection.getId())
|
|
||||||
.ownerId(similarImageProjection.getUserId())
|
|
||||||
.originalFileName(similarImageProjection.getOriginalFileName())
|
|
||||||
.fileSize(similarImageProjection.getFileSize())
|
|
||||||
.hammingDistance(hamming)
|
|
||||||
.similarityLevel(level)
|
|
||||||
.build();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,48 +0,0 @@
|
|||||||
package ru.soune.nocopy.service;
|
|
||||||
|
|
||||||
import com.vrt.fileprotection.image.phash.PHash;
|
|
||||||
import com.vrt.fileprotection.image.phash.PerceptualHashHelper;
|
|
||||||
import lombok.RequiredArgsConstructor;
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
|
||||||
import org.springframework.stereotype.Service;
|
|
||||||
import ru.soune.nocopy.entity.file.FileEntity;
|
|
||||||
import ru.soune.nocopy.entity.file.ImageHashEntity;
|
|
||||||
import ru.soune.nocopy.repository.ImageHashRepository;
|
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.nio.file.Path;
|
|
||||||
import java.time.LocalDateTime;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
@Service
|
|
||||||
@RequiredArgsConstructor
|
|
||||||
@Slf4j
|
|
||||||
public class ImageHashService {
|
|
||||||
|
|
||||||
private final ImageHashRepository repository;
|
|
||||||
|
|
||||||
public Map<String, Long> calculateHash(Path imagePath) throws IOException {
|
|
||||||
File file = imagePath.toFile();
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void create(FileEntity file, Map<String, Long> stringIntegerMap) {
|
|
||||||
ImageHashEntity entity = ImageHashEntity.builder()
|
|
||||||
.file(file)
|
|
||||||
.hash64Hi(stringIntegerMap.get("hi"))
|
|
||||||
.hash64Lo(stringIntegerMap.get("low"))
|
|
||||||
.hashAlgorithm("PHASH64")
|
|
||||||
.createdAt(LocalDateTime.now())
|
|
||||||
.build();
|
|
||||||
|
|
||||||
repository.save(entity);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,48 @@
|
|||||||
|
package ru.soune.nocopy.service.auth;
|
||||||
|
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.scheduling.annotation.Scheduled;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
import ru.soune.nocopy.entity.user.User;
|
||||||
|
import ru.soune.nocopy.entity.user.UserNotActive;
|
||||||
|
import ru.soune.nocopy.repository.UserNotActiveRepository;
|
||||||
|
import ru.soune.nocopy.repository.UserRepository;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
|
@Component
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public class CleanUpNotActiveUser {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private UserNotActiveRepository userNotActiveRepository;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private UserRepository userRepository;
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
@Scheduled(fixedDelay = 600000)
|
||||||
|
public void cleanupNotActiveUsers() {
|
||||||
|
LocalDateTime now = LocalDateTime.now();
|
||||||
|
|
||||||
|
List<UserNotActive> expiredTokens = userNotActiveRepository.findByBlockedUntilBefore(now);
|
||||||
|
|
||||||
|
for (UserNotActive userNotActive : expiredTokens) {
|
||||||
|
User user = userNotActive.getUser();
|
||||||
|
user.setActive(true);
|
||||||
|
user.setFailedLoginAttempts(0);
|
||||||
|
|
||||||
|
userRepository.save(user);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!expiredTokens.isEmpty()) {
|
||||||
|
userNotActiveRepository.deleteAll(expiredTokens);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -9,23 +9,15 @@ import ru.soune.nocopy.dto.file.FileResponse;
|
|||||||
import ru.soune.nocopy.entity.file.FileEntity;
|
import ru.soune.nocopy.entity.file.FileEntity;
|
||||||
import ru.soune.nocopy.entity.file.FileStatus;
|
import ru.soune.nocopy.entity.file.FileStatus;
|
||||||
import ru.soune.nocopy.entity.file.FileUploadSession;
|
import ru.soune.nocopy.entity.file.FileUploadSession;
|
||||||
import ru.soune.nocopy.entity.file.ProtectionStatus;
|
|
||||||
import ru.soune.nocopy.exception.DuplicateImageException;
|
|
||||||
import ru.soune.nocopy.exception.FileEntityNotFoundException;
|
import ru.soune.nocopy.exception.FileEntityNotFoundException;
|
||||||
import ru.soune.nocopy.repository.FileEntityRepository;
|
import ru.soune.nocopy.repository.FileEntityRepository;
|
||||||
import ru.soune.nocopy.repository.ImageHashRepository;
|
|
||||||
import ru.soune.nocopy.repository.SimilarImageProjection;
|
|
||||||
import ru.soune.nocopy.service.FileSimilarityService;
|
|
||||||
import ru.soune.nocopy.service.ImageHashService;
|
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.nio.file.Paths;
|
import java.nio.file.Paths;
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@@ -35,13 +27,7 @@ public class FileEntityService {
|
|||||||
|
|
||||||
private final FileEntityRepository fileEntityRepository;
|
private final FileEntityRepository fileEntityRepository;
|
||||||
|
|
||||||
private final ImageHashService imageHashService;
|
@Transactional
|
||||||
|
|
||||||
private final FileSimilarityService fileSimilarityService;
|
|
||||||
|
|
||||||
private final ImageHashRepository imageHashRepository;
|
|
||||||
|
|
||||||
@Transactional(noRollbackFor = DuplicateImageException.class)
|
|
||||||
public FileEntity createFromUploadSession(FileUploadSession session, String checksum) {
|
public FileEntity createFromUploadSession(FileUploadSession session, String checksum) {
|
||||||
log.info("Creating FileEntity for upload session: {}", session.getUploadId());
|
log.info("Creating FileEntity for upload session: {}", session.getUploadId());
|
||||||
|
|
||||||
@@ -52,21 +38,6 @@ public class FileEntityService {
|
|||||||
throw new IOException("File not found on disk: " + filePath);
|
throw new IOException("File not found on disk: " + filePath);
|
||||||
}
|
}
|
||||||
|
|
||||||
Map<String, Long> imageHash = Map.of();
|
|
||||||
|
|
||||||
if (session.getFileType().startsWith("image")) {
|
|
||||||
imageHash = imageHashService.calculateHash(filePath);
|
|
||||||
List<SimilarImageProjection> duplicatedByHash = fileSimilarityService.findDuplicatedByHash(
|
|
||||||
imageHash.get("hi"), imageHash.get("low"));
|
|
||||||
|
|
||||||
if (!duplicatedByHash.isEmpty()) {
|
|
||||||
SimilarImageProjection similarImageProjection = duplicatedByHash.get(0);
|
|
||||||
|
|
||||||
throw new DuplicateImageException("Duplicate", similarImageProjection.getId(),
|
|
||||||
similarImageProjection.getUserId());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
long fileSize = Files.size(filePath);
|
long fileSize = Files.size(filePath);
|
||||||
String originalName = session.getFileName();
|
String originalName = session.getFileName();
|
||||||
String storedName = filePath.getFileName().toString();
|
String storedName = filePath.getFileName().toString();
|
||||||
@@ -85,12 +56,11 @@ public class FileEntityService {
|
|||||||
.build();
|
.build();
|
||||||
|
|
||||||
FileEntity saved = fileEntityRepository.save(fileEntity);
|
FileEntity saved = fileEntityRepository.save(fileEntity);
|
||||||
|
log.info("FileEntity created successfully: {} (size: {} bytes)",
|
||||||
if (!imageHash.isEmpty()) {
|
saved.getId(), fileSize);
|
||||||
imageHashService.create(saved, imageHash);
|
|
||||||
}
|
|
||||||
|
|
||||||
return saved;
|
return saved;
|
||||||
|
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
log.error("Failed to create FileEntity for session {}: {}",
|
log.error("Failed to create FileEntity for session {}: {}",
|
||||||
session.getUploadId(), e.getMessage(), e);
|
session.getUploadId(), e.getMessage(), e);
|
||||||
@@ -98,6 +68,15 @@ public class FileEntityService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private String extractFileExtension(String fileName) {
|
||||||
|
int dotIndex = fileName.lastIndexOf('.');
|
||||||
|
if (dotIndex > 0 && dotIndex < fileName.length() - 1) {
|
||||||
|
return fileName.substring(dotIndex + 1).toLowerCase();
|
||||||
|
}
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
@Transactional(readOnly = true)
|
@Transactional(readOnly = true)
|
||||||
public FileEntityResponse getById(String fileId, int version) {
|
public FileEntityResponse getById(String fileId, int version) {
|
||||||
FileEntity fileEntity = fileEntityRepository.findById(fileId)
|
FileEntity fileEntity = fileEntityRepository.findById(fileId)
|
||||||
@@ -188,45 +167,24 @@ public class FileEntityService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public FileEntity markAsDeleted(FileEntity fileEntity) throws IOException {
|
public void markAsDeleted(FileEntity fileEntity) {
|
||||||
fileEntity.setStatus(FileStatus.DELETED);
|
fileEntity.setStatus(FileStatus.DELETED);
|
||||||
fileEntity.setUpdatedAt(LocalDateTime.now());
|
fileEntity.setUpdatedAt(LocalDateTime.now());
|
||||||
fileEntity.setProtectionStatus(ProtectionStatus.NOT_PROTECTED);
|
|
||||||
|
|
||||||
Path path = Paths.get(fileEntity.getProtectedFilePath());
|
|
||||||
Files.deleteIfExists(path);
|
|
||||||
fileEntity.setProtectedFilePath("");
|
|
||||||
|
|
||||||
return fileEntityRepository.save(fileEntity);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void softDeleteFileWithHash(FileEntity fileEntity) throws IOException {
|
|
||||||
if (fileEntity.getImageHash() != null) {
|
|
||||||
fileEntity.setImageHash(null);
|
|
||||||
}
|
|
||||||
|
|
||||||
fileEntity.setStatus(FileStatus.DELETED);
|
|
||||||
fileEntity.setUpdatedAt(LocalDateTime.now());
|
|
||||||
fileEntity.setProtectionStatus(ProtectionStatus.NOT_PROTECTED);
|
|
||||||
|
|
||||||
Path path = Paths.get(fileEntity.getProtectedFilePath());
|
|
||||||
Files.deleteIfExists(path);
|
|
||||||
fileEntity.setProtectedFilePath("");
|
|
||||||
|
|
||||||
fileEntityRepository.save(fileEntity);
|
fileEntityRepository.save(fileEntity);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public void deleteFromDisk(FileEntity fileEntity) throws IOException {
|
public boolean deleteFromDisk(FileEntity fileEntity) throws IOException {
|
||||||
Path path = Paths.get(fileEntity.getFilePath());
|
Path path = Paths.get(fileEntity.getFilePath());
|
||||||
|
|
||||||
if (!Files.exists(path)) {
|
if (!Files.exists(path)) {
|
||||||
return;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
Files.delete(path);
|
Files.delete(path);
|
||||||
|
|
||||||
fileEntityRepository.delete(fileEntity);
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Transactional(readOnly = true)
|
@Transactional(readOnly = true)
|
||||||
@@ -235,99 +193,6 @@ public class FileEntityService {
|
|||||||
return totalSize != null ? totalSize : 0L;
|
return totalSize != null ? totalSize : 0L;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void changeStatus(ProtectionStatus newStatus, String fileId) {
|
|
||||||
FileEntity fileEntity = fileEntityRepository.findById(fileId)
|
|
||||||
.orElseThrow(() -> new FileEntityNotFoundException(fileId));
|
|
||||||
fileEntity.setProtectionStatus(newStatus);
|
|
||||||
|
|
||||||
fileEntityRepository.save(fileEntity);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Transactional
|
|
||||||
public void writeProtectedFile(String id, byte[] data, String fileExt) throws IOException {
|
|
||||||
FileEntity fileEntity = fileEntityRepository.findById(id)
|
|
||||||
.orElseThrow(() -> new RuntimeException("File not found: " + id));
|
|
||||||
|
|
||||||
String extension = determineFileExtension(fileExt, fileEntity);
|
|
||||||
Path protectedFilePath = prepareProtectedPath(fileEntity, extension);
|
|
||||||
|
|
||||||
if (Files.exists(protectedFilePath)) {
|
|
||||||
Files.delete(protectedFilePath);
|
|
||||||
}
|
|
||||||
|
|
||||||
Files.write(protectedFilePath, data);
|
|
||||||
|
|
||||||
fileEntity.setProtectedFilePath(protectedFilePath.toString());
|
|
||||||
fileEntity.setProtectedAt(LocalDateTime.now());
|
|
||||||
fileEntity.setUpdatedAt(LocalDateTime.now());
|
|
||||||
fileEntity.setFileExtension(extension);
|
|
||||||
fileEntity.setProtectionStatus(ProtectionStatus.PROTECTED);
|
|
||||||
|
|
||||||
fileEntityRepository.save(fileEntity);
|
|
||||||
}
|
|
||||||
|
|
||||||
public FileEntity findBySignature(String signature) {
|
|
||||||
return fileEntityRepository.findBySignature(signature);
|
|
||||||
}
|
|
||||||
|
|
||||||
public String findFileIdByPath(String filePath) {
|
|
||||||
return fileEntityRepository.findFileIdByFilePath(filePath);
|
|
||||||
}
|
|
||||||
|
|
||||||
public File getFileById(String id) {
|
|
||||||
try {
|
|
||||||
FileEntity fileEntity = fileEntityRepository.findById(id).orElseThrow(() ->
|
|
||||||
new RuntimeException("File not found: " + id));
|
|
||||||
|
|
||||||
File file = new File(fileEntity.getFilePath());
|
|
||||||
|
|
||||||
if (!file.exists()) {
|
|
||||||
throw new RuntimeException("File not found on disk: " + fileEntity.getFilePath());
|
|
||||||
}
|
|
||||||
|
|
||||||
return file;
|
|
||||||
} catch (Exception e) {
|
|
||||||
log.error("Error getting file: {}", id, e);
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public Path prepareProtectedPath(FileEntity fileEntity, String extension) throws IOException {
|
|
||||||
Path originalPath = Paths.get(fileEntity.getFilePath());
|
|
||||||
|
|
||||||
String pathStr = originalPath.toString();
|
|
||||||
pathStr = pathStr.replaceFirst("/uploads/uploads/", "/uploads/protected/");
|
|
||||||
|
|
||||||
Path protectedPath = Paths.get(pathStr);
|
|
||||||
String fileName = protectedPath.getFileName().toString();
|
|
||||||
|
|
||||||
if (extension != null) {
|
|
||||||
String nameWithoutExt = fileName;
|
|
||||||
int lastDotIndex = fileName.lastIndexOf('.');
|
|
||||||
if (lastDotIndex > 0) {
|
|
||||||
nameWithoutExt = fileName.substring(0, lastDotIndex);
|
|
||||||
}
|
|
||||||
fileName = nameWithoutExt + "." + extension;
|
|
||||||
|
|
||||||
protectedPath = protectedPath.getParent().resolve(fileName);
|
|
||||||
}
|
|
||||||
|
|
||||||
Files.createDirectories(protectedPath.getParent());
|
|
||||||
|
|
||||||
return protectedPath;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void updateSignature(String signature, String fileId) throws IOException {
|
|
||||||
FileEntity fileEntity = fileEntityRepository.findByFileId(fileId);
|
|
||||||
fileEntity.setSignature(signature);
|
|
||||||
fileEntity.setProtectionStatus(ProtectionStatus.PROTECTED);
|
|
||||||
fileEntity.setUpdatedAt(LocalDateTime.now());
|
|
||||||
fileEntity.setProtectedAt(LocalDateTime.now());
|
|
||||||
fileEntity.setProtectedFilePath(prepareProtectedPath(fileEntity, fileEntity.getFileExtension()).toString());
|
|
||||||
|
|
||||||
fileEntityRepository.save(fileEntity);
|
|
||||||
}
|
|
||||||
|
|
||||||
private boolean checkFileExistsOnDisk(String filePath) {
|
private boolean checkFileExistsOnDisk(String filePath) {
|
||||||
try {
|
try {
|
||||||
return Files.exists(Paths.get(filePath));
|
return Files.exists(Paths.get(filePath));
|
||||||
@@ -337,14 +202,6 @@ public class FileEntityService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private String determineFileExtension(String fileExt, FileEntity fileEntity) {
|
|
||||||
if (fileExt != null && !fileExt.trim().isEmpty()) {
|
|
||||||
return fileExt;
|
|
||||||
} else {
|
|
||||||
return fileEntity.getFileExtension();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private FileEntityResponse convertToResponse(FileEntity fileEntity, int version) {
|
private FileEntityResponse convertToResponse(FileEntity fileEntity, int version) {
|
||||||
boolean existsOnDisk = checkFileExistsOnDisk(fileEntity.getFilePath());
|
boolean existsOnDisk = checkFileExistsOnDisk(fileEntity.getFilePath());
|
||||||
|
|
||||||
@@ -366,7 +223,6 @@ public class FileEntityService {
|
|||||||
.downloadUrl("/api/v" + version + "/files/download/" + fileEntity.getId())
|
.downloadUrl("/api/v" + version + "/files/download/" + fileEntity.getId())
|
||||||
.existsOnDisk(existsOnDisk)
|
.existsOnDisk(existsOnDisk)
|
||||||
.supportId(fileEntity.getSupportId())
|
.supportId(fileEntity.getSupportId())
|
||||||
.protectStatus(fileEntity.getProtectionStatus().toString())
|
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,53 +0,0 @@
|
|||||||
package ru.soune.nocopy.service.file;
|
|
||||||
|
|
||||||
import com.vrt.NoCopyFileService;
|
|
||||||
import com.vrt.fileprotection.FileProtector;
|
|
||||||
import lombok.RequiredArgsConstructor;
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
|
||||||
import org.springframework.scheduling.annotation.Scheduled;
|
|
||||||
import org.springframework.stereotype.Service;
|
|
||||||
import ru.soune.nocopy.entity.file.FileEntity;
|
|
||||||
import ru.soune.nocopy.entity.file.ProtectionStatus;
|
|
||||||
import ru.soune.nocopy.repository.FileEntityRepository;
|
|
||||||
import ru.soune.nocopy.util.FileUtil;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
@Slf4j
|
|
||||||
@Service
|
|
||||||
@RequiredArgsConstructor
|
|
||||||
public class FileProcessingOrchestrator {
|
|
||||||
|
|
||||||
private final NoCopyFileService noCopyFileService;
|
|
||||||
|
|
||||||
private final FileEntityRepository fileRepository;
|
|
||||||
|
|
||||||
private final FileUtil fileUtil;
|
|
||||||
|
|
||||||
public void initializeProcessingQueue() {
|
|
||||||
List<FileEntity> filesToProtect = fileRepository.findByProtectionStatus(ProtectionStatus.NOT_PROTECTED);
|
|
||||||
|
|
||||||
for (FileEntity fileEntity : filesToProtect) {
|
|
||||||
try {
|
|
||||||
FileProtector.FileInfo fileInfo = fileUtil.createFileInfo(fileEntity);
|
|
||||||
|
|
||||||
noCopyFileService.addFile(fileInfo);
|
|
||||||
|
|
||||||
log.info("Add to query: {}", fileEntity.getOriginalFileName());
|
|
||||||
} catch (Exception e) {
|
|
||||||
log.error("Fail add to query: {}", fileEntity.getId(), e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Scheduled(fixedDelay = 120000)
|
|
||||||
public void checkNewFilesForProtection() {
|
|
||||||
List<FileEntity> newFiles = fileRepository.findAllActiveFilesAndNotProtected();
|
|
||||||
|
|
||||||
for (FileEntity fileEntity : newFiles) {
|
|
||||||
FileProtector.FileInfo fileInfo = fileUtil.createFileInfo(fileEntity);
|
|
||||||
|
|
||||||
noCopyFileService.addFile(fileInfo);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -6,7 +6,6 @@ import ru.soune.nocopy.dto.file.FileInfoUserResponse;
|
|||||||
import ru.soune.nocopy.entity.file.FileEntity;
|
import ru.soune.nocopy.entity.file.FileEntity;
|
||||||
import ru.soune.nocopy.entity.file.FileStatus;
|
import ru.soune.nocopy.entity.file.FileStatus;
|
||||||
import ru.soune.nocopy.entity.file.FileType;
|
import ru.soune.nocopy.entity.file.FileType;
|
||||||
import ru.soune.nocopy.entity.file.ProtectionStatus;
|
|
||||||
import ru.soune.nocopy.repository.FileEntityRepository;
|
import ru.soune.nocopy.repository.FileEntityRepository;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@@ -27,37 +26,23 @@ public class FileStatsService {
|
|||||||
.fileCount(calculateTotalCount(files))
|
.fileCount(calculateTotalCount(files))
|
||||||
.filesCheck(calculateByStatus(files, FileStatus.CHECKED))
|
.filesCheck(calculateByStatus(files, FileStatus.CHECKED))
|
||||||
.filesViolation(calculateByStatus(files, FileStatus.VIOLATION))
|
.filesViolation(calculateByStatus(files, FileStatus.VIOLATION))
|
||||||
.protectedFilesCount(protectedUserFiles(files))
|
|
||||||
|
|
||||||
.imagesSize(calculateMediaSize(files, FileType.IMAGE))
|
.imagesSize(calculateMediaSize(files, FileType.IMAGE))
|
||||||
.imagesCount(calculateMediaCount(files, FileType.IMAGE))
|
.imagesCount(calculateMediaCount(files, FileType.IMAGE))
|
||||||
.imagesCheck(calculateMediaByStatus(files, FileType.IMAGE, FileStatus.CHECKED))
|
.imagesCheck(calculateMediaByStatus(files, FileType.IMAGE, FileStatus.CHECKED))
|
||||||
.imagesViolations(calculateMediaByStatus(files, FileType.IMAGE, FileStatus.VIOLATION))
|
.imagesViolations(calculateMediaByStatus(files, FileType.IMAGE, FileStatus.VIOLATION))
|
||||||
.protectedImageFilesCount(protectedUserFiles(files, FileType.IMAGE))
|
|
||||||
|
|
||||||
.videosSize(calculateMediaSize(files, FileType.VIDEO))
|
.videosSize(calculateMediaSize(files, FileType.VIDEO))
|
||||||
.videosCount(calculateMediaCount(files, FileType.VIDEO))
|
.videosCount(calculateMediaCount(files, FileType.VIDEO))
|
||||||
.videosCheck(calculateMediaByStatus(files, FileType.VIDEO, FileStatus.CHECKED))
|
.videosCheck(calculateMediaByStatus(files, FileType.VIDEO, FileStatus.CHECKED))
|
||||||
.videosViolations(calculateMediaByStatus(files, FileType.VIDEO, FileStatus.VIOLATION))
|
.videosViolations(calculateMediaByStatus(files, FileType.VIDEO, FileStatus.VIOLATION))
|
||||||
.protectedVideoFilesCount(protectedUserFiles(files, FileType.VIDEO))
|
|
||||||
|
|
||||||
.audiosSize(calculateMediaSize(files, FileType.AUDIO))
|
.audiosSize(calculateMediaSize(files, FileType.AUDIO))
|
||||||
.audiosCount(calculateMediaCount(files, FileType.AUDIO))
|
.audiosCount(calculateMediaCount(files, FileType.AUDIO))
|
||||||
.audiosCheck(calculateMediaByStatus(files, FileType.AUDIO, FileStatus.CHECKED))
|
.audiosCheck(calculateMediaByStatus(files, FileType.AUDIO, FileStatus.CHECKED))
|
||||||
.audiosViolations(calculateMediaByStatus(files, FileType.AUDIO, FileStatus.VIOLATION))
|
.audiosViolations(calculateMediaByStatus(files, FileType.AUDIO, FileStatus.VIOLATION))
|
||||||
.protectedAudioFilesCount(protectedUserFiles(files, FileType.AUDIO))
|
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
private Long protectedUserFiles(List<FileEntity> files) {
|
|
||||||
return files.stream().filter(file -> file.getProtectionStatus() == ProtectionStatus.PROTECTED).count();
|
|
||||||
}
|
|
||||||
|
|
||||||
private Long protectedUserFiles(List<FileEntity> files, FileType type) {
|
|
||||||
return files.stream().filter(file -> file.getProtectionStatus() == ProtectionStatus.PROTECTED &&
|
|
||||||
file.getMimeType().equals(type.getDisplayName())).count();
|
|
||||||
}
|
|
||||||
|
|
||||||
private Long calculateTotalSize(List<FileEntity> files) {
|
private Long calculateTotalSize(List<FileEntity> files) {
|
||||||
return files.stream()
|
return files.stream()
|
||||||
.filter(file -> file.getStatus() != FileStatus.DELETED)
|
.filter(file -> file.getStatus() != FileStatus.DELETED)
|
||||||
|
|||||||
@@ -10,13 +10,14 @@ public interface FileUploadService {
|
|||||||
FileUploadSession initUpload(Long userId, String fileName,
|
FileUploadSession initUpload(Long userId, String fileName,
|
||||||
String fileType, String extension, long fileSize);
|
String fileType, String extension, long fileSize);
|
||||||
|
|
||||||
void handleExpiredSession(FileUploadSession session);
|
UploadProgressResponse uploadChunk(String uploadId, Integer chunkNumber,
|
||||||
|
MultipartFile chunkFile) throws IOException;
|
||||||
void cancelUpload(String uploadId);
|
|
||||||
|
|
||||||
UploadProgressResponse uploadChunk(String uploadId, Integer chunkNumber, MultipartFile chunkFile, Integer findSimilar);
|
|
||||||
|
|
||||||
UploadProgressResponse getUploadProgress(String uploadId);
|
UploadProgressResponse getUploadProgress(String uploadId);
|
||||||
|
|
||||||
void completeFileProcessingAsync(FileUploadSession session);
|
void handleExpiredSession(FileUploadSession session);
|
||||||
|
|
||||||
|
public void retryFailedUpload(String uploadId);
|
||||||
|
|
||||||
|
public void cancelUpload(String uploadId);
|
||||||
}
|
}
|
||||||
|
|||||||
+135
-171
@@ -1,7 +1,5 @@
|
|||||||
package ru.soune.nocopy.service.file.impl;
|
package ru.soune.nocopy.service.file;
|
||||||
|
|
||||||
import com.vrt.NoCopyFileService;
|
|
||||||
import com.vrt.fileprotection.FileProtector;
|
|
||||||
import jakarta.annotation.PostConstruct;
|
import jakarta.annotation.PostConstruct;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
@@ -12,22 +10,12 @@ import org.springframework.stereotype.Service;
|
|||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
import ru.soune.nocopy.dto.file.UploadProgressResponse;
|
import ru.soune.nocopy.dto.file.UploadProgressResponse;
|
||||||
import ru.soune.nocopy.entity.file.FileEntity;
|
|
||||||
import ru.soune.nocopy.entity.file.FileStatus;
|
|
||||||
import ru.soune.nocopy.entity.file.FileUploadSession;
|
import ru.soune.nocopy.entity.file.FileUploadSession;
|
||||||
import ru.soune.nocopy.entity.file.UploadStatus;
|
import ru.soune.nocopy.entity.file.UploadStatus;
|
||||||
import ru.soune.nocopy.exception.ChunkSizeExceededException;
|
import ru.soune.nocopy.exception.ChunkSizeExceededException;
|
||||||
import ru.soune.nocopy.exception.DuplicateImageException;
|
|
||||||
import ru.soune.nocopy.exception.FileUploadException;
|
import ru.soune.nocopy.exception.FileUploadException;
|
||||||
import ru.soune.nocopy.exception.UploadSessionNotFoundException;
|
import ru.soune.nocopy.exception.UploadSessionNotFoundException;
|
||||||
import ru.soune.nocopy.repository.FileEntityRepository;
|
|
||||||
import ru.soune.nocopy.repository.FileUploadSessionRepository;
|
import ru.soune.nocopy.repository.FileUploadSessionRepository;
|
||||||
import ru.soune.nocopy.repository.SimilarImageProjection;
|
|
||||||
import ru.soune.nocopy.service.FileSimilarityService;
|
|
||||||
import ru.soune.nocopy.service.ImageHashService;
|
|
||||||
import ru.soune.nocopy.service.file.FileEntityService;
|
|
||||||
import ru.soune.nocopy.service.file.FileUploadService;
|
|
||||||
import ru.soune.nocopy.util.FileUtil;
|
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.nio.file.*;
|
import java.nio.file.*;
|
||||||
@@ -35,7 +23,10 @@ import java.nio.file.attribute.PosixFilePermission;
|
|||||||
import java.security.MessageDigest;
|
import java.security.MessageDigest;
|
||||||
import java.security.NoSuchAlgorithmException;
|
import java.security.NoSuchAlgorithmException;
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
import java.util.*;
|
import java.util.Comparator;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.Set;
|
||||||
|
import java.util.UUID;
|
||||||
import java.util.concurrent.CompletableFuture;
|
import java.util.concurrent.CompletableFuture;
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
@@ -66,22 +57,9 @@ public class FileUploadServiceImpl implements FileUploadService {
|
|||||||
|
|
||||||
private Path storageRoot;
|
private Path storageRoot;
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private ImageHashService imageHashService;
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private FileSimilarityService fileSimilarityService;
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private FileEntityRepository fileEntityRepository;
|
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private FileEntityService fileEntityService;
|
private FileEntityService fileEntityService;
|
||||||
|
|
||||||
private final NoCopyFileService noCopyFileService;
|
|
||||||
|
|
||||||
private final FileUtil fileUtil;
|
|
||||||
|
|
||||||
@PostConstruct
|
@PostConstruct
|
||||||
public void init() {
|
public void init() {
|
||||||
try {
|
try {
|
||||||
@@ -147,38 +125,14 @@ public class FileUploadServiceImpl implements FileUploadService {
|
|||||||
return savedSession;
|
return savedSession;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
@Transactional
|
|
||||||
public void handleExpiredSession(FileUploadSession session) {
|
|
||||||
session.setStatus(UploadStatus.FAILED);
|
|
||||||
session.setLastError("Upload session expired");
|
|
||||||
sessionRepository.save(session);
|
|
||||||
|
|
||||||
CompletableFuture.runAsync(() -> cleanupSessionFiles(session));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
@Transactional
|
|
||||||
public void cancelUpload(String uploadId) {
|
|
||||||
FileUploadSession session = sessionRepository.findById(uploadId)
|
|
||||||
.orElseThrow(() -> new UploadSessionNotFoundException(uploadId));
|
|
||||||
|
|
||||||
if (session.getStatus() == UploadStatus.COMPLETED || session.getStatus() == UploadStatus.CANCELLED) {
|
|
||||||
throw new FileUploadException("Cannot cancel completed or cancelled upload");
|
|
||||||
}
|
|
||||||
|
|
||||||
session.setStatus(UploadStatus.CANCELLED);
|
|
||||||
sessionRepository.save(session);
|
|
||||||
|
|
||||||
CompletableFuture.runAsync(() -> cleanupSessionFiles(session));
|
|
||||||
|
|
||||||
log.info("Upload cancelled: {}", uploadId);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@Transactional
|
@Transactional
|
||||||
public UploadProgressResponse uploadChunk(String uploadId, Integer chunkNumber,
|
public UploadProgressResponse uploadChunk(String uploadId, Integer chunkNumber,
|
||||||
MultipartFile chunkFile, Integer findSimilar) {
|
MultipartFile chunkFile) {
|
||||||
|
|
||||||
|
log.info("Processing chunk {} for session {}, size: {} bytes",
|
||||||
|
chunkNumber, uploadId, chunkFile.getSize());
|
||||||
|
|
||||||
FileUploadSession session = sessionRepository.findById(uploadId)
|
FileUploadSession session = sessionRepository.findById(uploadId)
|
||||||
.orElseThrow(() -> new UploadSessionNotFoundException(uploadId));
|
.orElseThrow(() -> new UploadSessionNotFoundException(uploadId));
|
||||||
|
|
||||||
@@ -204,7 +158,80 @@ public class FileUploadServiceImpl implements FileUploadService {
|
|||||||
throw new ChunkSizeExceededException(chunkFile.getSize(), chunkSize);
|
throw new ChunkSizeExceededException(chunkFile.getSize(), chunkSize);
|
||||||
}
|
}
|
||||||
|
|
||||||
return processChunk(session, chunkNumber, chunkFile, findSimilar);
|
return processChunk(session, chunkNumber, chunkFile);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@Transactional
|
||||||
|
public void handleExpiredSession(FileUploadSession session) {
|
||||||
|
session.setStatus(UploadStatus.FAILED);
|
||||||
|
session.setLastError("Upload session expired");
|
||||||
|
sessionRepository.save(session);
|
||||||
|
|
||||||
|
CompletableFuture.runAsync(() -> cleanupSessionFiles(session));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Async("fileUploadTaskExecutor")
|
||||||
|
@Transactional
|
||||||
|
public void assembleFileAsync(FileUploadSession session) {
|
||||||
|
try {
|
||||||
|
assembleFile(session);
|
||||||
|
log.info("File assembly completed successfully for session: {}",
|
||||||
|
session.getUploadId());
|
||||||
|
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("Failed to assemble file for session {}: {}",
|
||||||
|
session.getUploadId(), e.getMessage(), e);
|
||||||
|
|
||||||
|
handleAssemblyFailure(session, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@Transactional
|
||||||
|
public void cancelUpload(String uploadId) {
|
||||||
|
FileUploadSession session = sessionRepository.findById(uploadId)
|
||||||
|
.orElseThrow(() -> new UploadSessionNotFoundException(uploadId));
|
||||||
|
|
||||||
|
if (session.getStatus() == UploadStatus.COMPLETED || session.getStatus() == UploadStatus.CANCELLED) {
|
||||||
|
throw new FileUploadException("Cannot cancel completed or cancelled upload");
|
||||||
|
}
|
||||||
|
|
||||||
|
session.setStatus(UploadStatus.CANCELLED);
|
||||||
|
sessionRepository.save(session);
|
||||||
|
|
||||||
|
CompletableFuture.runAsync(() -> cleanupSessionFiles(session));
|
||||||
|
|
||||||
|
log.info("Upload cancelled: {}", uploadId);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@Transactional
|
||||||
|
public void retryFailedUpload(String uploadId) {
|
||||||
|
FileUploadSession session = sessionRepository.findById(uploadId)
|
||||||
|
.orElseThrow(() -> new UploadSessionNotFoundException(uploadId));
|
||||||
|
|
||||||
|
if (session.getStatus() != UploadStatus.FAILED) {
|
||||||
|
throw new FileUploadException("Only failed uploads can be retried");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (session.getRetryCount() >= maxRetryAttempts) {
|
||||||
|
throw new FileUploadException("Max retry attempts exceeded");
|
||||||
|
}
|
||||||
|
|
||||||
|
session.setStatus(UploadStatus.UPLOADING);
|
||||||
|
session.setRetryCount(session.getRetryCount() + 1);
|
||||||
|
session.setLastError(null);
|
||||||
|
|
||||||
|
sessionRepository.save(session);
|
||||||
|
|
||||||
|
if (session.getChunksUploaded().equals(session.getTotalChunks())) {
|
||||||
|
log.info("Retrying file assembly for session: {}", uploadId);
|
||||||
|
assembleFileAsync(session);
|
||||||
|
}
|
||||||
|
|
||||||
|
log.info("Upload retry initiated for session: {} (attempt {})",
|
||||||
|
uploadId, session.getRetryCount());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -215,47 +242,6 @@ public class FileUploadServiceImpl implements FileUploadService {
|
|||||||
return UploadProgressResponse.fromSession(session);
|
return UploadProgressResponse.fromSession(session);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Async("fileUploadTaskExecutor")
|
|
||||||
@Transactional
|
|
||||||
@Override
|
|
||||||
public void completeFileProcessingAsync(FileUploadSession session) {
|
|
||||||
try {
|
|
||||||
Path filePath = Paths.get(session.getFilePath());
|
|
||||||
String checksum = calculateChecksum(filePath);
|
|
||||||
|
|
||||||
FileEntity fileEntity = FileEntity.builder()
|
|
||||||
.userId(session.getUserId())
|
|
||||||
.originalFileName(session.getFileName())
|
|
||||||
.storedFileName(filePath.getFileName().toString())
|
|
||||||
.filePath(session.getFilePath())
|
|
||||||
.fileSize(session.getFileSize())
|
|
||||||
.mimeType(session.getFileType())
|
|
||||||
.fileExtension(session.getExtension())
|
|
||||||
.checksum(checksum)
|
|
||||||
.uploadSessionId(session.getUploadId())
|
|
||||||
.status(FileStatus.ACTIVE)
|
|
||||||
.build();
|
|
||||||
|
|
||||||
FileEntity saved = fileEntityRepository.save(fileEntity);
|
|
||||||
|
|
||||||
if (session.getFileType().equals("image")) {
|
|
||||||
Map<String, Long> hash = imageHashService.calculateHash(filePath);
|
|
||||||
|
|
||||||
imageHashService.create(saved, hash);
|
|
||||||
}
|
|
||||||
|
|
||||||
cleanupSessionFiles(session);
|
|
||||||
|
|
||||||
noCopyFileService.addFile(fileUtil.createFileInfo(fileEntity));
|
|
||||||
|
|
||||||
log.info("File processing completed for session: {}", session.getUploadId());
|
|
||||||
|
|
||||||
} catch (Exception e) {
|
|
||||||
log.error("Failed to complete file processing for session {}: {}",
|
|
||||||
session.getUploadId(), e.getMessage(), e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void validateSession(FileUploadSession session) {
|
private void validateSession(FileUploadSession session) {
|
||||||
UploadStatus status = session.getStatus();
|
UploadStatus status = session.getStatus();
|
||||||
|
|
||||||
@@ -285,13 +271,14 @@ public class FileUploadServiceImpl implements FileUploadService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private UploadProgressResponse processChunk(FileUploadSession session, Integer chunkNumber, MultipartFile chunkFile,
|
private UploadProgressResponse processChunk(FileUploadSession session,
|
||||||
Integer findSimilar) {
|
Integer chunkNumber,
|
||||||
|
MultipartFile chunkFile) {
|
||||||
String chunkPath = null;
|
String chunkPath = null;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (session.getChunkPaths().containsKey(chunkNumber)) {
|
if (session.getChunkPaths().containsKey(chunkNumber)) {
|
||||||
return handleExistingChunk(session, chunkNumber, chunkFile, findSimilar);
|
return handleExistingChunk(session, chunkNumber, chunkFile);
|
||||||
}
|
}
|
||||||
|
|
||||||
chunkPath = saveChunkWithIntegrityCheck(session, chunkNumber, chunkFile);
|
chunkPath = saveChunkWithIntegrityCheck(session, chunkNumber, chunkFile);
|
||||||
@@ -304,42 +291,17 @@ public class FileUploadServiceImpl implements FileUploadService {
|
|||||||
log.debug("Chunk {} saved successfully. Uploaded: {}/{}",
|
log.debug("Chunk {} saved successfully. Uploaded: {}/{}",
|
||||||
chunkNumber, session.getChunksUploaded(), session.getTotalChunks());
|
chunkNumber, session.getChunksUploaded(), session.getTotalChunks());
|
||||||
|
|
||||||
boolean isLastChunk = session.getChunksUploaded().equals(session.getTotalChunks());
|
if (session.getChunksUploaded().equals(session.getTotalChunks())) {
|
||||||
|
|
||||||
if (isLastChunk) {
|
|
||||||
log.info("All chunks uploaded for session {}. Starting assembly...",
|
log.info("All chunks uploaded for session {}. Starting assembly...",
|
||||||
session.getUploadId());
|
session.getUploadId());
|
||||||
|
sessionRepository.save(session);
|
||||||
String finalFilePath = assembleFileSynchronously(session);
|
assembleFileAsync(session);
|
||||||
|
|
||||||
if (session.getFileType().startsWith("image") && findSimilar == 0) {
|
|
||||||
checkForDuplicatesSynchronously(finalFilePath);
|
|
||||||
}
|
|
||||||
|
|
||||||
session.setStatus(UploadStatus.COMPLETED);
|
|
||||||
session.setFilePath(finalFilePath);
|
|
||||||
|
|
||||||
completeFileProcessingAsync(session);
|
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
sessionRepository.save(session);
|
sessionRepository.save(session);
|
||||||
}
|
}
|
||||||
|
|
||||||
return UploadProgressResponse.fromSession(session);
|
return UploadProgressResponse.fromSession(session);
|
||||||
|
|
||||||
} catch (DuplicateImageException e) {
|
|
||||||
if (chunkPath != null) {
|
|
||||||
cleanupFailedChunk(chunkPath);
|
|
||||||
}
|
|
||||||
|
|
||||||
log.warn("Duplicate image found for session {}: {}",
|
|
||||||
session.getUploadId(), e.getMessage());
|
|
||||||
|
|
||||||
session.setStatus(UploadStatus.FAILED);
|
|
||||||
session.setLastError(e.getMessage());
|
|
||||||
sessionRepository.save(session);
|
|
||||||
|
|
||||||
throw e;
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
if (chunkPath != null) {
|
if (chunkPath != null) {
|
||||||
cleanupFailedChunk(chunkPath);
|
cleanupFailedChunk(chunkPath);
|
||||||
@@ -356,56 +318,32 @@ public class FileUploadServiceImpl implements FileUploadService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private String assembleFileSynchronously(FileUploadSession session) throws IOException {
|
|
||||||
Path finalFilePath = prepareFinalFile(session);
|
|
||||||
|
|
||||||
validateAllChunksExist(session);
|
|
||||||
mergeChunksToFile(session, finalFilePath);
|
|
||||||
validateFinalFile(session, finalFilePath);
|
|
||||||
|
|
||||||
return finalFilePath.toString();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void checkForDuplicatesSynchronously(String filePath)
|
|
||||||
throws IOException , DuplicateImageException {
|
|
||||||
Path path = Paths.get(filePath);
|
|
||||||
Map<String, Long> hash = imageHashService.calculateHash(path);
|
|
||||||
|
|
||||||
List<SimilarImageProjection> duplicates = fileSimilarityService.findDuplicatedByHash(
|
|
||||||
hash.get("hi"), hash.get("low"));
|
|
||||||
|
|
||||||
if (!duplicates.isEmpty()) {
|
|
||||||
throw new DuplicateImageException("Duplicate", duplicates.getFirst().getId(),
|
|
||||||
duplicates.getFirst().getUserId());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private UploadProgressResponse handleExistingChunk(FileUploadSession session,
|
private UploadProgressResponse handleExistingChunk(FileUploadSession session,
|
||||||
Integer chunkNumber,
|
Integer chunkNumber,
|
||||||
MultipartFile chunkFile,
|
MultipartFile chunkFile) throws IOException {
|
||||||
Integer findSimilar) throws IOException {
|
|
||||||
String existingPath = session.getChunkPaths().get(chunkNumber);
|
String existingPath = session.getChunkPaths().get(chunkNumber);
|
||||||
Path chunkPath = Paths.get(existingPath);
|
Path chunkPath = Paths.get(existingPath);
|
||||||
|
|
||||||
if (!Files.exists(chunkPath)) {
|
if (!Files.exists(chunkPath)) {
|
||||||
|
log.warn("Chunk file missing, re-uploading: {}", chunkPath);
|
||||||
session.getChunkPaths().remove(chunkNumber);
|
session.getChunkPaths().remove(chunkNumber);
|
||||||
session.setChunksUploaded(session.getChunksUploaded() - 1);
|
session.setChunksUploaded(session.getChunksUploaded() - 1);
|
||||||
sessionRepository.save(session);
|
sessionRepository.save(session);
|
||||||
|
return processChunk(session, chunkNumber, chunkFile);
|
||||||
return processChunk(session, chunkNumber, chunkFile, findSimilar);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
long existingSize = Files.size(chunkPath);
|
long existingSize = Files.size(chunkPath);
|
||||||
|
|
||||||
if (existingSize != chunkFile.getSize()) {
|
if (existingSize != chunkFile.getSize()) {
|
||||||
|
log.warn("Chunk size mismatch, re-uploading: {} != {}",
|
||||||
|
existingSize, chunkFile.getSize());
|
||||||
Files.deleteIfExists(chunkPath);
|
Files.deleteIfExists(chunkPath);
|
||||||
session.getChunkPaths().remove(chunkNumber);
|
session.getChunkPaths().remove(chunkNumber);
|
||||||
session.setChunksUploaded(session.getChunksUploaded() - 1);
|
session.setChunksUploaded(session.getChunksUploaded() - 1);
|
||||||
sessionRepository.save(session);
|
sessionRepository.save(session);
|
||||||
|
return processChunk(session, chunkNumber, chunkFile);
|
||||||
return processChunk(session, chunkNumber, chunkFile, findSimilar);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
log.debug("Chunk {} already uploaded and valid", chunkNumber);
|
||||||
return UploadProgressResponse.fromSession(session);
|
return UploadProgressResponse.fromSession(session);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -464,7 +402,7 @@ public class FileUploadServiceImpl implements FileUploadService {
|
|||||||
session.getUploadId(), session.getFileName());
|
session.getUploadId(), session.getFileName());
|
||||||
|
|
||||||
Path finalFilePath = null;
|
Path finalFilePath = null;
|
||||||
String checksum;
|
String checksum = null;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
finalFilePath = prepareFinalFile(session);
|
finalFilePath = prepareFinalFile(session);
|
||||||
@@ -483,21 +421,13 @@ public class FileUploadServiceImpl implements FileUploadService {
|
|||||||
session.setChecksum(checksum);
|
session.setChecksum(checksum);
|
||||||
session.setStatus(UploadStatus.COMPLETED);
|
session.setStatus(UploadStatus.COMPLETED);
|
||||||
session.setCompletedAt(LocalDateTime.now());
|
session.setCompletedAt(LocalDateTime.now());
|
||||||
|
|
||||||
sessionRepository.save(session);
|
sessionRepository.save(session);
|
||||||
log.info("Upload session updated to COMPLETED: {}", session.getUploadId());
|
log.info("Upload session updated to COMPLETED: {}", session.getUploadId());
|
||||||
|
|
||||||
try {
|
try {
|
||||||
FileEntity fileEntity = fileEntityService.createFromUploadSession(session, checksum);
|
fileEntityService.createFromUploadSession(session, checksum);
|
||||||
|
|
||||||
FileProtector.FileInfo fileInfo = fileUtil.createFileInfo(fileEntity);
|
|
||||||
|
|
||||||
noCopyFileService.addFile(fileInfo);
|
|
||||||
|
|
||||||
log.info("FileEntity successfully created for session: {}",
|
log.info("FileEntity successfully created for session: {}",
|
||||||
session.getUploadId());
|
session.getUploadId());
|
||||||
} catch (DuplicateImageException e) {
|
|
||||||
throw new DuplicateImageException("Duplicate", e.duplicateFileId(), e.userId());
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("Failed to create FileEntity for session {} (file uploaded, but metadata not saved): {}",
|
log.error("Failed to create FileEntity for session {} (file uploaded, but metadata not saved): {}",
|
||||||
session.getUploadId(), e.getMessage());
|
session.getUploadId(), e.getMessage());
|
||||||
@@ -661,10 +591,44 @@ public class FileUploadServiceImpl implements FileUploadService {
|
|||||||
private void handleAssemblyFailure(FileUploadSession session, Exception e) {
|
private void handleAssemblyFailure(FileUploadSession session, Exception e) {
|
||||||
session.setStatus(UploadStatus.FAILED);
|
session.setStatus(UploadStatus.FAILED);
|
||||||
session.setLastError(e.getMessage());
|
session.setLastError(e.getMessage());
|
||||||
|
|
||||||
sessionRepository.save(session);
|
sessionRepository.save(session);
|
||||||
|
|
||||||
|
log.error("File assembly failed for session {}: {}",
|
||||||
|
session.getUploadId(), e.getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// @Override
|
||||||
|
// @Transactional
|
||||||
|
// public void cleanupExpiredSessions() {
|
||||||
|
// log.info("Starting cleanup of expired upload sessions");
|
||||||
|
//
|
||||||
|
// LocalDateTime expiryThreshold = LocalDateTime.now().minusHours(sessionExpiryHours);
|
||||||
|
//
|
||||||
|
// try {
|
||||||
|
// var expiredSessions = sessionRepository.findByStatusInAndCreatedAtBefore(
|
||||||
|
// Set.of(UploadStatus.INITIATED, UploadStatus.UPLOADING, UploadStatus.FAILED),
|
||||||
|
// expiryThreshold
|
||||||
|
// );
|
||||||
|
//
|
||||||
|
// int cleanedCount = 0;
|
||||||
|
// for (FileUploadSession session : expiredSessions) {
|
||||||
|
// session.setStatus(UploadStatus.FAILED);
|
||||||
|
// session.setLastError("Session expired during cleanup");
|
||||||
|
// sessionRepository.save(session);
|
||||||
|
//
|
||||||
|
// CompletableFuture.runAsync(() -> cleanupSessionFiles(session));
|
||||||
|
// cleanedCount++;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// log.info("Cleaned up {} expired upload sessions", cleanedCount);
|
||||||
|
//
|
||||||
|
// } catch (Exception e) {
|
||||||
|
// log.error("Error during session cleanup", e);
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
private Path getChunkDirectory(Long userId, String uploadId) {
|
private Path getChunkDirectory(Long userId, String uploadId) {
|
||||||
return storageRoot.resolve("temp")
|
return storageRoot.resolve("temp")
|
||||||
.resolve("user")
|
.resolve("user")
|
||||||
@@ -1,35 +0,0 @@
|
|||||||
package ru.soune.nocopy.service.file;
|
|
||||||
|
|
||||||
import lombok.RequiredArgsConstructor;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.scheduling.annotation.Scheduled;
|
|
||||||
import org.springframework.stereotype.Service;
|
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
|
||||||
import ru.soune.nocopy.entity.file.FileEntity;
|
|
||||||
import ru.soune.nocopy.entity.file.FileStatus;
|
|
||||||
import ru.soune.nocopy.repository.FileEntityRepository;
|
|
||||||
|
|
||||||
import java.nio.file.Files;
|
|
||||||
import java.nio.file.Paths;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
@Service
|
|
||||||
@RequiredArgsConstructor
|
|
||||||
public class NotExistFilesCleanupService {
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private FileEntityRepository fileEntityRepository;
|
|
||||||
|
|
||||||
@Transactional
|
|
||||||
@Scheduled(fixedDelay = 30000)
|
|
||||||
public void cleanUpNotExistFiles() {
|
|
||||||
List<FileEntity> fileEntities = fileEntityRepository.searchFileEntityByStatus(FileStatus.DELETED);
|
|
||||||
|
|
||||||
for (FileEntity fileEntity : fileEntities) {
|
|
||||||
boolean exists = Files.exists(Paths.get(fileEntity.getFilePath()));
|
|
||||||
if (!exists) {
|
|
||||||
fileEntityRepository.delete(fileEntity);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,44 +0,0 @@
|
|||||||
package ru.soune.nocopy.service.file.impl;
|
|
||||||
|
|
||||||
import com.vrt.AudioFilePathProvider;
|
|
||||||
import com.vrt.fileprotection.FileProtector;
|
|
||||||
import lombok.RequiredArgsConstructor;
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
|
||||||
import org.springframework.stereotype.Service;
|
|
||||||
import ru.soune.nocopy.entity.file.FileEntity;
|
|
||||||
import ru.soune.nocopy.repository.FileEntityRepository;
|
|
||||||
import ru.soune.nocopy.service.file.FileEntityService;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.util.Optional;
|
|
||||||
|
|
||||||
@Slf4j
|
|
||||||
@Service
|
|
||||||
@RequiredArgsConstructor
|
|
||||||
public class AudioFilePathProviderImpl implements AudioFilePathProvider {
|
|
||||||
|
|
||||||
private final FileEntityService fileEntityService;
|
|
||||||
|
|
||||||
private final FileEntityRepository fileEntityRepository;
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public @NotNull String providePath(@NotNull FileProtector.FileInfo fileInfo) {
|
|
||||||
String filePath;
|
|
||||||
Optional<FileEntity> optionalFileEntity = fileEntityRepository.findById(fileInfo.getId());
|
|
||||||
|
|
||||||
if (optionalFileEntity.isEmpty()) {
|
|
||||||
throw new NullPointerException();
|
|
||||||
}
|
|
||||||
|
|
||||||
FileEntity fileEntity = optionalFileEntity.get();
|
|
||||||
|
|
||||||
try {
|
|
||||||
filePath = fileEntityService.prepareProtectedPath(fileEntity, fileEntity.getFileExtension()).toString();
|
|
||||||
} catch (IOException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
|
||||||
|
|
||||||
return filePath;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,42 +0,0 @@
|
|||||||
package ru.soune.nocopy.service.file.impl;
|
|
||||||
|
|
||||||
import com.vrt.fileprotection.FileProtector;
|
|
||||||
import com.vrt.fileprotection.audio.AudioLocalSearch;
|
|
||||||
import lombok.RequiredArgsConstructor;
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
|
||||||
import org.jetbrains.annotations.Nullable;
|
|
||||||
import org.springframework.stereotype.Service;
|
|
||||||
import ru.soune.nocopy.entity.file.FileEntity;
|
|
||||||
import ru.soune.nocopy.service.file.FileEntityService;
|
|
||||||
import ru.soune.nocopy.util.FileUtil;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
|
|
||||||
@Slf4j
|
|
||||||
@Service
|
|
||||||
@RequiredArgsConstructor
|
|
||||||
public class AudioLocalSearchImpl implements AudioLocalSearch {
|
|
||||||
|
|
||||||
private final FileEntityService fileEntityService;
|
|
||||||
|
|
||||||
private final FileUtil fileUtil;
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public @Nullable FileProtector.FileInfo findBySignature(@NotNull String signature) {
|
|
||||||
FileEntity fileEntity = fileEntityService.findBySignature(signature);
|
|
||||||
|
|
||||||
return fileUtil.createFileInfo(fileEntity);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void updateSignature(@NotNull String signature, @NotNull FileProtector.FileInfo fileInfo) {
|
|
||||||
try {
|
|
||||||
fileEntityService.updateSignature(signature, fileInfo.getId());
|
|
||||||
} catch (IOException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,54 +0,0 @@
|
|||||||
package ru.soune.nocopy.service.file.impl;
|
|
||||||
|
|
||||||
import com.vrt.fileprotection.FileProtector;
|
|
||||||
import com.vrt.fileprotection.image.ImageLocalSearch;
|
|
||||||
import com.vrt.fileprotection.image.phash.PHash;
|
|
||||||
import lombok.RequiredArgsConstructor;
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
|
||||||
import org.jetbrains.annotations.Nullable;
|
|
||||||
import org.springframework.stereotype.Service;
|
|
||||||
import ru.soune.nocopy.dto.file.SimilarFileDTO;
|
|
||||||
import ru.soune.nocopy.entity.file.FileEntity;
|
|
||||||
import ru.soune.nocopy.entity.file.ImageHashEntity;
|
|
||||||
import ru.soune.nocopy.repository.FileEntityRepository;
|
|
||||||
import ru.soune.nocopy.repository.ImageHashRepository;
|
|
||||||
import ru.soune.nocopy.service.FileSimilarityService;
|
|
||||||
import ru.soune.nocopy.util.FileUtil;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
@Slf4j
|
|
||||||
@Service
|
|
||||||
@RequiredArgsConstructor
|
|
||||||
public class ImageLocalSearchImpl implements ImageLocalSearch {
|
|
||||||
|
|
||||||
private final FileSimilarityService fileSimilarityService;
|
|
||||||
|
|
||||||
private final ImageHashRepository imageHashRepository;
|
|
||||||
|
|
||||||
private final FileUtil fileUtil;
|
|
||||||
|
|
||||||
private final FileEntityRepository fileEntityRepository;
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public @NotNull List<Result> find(@NotNull FileProtector.FileInfo fileInfo) {
|
|
||||||
List<SimilarFileDTO> duplicateByHammingDistance =
|
|
||||||
fileSimilarityService.findDuplicateByHammingDistance(fileInfo.getId(), 10,
|
|
||||||
5, 10);
|
|
||||||
|
|
||||||
return duplicateByHammingDistance.stream()
|
|
||||||
.map(fileUtil::convertToResult)
|
|
||||||
.toList();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public @Nullable FileProtector.FileInfo findByPHash(@NotNull PHash pHash) {
|
|
||||||
ImageHashEntity imageHashEntity =
|
|
||||||
imageHashRepository.findByHash64HiAndHash64Lo(pHash.getFirstPart(),
|
|
||||||
pHash.getSecondPart());
|
|
||||||
FileEntity file = fileEntityRepository.findByFileId(imageHashEntity.getFileId());
|
|
||||||
|
|
||||||
return new FileProtector.FileInfo(FileProtector.Type.IMAGE, imageHashEntity.getFileId(),
|
|
||||||
String.valueOf(file.getUserId()));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,34 +0,0 @@
|
|||||||
package ru.soune.nocopy.service.file.impl;
|
|
||||||
|
|
||||||
import com.vrt.fileprotection.FileProtector;
|
|
||||||
import com.vrt.fileprotection.OperationResult;
|
|
||||||
import com.vrt.fileprotection.image.ImageUniqueCheck;
|
|
||||||
import lombok.RequiredArgsConstructor;
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
|
||||||
import org.springframework.stereotype.Service;
|
|
||||||
import ru.soune.nocopy.dto.file.SimilarFileDTO;
|
|
||||||
import ru.soune.nocopy.service.FileSimilarityService;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
@Slf4j
|
|
||||||
@Service
|
|
||||||
@RequiredArgsConstructor
|
|
||||||
public class ImageUniqueCheckImpl implements ImageUniqueCheck {
|
|
||||||
|
|
||||||
private final FileSimilarityService fileSimilarityService;
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public @NotNull OperationResult check(@NotNull FileProtector.FileInfo fileInfo) {
|
|
||||||
String fileId = fileInfo.getId();
|
|
||||||
List<SimilarFileDTO> duplicateByHammingDistance =
|
|
||||||
fileSimilarityService.findDuplicateByHammingDistance(fileId, 3, 3, 4);
|
|
||||||
|
|
||||||
if (duplicateByHammingDistance == null || duplicateByHammingDistance.isEmpty()) {
|
|
||||||
return OperationResult.Companion.success();
|
|
||||||
} else {
|
|
||||||
return OperationResult.Companion.failure("Duplicate file");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,36 +0,0 @@
|
|||||||
package ru.soune.nocopy.service.file.impl;
|
|
||||||
|
|
||||||
import com.vrt.fileprotection.FileProtector;
|
|
||||||
import lombok.AllArgsConstructor;
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
|
||||||
import org.springframework.stereotype.Component;
|
|
||||||
import ru.soune.nocopy.entity.file.ProtectionStatus;
|
|
||||||
import ru.soune.nocopy.service.file.FileEntityService;
|
|
||||||
|
|
||||||
@Slf4j
|
|
||||||
@Component
|
|
||||||
@AllArgsConstructor
|
|
||||||
public class NoCopyProcessingListenerImpl implements FileProtector.ProcessingListener {
|
|
||||||
|
|
||||||
private final FileEntityService fileEntityService;
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onStartProcessing(FileProtector.FileInfo fileInfo) {
|
|
||||||
fileEntityService.changeStatus(ProtectionStatus.PROCESSING, fileInfo.getId());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onProcessingFailed(FileProtector.FileInfo fileInfo) {
|
|
||||||
fileEntityService.changeStatus(ProtectionStatus.FAILED, fileInfo.getId());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onSavingFailed(FileProtector.FileInfo fileInfo) {
|
|
||||||
fileEntityService.changeStatus(ProtectionStatus.FAILED_SAVE, fileInfo.getId());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onFinish(FileProtector.FileInfo fileInfo) {
|
|
||||||
fileEntityService.changeStatus(ProtectionStatus.PROTECTED, fileInfo.getId());;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,108 +0,0 @@
|
|||||||
package ru.soune.nocopy.service.file.impl;
|
|
||||||
|
|
||||||
import com.vrt.fileprotection.FileProtector;
|
|
||||||
import com.vrt.fileprotection.OperationResult;
|
|
||||||
import lombok.RequiredArgsConstructor;
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
|
||||||
import org.jetbrains.annotations.Nullable;
|
|
||||||
import org.springframework.stereotype.Component;
|
|
||||||
import ru.soune.nocopy.service.file.FileEntityService;
|
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.nio.file.Files;
|
|
||||||
import java.nio.file.Path;
|
|
||||||
import java.nio.file.Paths;
|
|
||||||
|
|
||||||
@Slf4j
|
|
||||||
@Component
|
|
||||||
@RequiredArgsConstructor
|
|
||||||
public class ProtectionFileProviderImpl implements FileProtector.FileProvider {
|
|
||||||
|
|
||||||
private static final Path SIGNATURE_FILE_PATH = Paths.get("/data/uploads/signature");
|
|
||||||
|
|
||||||
private final FileEntityService fileEntityService;
|
|
||||||
|
|
||||||
@Nullable
|
|
||||||
@Override
|
|
||||||
public File getImageFile(@NotNull String id) {
|
|
||||||
return fileEntityService.getFileById(id);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Nullable
|
|
||||||
@Override
|
|
||||||
public File getVideoFile(@NotNull String id) {
|
|
||||||
return fileEntityService.getFileById(id);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Nullable
|
|
||||||
@Override
|
|
||||||
public File getAudioFile(@NotNull String id) {
|
|
||||||
return fileEntityService.getFileById(id);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public @Nullable File getSignature() {
|
|
||||||
File signatureFile = SIGNATURE_FILE_PATH.toFile();
|
|
||||||
|
|
||||||
if (signatureFile.exists() && signatureFile.isFile() && signatureFile.length() > 0) {
|
|
||||||
return signatureFile;
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public @NotNull OperationResult writeSignature(@NotNull byte[] bytes) {
|
|
||||||
try {
|
|
||||||
Path directory = SIGNATURE_FILE_PATH.getParent();
|
|
||||||
|
|
||||||
if (directory != null && !Files.exists(directory)) {
|
|
||||||
Files.createDirectories(directory);
|
|
||||||
}
|
|
||||||
|
|
||||||
Files.write(SIGNATURE_FILE_PATH, bytes);
|
|
||||||
|
|
||||||
return OperationResult.Companion.success();
|
|
||||||
} catch (IOException e) {
|
|
||||||
return OperationResult.Companion.failure("Not saved signature file");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@NotNull
|
|
||||||
@Override
|
|
||||||
public com.vrt.fileprotection.OperationResult writeAudioFile(@NotNull String id, @NotNull byte[] data, @Nullable String fileExt) {
|
|
||||||
try {
|
|
||||||
fileEntityService.writeProtectedFile(id, data, fileExt);
|
|
||||||
return OperationResult.Companion.success();
|
|
||||||
} catch (Exception e) {
|
|
||||||
log.error("Failed to create protected file: {}", id, e);
|
|
||||||
return OperationResult.Companion.failure("Failed to create protected file");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@NotNull
|
|
||||||
@Override
|
|
||||||
public com.vrt.fileprotection.OperationResult writeVideoFile(@NotNull String id, @NotNull byte[] data, @Nullable String fileExt) {
|
|
||||||
try {
|
|
||||||
fileEntityService.writeProtectedFile(id, data, fileExt);
|
|
||||||
return OperationResult.Companion.success();
|
|
||||||
} catch (Exception e) {
|
|
||||||
log.error("Failed to create protected file: {}", id, e);
|
|
||||||
return OperationResult.Companion.failure("Failed to create protected file");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@NotNull
|
|
||||||
@Override
|
|
||||||
public com.vrt.fileprotection.OperationResult writeImageFile(@NotNull String id, @NotNull byte[] data, @Nullable String fileExt) {
|
|
||||||
try {
|
|
||||||
fileEntityService.writeProtectedFile(id, data, fileExt);
|
|
||||||
return OperationResult.Companion.success();
|
|
||||||
} catch (Exception e) {
|
|
||||||
log.error("Failed to create protected file: {}", id, e);
|
|
||||||
return OperationResult.Companion.failure("Failed to create protected file");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,6 +1,5 @@
|
|||||||
package ru.soune.nocopy.service.mail;
|
package ru.soune.nocopy.service.mail;
|
||||||
|
|
||||||
import jakarta.mail.MessagingException;
|
|
||||||
import jakarta.mail.internet.MimeMessage;
|
import jakarta.mail.internet.MimeMessage;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
@@ -32,26 +31,56 @@ public class EmailService {
|
|||||||
@Value("${app.email.verification.standart-subject}")
|
@Value("${app.email.verification.standart-subject}")
|
||||||
private String standartSubject;
|
private String standartSubject;
|
||||||
|
|
||||||
|
@Value("${app.email.verification.drop-password-subject}")
|
||||||
|
private String dropPasswordSubject;
|
||||||
|
|
||||||
@Value("${app.email.verification.standart-address-from}")
|
@Value("${app.email.verification.standart-address-from}")
|
||||||
private String standartAdressFrom;
|
private String standartAdressFrom;
|
||||||
|
|
||||||
@Value("${app.email.verification.user}")
|
@Value("${app.email.verification.user}")
|
||||||
private String user;
|
private String user;
|
||||||
|
|
||||||
public void sendVerificationEmail(User user, String code) throws MessagingException, IOException {
|
public void sendVerificationEmail(User user, String code) {
|
||||||
MimeMessage message = mailSender.createMimeMessage();
|
try {
|
||||||
MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");
|
MimeMessage message = mailSender.createMimeMessage();
|
||||||
|
MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");
|
||||||
|
|
||||||
helper.setFrom(standartAdressFrom);
|
helper.setFrom(standartAdressFrom);
|
||||||
helper.setTo(user.getEmail());
|
helper.setTo(user.getEmail());
|
||||||
helper.setSubject(standartSubject);
|
helper.setSubject(standartSubject);
|
||||||
|
|
||||||
String htmlContent = loadAndProcessTemplate(user.getFullName(), code);
|
String htmlContent = loadAndProcessTemplate(user.getFullName(), code,
|
||||||
helper.setText(htmlContent, true);
|
"templates/email-verification.html");
|
||||||
|
helper.setText(htmlContent, true);
|
||||||
|
|
||||||
mailSender.send(message);
|
mailSender.send(message);
|
||||||
|
|
||||||
log.info("Email sent to: {}", user.getEmail());
|
log.info("Email sent to: {}", user.getEmail());
|
||||||
|
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("Failed to send email: {}", e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void sendResetPasswordEmail(User user, String code) {
|
||||||
|
try {
|
||||||
|
MimeMessage message = mailSender.createMimeMessage();
|
||||||
|
MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");
|
||||||
|
|
||||||
|
helper.setFrom(standartAdressFrom);
|
||||||
|
helper.setTo(user.getEmail());
|
||||||
|
helper.setSubject(dropPasswordSubject);
|
||||||
|
|
||||||
|
String htmlContent = loadAndProcessTemplate(user.getFullName(), code,
|
||||||
|
"templates/reset-password.html");
|
||||||
|
helper.setText(htmlContent, true);
|
||||||
|
|
||||||
|
mailSender.send(message);
|
||||||
|
|
||||||
|
log.info("Email sent to: {}", user.getEmail());
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("Failed to send email: {}", e.getMessage());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
@@ -71,8 +100,8 @@ public class EmailService {
|
|||||||
return String.valueOf(code);
|
return String.valueOf(code);
|
||||||
}
|
}
|
||||||
|
|
||||||
private String loadAndProcessTemplate(String username, String code) throws IOException {
|
private String loadAndProcessTemplate(String username, String code, String templatePath) throws IOException {
|
||||||
ClassPathResource resource = new ClassPathResource("templates/email-verification.html");
|
ClassPathResource resource = new ClassPathResource(templatePath);
|
||||||
String template = new String(resource.getInputStream().readAllBytes(), StandardCharsets.UTF_8);
|
String template = new String(resource.getInputStream().readAllBytes(), StandardCharsets.UTF_8);
|
||||||
String safeUsername = escapeHtml(username != null ? username : user);
|
String safeUsername = escapeHtml(username != null ? username : user);
|
||||||
String safeCode = escapeHtml(code);
|
String safeCode = escapeHtml(code);
|
||||||
|
|||||||
@@ -11,9 +11,11 @@ import ru.soune.nocopy.dto.register.LoginRequest;
|
|||||||
import ru.soune.nocopy.dto.register.RegRequest;
|
import ru.soune.nocopy.dto.register.RegRequest;
|
||||||
import ru.soune.nocopy.entity.user.AuthToken;
|
import ru.soune.nocopy.entity.user.AuthToken;
|
||||||
import ru.soune.nocopy.entity.user.User;
|
import ru.soune.nocopy.entity.user.User;
|
||||||
|
import ru.soune.nocopy.entity.user.UserNotActive;
|
||||||
import ru.soune.nocopy.exception.NotFoundAuthToken;
|
import ru.soune.nocopy.exception.NotFoundAuthToken;
|
||||||
import ru.soune.nocopy.exception.NotValidFieldException;
|
import ru.soune.nocopy.exception.NotValidFieldException;
|
||||||
import ru.soune.nocopy.repository.AuthTokenRepository;
|
import ru.soune.nocopy.repository.AuthTokenRepository;
|
||||||
|
import ru.soune.nocopy.repository.UserNotActiveRepository;
|
||||||
import ru.soune.nocopy.repository.UserRepository;
|
import ru.soune.nocopy.repository.UserRepository;
|
||||||
|
|
||||||
import java.security.SecureRandom;
|
import java.security.SecureRandom;
|
||||||
@@ -30,7 +32,7 @@ public class AuthService {
|
|||||||
|
|
||||||
private final PasswordEncoder passwordEncoder;
|
private final PasswordEncoder passwordEncoder;
|
||||||
|
|
||||||
private final MessageSource messageSource;
|
private final UserNotActiveRepository userNotActiveRepository;
|
||||||
|
|
||||||
private final SecureRandom secureRandom = new SecureRandom();
|
private final SecureRandom secureRandom = new SecureRandom();
|
||||||
|
|
||||||
@@ -76,14 +78,30 @@ public class AuthService {
|
|||||||
return authToken.getUser().getId();
|
return authToken.getUser().getId();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Transactional
|
|
||||||
public AuthToken login(LoginRequest request) {
|
public AuthToken login(LoginRequest request) {
|
||||||
User user = userRepository.findByEmail(request.getEmail());
|
User user = userRepository.findByEmail(request.getEmail());
|
||||||
|
|
||||||
|
if (!user.isActive()) {
|
||||||
|
throw new NotValidFieldException("User not active", new BaseResponse(20003,
|
||||||
|
MessageCode.USER_NOT_ACTIVE.getCode(),
|
||||||
|
MessageCode.USER_NOT_ACTIVE.getDescription(), Map.of("userId", user.getId())));
|
||||||
|
}
|
||||||
|
|
||||||
if (!passwordEncoder.matches(request.getPassword(), user.getPassword())) {
|
if (!passwordEncoder.matches(request.getPassword(), user.getPassword())) {
|
||||||
LoginAnswer loginAnswer = new LoginAnswer();
|
LoginAnswer loginAnswer = new LoginAnswer();
|
||||||
loginAnswer.setFieldErrors(Arrays.asList(Map.of("password", request.getPassword())));
|
loginAnswer.setFieldErrors(Arrays.asList(Map.of("password", request.getPassword())));
|
||||||
|
loginAnswer.setVerified(user.isEmailVerified());
|
||||||
|
loginAnswer.setActive(user.isActive());
|
||||||
|
|
||||||
|
int attempts = user.getFailedLoginAttempts() + 1;
|
||||||
|
user.setFailedLoginAttempts(attempts);
|
||||||
|
|
||||||
|
if (attempts >= 5) {
|
||||||
|
user.setActive(false);
|
||||||
|
blockUser(user);
|
||||||
|
}
|
||||||
|
|
||||||
|
userRepository.save(user);
|
||||||
|
|
||||||
throw new NotValidFieldException("Invalid password", new BaseResponse(20003,
|
throw new NotValidFieldException("Invalid password", new BaseResponse(20003,
|
||||||
MessageCode.AUTH_PASSWORD_NOT_MATCHES.getCode(),
|
MessageCode.AUTH_PASSWORD_NOT_MATCHES.getCode(),
|
||||||
@@ -91,6 +109,9 @@ public class AuthService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
user.setLastLoginAt(LocalDateTime.now());
|
user.setLastLoginAt(LocalDateTime.now());
|
||||||
|
user.setFailedLoginAttempts(0);
|
||||||
|
user.setActive(true);
|
||||||
|
|
||||||
User savedUser = userRepository.save(user);
|
User savedUser = userRepository.save(user);
|
||||||
|
|
||||||
AuthToken authToken = new AuthToken();
|
AuthToken authToken = new AuthToken();
|
||||||
@@ -106,6 +127,7 @@ public class AuthService {
|
|||||||
.ifPresent(authTokenRepository::delete);
|
.ifPresent(authTokenRepository::delete);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
public AuthToken generateAuthToken(User user) {
|
public AuthToken generateAuthToken(User user) {
|
||||||
return authTokenRepository.save(genereateAuthToken(user));
|
return authTokenRepository.save(genereateAuthToken(user));
|
||||||
}
|
}
|
||||||
@@ -123,4 +145,19 @@ public class AuthService {
|
|||||||
secureRandom.nextBytes(bytes);
|
secureRandom.nextBytes(bytes);
|
||||||
return Base64.getUrlEncoder().withoutPadding().encodeToString(bytes);
|
return Base64.getUrlEncoder().withoutPadding().encodeToString(bytes);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void blockUser(User user) {
|
||||||
|
Optional<UserNotActive> existing = userNotActiveRepository.findByUserId(user.getId());
|
||||||
|
if (existing.isEmpty()) {
|
||||||
|
LocalDateTime now = LocalDateTime.now();
|
||||||
|
|
||||||
|
UserNotActive notActive = UserNotActive.builder()
|
||||||
|
.user(user)
|
||||||
|
.createdAt(now)
|
||||||
|
.blockedUntil(now.plusMinutes(15))
|
||||||
|
.build();
|
||||||
|
|
||||||
|
userNotActiveRepository.save(notActive);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,205 +0,0 @@
|
|||||||
package ru.soune.nocopy.service.search;
|
|
||||||
|
|
||||||
import com.fasterxml.jackson.databind.DeserializationFeature;
|
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
||||||
import com.google.auth.oauth2.GoogleCredentials;
|
|
||||||
import com.google.cloud.vision.v1.*;
|
|
||||||
import com.google.protobuf.ByteString;
|
|
||||||
import jakarta.annotation.PostConstruct;
|
|
||||||
import lombok.RequiredArgsConstructor;
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
|
||||||
import org.springframework.core.io.ClassPathResource;
|
|
||||||
import org.springframework.stereotype.Service;
|
|
||||||
import ru.soune.nocopy.dto.BaseResponse;
|
|
||||||
import ru.soune.nocopy.dto.MessageCode;
|
|
||||||
import ru.soune.nocopy.dto.file.GoogleVisionSearchResponse;
|
|
||||||
import ru.soune.nocopy.entity.file.FileEntity;
|
|
||||||
import ru.soune.nocopy.exception.NotValidFieldException;
|
|
||||||
import ru.soune.nocopy.repository.FileEntityRepository;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.InputStream;
|
|
||||||
import java.nio.file.Files;
|
|
||||||
import java.nio.file.Path;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
@Slf4j
|
|
||||||
@Service
|
|
||||||
@RequiredArgsConstructor
|
|
||||||
public class GoogleVisionSearchService {
|
|
||||||
private final FileEntityRepository fileEntityRepository;
|
|
||||||
|
|
||||||
private final ObjectMapper objectMapper;
|
|
||||||
|
|
||||||
private ImageAnnotatorClient visionClient;
|
|
||||||
|
|
||||||
@PostConstruct
|
|
||||||
public void init() throws IOException {
|
|
||||||
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
|
||||||
objectMapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
|
|
||||||
|
|
||||||
initVisionClient();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void initVisionClient() throws IOException {
|
|
||||||
try (InputStream credentialsStream = new ClassPathResource("config/google-service-account.json").getInputStream()) {
|
|
||||||
GoogleCredentials credentials = GoogleCredentials.fromStream(credentialsStream);
|
|
||||||
|
|
||||||
ImageAnnotatorSettings settings = ImageAnnotatorSettings.newBuilder()
|
|
||||||
.setCredentialsProvider(() -> credentials)
|
|
||||||
.build();
|
|
||||||
|
|
||||||
this.visionClient = ImageAnnotatorClient.create(settings);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public GoogleVisionSearchResponse searchByFileEntity(String fileId) throws IOException {
|
|
||||||
FileEntity fileEntity = fileEntityRepository.findById(fileId)
|
|
||||||
.orElseThrow(() -> {
|
|
||||||
throw new NotValidFieldException("File not found", new BaseResponse(20007,
|
|
||||||
MessageCode.FILE_NOT_FOUND.getCode(), MessageCode.FILE_NOT_FOUND.getDescription(),
|
|
||||||
Map.of("fileId", fileId)));
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!isImageFile(fileEntity)) {
|
|
||||||
throw new NotValidFieldException("File not image", new BaseResponse(20007,
|
|
||||||
MessageCode.INVALID_FIELD.getCode(), MessageCode.INVALID_FIELD.getDescription(),
|
|
||||||
Map.of("file_type", fileEntity.getMimeType())));
|
|
||||||
}
|
|
||||||
|
|
||||||
byte[] fileBytes;
|
|
||||||
try {
|
|
||||||
fileBytes = readFileFromDisk(fileEntity);
|
|
||||||
} catch (IOException e) {
|
|
||||||
throw new NotValidFieldException("File not found or cannot read file", new BaseResponse(20007,
|
|
||||||
MessageCode.FILE_NOT_FOUND.getCode(), MessageCode.FILE_NOT_FOUND.getDescription(),
|
|
||||||
Map.of("fileId", fileId,
|
|
||||||
"filePath", fileEntity.getFilePath())));
|
|
||||||
}
|
|
||||||
|
|
||||||
return callGoogleVisionApi(fileBytes);
|
|
||||||
}
|
|
||||||
|
|
||||||
private boolean isImageFile(FileEntity fileEntity) {
|
|
||||||
String mimeType = fileEntity.getMimeType();
|
|
||||||
return mimeType != null && mimeType.startsWith("image");
|
|
||||||
}
|
|
||||||
|
|
||||||
private byte[] readFileFromDisk(FileEntity fileEntity) throws IOException {
|
|
||||||
Path filePath = Path.of(fileEntity.getFilePath());
|
|
||||||
|
|
||||||
if (!Files.exists(filePath) || !Files.isReadable(filePath)) {
|
|
||||||
throw new IOException("Cannot read file: " + fileEntity.getFilePath());
|
|
||||||
}
|
|
||||||
|
|
||||||
return Files.readAllBytes(filePath);
|
|
||||||
}
|
|
||||||
|
|
||||||
private GoogleVisionSearchResponse callGoogleVisionApi(byte[] imageBytes) throws IOException {
|
|
||||||
ByteString imgBytes = ByteString.copyFrom(imageBytes);
|
|
||||||
Image image = Image.newBuilder()
|
|
||||||
.setContent(imgBytes)
|
|
||||||
.build();
|
|
||||||
|
|
||||||
Feature feature = Feature.newBuilder()
|
|
||||||
.setType(Feature.Type.WEB_DETECTION)
|
|
||||||
.setMaxResults(20)
|
|
||||||
.build();
|
|
||||||
|
|
||||||
AnnotateImageRequest request = AnnotateImageRequest.newBuilder()
|
|
||||||
.addFeatures(feature)
|
|
||||||
.setImage(image)
|
|
||||||
.build();
|
|
||||||
|
|
||||||
BatchAnnotateImagesResponse response = visionClient.batchAnnotateImages(List.of(request));
|
|
||||||
|
|
||||||
if (response.getResponsesCount() == 0) {
|
|
||||||
throw new IOException("Empty response from Google Vision API");
|
|
||||||
}
|
|
||||||
|
|
||||||
AnnotateImageResponse singleResponse = response.getResponses(0);
|
|
||||||
|
|
||||||
if (singleResponse.hasError()) {
|
|
||||||
throw new IOException("Google Vision API error: " + singleResponse.getError().getMessage());
|
|
||||||
}
|
|
||||||
|
|
||||||
return convertToResponse(singleResponse.getWebDetection());
|
|
||||||
}
|
|
||||||
|
|
||||||
private GoogleVisionSearchResponse convertToResponse(WebDetection webDetection) {
|
|
||||||
GoogleVisionSearchResponse response = new GoogleVisionSearchResponse();
|
|
||||||
|
|
||||||
if (webDetection.getBestGuessLabelsCount() > 0) {
|
|
||||||
response.setBestGuessLabels(
|
|
||||||
webDetection.getBestGuessLabelsList().stream()
|
|
||||||
.map(label -> {
|
|
||||||
GoogleVisionSearchResponse.BestGuessLabel dtoLabel =
|
|
||||||
new GoogleVisionSearchResponse.BestGuessLabel();
|
|
||||||
dtoLabel.setLabel(label.getLabel());
|
|
||||||
dtoLabel.setLanguageCode(label.getLanguageCode());
|
|
||||||
return dtoLabel;
|
|
||||||
})
|
|
||||||
.toList()
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (webDetection.getFullMatchingImagesCount() > 0) {
|
|
||||||
response.setFullMatchingImages(
|
|
||||||
webDetection.getFullMatchingImagesList().stream()
|
|
||||||
.map(img -> {
|
|
||||||
GoogleVisionSearchResponse.ImageResult dtoImg =
|
|
||||||
new GoogleVisionSearchResponse.ImageResult();
|
|
||||||
dtoImg.setUrl(img.getUrl());
|
|
||||||
dtoImg.setScore(img.getScore());
|
|
||||||
return dtoImg;
|
|
||||||
})
|
|
||||||
.toList()
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (webDetection.getVisuallySimilarImagesCount() > 0) {
|
|
||||||
response.setVisuallySimilarImages(
|
|
||||||
webDetection.getVisuallySimilarImagesList().stream()
|
|
||||||
.map(img -> {
|
|
||||||
GoogleVisionSearchResponse.ImageResult dtoImg =
|
|
||||||
new GoogleVisionSearchResponse.ImageResult();
|
|
||||||
dtoImg.setUrl(img.getUrl());
|
|
||||||
dtoImg.setScore(img.getScore());
|
|
||||||
return dtoImg;
|
|
||||||
})
|
|
||||||
.toList()
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (webDetection.getPagesWithMatchingImagesCount() > 0) {
|
|
||||||
response.setPagesWithMatchingImages(
|
|
||||||
webDetection.getPagesWithMatchingImagesList().stream()
|
|
||||||
.map(page -> {
|
|
||||||
GoogleVisionSearchResponse.PageResult dtoPage =
|
|
||||||
new GoogleVisionSearchResponse.PageResult();
|
|
||||||
dtoPage.setUrl(page.getUrl());
|
|
||||||
dtoPage.setPageTitle(page.getPageTitle());
|
|
||||||
return dtoPage;
|
|
||||||
})
|
|
||||||
.toList()
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (webDetection.getPartialMatchingImagesCount() > 0) {
|
|
||||||
response.setPartialMatchingImages(
|
|
||||||
webDetection.getPartialMatchingImagesList().stream()
|
|
||||||
.map(img -> {
|
|
||||||
GoogleVisionSearchResponse.ImageResult dtoImg =
|
|
||||||
new GoogleVisionSearchResponse.ImageResult();
|
|
||||||
dtoImg.setUrl(img.getUrl());
|
|
||||||
dtoImg.setScore(img.getScore());
|
|
||||||
return dtoImg;
|
|
||||||
})
|
|
||||||
.toList()
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
return response;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,48 +0,0 @@
|
|||||||
package ru.soune.nocopy.util;
|
|
||||||
|
|
||||||
import com.vrt.fileprotection.FileProtector;
|
|
||||||
import com.vrt.fileprotection.image.ImageLocalSearch;
|
|
||||||
import com.vrt.fileprotection.image.ImageScore;
|
|
||||||
import org.springframework.stereotype.Service;
|
|
||||||
import ru.soune.nocopy.dto.file.SimilarFileDTO;
|
|
||||||
import ru.soune.nocopy.entity.file.FileEntity;
|
|
||||||
|
|
||||||
@Service
|
|
||||||
public class FileUtil {
|
|
||||||
public ImageLocalSearch.Result convertToResult(SimilarFileDTO response) {
|
|
||||||
FileProtector.FileInfo fileInfo =
|
|
||||||
new FileProtector.FileInfo(FileProtector.Type.IMAGE, response.getFileId(),
|
|
||||||
String.valueOf(response.getOwnerId()));
|
|
||||||
ImageScore imageScore = new ImageScore(ImageScore.Rate.valueOf(response.getSimilarityLevel()),
|
|
||||||
response.getHammingDistance());
|
|
||||||
|
|
||||||
return new ImageLocalSearch.Result(fileInfo, imageScore);
|
|
||||||
}
|
|
||||||
|
|
||||||
public FileProtector.FileInfo createFileInfo(FileEntity fileEntity) {
|
|
||||||
FileProtector.Type type = determineFileType(fileEntity.getMimeType());
|
|
||||||
|
|
||||||
return new FileProtector.FileInfo(type, fileEntity.getId(), String.valueOf(fileEntity.getUserId()));
|
|
||||||
}
|
|
||||||
|
|
||||||
public int hamming64(long aHi, long aLo, long bHi, long bLo) {
|
|
||||||
return Long.bitCount(aHi ^ bHi)
|
|
||||||
+ Long.bitCount(aLo ^ bLo);
|
|
||||||
}
|
|
||||||
|
|
||||||
private FileProtector.Type determineFileType(String mimeType) {
|
|
||||||
if (mimeType == null) {
|
|
||||||
return FileProtector.Type.IMAGE;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (mimeType.startsWith("image")) {
|
|
||||||
return FileProtector.Type.IMAGE;
|
|
||||||
} else if (mimeType.startsWith("video")) {
|
|
||||||
return FileProtector.Type.VIDEO;
|
|
||||||
} else if (mimeType.startsWith("audio")) {
|
|
||||||
return FileProtector.Type.AUDIO;
|
|
||||||
} else {
|
|
||||||
return FileProtector.Type.IMAGE;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -21,10 +21,6 @@ spring:
|
|||||||
# mail.smtp.starttls.enable: false
|
# mail.smtp.starttls.enable: false
|
||||||
# mail.debug: true
|
# mail.debug: true
|
||||||
|
|
||||||
flyway:
|
|
||||||
enabled: true
|
|
||||||
baseline-on-migrate: true
|
|
||||||
|
|
||||||
datasource:
|
datasource:
|
||||||
url: jdbc:postgresql://${POSTGRES_HOST}:${POSTGRES_PORT}/${POSTGRES_DB}
|
url: jdbc:postgresql://${POSTGRES_HOST}:${POSTGRES_PORT}/${POSTGRES_DB}
|
||||||
username: ${POSTGRES_USER}
|
username: ${POSTGRES_USER}
|
||||||
@@ -87,5 +83,6 @@ app:
|
|||||||
email:
|
email:
|
||||||
verification:
|
verification:
|
||||||
standart-subject: NO COPY - Подтверждение email адреса
|
standart-subject: NO COPY - Подтверждение email адреса
|
||||||
|
drop-password-subject: NO COPY - сброс пароля
|
||||||
standart-address-from: NO COPY <noreply@nocopy.com>
|
standart-address-from: NO COPY <noreply@nocopy.com>
|
||||||
user: Пользователь
|
user: Пользователь
|
||||||
@@ -1,13 +0,0 @@
|
|||||||
{
|
|
||||||
"type": "service_account",
|
|
||||||
"project_id": "ncp-1-483610",
|
|
||||||
"private_key_id": "94ea762d9a56a3362155ab33f8959bfec5ae8b42",
|
|
||||||
"private_key": "-----BEGIN PRIVATE KEY-----\nMIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCwjY9sYQfeqHwr\nqKgr8sw3Qfao/vzTa9wnmbZvrM6UJ3ukMkj08w50rP0udKxTRWnH6rpSQnD696GS\nY0/qrjEO1LeMdPJ+R0v8B+Z3PlpuQtfmxXALF4QbGQxeWg9NK7o4JM0kANkV80me\nxo6T7WnywQm1SfBIPZjwLBhkn7pfcdTDJFiZkH073yB6/tPsgd6TRQDmlvGsrGmq\nCf8E/DhxaFIHUpoUEGpxeuFPmvAicEWdURCtCXeiAxRQv+Gy0mP7uGGY6C3SeYfJ\n5PNLopgnN2ATCeIp1EjJnH28sAEvE0e/K/9z4ZThgh1mB1CaLkaQweq3YQWoiyGF\nfcoPpJ8LAgMBAAECggEABluSppx35BS9I/VW2P5NTFAbycok4JgpvWNTpoHajos9\ncJQ+/FMkWh9AnsOk0PrW7FQPkZbC6sawEs1wu0q6iYYvdedgNBvtKI5ARlsAdqgB\njlXdywh1wWQNhfhIRMcmVY89s+Yz6w5vwD/2Mm2COzOoXdbjyDYvo7ZyymzWEUnA\nwaxg7aF5cGC+iqf+l2Ym8XdY2ZavvyZ8RwgKfQGfcPuhMogg+H6au4yvXPBNxv0u\nxDCbOm2ezA4cLyKyFqi6S5KiGmk2LnCOoLrkIalmTQhnptFZofGs4ezuOvhZ6ZTi\nQVDayHzbBygmFzJn1wzw83IQ7QgMmWYoEjC+JcRlgQKBgQDg6U3QPxhdvX1dCzBj\nef6yUrIeJhiILvKnAzi6MUhaWP0mU6kcIIn8DS78ojML+leKh2R7JQFFrt87kHjK\npcaTC7wkgNCa6e3d+U95EIVUvPVrvQ5O4zIM7evTiPEeZhN9Dhuh0o3eeYxfIqZO\nsN0y8F/xPWdGW5CcdL7dcTOUkwKBgQDI9Qzr7poRaHM8jzS0M4Q4cVsYdC2P6KgI\nWD9Qbbl1rVi14YLt1FSd4w7U4YJ/kaWOrdtKklqAvM3oq5J0oEQekpixsxFvXdW+\ngJTzOoOq2lgCThBagwVKOFkAH/CwPkhFPwuVBgfVNiajbrcGF6i5VWzIHCXT+IDe\naSw64dqOqQKBgDp9EYpNTjXaeEaBCWVlLVIMZVunxotrwhiiotbwyAMOz05vRTQW\nVivg9c4nFCVSRf+1c/D+T5Vig5UG3hK9B6Xn0Fah1R3kJcKq+frey/2cYipRcO4c\n8UAhg0lwfFvOadUEnTT4/4HSlCmNZjhikDOWBS1ELZ5DY5j8V0JZFPPXAoGAXi0B\nLjw2dbwGbTYLk/ukljMBZvdjNtLolGiO22lghbaEIVCa5Ewij4+OLtO0LYabGL9/\nSnZF9ZkFwmlNjFxjMBSxfG2X2SIXflyR8V7Vv6btob7lyRUn0H2RsA5H5MB7bAA8\ntE0MNK5Y8zR6j19dEeXnwev3ClymQBT3xmx72WkCgYAEhx+piCTvD+2SFBweM3J4\ng6LefBXFWHSlftQF1Er5mI+5kr4i3M5d6zh8lKcLpygSS1q5/03CMUxEiLTZMLmG\nVX/+UgEWzdEe1Dr/7YPYcOSPiBpTRyPiq6KQC/KtPPWHouGyaPO1SAAZhlk97dQs\n9mosf4Re69Re8U6MxxGw9Q==\n-----END PRIVATE KEY-----\n",
|
|
||||||
"client_email": "image-search-service@ncp-1-483610.iam.gserviceaccount.com",
|
|
||||||
"client_id": "105492884344006453429",
|
|
||||||
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
|
|
||||||
"token_uri": "https://oauth2.googleapis.com/token",
|
|
||||||
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
|
|
||||||
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/image-search-service%40ncp-1-483610.iam.gserviceaccount.com",
|
|
||||||
"universe_domain": "googleapis.com"
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,201 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
|
<title>Восстановление пароля</title>
|
||||||
|
<style>
|
||||||
|
body {
|
||||||
|
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
|
||||||
|
line-height: 1.6;
|
||||||
|
color: #333;
|
||||||
|
margin: 0;
|
||||||
|
padding: 0;
|
||||||
|
background-color: #f5f5f5;
|
||||||
|
}
|
||||||
|
.container {
|
||||||
|
max-width: 600px;
|
||||||
|
margin: 0 auto;
|
||||||
|
background: white;
|
||||||
|
border-radius: 12px;
|
||||||
|
overflow: hidden;
|
||||||
|
box-shadow: 0 6px 20px rgba(0,0,0,0.1);
|
||||||
|
}
|
||||||
|
.header {
|
||||||
|
background: linear-gradient(135deg, #ff6b6b 0%, #ee5a52 100%);
|
||||||
|
color: white;
|
||||||
|
padding: 40px 20px;
|
||||||
|
text-align: center;
|
||||||
|
}
|
||||||
|
.logo {
|
||||||
|
font-size: 32px;
|
||||||
|
font-weight: 800;
|
||||||
|
margin-bottom: 10px;
|
||||||
|
letter-spacing: 1px;
|
||||||
|
}
|
||||||
|
.content {
|
||||||
|
padding: 40px 30px;
|
||||||
|
}
|
||||||
|
.greeting {
|
||||||
|
font-size: 18px;
|
||||||
|
margin-bottom: 20px;
|
||||||
|
}
|
||||||
|
.username {
|
||||||
|
color: #ee5a52;
|
||||||
|
font-weight: bold;
|
||||||
|
}
|
||||||
|
.code-container {
|
||||||
|
background: #f8f9fa;
|
||||||
|
border: 2px dashed #ff6b6b;
|
||||||
|
border-radius: 10px;
|
||||||
|
padding: 25px;
|
||||||
|
text-align: center;
|
||||||
|
margin: 30px 0;
|
||||||
|
}
|
||||||
|
.verification-code {
|
||||||
|
font-size: 42px;
|
||||||
|
font-weight: 800;
|
||||||
|
letter-spacing: 8px;
|
||||||
|
color: #333;
|
||||||
|
margin: 0;
|
||||||
|
}
|
||||||
|
.info-text {
|
||||||
|
color: #666;
|
||||||
|
font-size: 14px;
|
||||||
|
margin: 10px 0;
|
||||||
|
}
|
||||||
|
.warning {
|
||||||
|
background: #fff3cd;
|
||||||
|
border: 1px solid #ffeaa7;
|
||||||
|
color: #856404;
|
||||||
|
padding: 15px;
|
||||||
|
border-radius: 8px;
|
||||||
|
margin: 25px 0;
|
||||||
|
font-size: 14px;
|
||||||
|
}
|
||||||
|
.action-button {
|
||||||
|
display: inline-block;
|
||||||
|
background: linear-gradient(135deg, #ff6b6b 0%, #ee5a52 100%);
|
||||||
|
color: white;
|
||||||
|
padding: 14px 32px;
|
||||||
|
text-decoration: none;
|
||||||
|
border-radius: 8px;
|
||||||
|
font-weight: 600;
|
||||||
|
margin: 20px 0;
|
||||||
|
text-align: center;
|
||||||
|
transition: all 0.3s ease;
|
||||||
|
}
|
||||||
|
.action-button:hover {
|
||||||
|
background: linear-gradient(135deg, #ee5a52 0%, #d64541 100%);
|
||||||
|
transform: translateY(-2px);
|
||||||
|
box-shadow: 0 4px 12px rgba(238, 90, 82, 0.3);
|
||||||
|
}
|
||||||
|
.steps {
|
||||||
|
margin: 30px 0;
|
||||||
|
padding-left: 20px;
|
||||||
|
}
|
||||||
|
.steps li {
|
||||||
|
margin-bottom: 10px;
|
||||||
|
padding-left: 5px;
|
||||||
|
}
|
||||||
|
.footer {
|
||||||
|
text-align: center;
|
||||||
|
padding: 25px;
|
||||||
|
color: #666;
|
||||||
|
font-size: 12px;
|
||||||
|
border-top: 1px solid #eee;
|
||||||
|
background: #f9f9f9;
|
||||||
|
}
|
||||||
|
.highlight {
|
||||||
|
color: #ee5a52;
|
||||||
|
font-weight: 600;
|
||||||
|
}
|
||||||
|
.urgent {
|
||||||
|
color: #d63031;
|
||||||
|
font-weight: 700;
|
||||||
|
}
|
||||||
|
.link-fallback {
|
||||||
|
margin-top: 15px;
|
||||||
|
font-size: 13px;
|
||||||
|
color: #666;
|
||||||
|
}
|
||||||
|
.timer {
|
||||||
|
background: #ffebee;
|
||||||
|
border-left: 4px solid #ff6b6b;
|
||||||
|
padding: 12px 15px;
|
||||||
|
margin: 20px 0;
|
||||||
|
border-radius: 0 8px 8px 0;
|
||||||
|
}
|
||||||
|
.timer-text {
|
||||||
|
color: #d32f2f;
|
||||||
|
font-weight: 600;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div class="container">
|
||||||
|
<div class="header">
|
||||||
|
<div class="logo">NO COPY</div>
|
||||||
|
<h1>Восстановление пароля</h1>
|
||||||
|
<p>Безопасный доступ к вашему аккаунту</p>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="content">
|
||||||
|
<p class="greeting">
|
||||||
|
Здравствуйте, <span class="username">{{username}}</span>!
|
||||||
|
</p>
|
||||||
|
|
||||||
|
<p>Мы получили запрос на восстановление пароля для вашего аккаунта NO COPY.</p>
|
||||||
|
<p>Для продолжения восстановления доступа, пожалуйста, используйте следующий код подтверждения:</p>
|
||||||
|
|
||||||
|
<div class="code-container">
|
||||||
|
<p class="verification-code">{{code}}</p>
|
||||||
|
<p class="info-text">Код для восстановления пароля</p>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="timer">
|
||||||
|
<p class="timer-text">Код действителен в течение 15 минут</p>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<h3>Как использовать этот код:</h3>
|
||||||
|
<ol class="steps">
|
||||||
|
<li>Вернитесь на страницу восстановления пароля</li>
|
||||||
|
<li>Введите полученный код в поле подтверждения</li>
|
||||||
|
<li>Создайте новый надежный пароль</li>
|
||||||
|
<li>Войдите в аккаунт с новым паролем</li>
|
||||||
|
</ol>
|
||||||
|
|
||||||
|
<div style="text-align: center;">
|
||||||
|
<a href="{{resetLink}}" class="action-button">Перейти к восстановлению пароля</a>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<p class="link-fallback">
|
||||||
|
Если кнопка не работает, скопируйте и вставьте эту ссылку в браузер:<br>
|
||||||
|
<span style="color: #3498db; word-break: break-all;">{{resetLink}}</span>
|
||||||
|
</p>
|
||||||
|
|
||||||
|
<div class="warning">
|
||||||
|
<strong>Важная информация:</strong><br>
|
||||||
|
1. Если вы не запрашивали восстановление пароля, проигнорируйте это письмо.<br>
|
||||||
|
2. Никому не сообщайте этот код, включая сотрудников поддержки.<br>
|
||||||
|
3. После успешного восстановления старый пароль будет недействителен.
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<p style="font-size: 13px; color: #7f8c8d;">
|
||||||
|
<strong>Совет по безопасности:</strong> Используйте пароль длиной не менее 8 символов,
|
||||||
|
содержащий буквы (заглавные и строчные), цифры и специальные символы.
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="footer">
|
||||||
|
<p>© 2024 NO COPY. Все права защищены.</p>
|
||||||
|
<p>Это письмо отправлено автоматически, пожалуйста, не отвечайте на него.</p>
|
||||||
|
<p>Если у вас возникли вопросы или вы не запрашивали восстановление пароля,
|
||||||
|
<a href="mailto:support@nocopy.com" style="color: #ee5a52;">свяжитесь со службой поддержки</a>.</p>
|
||||||
|
<p style="font-size: 11px; margin-top: 10px;">
|
||||||
|
IP запроса: {{ipAddress}} • Время: {{timestamp}} • Устройство: {{deviceInfo}}
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
Reference in New Issue
Block a user