@@ -0,0 +1,100 @@
|
||||
package ru.soune.nocopy.service;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.context.MessageSource;
|
||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import ru.soune.nocopy.dto.LoginRequest;
|
||||
import ru.soune.nocopy.dto.RegisterRequest;
|
||||
import ru.soune.nocopy.entity.AuthToken;
|
||||
import ru.soune.nocopy.entity.User;
|
||||
import ru.soune.nocopy.exception.NotValidationPasswordException;
|
||||
import ru.soune.nocopy.exception.UserAlreadyExistsException;
|
||||
import ru.soune.nocopy.exception.UserNotFoundException;
|
||||
import ru.soune.nocopy.repository.AuthTokenRepository;
|
||||
import ru.soune.nocopy.repository.UserRepository;
|
||||
|
||||
import java.security.SecureRandom;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Base64;
|
||||
import java.util.Optional;
|
||||
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class AuthService {
|
||||
|
||||
private final UserRepository userRepository;
|
||||
|
||||
private final AuthTokenRepository authTokenRepository;
|
||||
|
||||
private final PasswordEncoder passwordEncoder;
|
||||
|
||||
private final MessageSource messageSource;
|
||||
|
||||
private final SecureRandom secureRandom = new SecureRandom();
|
||||
|
||||
@Transactional
|
||||
public AuthToken register(RegisterRequest registerRequest) {
|
||||
if (userRepository.existsByEmail(registerRequest.email())) {
|
||||
throw new UserAlreadyExistsException("User already exists with email: " + registerRequest.email());
|
||||
}
|
||||
|
||||
User user = new User();
|
||||
user.setFullName(registerRequest.fullName());
|
||||
user.setEmail(registerRequest.email());
|
||||
user.setPassword(passwordEncoder.encode(registerRequest.password()));
|
||||
|
||||
if (registerRequest.companyName() != null) {
|
||||
user.setCompany(registerRequest.companyName());
|
||||
}
|
||||
|
||||
if (registerRequest.phone() != null) {
|
||||
user.setPhone(registerRequest.phone());
|
||||
}
|
||||
|
||||
User savedUser = userRepository.save(user);
|
||||
|
||||
AuthToken authToken = new AuthToken();
|
||||
authToken.setToken(generateAuthToken());
|
||||
authToken.setUser(savedUser);
|
||||
|
||||
return authTokenRepository.save(authToken);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public AuthToken login(LoginRequest request) {
|
||||
Optional<User> userOpt = userRepository.findByEmail(request.getEmail());
|
||||
|
||||
if (userOpt.isEmpty()) {
|
||||
throw new UserNotFoundException("User with email " + request.getEmail() + " not found");
|
||||
}
|
||||
|
||||
User user = userOpt.get();
|
||||
|
||||
if (!passwordEncoder.matches(request.getPassword(), user.getPassword())) {
|
||||
throw new NotValidationPasswordException("Invalid password");
|
||||
}
|
||||
|
||||
user.setLastLoginAt(LocalDateTime.now());
|
||||
User savedUser = userRepository.save(user);
|
||||
|
||||
AuthToken authToken = new AuthToken();
|
||||
authToken.setToken(generateAuthToken());
|
||||
authToken.setUser(savedUser);
|
||||
|
||||
return authTokenRepository.save(authToken);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void logout(String token) {
|
||||
authTokenRepository.findByToken(token)
|
||||
.ifPresent(authTokenRepository::delete);
|
||||
}
|
||||
|
||||
private String generateAuthToken() {
|
||||
byte[] bytes = new byte[32];
|
||||
secureRandom.nextBytes(bytes);
|
||||
return Base64.getUrlEncoder().withoutPadding().encodeToString(bytes);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
package ru.soune.nocopy.service;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import ru.soune.nocopy.dto.UserContentRequest;
|
||||
import ru.soune.nocopy.dto.UserContentUpdateRequest;
|
||||
import ru.soune.nocopy.entity.file.FileType;
|
||||
import ru.soune.nocopy.entity.User;
|
||||
import ru.soune.nocopy.entity.UserContent;
|
||||
import ru.soune.nocopy.exception.ContentNotFoundException;
|
||||
import ru.soune.nocopy.exception.UserNotFoundException;
|
||||
import ru.soune.nocopy.mapper.UserContentMapper;
|
||||
import ru.soune.nocopy.repository.UserContentRepository;
|
||||
import ru.soune.nocopy.repository.UserRepository;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
@Service
|
||||
@AllArgsConstructor
|
||||
public class UserContentService {
|
||||
|
||||
private final UserContentRepository userContentRepository;
|
||||
|
||||
private final UserRepository userRepository;
|
||||
|
||||
private final UserContentMapper userContentMapper;
|
||||
|
||||
public List<UserContent> findByUser(long userId) {
|
||||
return userContentRepository.findByUserId(userId);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public UserContent addUserContent(UserContentRequest userContentRequest) {
|
||||
UserContent userContent = new UserContent();
|
||||
|
||||
Optional<User> byEmail = userRepository.findByEmail(userContentRequest.userEmail());
|
||||
User user = byEmail.orElseThrow(() -> new UserNotFoundException(userContentRequest.userEmail()));
|
||||
|
||||
userContent.setUser(user);
|
||||
userContent.setFilename(userContentRequest.fileName());
|
||||
userContent.setFileExtension(userContentRequest.fileExtension());
|
||||
userContent.setFilePath(userContentRequest.filePath());
|
||||
userContent.setFileType(FileType.valueOf(userContentRequest.fileType()));
|
||||
userContent.setOriginalFilename(userContentRequest.originalFilename());
|
||||
userContent.setProtectionLevel(userContentRequest.protectionLevel());
|
||||
userContent.setFileSize(userContentRequest.fileSize());
|
||||
|
||||
//TODO add
|
||||
// @Column(name = "protection_hash", length = 64)
|
||||
// private String protectionHash;
|
||||
// @Column(name = "watermark_id", length = 50)
|
||||
// private String watermarkId;
|
||||
|
||||
return userContentRepository.save(userContent);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public UserContent updateUserContent(UserContentUpdateRequest userContentRequest) {
|
||||
UserContent entity = userContentRepository.findById(userContentRequest.id())
|
||||
.orElseThrow(() -> new ContentNotFoundException("Content not found with: " + userContentRequest.id()));
|
||||
|
||||
userContentMapper.updateEntityFromDto(userContentRequest, entity);
|
||||
|
||||
return userContentRepository.save(entity);
|
||||
}
|
||||
|
||||
public void deleteUserContent(long userContentId) {
|
||||
userContentRepository.deleteById(userContentId);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
package ru.soune.nocopy.service;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import ru.soune.nocopy.dto.ChangePasswordRequest;
|
||||
import ru.soune.nocopy.dto.UserDTO;
|
||||
import ru.soune.nocopy.dto.UserRequest;
|
||||
import ru.soune.nocopy.entity.User;
|
||||
import ru.soune.nocopy.exception.InvalidUserEmail;
|
||||
import ru.soune.nocopy.exception.NotValidationPasswordException;
|
||||
import ru.soune.nocopy.exception.UserNotFoundException;
|
||||
import ru.soune.nocopy.repository.UserRepository;
|
||||
|
||||
@Service
|
||||
@AllArgsConstructor
|
||||
public class UserService {
|
||||
|
||||
private final UserRepository userRepository;
|
||||
|
||||
private final PasswordEncoder passwordEncoder;
|
||||
|
||||
@Transactional
|
||||
public User changePassword(User user, ChangePasswordRequest request) {
|
||||
if (!passwordEncoder.matches(request.getCurrentPassword(), user.getPassword())) {
|
||||
throw new NotValidationPasswordException("Current password is incorrect");
|
||||
}
|
||||
|
||||
user.setPassword(passwordEncoder.encode(request.getNewPassword()));
|
||||
|
||||
return userRepository.save(user);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public UserDTO updateUser(UserRequest userRequest, User currentUser) {
|
||||
User user = userRepository.findById(currentUser.getId())
|
||||
.orElseThrow(() -> new UserNotFoundException("User not found with id: " + currentUser.getId()));
|
||||
|
||||
updateUserFromRequest(user, userRequest);
|
||||
|
||||
User updatedUser = userRepository.save(user);
|
||||
|
||||
return mapToDTO(updatedUser);
|
||||
}
|
||||
|
||||
private void updateUserFromRequest(User user, UserRequest request) {
|
||||
if (request.getFullName() != null) {
|
||||
user.setFullName(request.getFullName());
|
||||
}
|
||||
|
||||
if (request.getCompany() != null) {
|
||||
user.setCompany(request.getCompany());
|
||||
}
|
||||
|
||||
if (request.getEmail() != null && !request.getEmail().equals(user.getEmail())) {
|
||||
if (userRepository.existsByEmail(request.getEmail())) {
|
||||
throw new InvalidUserEmail("Email already exists: " + request.getEmail());
|
||||
}
|
||||
user.setEmail(request.getEmail());
|
||||
}
|
||||
|
||||
if (request.getPhone() != null) {
|
||||
user.setPhone(request.getPhone());
|
||||
}
|
||||
if (request.getGenderType() != null) {
|
||||
user.setGenderType(request.getGenderType());
|
||||
}
|
||||
if (request.getBirthday() != null) {
|
||||
user.setBirthday(request.getBirthday());
|
||||
}
|
||||
}
|
||||
|
||||
private UserDTO mapToDTO(User user) {
|
||||
return UserDTO.builder()
|
||||
.fullName(user.getFullName())
|
||||
.email(user.getEmail())
|
||||
.company(user.getCompany())
|
||||
.phone(user.getPhone())
|
||||
.genderType(user.getGenderType())
|
||||
.birthday(user.getBirthday())
|
||||
.subscriptionType(user.getSubscriptionType())
|
||||
.createdAt(user.getCreatedAt())
|
||||
.build();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
package ru.soune.nocopy.service.file;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.stereotype.Component;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.*;
|
||||
import java.nio.file.attribute.BasicFileAttributes;
|
||||
import java.time.Instant;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.ZoneId;
|
||||
|
||||
@Slf4j
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
public class FileCleanupService {
|
||||
|
||||
@Value("${file.storage.base-path}")
|
||||
private String basePath;
|
||||
|
||||
@Value("${file.storage.temp-ttl-hours}")
|
||||
private int tempTtlHours;
|
||||
|
||||
@Scheduled(cron = "0 0 3 * * *")
|
||||
public void cleanupExpiredFiles() {
|
||||
log.info("Starting cleanup of expired temporary files");
|
||||
|
||||
Path tempDir = Paths.get(basePath, "temp");
|
||||
if (!Files.exists(tempDir)) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
Files.walkFileTree(tempDir, new SimpleFileVisitor<Path>() {
|
||||
@Override
|
||||
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
|
||||
throws IOException {
|
||||
|
||||
LocalDateTime fileTime = LocalDateTime.ofInstant(
|
||||
Instant.ofEpochMilli(attrs.lastModifiedTime().toMillis()),
|
||||
ZoneId.systemDefault()
|
||||
);
|
||||
|
||||
LocalDateTime cutoffTime = LocalDateTime.now()
|
||||
.minusHours(tempTtlHours);
|
||||
|
||||
if (fileTime.isBefore(cutoffTime)) {
|
||||
Files.delete(file);
|
||||
log.debug("Deleted expired file: {}", file);
|
||||
}
|
||||
|
||||
return FileVisitResult.CONTINUE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public FileVisitResult postVisitDirectory(Path dir, IOException exc)
|
||||
throws IOException {
|
||||
|
||||
if (exc == null) {
|
||||
if (Files.list(dir).count() == 0 &&
|
||||
!dir.equals(tempDir)) {
|
||||
Files.delete(dir);
|
||||
log.debug("Deleted empty directory: {}", dir);
|
||||
}
|
||||
}
|
||||
|
||||
return FileVisitResult.CONTINUE;
|
||||
}
|
||||
});
|
||||
|
||||
log.info("Cleanup completed successfully");
|
||||
|
||||
} catch (IOException e) {
|
||||
log.error("Error during cleanup", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,277 @@
|
||||
package ru.soune.nocopy.service.file;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import ru.soune.nocopy.dto.file.FileEntityResponse;
|
||||
import ru.soune.nocopy.dto.file.FileListResponse;
|
||||
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.exception.FileEntityNotFoundException;
|
||||
import ru.soune.nocopy.repository.FileEntityRepository;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Slf4j
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class FileEntityService {
|
||||
|
||||
private final FileEntityRepository fileEntityRepository;
|
||||
|
||||
/**
|
||||
* Создает FileEntity на основе завершенной сессии загрузки
|
||||
*/
|
||||
@Transactional
|
||||
public FileEntity createFromUploadSession(FileUploadSession session, String checksum) {
|
||||
log.info("Creating FileEntity for upload session: {}", session.getUploadId());
|
||||
|
||||
try {
|
||||
Path filePath = Paths.get(session.getFilePath());
|
||||
|
||||
if (!Files.exists(filePath)) {
|
||||
throw new IOException("File not found on disk: " + filePath);
|
||||
}
|
||||
|
||||
long fileSize = Files.size(filePath);
|
||||
String originalName = session.getFileName();
|
||||
String extension = extractFileExtension(originalName);
|
||||
String storedName = filePath.getFileName().toString();
|
||||
|
||||
FileEntity fileEntity = FileEntity.builder()
|
||||
.userId(session.getUserId())
|
||||
.originalFileName(originalName)
|
||||
.storedFileName(storedName)
|
||||
.filePath(session.getFilePath())
|
||||
.fileSize(fileSize)
|
||||
.mimeType(session.getFileType())
|
||||
.fileExtension(extension)
|
||||
.checksum(checksum)
|
||||
.uploadSessionId(session.getUploadId())
|
||||
.status(FileStatus.ACTIVE)
|
||||
.build();
|
||||
|
||||
FileEntity saved = fileEntityRepository.save(fileEntity);
|
||||
log.info("FileEntity created successfully: {} (size: {} bytes)",
|
||||
saved.getId(), fileSize);
|
||||
|
||||
return saved;
|
||||
|
||||
} catch (IOException e) {
|
||||
log.error("Failed to create FileEntity for session {}: {}",
|
||||
session.getUploadId(), e.getMessage(), e);
|
||||
throw new RuntimeException("Failed to create file entity: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Извлекает расширение файла из имени
|
||||
*/
|
||||
private String extractFileExtension(String fileName) {
|
||||
int dotIndex = fileName.lastIndexOf('.');
|
||||
if (dotIndex > 0 && dotIndex < fileName.length() - 1) {
|
||||
return fileName.substring(dotIndex + 1).toLowerCase();
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
/**
|
||||
* Получает FileEntity по ID
|
||||
*/
|
||||
@Transactional(readOnly = true)
|
||||
public FileEntityResponse getById(String fileId) {
|
||||
FileEntity fileEntity = fileEntityRepository.findById(fileId)
|
||||
.orElseThrow(() -> new FileEntityNotFoundException(fileId));
|
||||
|
||||
return convertToResponse(fileEntity);
|
||||
}
|
||||
|
||||
/**
|
||||
* Получает FileEntity по ID сессии загрузки
|
||||
*/
|
||||
@Transactional(readOnly = true)
|
||||
public FileEntityResponse getByUploadSessionId(String uploadSessionId) {
|
||||
FileEntity fileEntity = fileEntityRepository.findByUploadSessionId(uploadSessionId)
|
||||
.orElseThrow(() -> new FileEntityNotFoundException(
|
||||
"Not found for upload session: " + uploadSessionId));
|
||||
|
||||
return convertToResponse(fileEntity);
|
||||
}
|
||||
|
||||
/**
|
||||
* Получает FileEntity по пути файла
|
||||
*/
|
||||
@Transactional(readOnly = true)
|
||||
public FileEntityResponse getByFilePath(String filePath) {
|
||||
FileEntity fileEntity = fileEntityRepository.findByFilePath(filePath)
|
||||
.orElseThrow(() -> new FileEntityNotFoundException("Path: " + filePath));
|
||||
|
||||
return convertToResponse(fileEntity);
|
||||
}
|
||||
|
||||
/**
|
||||
* Получает все файлы пользователя
|
||||
*/
|
||||
@Transactional(readOnly = true)
|
||||
public FileListResponse getAllUserFiles(Long userId) {
|
||||
List<FileEntity> fileEntities = fileEntityRepository.findByUserIdAndStatus(
|
||||
userId, FileStatus.ACTIVE);
|
||||
|
||||
List<FileEntityResponse> files = fileEntities.stream()
|
||||
.map(this::convertToResponse)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
long totalSize = fileEntities.stream()
|
||||
.mapToLong(FileEntity::getFileSize)
|
||||
.sum();
|
||||
|
||||
return FileListResponse.builder()
|
||||
.files(files)
|
||||
.totalCount(files.size())
|
||||
.totalSize(totalSize)
|
||||
.formattedTotalSize(formatFileSize(totalSize))
|
||||
.page(1)
|
||||
.pageSize(files.size())
|
||||
.build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Получает файлы пользователя с пагинацией
|
||||
*/
|
||||
@Transactional(readOnly = true)
|
||||
public FileListResponse getUserFiles(Long userId, int page, int pageSize) {
|
||||
List<FileEntity> allFiles = fileEntityRepository.findByUserIdAndStatus(
|
||||
userId, FileStatus.ACTIVE);
|
||||
|
||||
int start = (page - 1) * pageSize;
|
||||
int end = Math.min(start + pageSize, allFiles.size());
|
||||
|
||||
if (start >= allFiles.size()) {
|
||||
return FileListResponse.builder()
|
||||
.files(List.of())
|
||||
.totalCount(allFiles.size())
|
||||
.totalSize(0)
|
||||
.formattedTotalSize("0 B")
|
||||
.page(page)
|
||||
.pageSize(pageSize)
|
||||
.build();
|
||||
}
|
||||
|
||||
List<FileEntity> pageFiles = allFiles.subList(start, end);
|
||||
|
||||
List<FileEntityResponse> files = pageFiles.stream()
|
||||
.map(this::convertToResponse)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
long totalSize = allFiles.stream()
|
||||
.mapToLong(FileEntity::getFileSize)
|
||||
.sum();
|
||||
|
||||
return FileListResponse.builder()
|
||||
.files(files)
|
||||
.totalCount(allFiles.size())
|
||||
.totalSize(totalSize)
|
||||
.formattedTotalSize(formatFileSize(totalSize))
|
||||
.page(page)
|
||||
.pageSize(pageSize)
|
||||
.build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Помечает файл как удаленный (мягкое удаление)
|
||||
*/
|
||||
@Transactional
|
||||
public void markAsDeleted(String fileId) {
|
||||
FileEntity fileEntity = fileEntityRepository.findById(fileId)
|
||||
.orElseThrow(() -> new FileEntityNotFoundException(fileId));
|
||||
|
||||
fileEntity.setStatus(FileStatus.DELETED);
|
||||
fileEntity.setUpdatedAt(LocalDateTime.now());
|
||||
fileEntityRepository.save(fileEntity);
|
||||
|
||||
log.info("FileEntity marked as deleted: {}", fileId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Получает общий размер файлов пользователя
|
||||
*/
|
||||
@Transactional(readOnly = true)
|
||||
public long getUserStorageUsed(Long userId) {
|
||||
Long totalSize = fileEntityRepository.getTotalSizeByUserId(userId);
|
||||
return totalSize != null ? totalSize : 0L;
|
||||
}
|
||||
|
||||
/**
|
||||
* Поиск файлов пользователя по имени
|
||||
*/
|
||||
@Transactional(readOnly = true)
|
||||
public List<FileEntityResponse> searchFiles(Long userId, String query) {
|
||||
List<FileEntity> files = fileEntityRepository.searchByFileName(userId, query);
|
||||
|
||||
return files.stream()
|
||||
.filter(f -> f.getStatus() == FileStatus.ACTIVE)
|
||||
.map(this::convertToResponse)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
/**
|
||||
* Проверяет, существует ли файл на диске
|
||||
*/
|
||||
private boolean checkFileExistsOnDisk(String filePath) {
|
||||
try {
|
||||
return Files.exists(Paths.get(filePath));
|
||||
} catch (Exception e) {
|
||||
log.warn("Error checking file existence: {}", filePath, e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Конвертирует FileEntity в DTO
|
||||
*/
|
||||
private FileEntityResponse convertToResponse(FileEntity fileEntity) {
|
||||
boolean existsOnDisk = checkFileExistsOnDisk(fileEntity.getFilePath());
|
||||
|
||||
return FileEntityResponse.builder()
|
||||
.id(fileEntity.getId())
|
||||
.userId(fileEntity.getUserId())
|
||||
.originalFileName(fileEntity.getOriginalFileName())
|
||||
.storedFileName(fileEntity.getStoredFileName())
|
||||
.filePath(fileEntity.getFilePath())
|
||||
.fileSize(fileEntity.getFileSize())
|
||||
.mimeType(fileEntity.getMimeType())
|
||||
.fileExtension(fileEntity.getFileExtension())
|
||||
.checksum(fileEntity.getChecksum())
|
||||
.uploadSessionId(fileEntity.getUploadSessionId())
|
||||
.status(fileEntity.getStatus())
|
||||
.createdAt(fileEntity.getCreatedAt())
|
||||
.updatedAt(fileEntity.getUpdatedAt())
|
||||
.formattedSize(formatFileSize(fileEntity.getFileSize()))
|
||||
.downloadUrl("/api/files/download/" + fileEntity.getId())
|
||||
.existsOnDisk(existsOnDisk)
|
||||
.build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Форматирует размер файла в читаемый вид
|
||||
*/
|
||||
private String formatFileSize(long size) {
|
||||
if (size < 1024) {
|
||||
return size + " B";
|
||||
} else if (size < 1024 * 1024) {
|
||||
return String.format("%.1f KB", size / 1024.0);
|
||||
} else if (size < 1024 * 1024 * 1024) {
|
||||
return String.format("%.1f MB", size / (1024.0 * 1024.0));
|
||||
} else {
|
||||
return String.format("%.1f GB", size / (1024.0 * 1024.0 * 1024.0));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package ru.soune.nocopy.service.file;
|
||||
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
import ru.soune.nocopy.dto.file.UploadProgressResponse;
|
||||
import ru.soune.nocopy.entity.file.FileUploadSession;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public interface FileUploadService {
|
||||
FileUploadSession initUpload(Long userId, String fileName,
|
||||
String fileType, String extension, long fileSize);
|
||||
|
||||
UploadProgressResponse uploadChunk(String uploadId, Integer chunkNumber,
|
||||
MultipartFile chunkFile) throws IOException;
|
||||
|
||||
UploadProgressResponse getUploadProgress(String uploadId);
|
||||
|
||||
void cleanupExpiredSessions();
|
||||
|
||||
public void retryFailedUpload(String uploadId);
|
||||
|
||||
public void cancelUpload(String uploadId);
|
||||
}
|
||||
@@ -0,0 +1,733 @@
|
||||
package ru.soune.nocopy.service.file;
|
||||
|
||||
import jakarta.annotation.PostConstruct;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.scheduling.annotation.Async;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
import ru.soune.nocopy.dto.file.UploadProgressResponse;
|
||||
import ru.soune.nocopy.entity.file.FileUploadSession;
|
||||
import ru.soune.nocopy.entity.file.UploadStatus;
|
||||
import ru.soune.nocopy.exception.ChunkSizeExceededException;
|
||||
import ru.soune.nocopy.exception.FileUploadException;
|
||||
import ru.soune.nocopy.exception.UploadSessionNotFoundException;
|
||||
import ru.soune.nocopy.repository.FileUploadSessionRepository;
|
||||
|
||||
import java.io.*;
|
||||
import java.nio.file.*;
|
||||
import java.nio.file.attribute.PosixFilePermission;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Comparator;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
@Slf4j
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class FileUploadServiceImpl implements FileUploadService {
|
||||
|
||||
private final FileUploadSessionRepository sessionRepository;
|
||||
|
||||
@Value("${file.storage.base-path}")
|
||||
private String basePath;
|
||||
|
||||
@Value("${file.storage.chunk-size}")
|
||||
private int chunkSize;
|
||||
|
||||
@Value("${file.storage.max-file-size}")
|
||||
private long maxFileSize;
|
||||
|
||||
@Value("${file.storage.max-retry-attempts:3}")
|
||||
private int maxRetryAttempts;
|
||||
|
||||
@Value("${file.storage.chunk-timeout-ms:300000}") // 5 минут
|
||||
private long chunkTimeoutMs;
|
||||
|
||||
@Value("${file.storage.session-expiry-hours:24}")
|
||||
private int sessionExpiryHours;
|
||||
|
||||
private Path storageRoot;
|
||||
|
||||
@Autowired
|
||||
private FileEntityService fileEntityService;
|
||||
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
try {
|
||||
storageRoot = Paths.get(basePath).toAbsolutePath().normalize();
|
||||
log.info("Initializing file storage at: {}", storageRoot);
|
||||
|
||||
createDirectoryIfNotExists(storageRoot);
|
||||
createDirectoryIfNotExists(storageRoot.resolve("temp"));
|
||||
createDirectoryIfNotExists(storageRoot.resolve("uploads"));
|
||||
|
||||
if (!System.getProperty("os.name").toLowerCase().contains("win")) {
|
||||
setDirectoryPermissions(storageRoot);
|
||||
}
|
||||
|
||||
log.info("File storage initialized successfully");
|
||||
|
||||
} catch (IOException e) {
|
||||
log.error("Failed to initialize file storage at: {}", basePath, e);
|
||||
throw new RuntimeException("Storage initialization failed", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public FileUploadSession initUpload(Long userId, String fileName,
|
||||
String fileType, String extension, long fileSize) {
|
||||
log.info("Initializing upload for user {}: {} ({} bytes, type: {})",
|
||||
userId, fileName, fileSize, fileType);
|
||||
|
||||
if (fileSize > maxFileSize) {
|
||||
throw new FileUploadException(
|
||||
String.format("File size %d exceeds maximum allowed size %d",
|
||||
fileSize, maxFileSize));
|
||||
}
|
||||
|
||||
if (fileSize <= 0) {
|
||||
throw new FileUploadException("File size must be positive");
|
||||
}
|
||||
|
||||
int totalChunks = (int) Math.ceil((double) fileSize / chunkSize);
|
||||
log.debug("File will be split into {} chunks (chunk size: {} bytes)",
|
||||
totalChunks, chunkSize);
|
||||
|
||||
FileUploadSession session = FileUploadSession.builder()
|
||||
.userId(userId)
|
||||
.extension(extension)
|
||||
.fileName(fileName)
|
||||
.fileType(fileType)
|
||||
.fileSize(fileSize)
|
||||
.totalChunks(totalChunks)
|
||||
.chunksUploaded(0)
|
||||
.status(UploadStatus.INITIATED)
|
||||
.expiresAt(LocalDateTime.now().plusHours(sessionExpiryHours))
|
||||
.retryCount(0)
|
||||
.build();
|
||||
|
||||
FileUploadSession savedSession = sessionRepository.save(session);
|
||||
log.info("Upload session created: {} for file: {}",
|
||||
savedSession.getUploadId(), fileName);
|
||||
|
||||
return savedSession;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public UploadProgressResponse uploadChunk(String uploadId, Integer chunkNumber,
|
||||
MultipartFile chunkFile) {
|
||||
|
||||
log.info("Processing chunk {} for session {}, size: {} bytes",
|
||||
chunkNumber, uploadId, chunkFile.getSize());
|
||||
|
||||
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 0-%d",
|
||||
chunkNumber, session.getTotalChunks() - 1));
|
||||
}
|
||||
|
||||
if (chunkFile.getSize() > chunkSize) {
|
||||
throw new ChunkSizeExceededException(chunkFile.getSize(), chunkSize);
|
||||
}
|
||||
|
||||
return processChunk(session, chunkNumber, chunkFile);
|
||||
}
|
||||
|
||||
private void validateSession(FileUploadSession session) {
|
||||
UploadStatus status = session.getStatus();
|
||||
|
||||
if (status == UploadStatus.FAILED) {
|
||||
if (session.getRetryCount() >= maxRetryAttempts) {
|
||||
throw new FileUploadException(
|
||||
"Upload failed after maximum retry attempts");
|
||||
}
|
||||
session.setStatus(UploadStatus.UPLOADING);
|
||||
session.setRetryCount(session.getRetryCount() + 1);
|
||||
sessionRepository.save(session);
|
||||
log.info("Retrying failed upload session: {}, attempt: {}",
|
||||
session.getUploadId(), session.getRetryCount());
|
||||
}
|
||||
|
||||
if (status == UploadStatus.COMPLETED) {
|
||||
throw new FileUploadException("Upload already completed");
|
||||
}
|
||||
|
||||
if (status == UploadStatus.CANCELLED) {
|
||||
throw new FileUploadException("Upload was cancelled");
|
||||
}
|
||||
|
||||
if (status == UploadStatus.INITIATED) {
|
||||
session.setStatus(UploadStatus.UPLOADING);
|
||||
sessionRepository.save(session);
|
||||
}
|
||||
}
|
||||
|
||||
private void handleExpiredSession(FileUploadSession session) {
|
||||
session.setStatus(UploadStatus.FAILED);
|
||||
session.setLastError("Upload session expired");
|
||||
sessionRepository.save(session);
|
||||
|
||||
CompletableFuture.runAsync(() -> cleanupSessionFiles(session));
|
||||
}
|
||||
|
||||
private UploadProgressResponse processChunk(FileUploadSession session,
|
||||
Integer chunkNumber,
|
||||
MultipartFile chunkFile) {
|
||||
String chunkPath = null;
|
||||
|
||||
try {
|
||||
if (session.getChunkPaths().containsKey(chunkNumber)) {
|
||||
return handleExistingChunk(session, chunkNumber, chunkFile);
|
||||
}
|
||||
|
||||
chunkPath = saveChunkWithIntegrityCheck(session, chunkNumber, chunkFile);
|
||||
|
||||
session.getChunkPaths().put(chunkNumber, chunkPath);
|
||||
session.setChunksUploaded(session.getChunksUploaded() + 1);
|
||||
session.setStatus(UploadStatus.UPLOADING);
|
||||
|
||||
log.debug("Chunk {} saved successfully. Uploaded: {}/{}",
|
||||
chunkNumber, session.getChunksUploaded(), session.getTotalChunks());
|
||||
|
||||
if (session.getChunksUploaded().equals(session.getTotalChunks())) {
|
||||
log.info("All chunks uploaded for session {}. Starting assembly...",
|
||||
session.getUploadId());
|
||||
sessionRepository.save(session);
|
||||
assembleFileAsync(session);
|
||||
} 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);
|
||||
}
|
||||
}
|
||||
|
||||
private UploadProgressResponse handleExistingChunk(FileUploadSession session,
|
||||
Integer chunkNumber,
|
||||
MultipartFile chunkFile) throws IOException {
|
||||
String existingPath = session.getChunkPaths().get(chunkNumber);
|
||||
Path chunkPath = Paths.get(existingPath);
|
||||
|
||||
if (!Files.exists(chunkPath)) {
|
||||
log.warn("Chunk file missing, re-uploading: {}", chunkPath);
|
||||
session.getChunkPaths().remove(chunkNumber);
|
||||
session.setChunksUploaded(session.getChunksUploaded() - 1);
|
||||
sessionRepository.save(session);
|
||||
return processChunk(session, chunkNumber, chunkFile);
|
||||
}
|
||||
|
||||
long existingSize = Files.size(chunkPath);
|
||||
if (existingSize != chunkFile.getSize()) {
|
||||
log.warn("Chunk size mismatch, re-uploading: {} != {}",
|
||||
existingSize, chunkFile.getSize());
|
||||
Files.deleteIfExists(chunkPath);
|
||||
session.getChunkPaths().remove(chunkNumber);
|
||||
session.setChunksUploaded(session.getChunksUploaded() - 1);
|
||||
sessionRepository.save(session);
|
||||
return processChunk(session, chunkNumber, chunkFile);
|
||||
}
|
||||
|
||||
log.debug("Chunk {} already uploaded and valid", chunkNumber);
|
||||
return UploadProgressResponse.fromSession(session);
|
||||
}
|
||||
|
||||
private String saveChunkWithIntegrityCheck(FileUploadSession session,
|
||||
Integer chunkNumber,
|
||||
MultipartFile chunkFile) throws IOException {
|
||||
|
||||
Long userId = session.getUserId();
|
||||
String uploadId = session.getUploadId();
|
||||
|
||||
Path chunkDir = getChunkDirectory(userId, uploadId);
|
||||
Files.createDirectories(chunkDir);
|
||||
|
||||
String tempFileName = String.format("chunk_%04d.%s.tmp",
|
||||
chunkNumber, UUID.randomUUID());
|
||||
String finalFileName = String.format("chunk_%04d.tmp", chunkNumber);
|
||||
|
||||
Path tempPath = chunkDir.resolve(tempFileName);
|
||||
Path finalPath = chunkDir.resolve(finalFileName);
|
||||
|
||||
try {
|
||||
chunkFile.transferTo(tempPath.toFile());
|
||||
|
||||
long savedSize = Files.size(tempPath);
|
||||
long uploadedSize = chunkFile.getSize();
|
||||
|
||||
if (uploadedSize > 0 && savedSize != uploadedSize) {
|
||||
throw new IOException(
|
||||
String.format("Size mismatch: saved %d, uploaded %d",
|
||||
savedSize, uploadedSize));
|
||||
}
|
||||
|
||||
Files.move(tempPath, finalPath, StandardCopyOption.ATOMIC_MOVE);
|
||||
|
||||
log.debug("Chunk {} saved successfully: {} bytes",
|
||||
chunkNumber, savedSize);
|
||||
|
||||
return finalPath.toString();
|
||||
|
||||
} finally {
|
||||
Files.deleteIfExists(tempPath);
|
||||
}
|
||||
}
|
||||
|
||||
private void cleanupFailedChunk(String chunkPath) {
|
||||
try {
|
||||
Files.deleteIfExists(Paths.get(chunkPath));
|
||||
log.debug("Cleaned up failed chunk: {}", chunkPath);
|
||||
} catch (IOException e) {
|
||||
log.warn("Failed to cleanup chunk: {}", chunkPath, e);
|
||||
}
|
||||
}
|
||||
|
||||
@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);
|
||||
}
|
||||
}
|
||||
|
||||
// private void assembleFile(FileUploadSession session) throws IOException {
|
||||
// log.info("Starting file assembly for session: {} ({})",
|
||||
// session.getUploadId(), session.getFileName());
|
||||
//
|
||||
// Path finalFilePath = null;
|
||||
//
|
||||
// try {
|
||||
// finalFilePath = prepareFinalFile(session);
|
||||
//
|
||||
// validateAllChunksExist(session);
|
||||
//
|
||||
// mergeChunksToFile(session, finalFilePath);
|
||||
//
|
||||
// validateFinalFile(session, finalFilePath);
|
||||
//
|
||||
// updateSessionOnSuccess(session, finalFilePath);
|
||||
//
|
||||
// cleanupSessionFiles(session);
|
||||
//
|
||||
// log.info("File assembly completed: {} -> {} ({} bytes)",
|
||||
// session.getFileName(), finalFilePath, session.getFileSize());
|
||||
//
|
||||
// } catch (Exception e) {
|
||||
// if (finalFilePath != null) {
|
||||
// Files.deleteIfExists(finalFilePath);
|
||||
// }
|
||||
// throw e;
|
||||
// }
|
||||
// }
|
||||
|
||||
private void assembleFile(FileUploadSession session) throws IOException {
|
||||
log.info("Starting file assembly for session: {} ({})",
|
||||
session.getUploadId(), session.getFileName());
|
||||
|
||||
Path finalFilePath = null;
|
||||
String checksum = null;
|
||||
|
||||
try {
|
||||
finalFilePath = prepareFinalFile(session);
|
||||
log.info("Final file path: {}", finalFilePath);
|
||||
|
||||
validateAllChunksExist(session);
|
||||
|
||||
mergeChunksToFile(session, finalFilePath);
|
||||
|
||||
validateFinalFile(session, finalFilePath);
|
||||
|
||||
checksum = calculateChecksum(finalFilePath);
|
||||
log.debug("File checksum calculated: {}", checksum);
|
||||
|
||||
session.setFilePath(finalFilePath.toString());
|
||||
session.setChecksum(checksum);
|
||||
session.setStatus(UploadStatus.COMPLETED);
|
||||
session.setCompletedAt(LocalDateTime.now());
|
||||
sessionRepository.save(session);
|
||||
log.info("Upload session updated to COMPLETED: {}", session.getUploadId());
|
||||
|
||||
try {
|
||||
fileEntityService.createFromUploadSession(session, checksum);
|
||||
log.info("FileEntity successfully created for session: {}",
|
||||
session.getUploadId());
|
||||
} catch (Exception e) {
|
||||
log.error("Failed to create FileEntity for session {} (file uploaded, but metadata not saved): {}",
|
||||
session.getUploadId(), e.getMessage());
|
||||
}
|
||||
|
||||
cleanupSessionFiles(session);
|
||||
|
||||
log.info("File assembly completed successfully: {} -> {} ({} bytes)",
|
||||
session.getFileName(), finalFilePath, session.getFileSize());
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("File assembly failed for session {}: {}",
|
||||
session.getUploadId(), e.getMessage(), e);
|
||||
|
||||
if (finalFilePath != null) {
|
||||
try {
|
||||
Files.deleteIfExists(finalFilePath);
|
||||
log.debug("Cleaned up partial file: {}", finalFilePath);
|
||||
} catch (IOException ioException) {
|
||||
log.warn("Failed to cleanup partial file: {}", finalFilePath, ioException);
|
||||
}
|
||||
}
|
||||
|
||||
session.setStatus(UploadStatus.FAILED);
|
||||
session.setLastError(e.getMessage());
|
||||
sessionRepository.save(session);
|
||||
|
||||
throw new IOException("File assembly failed: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
private Path prepareFinalFile(FileUploadSession session) throws IOException {
|
||||
Path userUploadsDir = storageRoot.resolve("uploads")
|
||||
.resolve(String.valueOf(session.getUserId()))
|
||||
.resolve(extractCategory(session.getFileType()));
|
||||
|
||||
Files.createDirectories(userUploadsDir);
|
||||
|
||||
String safeFileName = generateUniqueFileName(session.getFileName(), session.getExtension());
|
||||
Path finalPath = userUploadsDir.resolve(safeFileName);
|
||||
|
||||
if (Files.exists(finalPath)) {
|
||||
throw new IOException("File already exists: " + finalPath);
|
||||
}
|
||||
|
||||
return finalPath;
|
||||
}
|
||||
|
||||
private String generateUniqueFileName(String originalName, String extension) {
|
||||
int dotIndex = originalName.lastIndexOf('.');
|
||||
log.info("dotIndex: {}", dotIndex);
|
||||
log.info("originalName: {}", originalName);
|
||||
String nameWithoutExtension;
|
||||
|
||||
if (dotIndex > 0) {
|
||||
nameWithoutExtension = originalName.substring(0, dotIndex)
|
||||
.replaceAll("[^a-zA-Z0-9\\-_]", "_");
|
||||
} else {
|
||||
nameWithoutExtension = originalName.replaceAll("[^a-zA-Z0-9\\-_]", "_");
|
||||
}
|
||||
|
||||
String timestamp = String.valueOf(System.currentTimeMillis());
|
||||
String uuid = UUID.randomUUID().toString().substring(0, 8);
|
||||
|
||||
return String.format("%s_%s_%s.%s",
|
||||
nameWithoutExtension, timestamp, uuid, extension);
|
||||
}
|
||||
|
||||
private void validateAllChunksExist(FileUploadSession session) throws IOException {
|
||||
Path chunkDir = getChunkDirectory(session.getUserId(), session.getUploadId());
|
||||
|
||||
for (int i = 0; i < session.getTotalChunks(); i++) {
|
||||
Path chunkPath = chunkDir.resolve(String.format("chunk_%04d.tmp", i));
|
||||
if (!Files.exists(chunkPath)) {
|
||||
throw new IOException("Missing chunk: " + chunkPath.getFileName());
|
||||
}
|
||||
|
||||
if (Files.size(chunkPath) == 0) {
|
||||
throw new IOException("Empty chunk: " + chunkPath.getFileName());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void mergeChunksToFile(FileUploadSession session, Path finalPath)
|
||||
throws IOException {
|
||||
|
||||
Path chunkDir = getChunkDirectory(session.getUserId(), session.getUploadId());
|
||||
byte[] buffer = new byte[8192];
|
||||
|
||||
try (BufferedOutputStream outputStream = new BufferedOutputStream(
|
||||
Files.newOutputStream(finalPath, StandardOpenOption.CREATE_NEW))) {
|
||||
|
||||
long totalWritten = 0;
|
||||
|
||||
for (int i = 0; i < session.getTotalChunks(); i++) {
|
||||
Path chunkPath = chunkDir.resolve(String.format("chunk_%04d.tmp", i));
|
||||
|
||||
try (BufferedInputStream inputStream = new BufferedInputStream(
|
||||
Files.newInputStream(chunkPath))) {
|
||||
|
||||
int bytesRead;
|
||||
while ((bytesRead = inputStream.read(buffer)) != -1) {
|
||||
outputStream.write(buffer, 0, bytesRead);
|
||||
totalWritten += bytesRead;
|
||||
}
|
||||
}
|
||||
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Processed chunk {}: {} bytes (total: {})",
|
||||
i, Files.size(chunkPath), totalWritten);
|
||||
}
|
||||
}
|
||||
|
||||
outputStream.flush();
|
||||
log.info("Total bytes written to final file: {}", totalWritten);
|
||||
}
|
||||
}
|
||||
|
||||
private void validateFinalFile(FileUploadSession session, Path finalPath)
|
||||
throws IOException {
|
||||
|
||||
long actualSize = Files.size(finalPath);
|
||||
|
||||
if (actualSize != session.getFileSize()) {
|
||||
throw new IOException(
|
||||
String.format("File size mismatch: expected %d, got %d",
|
||||
session.getFileSize(), actualSize));
|
||||
}
|
||||
|
||||
String checksum = calculateChecksum(finalPath);
|
||||
log.debug("Final file checksum: {}", checksum);
|
||||
}
|
||||
|
||||
private void updateSessionOnSuccess(FileUploadSession session, Path finalPath) {
|
||||
session.setFilePath(finalPath.toString());
|
||||
session.setChecksum(calculateChecksum(finalPath));
|
||||
session.setStatus(UploadStatus.COMPLETED);
|
||||
session.setCompletedAt(LocalDateTime.now());
|
||||
sessionRepository.save(session);
|
||||
}
|
||||
|
||||
private void cleanupSessionFiles(FileUploadSession session) {
|
||||
try {
|
||||
Path chunkDir = getChunkDirectory(session.getUserId(), session.getUploadId());
|
||||
if (Files.exists(chunkDir)) {
|
||||
deleteDirectoryRecursively(chunkDir);
|
||||
log.info("Cleaned up temp directory for session: {}",
|
||||
session.getUploadId());
|
||||
}
|
||||
} catch (IOException e) {
|
||||
log.warn("Failed to cleanup temp files for session {}: {}",
|
||||
session.getUploadId(), e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
private void deleteDirectoryRecursively(Path directory) throws IOException {
|
||||
try (Stream<Path> walk = Files.walk(directory)) {
|
||||
walk.sorted(Comparator.reverseOrder())
|
||||
.forEach(path -> {
|
||||
try {
|
||||
Files.deleteIfExists(path);
|
||||
} catch (IOException e) {
|
||||
log.warn("Failed to delete: {}", path, e);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private void handleAssemblyFailure(FileUploadSession session, Exception e) {
|
||||
session.setStatus(UploadStatus.FAILED);
|
||||
session.setLastError(e.getMessage());
|
||||
sessionRepository.save(session);
|
||||
|
||||
log.error("File assembly failed for session {}: {}",
|
||||
session.getUploadId(), e.getMessage());
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public void cancelUpload(String uploadId) {
|
||||
FileUploadSession session = sessionRepository.findById(uploadId)
|
||||
.orElseThrow(() -> new UploadSessionNotFoundException(uploadId));
|
||||
|
||||
if (session.getStatus() == UploadStatus.COMPLETED) {
|
||||
throw new FileUploadException("Cannot cancel completed 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
|
||||
public UploadProgressResponse getUploadProgress(String uploadId) {
|
||||
FileUploadSession session = sessionRepository.findById(uploadId)
|
||||
.orElseThrow(() -> new UploadSessionNotFoundException(uploadId));
|
||||
|
||||
return UploadProgressResponse.fromSession(session);
|
||||
}
|
||||
|
||||
@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) {
|
||||
return storageRoot.resolve("temp")
|
||||
.resolve("user")
|
||||
.resolve(String.valueOf(userId))
|
||||
.resolve(uploadId);
|
||||
}
|
||||
|
||||
private String extractCategory(String fileType) {
|
||||
if (fileType == null) {
|
||||
return "other";
|
||||
}
|
||||
|
||||
String lowerType = fileType.toLowerCase();
|
||||
if (lowerType.startsWith("image")) {
|
||||
return "images";
|
||||
} else if (lowerType.startsWith("video")) {
|
||||
return "videos";
|
||||
} else if (lowerType.startsWith("audio")) {
|
||||
return "audio";
|
||||
} else if (lowerType.contains("document")) {
|
||||
return "documents";
|
||||
} else {
|
||||
return "other";
|
||||
}
|
||||
}
|
||||
|
||||
private String calculateChecksum(Path filePath) {
|
||||
try {
|
||||
MessageDigest md = MessageDigest.getInstance("SHA-256");
|
||||
byte[] buffer = new byte[8192];
|
||||
|
||||
try (InputStream inputStream = Files.newInputStream(filePath)) {
|
||||
int bytesRead;
|
||||
while ((bytesRead = inputStream.read(buffer)) != -1) {
|
||||
md.update(buffer, 0, bytesRead);
|
||||
}
|
||||
}
|
||||
|
||||
byte[] digest = md.digest();
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (byte b : digest) {
|
||||
sb.append(String.format("%02x", b));
|
||||
}
|
||||
|
||||
return sb.toString();
|
||||
|
||||
} catch (NoSuchAlgorithmException | IOException e) {
|
||||
log.warn("Failed to calculate checksum for file: {}", filePath, e);
|
||||
return "N/A";
|
||||
}
|
||||
}
|
||||
|
||||
private void createDirectoryIfNotExists(Path directory) throws IOException {
|
||||
if (!Files.exists(directory)) {
|
||||
Files.createDirectories(directory);
|
||||
log.debug("Created directory: {}", directory);
|
||||
}
|
||||
}
|
||||
|
||||
private void setDirectoryPermissions(Path directory) throws IOException {
|
||||
try {
|
||||
Set<PosixFilePermission> permissions = new HashSet<>();
|
||||
permissions.add(PosixFilePermission.OWNER_READ);
|
||||
permissions.add(PosixFilePermission.OWNER_WRITE);
|
||||
permissions.add(PosixFilePermission.OWNER_EXECUTE);
|
||||
permissions.add(PosixFilePermission.GROUP_READ);
|
||||
permissions.add(PosixFilePermission.GROUP_EXECUTE);
|
||||
permissions.add(PosixFilePermission.OTHERS_READ);
|
||||
|
||||
Files.setPosixFilePermissions(directory, permissions);
|
||||
log.debug("Set permissions for directory: {}", directory);
|
||||
|
||||
} catch (UnsupportedOperationException e) {
|
||||
log.debug("Posix permissions not supported on this system");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user