Merge branch 'dev' into NCBACK-35
# Conflicts: # src/main/java/ru/soune/nocopy/controller/ApiController.java # src/main/java/ru/soune/nocopy/repository/FileEntityRepository.java # src/main/java/ru/soune/nocopy/service/file/impl/FileUploadServiceImpl.java
This commit is contained in:
@@ -19,6 +19,7 @@ import org.springframework.web.multipart.MultipartFile;
|
||||
import ru.soune.nocopy.dto.file.UploadProgressResponse;
|
||||
import ru.soune.nocopy.entity.file.*;
|
||||
import ru.soune.nocopy.entity.notification.NotificationType;
|
||||
import ru.soune.nocopy.entity.tokenoperation.OperationType;
|
||||
import ru.soune.nocopy.exception.*;
|
||||
import ru.soune.nocopy.repository.FileEntityRepository;
|
||||
import ru.soune.nocopy.repository.FileUploadSessionRepository;
|
||||
@@ -30,7 +31,9 @@ import ru.soune.nocopy.service.file.FileEntityService;
|
||||
import ru.soune.nocopy.service.file.FileUploadService;
|
||||
import ru.soune.nocopy.service.file.cloud.CloudStorageService;
|
||||
import ru.soune.nocopy.service.tariff.TariffConstants;
|
||||
import ru.soune.nocopy.service.file.moderation.ModerationFileService;
|
||||
import ru.soune.nocopy.service.notification.NotificationService;
|
||||
import ru.soune.nocopy.service.register.AuthService;
|
||||
import ru.soune.nocopy.service.tariff.TariffInfoService;
|
||||
import ru.soune.nocopy.util.FileUtil;
|
||||
|
||||
@@ -91,6 +94,10 @@ public class FileUploadServiceImpl implements FileUploadService {
|
||||
|
||||
private final NotificationService notificationService;
|
||||
|
||||
private final AuthService authService;
|
||||
|
||||
private final ModerationFileService moderationFileService;
|
||||
|
||||
private final CloudStorageService cloudStorageService;
|
||||
|
||||
@PostConstruct
|
||||
@@ -189,35 +196,28 @@ public class FileUploadServiceImpl implements FileUploadService {
|
||||
|
||||
@Override
|
||||
public UploadProgressResponse uploadChunk(String uploadId, Integer chunkNumber,
|
||||
MultipartFile chunkFile, Integer findSimilar) {
|
||||
MultipartFile chunkFile, Integer findSimilar) {
|
||||
FileUploadSession session = sessionRepository.findById(uploadId)
|
||||
.orElseThrow(() -> new UploadSessionNotFoundException(uploadId));
|
||||
|
||||
validateSession(session);
|
||||
|
||||
if (session.getExpiresAt().isBefore(LocalDateTime.now())) {
|
||||
handleExpiredSession(session);
|
||||
throw new FileUploadException("Upload session expired");
|
||||
}
|
||||
|
||||
if (chunkNumber < 0 || chunkNumber >= session.getTotalChunks()) {
|
||||
throw new FileUploadException(
|
||||
String.format("Invalid chunk number %d. Expected number: %d",
|
||||
chunkNumber, session.getTotalChunks() - 1));
|
||||
}
|
||||
|
||||
if (chunkNumber == 0 && chunkFile.getSize() > chunkSize || chunkNumber + 1 == (session.getTotalChunks())
|
||||
&& chunkFile.getSize() > chunkSize) {
|
||||
throw new ChunkSizeExceededException(chunkFile.getSize(), chunkSize);
|
||||
}
|
||||
|
||||
if (chunkNumber > 0 && chunkNumber + 1 < session.getTotalChunks() && chunkFile.getSize() != chunkSize) {
|
||||
throw new ChunkSizeExceededException(chunkFile.getSize(), chunkSize);
|
||||
}
|
||||
validateSession(session, chunkNumber, chunkFile);
|
||||
|
||||
return processChunk(session, chunkNumber, chunkFile, findSimilar);
|
||||
}
|
||||
|
||||
@Override
|
||||
public UploadProgressResponse uploadPassportChunk(String uploadId, Integer chunkNumber, MultipartFile chunkFile,
|
||||
Long userId) {
|
||||
FileUploadSession session = sessionRepository.findById(uploadId).orElseThrow(() ->
|
||||
new UploadSessionNotFoundException(uploadId));
|
||||
|
||||
validateSession(session, chunkNumber, chunkFile);
|
||||
|
||||
UploadProgressResponse uploadProgressResponse = processChunk(session, chunkNumber, chunkFile);
|
||||
|
||||
return uploadProgressResponse;
|
||||
}
|
||||
|
||||
@Override
|
||||
public UploadProgressResponse getUploadProgress(String uploadId) {
|
||||
FileUploadSession session = sessionRepository.findById(uploadId)
|
||||
@@ -243,20 +243,23 @@ public class FileUploadServiceImpl implements FileUploadService {
|
||||
.fileExtension(session.getExtension())
|
||||
.checksum(checksum)
|
||||
.uploadSessionId(session.getUploadId())
|
||||
.protectionStatus(ProtectionStatus.PROCESSING)
|
||||
.status(status)
|
||||
.build();
|
||||
|
||||
if (status != FileStatus.TEMP) {
|
||||
fileEntity.setProtectionStatus(ProtectionStatus.PROCESSING);
|
||||
}
|
||||
|
||||
FileEntity saved = fileEntityRepository.save(fileEntity);
|
||||
|
||||
if (session.getFileType().equals("image")) {
|
||||
if (session.getFileType().equals("image") && status != FileStatus.PRIVATE) {
|
||||
Map<String, Long> hash = imageHashService.calculateHash(filePath);
|
||||
imageHashService.create(saved, hash);
|
||||
}
|
||||
|
||||
cleanupSessionFiles(session);
|
||||
|
||||
if (status != FileStatus.TEMP) {
|
||||
if (status != FileStatus.TEMP && status != FileStatus.PRIVATE) {
|
||||
FileProtector.FileInfo fileInfo = fileUtil.createFileInfo(fileEntity, session.getConvertTo());
|
||||
noCopyFileService.addFile(fileInfo);
|
||||
}
|
||||
@@ -271,7 +274,8 @@ public class FileUploadServiceImpl implements FileUploadService {
|
||||
}
|
||||
}
|
||||
|
||||
private void validateSession(FileUploadSession session) {
|
||||
private void validateSession(FileUploadSession session, Integer chunkNumber,
|
||||
MultipartFile chunkFile) {
|
||||
UploadStatus status = session.getStatus();
|
||||
|
||||
if (status == UploadStatus.FAILED) {
|
||||
@@ -298,6 +302,67 @@ public class FileUploadServiceImpl implements FileUploadService {
|
||||
session.setStatus(UploadStatus.UPLOADING);
|
||||
sessionRepository.save(session);
|
||||
}
|
||||
|
||||
if (session.getExpiresAt().isBefore(LocalDateTime.now())) {
|
||||
handleExpiredSession(session);
|
||||
throw new FileUploadException("Upload session expired");
|
||||
}
|
||||
|
||||
if (chunkNumber < 0 || chunkNumber >= session.getTotalChunks()) {
|
||||
throw new FileUploadException(
|
||||
String.format("Invalid chunk number %d. Expected number: %d",
|
||||
chunkNumber, session.getTotalChunks() - 1));
|
||||
}
|
||||
|
||||
if (chunkNumber == 0 && chunkFile.getSize() > chunkSize || chunkNumber + 1 == (session.getTotalChunks())
|
||||
&& chunkFile.getSize() > chunkSize) {
|
||||
throw new ChunkSizeExceededException(chunkFile.getSize(), chunkSize);
|
||||
}
|
||||
|
||||
if (chunkNumber > 0 && chunkNumber + 1 < session.getTotalChunks() && chunkFile.getSize() != chunkSize) {
|
||||
throw new ChunkSizeExceededException(chunkFile.getSize(), chunkSize);
|
||||
}
|
||||
}
|
||||
|
||||
private UploadProgressResponse processChunk(FileUploadSession session, Integer chunkNumber, MultipartFile chunkFile) {
|
||||
String chunkPath = null;
|
||||
|
||||
try {
|
||||
chunkPath = saveChunkWithIntegrityCheck(session, chunkNumber, chunkFile);
|
||||
|
||||
session.getChunkPaths().put(chunkNumber, chunkPath);
|
||||
session.setChunksUploaded(session.getChunksUploaded() + 1);
|
||||
session.setStatus(UploadStatus.UPLOADING);
|
||||
session.setExpiresAt(LocalDateTime.now().plusMinutes(1));
|
||||
|
||||
boolean isLastChunk = session.getChunksUploaded().equals(session.getTotalChunks());
|
||||
|
||||
if (isLastChunk) {
|
||||
String finalFilePath = assembleFileSynchronously(session);
|
||||
|
||||
session.setStatus(UploadStatus.COMPLETED);
|
||||
session.setFilePath(finalFilePath);
|
||||
|
||||
completeFileProcessingAsync(session, FileStatus.PRIVATE);
|
||||
} else {
|
||||
sessionRepository.save(session);
|
||||
}
|
||||
|
||||
return UploadProgressResponse.fromSession(session);
|
||||
} catch (Exception e) {
|
||||
if (chunkPath != null) {
|
||||
cleanupFailedChunk(chunkPath);
|
||||
}
|
||||
|
||||
log.error("Failed to process chunk {} for session {}: {}",
|
||||
chunkNumber, session.getUploadId(), e.getMessage(), e);
|
||||
|
||||
session.setStatus(UploadStatus.FAILED);
|
||||
session.setLastError(e.getMessage());
|
||||
sessionRepository.save(session);
|
||||
|
||||
throw new FileUploadException("Failed to upload chunk: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
@Autowired
|
||||
@@ -397,7 +462,7 @@ public class FileUploadServiceImpl implements FileUploadService {
|
||||
if (status != FileStatus.TEMP) {
|
||||
String fileType = session.getFileType();
|
||||
|
||||
tariffInfoService.writeOffTokens(session.getUserId(), costService.protectFileCost(fileType));
|
||||
tariffInfoService.writeOffTokens(session.getUserId(), costService.protectFileCost(fileType), OperationType.FILE_UPLOAD);
|
||||
}
|
||||
} else {
|
||||
sessionRepository.save(session);
|
||||
|
||||
Reference in New Issue
Block a user