NCBACK-25 work version for protect file
Test Workflow / test (push) Successful in 3s

This commit is contained in:
vladp
2026-01-13 13:28:09 +07:00
parent f136ac1f0a
commit 20e1ab321d
12 changed files with 181 additions and 92 deletions
@@ -1,7 +1,7 @@
package ru.soune.nocopy.service.file;
import com.vrt.NoCopyFileService;
import com.vrt.fileprocessor.FileProcessor;
import com.vrt.fileprotection.FileProtector;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
@@ -16,6 +16,7 @@ import java.util.List;
@Service
@RequiredArgsConstructor
public class FileProcessingOrchestrator {
private final NoCopyFileService noCopyFileService;
private final FileEntityRepository fileRepository;
@@ -25,7 +26,7 @@ public class FileProcessingOrchestrator {
for (FileEntity fileEntity : filesToProtect) {
try {
FileProcessor.FileInfo fileInfo = createFileInfo(fileEntity);
FileProtector.FileInfo fileInfo = createFileInfo(fileEntity);
noCopyFileService.addFile(fileInfo);
@@ -38,36 +39,34 @@ public class FileProcessingOrchestrator {
@Scheduled(fixedDelay = 120000)
public void checkNewFilesForProtection() {
List<FileEntity> newFiles = fileRepository.findByProtectionStatus(ProtectionStatus.NOT_PROTECTED);
List<FileEntity> newFiles = fileRepository.findAllActiveFilesAndNotProtected();
for (FileEntity fileEntity : newFiles) {
FileProcessor.FileInfo fileInfo = createFileInfo(fileEntity);
noCopyFileService.addFile(fileInfo);
FileProtector.FileInfo fileInfo = createFileInfo(fileEntity);
fileEntity.setProtectionStatus(ProtectionStatus.PROCESSING);
fileRepository.save(fileEntity);
noCopyFileService.addFile(fileInfo);
}
}
private FileProcessor.FileInfo createFileInfo(FileEntity fileEntity) {
FileProcessor.Type type = determineFileType(fileEntity.getMimeType());
private FileProtector.FileInfo createFileInfo(FileEntity fileEntity) {
FileProtector.Type type = determineFileType(fileEntity.getMimeType());
return new FileProcessor.FileInfo(type, fileEntity.getId());
return new FileProtector.FileInfo(type, fileEntity.getId(), String.valueOf(fileEntity.getUserId()));
}
private FileProcessor.Type determineFileType(String mimeType) {
private FileProtector.Type determineFileType(String mimeType) {
if (mimeType == null) {
return FileProcessor.Type.IMAGE;
return FileProtector.Type.IMAGE;
}
if (mimeType.startsWith("image")) {
return FileProcessor.Type.IMAGE;
return FileProtector.Type.IMAGE;
} else if (mimeType.startsWith("video")) {
return FileProcessor.Type.VIDEO;
return FileProtector.Type.VIDEO;
} else if (mimeType.startsWith("audio")) {
return FileProcessor.Type.AUDIO;
return FileProtector.Type.AUDIO;
} else {
return FileProcessor.Type.IMAGE;
return FileProtector.Type.IMAGE;
}
}
}
@@ -1,26 +1,46 @@
package ru.soune.nocopy.service.file;
import com.vrt.fileprocessor.FileProcessor;
import com.vrt.fileprotection.FileProtector;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
//Менять статус защиты
import ru.soune.nocopy.entity.file.FileEntity;
import ru.soune.nocopy.entity.file.ProtectionStatus;
import ru.soune.nocopy.exception.FileEntityNotFoundException;
import ru.soune.nocopy.repository.FileEntityRepository;
@Slf4j
@Component
public class NoCopyProcessingListener implements FileProcessor.ProcessingListener {
@AllArgsConstructor
public class NoCopyProcessingListener implements FileProtector.ProcessingListener {
private final FileEntityRepository fileEntityRepository;
@Override
public void onStartProcessing(FileProcessor.FileInfo fileInfo) {
public void onStartProcessing(FileProtector.FileInfo fileInfo) {
changeStatus(ProtectionStatus.PROCESSING, fileInfo.getId());
}
@Override
public void onProcessingFailed(FileProcessor.FileInfo fileInfo) {
public void onProcessingFailed(FileProtector.FileInfo fileInfo) {
changeStatus(ProtectionStatus.FAILED, fileInfo.getId());
}
@Override
public void onSavingFailed(FileProcessor.FileInfo fileInfo) {
public void onSavingFailed(FileProtector.FileInfo fileInfo) {
changeStatus(ProtectionStatus.FAILED_SAVE, fileInfo.getId());
}
@Override
public void onFinish(FileProcessor.FileInfo fileInfo) {
public void onFinish(FileProtector.FileInfo fileInfo) {
changeStatus(ProtectionStatus.PROTECTED, fileInfo.getId());;
}
private void changeStatus(ProtectionStatus newStatus, String fileId) {
FileEntity fileEntity = fileEntityRepository.findById(fileId)
.orElseThrow(() -> new FileEntityNotFoundException(fileId));
fileEntity.setProtectionStatus(newStatus);
fileEntityRepository.save(fileEntity);
}
}
@@ -1,13 +1,13 @@
package ru.soune.nocopy.service.file;
import com.vrt.fileprocessor.FileProcessor;
import kotlin.Result;
import kotlin.Unit;
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.entity.file.FileEntity;
import ru.soune.nocopy.entity.file.ProtectionStatus;
import ru.soune.nocopy.repository.FileEntityRepository;
import java.io.File;
@@ -16,62 +16,80 @@ import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.LocalDateTime;
//Сохранение и запись защищенных файлов
@Slf4j
@Component
@RequiredArgsConstructor
public class ProtectionFileProvider implements FileProcessor.FileProvider {
public class ProtectionFileProvider implements FileProtector.FileProvider {
private final FileEntityRepository fileRepository;
// @Override
public Result<Unit> writeAudioFile(String id, byte[] data, String fileExt) {
try {
writeProtectedFile(id, data, fileExt);
return new Result<>();
} catch (Exception e) {
log.error("Failed to create protected file: {}", id, e);
return new Result<>();
}
}
// @Override
public Result<Unit> writeImageFile(String id, byte[] data, String fileExt) {
try {
writeProtectedFile(id, data, fileExt);
return new Result<>();
} catch (Exception e) {
log.error("Failed to create protected file: {}", id, e);
return new Result<>();
}
}
// @Override
public Result<Unit> writeVideoFile(String id, byte[] data, String fileExt) {
try {
writeProtectedFile(id, data, fileExt);
return new Result<>();
} catch (Exception e) {
log.error("Failed to create protected file: {}", id, e);
return new Result<>();
}
}
@Nullable
@Override
public File getImageFile(String id) {
public File getImageFile(@NotNull String id) {
return getFileById(id);
}
@Nullable
@Override
public File getVideoFile(String id) {
public File getVideoFile(@NotNull String id) {
return getFileById(id);
}
@Nullable
@Override
public File getAudioFile(String id) {
public File getAudioFile(@NotNull String id) {
return getFileById(id);
}
@NotNull
@Override
public com.vrt.fileprotection.OperationResult writeSignature(@NotNull String s, @NotNull byte[] bytes) {
return null;
}
@NotNull
@Override
public com.vrt.fileprotection.OperationResult writeAudioFile(@NotNull String id, @NotNull byte[] data, @Nullable String fileExt) {
try {
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 {
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 {
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");
}
}
@Nullable
@Override
public File getSignature(@NotNull String s) {
return null;
}
private void writeProtectedFile(String id, byte[] data, String fileExt) throws IOException {
FileEntity fileEntity = fileRepository.findById(id)
@@ -87,7 +105,6 @@ public class ProtectionFileProvider implements FileProcessor.FileProvider {
Files.write(protectedFilePath, data);
fileEntity.setProtectedFilePath(protectedFilePath.toString());
fileEntity.setProtectionStatus(ProtectionStatus.PROTECTED);
fileEntity.setProtectedAt(LocalDateTime.now());
fileEntity.setUpdatedAt(LocalDateTime.now());
fileEntity.setFileExtension(extension);
@@ -126,7 +143,7 @@ public class ProtectionFileProvider implements FileProcessor.FileProvider {
Path originalPath = Paths.get(fileEntity.getFilePath());
String pathStr = originalPath.toString();
pathStr = pathStr.replaceFirst("/uploads/", "/protected/");
pathStr = pathStr.replaceFirst("/uploads/uploads/", "/uploads/protected/");
Path protectedPath = Paths.get(pathStr);
String fileName = protectedPath.getFileName().toString();