@@ -5,7 +5,7 @@ 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.dto.file.FileResponse;
|
||||
import ru.soune.nocopy.entity.file.FileEntity;
|
||||
import ru.soune.nocopy.entity.file.FileStatus;
|
||||
import ru.soune.nocopy.entity.file.FileUploadSession;
|
||||
@@ -27,9 +27,6 @@ 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());
|
||||
@@ -72,9 +69,7 @@ public class FileEntityService {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Извлекает расширение файла из имени
|
||||
*/
|
||||
|
||||
private String extractFileExtension(String fileName) {
|
||||
int dotIndex = fileName.lastIndexOf('.');
|
||||
if (dotIndex > 0 && dotIndex < fileName.length() - 1) {
|
||||
@@ -83,9 +78,6 @@ public class FileEntityService {
|
||||
return "";
|
||||
}
|
||||
|
||||
/**
|
||||
* Получает FileEntity по ID
|
||||
*/
|
||||
@Transactional(readOnly = true)
|
||||
public FileEntityResponse getById(String fileId) {
|
||||
FileEntity fileEntity = fileEntityRepository.findById(fileId)
|
||||
@@ -94,9 +86,6 @@ public class FileEntityService {
|
||||
return convertToResponse(fileEntity);
|
||||
}
|
||||
|
||||
/**
|
||||
* Получает FileEntity по ID сессии загрузки
|
||||
*/
|
||||
@Transactional(readOnly = true)
|
||||
public FileEntityResponse getByUploadSessionId(String uploadSessionId) {
|
||||
FileEntity fileEntity = fileEntityRepository.findByUploadSessionId(uploadSessionId)
|
||||
@@ -106,9 +95,6 @@ public class FileEntityService {
|
||||
return convertToResponse(fileEntity);
|
||||
}
|
||||
|
||||
/**
|
||||
* Получает FileEntity по пути файла
|
||||
*/
|
||||
@Transactional(readOnly = true)
|
||||
public FileEntityResponse getByFilePath(String filePath) {
|
||||
FileEntity fileEntity = fileEntityRepository.findByFilePath(filePath)
|
||||
@@ -117,11 +103,9 @@ public class FileEntityService {
|
||||
return convertToResponse(fileEntity);
|
||||
}
|
||||
|
||||
/**
|
||||
* Получает все файлы пользователя
|
||||
*/
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public FileListResponse getAllUserFiles(Long userId) {
|
||||
public FileResponse getAllUserFiles(Long userId) {
|
||||
List<FileEntity> fileEntities = fileEntityRepository.findByUserIdAndStatus(
|
||||
userId, FileStatus.ACTIVE);
|
||||
|
||||
@@ -133,7 +117,7 @@ public class FileEntityService {
|
||||
.mapToLong(FileEntity::getFileSize)
|
||||
.sum();
|
||||
|
||||
return FileListResponse.builder()
|
||||
return FileResponse.builder()
|
||||
.files(files)
|
||||
.totalCount(files.size())
|
||||
.totalSize(totalSize)
|
||||
@@ -143,11 +127,9 @@ public class FileEntityService {
|
||||
.build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Получает файлы пользователя с пагинацией
|
||||
*/
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public FileListResponse getUserFiles(Long userId, int page, int pageSize) {
|
||||
public FileResponse getUserFiles(Long userId, int page, int pageSize) {
|
||||
List<FileEntity> allFiles = fileEntityRepository.findByUserIdAndStatus(
|
||||
userId, FileStatus.ACTIVE);
|
||||
|
||||
@@ -155,7 +137,7 @@ public class FileEntityService {
|
||||
int end = Math.min(start + pageSize, allFiles.size());
|
||||
|
||||
if (start >= allFiles.size()) {
|
||||
return FileListResponse.builder()
|
||||
return FileResponse.builder()
|
||||
.files(List.of())
|
||||
.totalCount(allFiles.size())
|
||||
.totalSize(0)
|
||||
@@ -175,7 +157,7 @@ public class FileEntityService {
|
||||
.mapToLong(FileEntity::getFileSize)
|
||||
.sum();
|
||||
|
||||
return FileListResponse.builder()
|
||||
return FileResponse.builder()
|
||||
.files(files)
|
||||
.totalCount(allFiles.size())
|
||||
.totalSize(totalSize)
|
||||
@@ -185,9 +167,6 @@ public class FileEntityService {
|
||||
.build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Помечает файл как удаленный (мягкое удаление)
|
||||
*/
|
||||
@Transactional
|
||||
public void markAsDeleted(String fileId) {
|
||||
FileEntity fileEntity = fileEntityRepository.findById(fileId)
|
||||
@@ -200,18 +179,12 @@ public class FileEntityService {
|
||||
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);
|
||||
@@ -222,9 +195,6 @@ public class FileEntityService {
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
/**
|
||||
* Проверяет, существует ли файл на диске
|
||||
*/
|
||||
private boolean checkFileExistsOnDisk(String filePath) {
|
||||
try {
|
||||
return Files.exists(Paths.get(filePath));
|
||||
@@ -234,9 +204,6 @@ public class FileEntityService {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Конвертирует FileEntity в DTO
|
||||
*/
|
||||
private FileEntityResponse convertToResponse(FileEntity fileEntity) {
|
||||
boolean existsOnDisk = checkFileExistsOnDisk(fileEntity.getFilePath());
|
||||
|
||||
@@ -260,9 +227,6 @@ public class FileEntityService {
|
||||
.build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Форматирует размер файла в читаемый вид
|
||||
*/
|
||||
private String formatFileSize(long size) {
|
||||
if (size < 1024) {
|
||||
return size + " B";
|
||||
|
||||
Reference in New Issue
Block a user