2025-12-17 22:37:46 +07:00
|
|
|
package ru.soune.nocopy.service.file;
|
2025-12-17 01:19:48 +07:00
|
|
|
|
|
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
2025-12-17 22:37:46 +07:00
|
|
|
import ru.soune.nocopy.dto.file.FileEntityResponse;
|
2025-12-18 01:13:19 +07:00
|
|
|
import ru.soune.nocopy.dto.file.FileResponse;
|
2025-12-17 22:37:46 +07:00
|
|
|
import ru.soune.nocopy.entity.file.FileEntity;
|
|
|
|
|
import ru.soune.nocopy.entity.file.FileStatus;
|
|
|
|
|
import ru.soune.nocopy.entity.file.FileUploadSession;
|
2026-01-22 19:55:09 +07:00
|
|
|
import ru.soune.nocopy.entity.file.ProtectionStatus;
|
2026-01-28 19:26:40 +07:00
|
|
|
import ru.soune.nocopy.entity.user.User;
|
2026-01-19 15:49:27 +07:00
|
|
|
import ru.soune.nocopy.exception.DuplicateImageException;
|
2025-12-17 22:37:46 +07:00
|
|
|
import ru.soune.nocopy.exception.FileEntityNotFoundException;
|
|
|
|
|
import ru.soune.nocopy.repository.FileEntityRepository;
|
2026-01-19 15:49:27 +07:00
|
|
|
import ru.soune.nocopy.repository.SimilarImageProjection;
|
2026-01-28 19:26:40 +07:00
|
|
|
import ru.soune.nocopy.repository.UserRepository;
|
2026-01-13 22:40:52 +07:00
|
|
|
import ru.soune.nocopy.service.FileSimilarityService;
|
|
|
|
|
import ru.soune.nocopy.service.ImageHashService;
|
2025-12-17 01:19:48 +07:00
|
|
|
|
2026-01-22 19:55:09 +07:00
|
|
|
import java.io.File;
|
2025-12-17 01:19:48 +07:00
|
|
|
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;
|
2026-01-19 15:49:27 +07:00
|
|
|
import java.util.Map;
|
2025-12-17 01:19:48 +07:00
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
|
|
@Slf4j
|
|
|
|
|
@Service
|
|
|
|
|
@RequiredArgsConstructor
|
|
|
|
|
public class FileEntityService {
|
|
|
|
|
|
|
|
|
|
private final FileEntityRepository fileEntityRepository;
|
|
|
|
|
|
2026-01-13 22:40:52 +07:00
|
|
|
private final ImageHashService imageHashService;
|
|
|
|
|
|
|
|
|
|
private final FileSimilarityService fileSimilarityService;
|
|
|
|
|
|
2026-01-28 19:26:40 +07:00
|
|
|
private final UserRepository userRepository;
|
2026-01-27 08:09:54 +07:00
|
|
|
|
2026-01-19 15:49:27 +07:00
|
|
|
@Transactional(noRollbackFor = DuplicateImageException.class)
|
2025-12-17 01:19:48 +07:00
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-23 13:22:02 +07:00
|
|
|
Map<String, Long> imageHash = Map.of();
|
2026-01-19 15:49:27 +07:00
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
2026-01-24 12:23:05 +07:00
|
|
|
throw new DuplicateImageException("Duplicate", similarImageProjection.getId(),
|
2026-01-19 15:49:27 +07:00
|
|
|
similarImageProjection.getUserId());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-17 01:19:48 +07:00
|
|
|
long fileSize = Files.size(filePath);
|
|
|
|
|
String originalName = session.getFileName();
|
|
|
|
|
String storedName = filePath.getFileName().toString();
|
|
|
|
|
|
|
|
|
|
FileEntity fileEntity = FileEntity.builder()
|
|
|
|
|
.userId(session.getUserId())
|
|
|
|
|
.originalFileName(originalName)
|
|
|
|
|
.storedFileName(storedName)
|
|
|
|
|
.filePath(session.getFilePath())
|
|
|
|
|
.fileSize(fileSize)
|
|
|
|
|
.mimeType(session.getFileType())
|
2025-12-24 13:58:46 +07:00
|
|
|
.fileExtension(session.getExtension())
|
2025-12-17 01:19:48 +07:00
|
|
|
.checksum(checksum)
|
|
|
|
|
.uploadSessionId(session.getUploadId())
|
|
|
|
|
.status(FileStatus.ACTIVE)
|
|
|
|
|
.build();
|
|
|
|
|
|
|
|
|
|
FileEntity saved = fileEntityRepository.save(fileEntity);
|
2026-01-13 22:40:52 +07:00
|
|
|
|
2026-01-19 15:49:27 +07:00
|
|
|
if (!imageHash.isEmpty()) {
|
|
|
|
|
imageHashService.create(saved, imageHash);
|
2026-01-13 22:40:52 +07:00
|
|
|
}
|
2025-12-17 01:19:48 +07:00
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Transactional(readOnly = true)
|
2025-12-18 14:40:27 +07:00
|
|
|
public FileEntityResponse getById(String fileId, int version) {
|
2025-12-17 01:19:48 +07:00
|
|
|
FileEntity fileEntity = fileEntityRepository.findById(fileId)
|
|
|
|
|
.orElseThrow(() -> new FileEntityNotFoundException(fileId));
|
|
|
|
|
|
2025-12-18 14:40:27 +07:00
|
|
|
return convertToResponse(fileEntity, version);
|
2025-12-17 01:19:48 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Transactional(readOnly = true)
|
2025-12-18 14:40:27 +07:00
|
|
|
public FileEntityResponse getByUploadSessionId(String uploadSessionId, int version) {
|
2025-12-17 01:19:48 +07:00
|
|
|
FileEntity fileEntity = fileEntityRepository.findByUploadSessionId(uploadSessionId)
|
|
|
|
|
.orElseThrow(() -> new FileEntityNotFoundException(
|
|
|
|
|
"Not found for upload session: " + uploadSessionId));
|
|
|
|
|
|
2025-12-18 14:40:27 +07:00
|
|
|
return convertToResponse(fileEntity, version);
|
2025-12-17 01:19:48 +07:00
|
|
|
}
|
|
|
|
|
|
2026-01-22 19:55:09 +07:00
|
|
|
@Transactional(readOnly = true)
|
|
|
|
|
public FileEntityResponse getByFilePath(String filePath, int version) {
|
|
|
|
|
FileEntity fileEntity = fileEntityRepository.findByFilePath(filePath)
|
|
|
|
|
.orElseThrow(() -> new FileEntityNotFoundException("Path: " + filePath));
|
|
|
|
|
|
|
|
|
|
return convertToResponse(fileEntity, version);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Transactional(readOnly = true)
|
|
|
|
|
public FileResponse getAllUserFiles(Long userId, int version) {
|
|
|
|
|
List<FileEntity> fileEntities = fileEntityRepository.findByUserIdAndStatus(
|
|
|
|
|
userId, FileStatus.ACTIVE);
|
|
|
|
|
|
|
|
|
|
List<FileEntityResponse> files = fileEntities.stream()
|
|
|
|
|
.map(file -> convertToResponse(file, version))
|
|
|
|
|
.collect(Collectors.toList());
|
|
|
|
|
|
|
|
|
|
long totalSize = fileEntities.stream()
|
|
|
|
|
.mapToLong(FileEntity::getFileSize)
|
|
|
|
|
.sum();
|
|
|
|
|
|
|
|
|
|
return FileResponse.builder()
|
|
|
|
|
.files(files)
|
|
|
|
|
.totalCount(files.size())
|
|
|
|
|
.totalSize(totalSize)
|
|
|
|
|
.formattedTotalSize(formatFileSize(totalSize))
|
|
|
|
|
.page(1)
|
|
|
|
|
.pageSize(files.size())
|
|
|
|
|
.build();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2025-12-17 01:19:48 +07:00
|
|
|
@Transactional(readOnly = true)
|
2025-12-18 14:40:27 +07:00
|
|
|
public FileResponse getUserFiles(Long userId, int page, int pageSize, int version) {
|
2025-12-17 01:19:48 +07:00
|
|
|
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()) {
|
2025-12-18 01:13:19 +07:00
|
|
|
return FileResponse.builder()
|
2025-12-17 01:19:48 +07:00
|
|
|
.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()
|
2025-12-18 14:40:27 +07:00
|
|
|
.map(file -> convertToResponse(file, version))
|
2025-12-17 01:19:48 +07:00
|
|
|
.collect(Collectors.toList());
|
|
|
|
|
|
|
|
|
|
long totalSize = allFiles.stream()
|
|
|
|
|
.mapToLong(FileEntity::getFileSize)
|
|
|
|
|
.sum();
|
|
|
|
|
|
2025-12-18 01:13:19 +07:00
|
|
|
return FileResponse.builder()
|
2025-12-17 01:19:48 +07:00
|
|
|
.files(files)
|
|
|
|
|
.totalCount(allFiles.size())
|
|
|
|
|
.totalSize(totalSize)
|
|
|
|
|
.formattedTotalSize(formatFileSize(totalSize))
|
|
|
|
|
.page(page)
|
|
|
|
|
.pageSize(pageSize)
|
|
|
|
|
.build();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Transactional
|
2026-01-27 08:09:54 +07:00
|
|
|
public FileEntity markAsDeleted(FileEntity fileEntity) throws IOException {
|
2025-12-17 01:19:48 +07:00
|
|
|
fileEntity.setStatus(FileStatus.DELETED);
|
|
|
|
|
fileEntity.setUpdatedAt(LocalDateTime.now());
|
2026-01-27 08:09:54 +07:00
|
|
|
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("");
|
2025-12-17 01:19:48 +07:00
|
|
|
|
2025-12-26 16:23:17 +07:00
|
|
|
fileEntityRepository.save(fileEntity);
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-27 08:09:54 +07:00
|
|
|
public void deleteFromDisk(FileEntity fileEntity) throws IOException {
|
2025-12-26 16:23:17 +07:00
|
|
|
Path path = Paths.get(fileEntity.getFilePath());
|
|
|
|
|
|
|
|
|
|
if (!Files.exists(path)) {
|
2026-01-27 08:09:54 +07:00
|
|
|
return;
|
2025-12-26 16:23:17 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Files.delete(path);
|
|
|
|
|
|
2026-01-26 21:03:45 +07:00
|
|
|
fileEntityRepository.delete(fileEntity);
|
2025-12-17 01:19:48 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Transactional(readOnly = true)
|
|
|
|
|
public long getUserStorageUsed(Long userId) {
|
|
|
|
|
Long totalSize = fileEntityRepository.getTotalSizeByUserId(userId);
|
|
|
|
|
return totalSize != null ? totalSize : 0L;
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-22 19:55:09 +07:00
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-30 15:09:39 +07:00
|
|
|
public void clearTempFiles(long userId) throws IOException {
|
|
|
|
|
List<FileEntity> fileByUserIdAndStatus = fileEntityRepository.findFileByUserIdAndStatus(userId, FileStatus.TEMP);
|
|
|
|
|
|
|
|
|
|
for (FileEntity fileEntity : fileByUserIdAndStatus) {
|
|
|
|
|
deleteFromDisk(fileEntity);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-22 19:55:09 +07:00
|
|
|
public FileEntity findBySignature(String signature) {
|
|
|
|
|
return fileEntityRepository.findBySignature(signature);
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-26 21:03:45 +07:00
|
|
|
public String findFileIdByPath(String filePath) {
|
|
|
|
|
return fileEntityRepository.findFileIdByFilePath(filePath);
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-22 19:55:09 +07:00
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-17 01:19:48 +07:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-22 19:55:09 +07:00
|
|
|
private String determineFileExtension(String fileExt, FileEntity fileEntity) {
|
|
|
|
|
if (fileExt != null && !fileExt.trim().isEmpty()) {
|
|
|
|
|
return fileExt;
|
|
|
|
|
} else {
|
|
|
|
|
return fileEntity.getFileExtension();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-18 14:40:27 +07:00
|
|
|
private FileEntityResponse convertToResponse(FileEntity fileEntity, int version) {
|
2025-12-17 01:19:48 +07:00
|
|
|
boolean existsOnDisk = checkFileExistsOnDisk(fileEntity.getFilePath());
|
2026-01-28 19:26:40 +07:00
|
|
|
User user = userRepository.findById(fileEntity.getUserId()).get();
|
2025-12-17 01:19:48 +07:00
|
|
|
|
|
|
|
|
return FileEntityResponse.builder()
|
|
|
|
|
.id(fileEntity.getId())
|
|
|
|
|
.userId(fileEntity.getUserId())
|
|
|
|
|
.originalFileName(fileEntity.getOriginalFileName())
|
|
|
|
|
.storedFileName(fileEntity.getStoredFileName())
|
2026-01-28 18:38:43 +07:00
|
|
|
.filePath(fileEntity.getProtectedFilePath())
|
2025-12-17 01:19:48 +07:00
|
|
|
.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()))
|
2025-12-18 14:40:27 +07:00
|
|
|
.downloadUrl("/api/v" + version + "/files/download/" + fileEntity.getId())
|
2025-12-17 01:19:48 +07:00
|
|
|
.existsOnDisk(existsOnDisk)
|
2026-01-20 21:55:42 +07:00
|
|
|
.supportId(fileEntity.getSupportId())
|
2026-01-26 14:02:10 +07:00
|
|
|
.protectStatus(fileEntity.getProtectionStatus().toString())
|
2026-01-28 19:26:40 +07:00
|
|
|
.fileName(fileEntity.getOriginalFileName())
|
|
|
|
|
.ownerName(user.getFullName())
|
|
|
|
|
.ownerEmail(user.getEmail())
|
2026-01-29 21:46:37 +07:00
|
|
|
.ownerCompany(user.getCompanyName())
|
2026-01-28 19:26:40 +07:00
|
|
|
//TODO fix after add logic for check for protect file
|
|
|
|
|
.checksCount(0)
|
|
|
|
|
.fileUploadDate(fileEntity.getCreatedAt())
|
2025-12-17 01:19:48 +07:00
|
|
|
.build();
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-26 13:52:53 +07:00
|
|
|
public String formatFileSize(long size) {
|
2025-12-17 01:19:48 +07:00
|
|
|
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));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|