Files
no-copy/src/main/java/ru/soune/nocopy/service/file/FileCleanupService.java
T

139 lines
4.5 KiB
Java
Raw Normal View History

2025-12-17 13:41:05 +07:00
package ru.soune.nocopy.service.file;
2025-12-15 19:49:09 +07:00
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;
2026-02-05 18:55:54 +07:00
import ru.soune.nocopy.entity.user.ProtectedFileCheck;
import ru.soune.nocopy.repository.ProtectedFileCheckRepository;
2025-12-15 19:49:09 +07:00
import java.io.File;
2025-12-15 19:49:09 +07:00
import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.time.Instant;
2026-02-05 18:55:54 +07:00
import java.time.LocalDate;
2025-12-15 19:49:09 +07:00
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.temporal.ChronoUnit;
2026-02-05 18:55:54 +07:00
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
2025-12-15 19:49:09 +07:00
@Slf4j
@Component
@RequiredArgsConstructor
public class FileCleanupService {
@Value("${file.storage.base-path}")
private String basePath;
@Value("${file.storage.temp-ttl-hours}")
private int tempTtlHours;
2026-02-05 18:55:54 +07:00
private final ProtectedFileCheckRepository protectedFileCheckRepository;
2025-12-15 19:49:09 +07:00
@Scheduled(cron = "0 0 3 * * *")
public void cleanupExpiredFiles() {
log.info("Starting cleanup of expired temporary files");
cleanupTempFilesNightly();
2026-02-05 18:55:54 +07:00
2025-12-15 19:49:09 +07:00
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);
}
}
2026-02-05 18:55:54 +07:00
private void cleanupTempFilesNightly() {
log.info("Starting nightly cleanup of temporary files");
String tempDir = System.getProperty("java.io.tmpdir");
Path tempPath = Paths.get(tempDir);
if (!Files.exists(tempPath)) {
return;
}
Instant cutoffTime = Instant.now().minus(24, ChronoUnit.HOURS); // 24 часа
int deletedCount = 0;
try (Stream<Path> files = Files.list(tempPath)) {
List<Path> tempFiles = files
.filter(path -> {
String filename = path.getFileName().toString();
return filename.contains("_") &&
!filename.endsWith(".tmp") &&
Files.isRegularFile(path);
})
.collect(Collectors.toList());
for (Path filePath : tempFiles) {
try {
File file = filePath.toFile();
Instant lastModified = Instant.ofEpochMilli(file.lastModified());
if (lastModified.isBefore(cutoffTime)) {
boolean deleted = Files.deleteIfExists(filePath);
if (deleted) {
deletedCount++;
}
}
} catch (IOException e) {
log.error("Error deleting file: {}", filePath, e);
}
}
log.info("Nightly cleanup deleted {} temporary files", deletedCount);
} catch (IOException e) {
log.error("Failed to list temp directory", e);
}
}
2025-12-15 19:49:09 +07:00
}