save chunks
Test Workflow / test (push) Successful in 4s

This commit is contained in:
vladp
2025-12-16 03:44:59 +07:00
parent 4ddf2f5dde
commit 997daae639
8 changed files with 703 additions and 217 deletions
+5 -1
View File
@@ -22,6 +22,9 @@ services:
build: . build: .
container_name: app-backend container_name: app-backend
environment: environment:
FILE_STORAGE_PATH: /data/uploads
MAX_FILE_SIZE: 1073741824
FILE_CHUNK_SIZE: 5242880
POSTGRES_DB: no_copy_ POSTGRES_DB: no_copy_
POSTGRES_USER: postgres POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres POSTGRES_PASSWORD: postgres
@@ -38,7 +41,7 @@ services:
- backend - backend
- api - api
volumes: volumes:
- ./uploads:/data/uploads - uploads_volume:/data/uploads
grafana: grafana:
image: grafana/grafana:10.3.1 image: grafana/grafana:10.3.1
@@ -143,6 +146,7 @@ volumes:
loki_chunks: loki_chunks:
loki_index: loki_index:
loki_rules: loki_rules:
uploads_volume:
networks: networks:
app-network: app-network:
@@ -3,23 +3,25 @@ package ru.soune.no_copy.controller.file;
import jakarta.validation.Valid; import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import ru.soune.no_copy.dto.file.ChunkUploadRequest;
import ru.soune.no_copy.dto.file.FileApiResponse; import ru.soune.no_copy.dto.file.FileApiResponse;
import ru.soune.no_copy.dto.file.InitUploadRequest; import ru.soune.no_copy.dto.file.InitUploadRequest;
import ru.soune.no_copy.dto.file.UploadProgressResponse; import ru.soune.no_copy.dto.file.UploadProgressResponse;
import ru.soune.no_copy.entity.AuthToken; import ru.soune.no_copy.entity.AuthToken;
import ru.soune.no_copy.entity.file.FileType; import ru.soune.no_copy.entity.file.FileType;
import ru.soune.no_copy.entity.file.FileUploadSession; import ru.soune.no_copy.entity.file.FileUploadSession;
import ru.soune.no_copy.entity.file.UploadStatus;
import ru.soune.no_copy.exception.FileUploadException;
import ru.soune.no_copy.exception.NotFoundAuthToken; import ru.soune.no_copy.exception.NotFoundAuthToken;
import ru.soune.no_copy.exception.UploadSessionNotFoundException;
import ru.soune.no_copy.repository.AuthTokenRepository; import ru.soune.no_copy.repository.AuthTokenRepository;
import ru.soune.no_copy.repository.FileUploadSessionRepository;
import ru.soune.no_copy.service.file.FileUploadService; import ru.soune.no_copy.service.file.FileUploadService;
import java.util.Arrays; import java.util.*;
import java.util.List;
import java.util.Optional;
@Slf4j @Slf4j
@RestController @RestController
@@ -31,6 +33,8 @@ public class FileUploadController {
private final AuthTokenRepository authTokenRepository; private final AuthTokenRepository authTokenRepository;
private final FileUploadSessionRepository fileUploadSessionRepository;
//TODO ADD TEMPLATE JSON, RESPONSE OK,ADD MESSAGE CODE //TODO ADD TEMPLATE JSON, RESPONSE OK,ADD MESSAGE CODE
@PostMapping("/init") @PostMapping("/init")
public ResponseEntity<FileApiResponse<FileUploadSession>> initUpload(@Valid @RequestBody InitUploadRequest request, public ResponseEntity<FileApiResponse<FileUploadSession>> initUpload(@Valid @RequestBody InitUploadRequest request,
@@ -61,17 +65,26 @@ public class FileUploadController {
@PostMapping("/chunk") @PostMapping("/chunk")
public ResponseEntity<FileApiResponse<UploadProgressResponse>> uploadChunk( public ResponseEntity<FileApiResponse<UploadProgressResponse>> uploadChunk(
@Valid @RequestBody ChunkUploadRequest chunkUploadRequest, @RequestParam("uploadId") String uploadId,
@RequestParam("file") MultipartFile chunk, @RequestHeader("Authorization") String tokenHeader) { @RequestParam("chunkNumber") Integer chunkNumber,
String token = tokenHeader.replace("Bearer ", ""); @RequestParam("chunk") MultipartFile chunk) {
Optional<AuthToken> tokenOptional = authTokenRepository.findByToken(token); log.info("Uploading chunk {} for session {}, file size: {} bytes",
chunkNumber, uploadId, chunk.getSize());
tokenOptional.orElseThrow(() -> new NotFoundAuthToken("Token: " + token + "not found")); if (chunk.isEmpty()) {
return ResponseEntity.badRequest()
.body(FileApiResponse.error("Chunk file is empty"));
}
if (uploadId == null || uploadId.isBlank()) {
return ResponseEntity.badRequest()
.body(FileApiResponse.error("Upload ID is required"));
}
try { try {
UploadProgressResponse progress = fileUploadService.uploadChunk( UploadProgressResponse progress = fileUploadService.uploadChunk(
chunkUploadRequest.getUploadId(), chunkUploadRequest.getChunkNumber(), chunk); uploadId, chunkNumber, chunk);
return ResponseEntity.ok(FileApiResponse.success(progress)); return ResponseEntity.ok(FileApiResponse.success(progress));
@@ -82,39 +95,130 @@ public class FileUploadController {
} }
} }
// @GetMapping("/progress/{uploadId}") @GetMapping("/progress/{uploadId}")
// public ResponseEntity<FileApiResponse<UploadProgressResponse>> getProgress( public ResponseEntity<FileApiResponse<UploadProgressResponse>> getProgress(
// @PathVariable String uploadId, @PathVariable String uploadId) {
// @RequestHeader(value = "X-User-Id", required = false, defaultValue = "1") Long userId) {
// try {
// try { UploadProgressResponse progress = fileUploadService.getUploadProgress(uploadId);
// UploadProgressResponse progress = fileUploadService.getUploadProgress(uploadId); return ResponseEntity.ok(FileApiResponse.success(progress));
// return ResponseEntity.ok(FileApiResponse.success(progress));
// } catch (UploadSessionNotFoundException e) {
// } catch (Exception e) { return ResponseEntity.status(HttpStatus.NOT_FOUND)
// log.error("Error getting progress", e); .body(FileApiResponse.error("Upload session not found"));
// return ResponseEntity.badRequest() } catch (Exception e) {
// .body(FileApiResponse.error(e.getMessage())); log.error("Error getting progress", e);
// } return ResponseEntity.badRequest()
// } .body(FileApiResponse.error("Failed to get progress"));
// }
// @PostMapping("/complete/{uploadId}") }
// public ResponseEntity<FileApiResponse<UploadProgressResponse>> completeUpload(
// @PathVariable String uploadId, @PostMapping("/{uploadId}/complete")
// @RequestHeader(value = "X-User-Id", required = false, defaultValue = "1") Long userId) { public ResponseEntity<FileApiResponse<UploadProgressResponse>> completeUpload(
// @PathVariable String uploadId) {
// log.info("Completing upload for session {}", uploadId);
// log.info("Manual completion requested for session: {}", uploadId);
// try {
// UploadProgressResponse progress = fileUploadService.getUploadProgress(uploadId); try {
// UploadProgressResponse progress = fileUploadService.getUploadProgress(uploadId);
// return ResponseEntity.ok(FileApiResponse.success(
// "File assembly in progress", progress)); if (progress.getStatus() == UploadStatus.COMPLETED) {
// return ResponseEntity.ok(FileApiResponse.success(
// } catch (Exception e) { "Upload already completed", progress));
// log.error("Error completing upload", e); }
// return ResponseEntity.badRequest()
// .body(FileApiResponse.error(e.getMessage())); if (progress.getUploadedChunks() < progress.getTotalChunks()) {
// } return ResponseEntity.badRequest()
// } .body(FileApiResponse.error(
String.format("Not all chunks uploaded: %d/%d",
progress.getUploadedChunks(), progress.getTotalChunks())));
}
return ResponseEntity.ok(FileApiResponse.success(
"File assembly in progress", progress));
} catch (UploadSessionNotFoundException e) {
return ResponseEntity.status(HttpStatus.NOT_FOUND)
.body(FileApiResponse.error("Upload session not found"));
} catch (Exception e) {
log.error("Error completing upload", e);
return ResponseEntity.internalServerError()
.body(FileApiResponse.error("Failed to complete upload"));
}
}
@PostMapping("/{uploadId}/cancel")
public ResponseEntity<FileApiResponse<Void>> cancelUpload(
@PathVariable String uploadId) {
log.info("Cancelling upload session: {}", uploadId);
try {
fileUploadService.cancelUpload(uploadId);
return ResponseEntity.ok(FileApiResponse.success(
"Upload cancelled successfully", null));
} catch (UploadSessionNotFoundException e) {
return ResponseEntity.status(HttpStatus.NOT_FOUND)
.body(FileApiResponse.error("Upload session not found"));
} catch (FileUploadException e) {
return ResponseEntity.badRequest()
.body(FileApiResponse.error(e.getMessage()));
} catch (Exception e) {
log.error("Error cancelling upload", e);
return ResponseEntity.internalServerError()
.body(FileApiResponse.error("Failed to cancel upload"));
}
}
@PostMapping("/{uploadId}/retry")
public ResponseEntity<FileApiResponse<UploadProgressResponse>> retryUpload(
@PathVariable String uploadId) {
log.info("Retry requested for failed upload: {}", uploadId);
try {
fileUploadService.retryFailedUpload(uploadId);
UploadProgressResponse progress = fileUploadService.getUploadProgress(uploadId);
return ResponseEntity.ok(FileApiResponse.success(
"Upload retry initiated", progress));
} catch (UploadSessionNotFoundException e) {
return ResponseEntity.status(HttpStatus.NOT_FOUND)
.body(FileApiResponse.error("Upload session not found"));
} catch (FileUploadException e) {
return ResponseEntity.badRequest()
.body(FileApiResponse.error(e.getMessage()));
} catch (Exception e) {
log.error("Error retrying upload", e);
return ResponseEntity.internalServerError()
.body(FileApiResponse.error("Failed to retry upload"));
}
}
@GetMapping("/{uploadId}/chunks")
public ResponseEntity<FileApiResponse<Map<Integer, Boolean>>> getChunkStatus(
@PathVariable String uploadId) {
try {
FileUploadSession session = fileUploadSessionRepository.findById(uploadId)
.orElseThrow(() -> new UploadSessionNotFoundException(uploadId));
Map<Integer, Boolean> chunkStatus = new HashMap<>();
for (int i = 0; i < session.getTotalChunks(); i++) {
chunkStatus.put(i, session.getChunkPaths().containsKey(i));
}
return ResponseEntity.ok(FileApiResponse.success(chunkStatus));
} catch (UploadSessionNotFoundException e) {
return ResponseEntity.status(HttpStatus.NOT_FOUND)
.body(FileApiResponse.error("Upload session not found"));
} catch (Exception e) {
log.error("Error getting chunk status", e);
return ResponseEntity.internalServerError()
.body(FileApiResponse.error("Failed to get chunk status"));
}
}
} }
@@ -0,0 +1,23 @@
package ru.soune.no_copy.dto.file;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import ru.soune.no_copy.entity.file.UploadStatus;
import java.util.List;
import java.util.Map;
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class ChunkStatusResponse {
private String uploadId;
private Integer totalChunks;
private Integer uploadedChunks;
private List<Integer> missingChunks;
private Map<Integer, String> chunkPaths;
private UploadStatus status;
}
@@ -53,6 +53,18 @@ public class FileUploadSession {
@Column(name = "created_at", updatable = false, nullable = false) @Column(name = "created_at", updatable = false, nullable = false)
private LocalDateTime createdAt; private LocalDateTime createdAt;
@Column(name = "expires_at")
private LocalDateTime expiresAt;
@Version
private Long version;
@Column(name = "last_error")
private String lastError;
@Column(name = "retry_count")
private Integer retryCount = 0;
@Column(name = "completed_at") @Column(name = "completed_at")
private LocalDateTime completedAt; private LocalDateTime completedAt;
@@ -64,4 +76,14 @@ public class FileUploadSession {
@MapKeyColumn(name = "chunk_number") @MapKeyColumn(name = "chunk_number")
@Column(name = "chunk_path") @Column(name = "chunk_path")
private Map<Integer, String> chunkPaths = new HashMap<>(); private Map<Integer, String> chunkPaths = new HashMap<>();
@PrePersist
public void prePersist() {
this.createdAt = LocalDateTime.now();
this.expiresAt = LocalDateTime.now().plusHours(24);
this.chunksUploaded = 0;
if (this.status == null) {
this.status = UploadStatus.INITIATED;
}
}
} }
@@ -1,9 +1,30 @@
package ru.soune.no_copy.repository; package ru.soune.no_copy.repository;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
import ru.soune.no_copy.entity.file.FileUploadSession; import ru.soune.no_copy.entity.file.FileUploadSession;
import ru.soune.no_copy.entity.file.UploadStatus;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Set;
@Repository @Repository
public interface FileUploadSessionRepository extends JpaRepository<FileUploadSession, String> { public interface FileUploadSessionRepository extends JpaRepository<FileUploadSession, String> {
List<FileUploadSession> findByUserId(Long userId);
List<FileUploadSession> findByStatus(UploadStatus status);
@Query("SELECT s FROM FileUploadSession s WHERE s.status IN :statuses AND s.createdAt < :threshold")
List<FileUploadSession> findByStatusInAndCreatedAtBefore(
@Param("statuses") Set<UploadStatus> statuses,
@Param("threshold") LocalDateTime threshold);
@Query("SELECT s FROM FileUploadSession s WHERE s.expiresAt < :now AND s.status IN :statuses")
List<FileUploadSession> findExpiredSessions(
@Param("now") LocalDateTime now,
@Param("statuses") Set<UploadStatus> statuses);
} }
@@ -4,14 +4,20 @@ import org.springframework.web.multipart.MultipartFile;
import ru.soune.no_copy.dto.file.UploadProgressResponse; import ru.soune.no_copy.dto.file.UploadProgressResponse;
import ru.soune.no_copy.entity.file.FileUploadSession; import ru.soune.no_copy.entity.file.FileUploadSession;
import java.io.IOException;
public interface FileUploadService { public interface FileUploadService {
FileUploadSession initUpload(Long userId, String fileName, FileUploadSession initUpload(Long userId, String fileName,
String fileType, long fileSize); String fileType, long fileSize);
UploadProgressResponse uploadChunk(String uploadId, Integer chunkNumber, UploadProgressResponse uploadChunk(String uploadId, Integer chunkNumber,
MultipartFile chunkFile); MultipartFile chunkFile) throws IOException;
UploadProgressResponse getUploadProgress(String uploadId); UploadProgressResponse getUploadProgress(String uploadId);
void cleanupExpiredSessions(); void cleanupExpiredSessions();
public void retryFailedUpload(String uploadId);
public void cancelUpload(String uploadId);
} }
@@ -17,14 +17,16 @@ import ru.soune.no_copy.exception.UploadSessionNotFoundException;
import ru.soune.no_copy.repository.FileUploadSessionRepository; import ru.soune.no_copy.repository.FileUploadSessionRepository;
import java.io.*; import java.io.*;
import java.nio.file.Files; import java.nio.file.*;
import java.nio.file.Path; import java.nio.file.attribute.PosixFilePermission;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.security.MessageDigest; import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.util.Comparator; 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; import java.util.stream.Stream;
@Slf4j @Slf4j
@@ -43,24 +45,36 @@ public class FileUploadServiceImpl implements FileUploadService {
@Value("${file.storage.max-file-size}") @Value("${file.storage.max-file-size}")
private long maxFileSize; 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;
@PostConstruct @PostConstruct
public void init() { public void init() {
try { try {
Path storageRoot = Paths.get(basePath).toAbsolutePath().normalize(); storageRoot = Paths.get(basePath).toAbsolutePath().normalize();
log.info("Initializing file storage at: {}", storageRoot);
log.info("Storage will be at: {}", storageRoot); createDirectoryIfNotExists(storageRoot);
createDirectoryIfNotExists(storageRoot.resolve("temp"));
createDirectoryIfNotExists(storageRoot.resolve("uploads"));
if (!Files.exists(storageRoot)) { if (!System.getProperty("os.name").toLowerCase().contains("win")) {
Files.createDirectories(storageRoot); setDirectoryPermissions(storageRoot);
log.info("Created: {}", storageRoot);
} }
Files.createDirectories(storageRoot.resolve("temp")); log.info("File storage initialized successfully");
Files.createDirectories(storageRoot.resolve("user"));
} catch (IOException e) { } catch (IOException e) {
log.error("CANNOT CREATE STORAGE!", e); log.error("Failed to initialize file storage at: {}", basePath, e);
throw new RuntimeException("Storage failed", e); throw new RuntimeException("Storage initialization failed", e);
} }
} }
@@ -68,13 +82,22 @@ public class FileUploadServiceImpl implements FileUploadService {
@Transactional @Transactional
public FileUploadSession initUpload(Long userId, String fileName, public FileUploadSession initUpload(Long userId, String fileName,
String fileType, long fileSize) { String fileType, long fileSize) {
log.info("Initializing upload for user {}: {} ({} bytes, type: {})",
userId, fileName, fileSize, fileType);
if (fileSize > maxFileSize) { if (fileSize > maxFileSize) {
throw new FileUploadException( throw new FileUploadException(
String.format("File size %d exceeds maximum allowed size %d", String.format("File size %d exceeds maximum allowed size %d",
fileSize, maxFileSize)); fileSize, maxFileSize));
} }
if (fileSize <= 0) {
throw new FileUploadException("File size must be positive");
}
int totalChunks = (int) Math.ceil((double) fileSize / chunkSize); 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() FileUploadSession session = FileUploadSession.builder()
.userId(userId) .userId(userId)
@@ -84,9 +107,15 @@ public class FileUploadServiceImpl implements FileUploadService {
.totalChunks(totalChunks) .totalChunks(totalChunks)
.chunksUploaded(0) .chunksUploaded(0)
.status(UploadStatus.INITIATED) .status(UploadStatus.INITIATED)
.expiresAt(LocalDateTime.now().plusHours(sessionExpiryHours))
.retryCount(0)
.build(); .build();
return sessionRepository.save(session); FileUploadSession savedSession = sessionRepository.save(session);
log.info("Upload session created: {} for file: {}",
savedSession.getUploadId(), fileName);
return savedSession;
} }
@Override @Override
@@ -94,38 +123,92 @@ public class FileUploadServiceImpl implements FileUploadService {
public UploadProgressResponse uploadChunk(String uploadId, Integer chunkNumber, public UploadProgressResponse uploadChunk(String uploadId, Integer chunkNumber,
MultipartFile chunkFile) { MultipartFile chunkFile) {
log.info("Uploading chunk {} for session {}, chunk size: {} bytes", log.info("Processing chunk {} for session {}, size: {} bytes",
chunkNumber, uploadId, chunkFile.getSize()); chunkNumber, uploadId, chunkFile.getSize());
FileUploadSession session = sessionRepository.findById(uploadId) FileUploadSession session = sessionRepository.findById(uploadId)
.orElseThrow(() -> new UploadSessionNotFoundException(uploadId)); .orElseThrow(() -> new UploadSessionNotFoundException(uploadId));
if (session.getChunkPaths().containsKey(chunkNumber)) { validateSession(session);
log.info("Chunk {} already uploaded for session {}", chunkNumber, uploadId);
return UploadProgressResponse.fromSession(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) { if (chunkFile.getSize() > chunkSize) {
throw new ChunkSizeExceededException(chunkFile.getSize(), chunkSize); throw new ChunkSizeExceededException(chunkFile.getSize(), chunkSize);
} }
try { return processChunk(session, chunkNumber, chunkFile);
log.debug("Chunk {} actual size: {} bytes", chunkNumber, chunkFile.getSize()); }
String chunkPath = saveChunkToDisk(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.getChunkPaths().put(chunkNumber, chunkPath);
session.setChunksUploaded(session.getChunksUploaded() + 1); session.setChunksUploaded(session.getChunksUploaded() + 1);
session.setStatus(UploadStatus.UPLOADING);
log.info("Chunk {} uploaded successfully. Total uploaded: {}/{}", log.debug("Chunk {} saved successfully. Uploaded: {}/{}",
chunkNumber, session.getChunksUploaded(), session.getTotalChunks()); chunkNumber, session.getChunksUploaded(), session.getTotalChunks());
if (session.getChunksUploaded().equals(session.getTotalChunks())) { if (session.getChunksUploaded().equals(session.getTotalChunks())) {
session.setStatus(UploadStatus.UPLOADING); log.info("All chunks uploaded for session {}. Starting assembly...",
session.getUploadId());
sessionRepository.save(session); sessionRepository.save(session);
log.info("All chunks uploaded for session {}. Starting assembly...", uploadId);
assembleFileAsync(session); assembleFileAsync(session);
} else { } else {
sessionRepository.save(session); sessionRepository.save(session);
@@ -133,53 +216,99 @@ public class FileUploadServiceImpl implements FileUploadService {
return UploadProgressResponse.fromSession(session); return UploadProgressResponse.fromSession(session);
} catch (IOException e) { } catch (Exception e) {
log.error("Failed to save chunk {}", chunkNumber, 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.setStatus(UploadStatus.FAILED);
session.setLastError(e.getMessage());
sessionRepository.save(session); sessionRepository.save(session);
throw new RuntimeException("Failed to save chunk: " + e.getMessage(), e);
throw new FileUploadException("Failed to upload chunk: " + e.getMessage(), e);
} }
} }
private String saveChunkToDisk(FileUploadSession session, private UploadProgressResponse handleExistingChunk(FileUploadSession session,
Integer chunkNumber, Integer chunkNumber,
MultipartFile chunkFile) throws IOException { 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(); Long userId = session.getUserId();
String uploadId = session.getUploadId(); String uploadId = session.getUploadId();
Path chunkDir = Paths.get(basePath, "temp", Path chunkDir = getChunkDirectory(userId, uploadId);
"user", String.valueOf(userId), uploadId);
Files.createDirectories(chunkDir); Files.createDirectories(chunkDir);
String chunkFileName = String.format("chunk_%03d.tmp", chunkNumber); String tempFileName = String.format("chunk_%04d.%s.tmp",
Path chunkPath = chunkDir.resolve(chunkFileName); chunkNumber, UUID.randomUUID());
String finalFileName = String.format("chunk_%04d.tmp", chunkNumber);
try (InputStream inputStream = chunkFile.getInputStream(); Path tempPath = chunkDir.resolve(tempFileName);
OutputStream outputStream = Files.newOutputStream(chunkPath, Path finalPath = chunkDir.resolve(finalFileName);
StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING)) {
byte[] buffer = new byte[8192]; try {
int bytesRead; chunkFile.transferTo(tempPath.toFile());
long totalBytes = 0;
while ((bytesRead = inputStream.read(buffer)) != -1) { long savedSize = Files.size(tempPath);
outputStream.write(buffer, 0, bytesRead); long uploadedSize = chunkFile.getSize();
totalBytes += bytesRead;
if (uploadedSize > 0 && savedSize != uploadedSize) {
throw new IOException(
String.format("Size mismatch: saved %d, uploaded %d",
savedSize, uploadedSize));
} }
log.debug("Chunk {} saved to {}: {} bytes written", Files.move(tempPath, finalPath, StandardCopyOption.ATOMIC_MOVE);
chunkNumber, chunkPath, totalBytes);
} catch (Exception e) { log.debug("Chunk {} saved successfully: {} bytes",
log.error("Error saving chunk {} to {}", chunkNumber, chunkPath, e); chunkNumber, savedSize);
throw e;
return finalPath.toString();
} finally {
Files.deleteIfExists(tempPath);
} }
}
long savedSize = Files.size(chunkPath); private void cleanupFailedChunk(String chunkPath) {
log.debug("Chunk {} saved size: {} bytes", chunkNumber, savedSize); try {
Files.deleteIfExists(Paths.get(chunkPath));
return chunkPath.toString(); log.debug("Cleaned up failed chunk: {}", chunkPath);
} catch (IOException e) {
log.warn("Failed to cleanup chunk: {}", chunkPath, e);
}
} }
@Async("fileUploadTaskExecutor") @Async("fileUploadTaskExecutor")
@@ -187,130 +316,307 @@ public class FileUploadServiceImpl implements FileUploadService {
public void assembleFileAsync(FileUploadSession session) { public void assembleFileAsync(FileUploadSession session) {
try { try {
assembleFile(session); assembleFile(session);
log.info("File assembly completed for session {}", session.getUploadId()); log.info("File assembly completed successfully for session: {}",
session.getUploadId());
} catch (Exception e) { } catch (Exception e) {
log.error("Failed to assemble file for session {}", log.error("Failed to assemble file for session {}: {}",
session.getUploadId(), e); session.getUploadId(), e.getMessage(), e);
session.setStatus(UploadStatus.FAILED); handleAssemblyFailure(session, e);
sessionRepository.save(session);
} }
} }
private void assembleFile(FileUploadSession session) throws IOException { private void assembleFile(FileUploadSession session) throws IOException {
Long userId = session.getUserId(); log.info("Starting file assembly for session: {} ({})",
String uploadId = session.getUploadId(); session.getUploadId(), session.getFileName());
log.info("Assembling file for session {}: {} ({} chunks)", Path finalFilePath = null;
uploadId, session.getFileName(), session.getTotalChunks());
Path finalDir = createUserDirectory(userId, session.getFileType());
String safeFileName = generateSafeFileName(session.getFileName());
Path finalFilePath = finalDir.resolve(safeFileName);
log.debug("Final file path: {}", finalFilePath);
try (OutputStream outputStream = new BufferedOutputStream(
Files.newOutputStream(finalFilePath, StandardOpenOption.CREATE))) {
Path chunkDir = getChunkDirectory(userId, uploadId);
long totalAssembled = 0;
for (int i = 0; i < session.getTotalChunks(); i++) {
Path chunkPath = chunkDir.resolve(String.format("chunk_%03d.tmp", i));
if (!Files.exists(chunkPath)) {
throw new IOException("Missing chunk: " + chunkPath);
}
byte[] chunkData = Files.readAllBytes(chunkPath);
outputStream.write(chunkData);
totalAssembled += chunkData.length;
log.debug("Added chunk {}: {} bytes (total: {})",
i, chunkData.length, totalAssembled);
}
log.info("File assembled: {} bytes total", totalAssembled);
}
try { try {
validateFileIntegrity(finalFilePath, session.getFileSize()); finalFilePath = prepareFinalFile(session);
} catch (IOException e) {
log.warn("File size mismatch (temporarily ignored): {}", e.getMessage()); 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 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());
Path finalPath = userUploadsDir.resolve(safeFileName);
if (Files.exists(finalPath)) {
throw new IOException("File already exists: " + finalPath);
} }
session.setFilePath(finalFilePath.toString()); return finalPath;
session.setChecksum(calculateChecksum(finalFilePath)); }
private String generateUniqueFileName(String originalName) {
String safeName = originalName.replaceAll("[^a-zA-Z0-9\\.\\-_]", "_");
String timestamp = String.valueOf(System.currentTimeMillis());
String uuid = UUID.randomUUID().toString().substring(0, 8);
int dotIndex = safeName.lastIndexOf('.');
if (dotIndex > 0) {
String name = safeName.substring(0, dotIndex);
String extension = safeName.substring(dotIndex);
return String.format("%s_%s_%s%s", name, timestamp, uuid, extension);
} else {
return String.format("%s_%s_%s", safeName, timestamp, uuid);
}
}
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]; // 8KB буфер
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.setStatus(UploadStatus.COMPLETED);
session.setCompletedAt(LocalDateTime.now()); session.setCompletedAt(LocalDateTime.now());
sessionRepository.save(session); sessionRepository.save(session);
}
log.info("File assembled successfully: {} -> {}", private void cleanupSessionFiles(FileUploadSession session) {
session.getFileName(), finalFilePath);
try { try {
cleanupChunks(getChunkDirectory(userId, uploadId)); 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) { } catch (IOException e) {
log.warn("Failed to cleanup chunks: {}", e.getMessage()); log.warn("Failed to cleanup temp files for session {}: {}",
session.getUploadId(), e.getMessage());
} }
} }
private Path createUserDirectory(Long userId, String fileType) throws IOException { private void deleteDirectoryRecursively(Path directory) throws IOException {
Path userDir = Paths.get(basePath, "user", String.valueOf(userId), extractCategory(fileType)); 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);
}
});
}
}
Files.createDirectories(userDir); private void handleAssemblyFailure(FileUploadSession session, Exception e) {
session.setStatus(UploadStatus.FAILED);
session.setLastError(e.getMessage());
sessionRepository.save(session);
return userDir; 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) { private String extractCategory(String fileType) {
if (fileType.startsWith("image/")) { if (fileType == null) {
return "other";
}
String lowerType = fileType.toLowerCase();
if (lowerType.startsWith("image/")) {
return "images"; return "images";
} else if (fileType.startsWith("video/")) { } else if (lowerType.startsWith("video/")) {
return "videos"; return "videos";
} else if (fileType.startsWith("audio/")) { } else if (lowerType.startsWith("audio/")) {
return "audio"; return "audio";
} else if (fileType.contains("pdf")) { } else if (lowerType.contains("pdf") ||
lowerType.contains("document") ||
lowerType.contains("text") ||
lowerType.contains("msword") ||
lowerType.contains("wordprocessing") ||
lowerType.contains("spreadsheet")) {
return "documents"; return "documents";
} else if (fileType.contains("text")) {
return "text";
} else { } else {
return "other"; return "other";
} }
} }
private String generateSafeFileName(String originalName) { private String calculateChecksum(Path filePath) {
String safeName = originalName.replaceAll("[^a-zA-Z0-9\\.\\-_]", "_");
String timestamp = String.valueOf(System.currentTimeMillis());
int dotIndex = safeName.lastIndexOf('.');
if (dotIndex > 0) {
String name = safeName.substring(0, dotIndex);
String extension = safeName.substring(dotIndex);
return name + "_" + timestamp + extension;
} else {
return safeName + "_" + timestamp;
}
}
private void validateFileIntegrity(Path filePath, long expectedSize) throws IOException {
long actualSize = Files.size(filePath);
log.info("File integrity check: expected={}, actual={}", expectedSize, actualSize);
if (actualSize != expectedSize) {
throw new IOException(
String.format("File size mismatch: expected %d, got %d",
expectedSize, actualSize));
}
}
private String calculateChecksum(Path filePath) throws IOException {
try { try {
MessageDigest md = MessageDigest.getInstance("MD5"); MessageDigest md = MessageDigest.getInstance("SHA-256");
byte[] buffer = new byte[8192]; byte[] buffer = new byte[8192];
try (InputStream inputStream = Files.newInputStream(filePath)) { try (InputStream inputStream = Files.newInputStream(filePath)) {
@@ -328,37 +634,34 @@ public class FileUploadServiceImpl implements FileUploadService {
return sb.toString(); return sb.toString();
} catch (NoSuchAlgorithmException e) { } catch (NoSuchAlgorithmException | IOException e) {
log.warn("MD5 algorithm not available, skipping checksum", e); log.warn("Failed to calculate checksum for file: {}", filePath, e);
return "N/A"; return "N/A";
} }
} }
private void cleanupChunks(Path chunkDir) throws IOException { private void createDirectoryIfNotExists(Path directory) throws IOException {
if (Files.exists(chunkDir)) { if (!Files.exists(directory)) {
try (Stream<Path> walk = Files.walk(chunkDir)) { Files.createDirectories(directory);
walk.sorted(Comparator.reverseOrder()) log.debug("Created directory: {}", directory);
.map(Path::toFile)
.forEach(File::delete);
log.info("Cleaned up chunk directory: {}", chunkDir);
}
} }
} }
private Path getChunkDirectory(Long userId, String uploadId) { private void setDirectoryPermissions(Path directory) throws IOException {
return Paths.get(basePath, "temp", "user", String.valueOf(userId), uploadId); 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);
@Override Files.setPosixFilePermissions(directory, permissions);
public UploadProgressResponse getUploadProgress(String uploadId) { log.debug("Set permissions for directory: {}", directory);
FileUploadSession session = sessionRepository.findById(uploadId)
.orElseThrow(() -> new UploadSessionNotFoundException(uploadId));
return UploadProgressResponse.fromSession(session); } catch (UnsupportedOperationException e) {
log.debug("Posix permissions not supported on this system");
}
} }
}
@Override
public void cleanupExpiredSessions() {
log.info("Cleanup not implemented yet");
}
}
+7 -4
View File
@@ -23,10 +23,13 @@ spring:
file: file:
storage: storage:
base-path: ${FILE_STORAGE_PATH:./uploads} base-path: ${FILE_STORAGE_PATH:/data/uploads}
chunk-size: 5242880 # 5MB chunk-size: ${FILE_CHUNK_SIZE:5242880} # 5MB
temp-ttl-hours: 24 max-file-size: ${MAX_FILE_SIZE:10737418240} # 10GB
max-file-size: 10737418240 # 10GB max-retry-attempts: ${MAX_RETRY_ATTEMPTS:3}
chunk-timeout-ms: ${CHUNK_TIMEOUT_MS:300000} # 5 минут
temp-ttl-hours: ${TEMP_TTL_HOURS:72} # 3 дня
session-expiry-hours: ${SESSION_EXPIRY_HOURS:24}
server: server:
port: ${SERVER_PORT:8080} port: ${SERVER_PORT:8080}