mirror of
https://github.com/dedicatedcode/reitti.git
synced 2026-01-09 01:17:57 -05:00
Add cleanup job (#367)
This commit is contained in:
@@ -0,0 +1,70 @@
|
||||
package com.dedicatedcode.reitti.service;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
public class MemoryCleanupJob {
|
||||
private static final Logger log = LoggerFactory.getLogger(MemoryCleanupJob.class);
|
||||
|
||||
private final StorageService storageService;
|
||||
private final JdbcTemplate jdbcTemplate;
|
||||
|
||||
public MemoryCleanupJob(StorageService storageService, JdbcTemplate jdbcTemplate) {
|
||||
this.storageService = storageService;
|
||||
this.jdbcTemplate = jdbcTemplate;
|
||||
}
|
||||
|
||||
@Scheduled(cron = "${reitti.storage.cleanup.cron}")
|
||||
public void cleanUp() {
|
||||
List<String> itemNames;
|
||||
try {
|
||||
itemNames = this.storageService.getChildren("/memories");
|
||||
} catch (Exception e) {
|
||||
log.error("Failed to get memory directories for cleanup", e);
|
||||
return;
|
||||
}
|
||||
|
||||
for (String itemName : itemNames) {
|
||||
try {
|
||||
boolean exists = this.jdbcTemplate.queryForObject("SELECT COUNT(*) FROM memory WHERE id = ?::int", Integer.class, itemName) > 0;
|
||||
if (!exists) {
|
||||
String pathToDelete = "/memories/" + itemName;
|
||||
this.storageService.remove(pathToDelete);
|
||||
log.info("deleted path [{}] since memory [{}] does not exists", pathToDelete, itemName);
|
||||
} else {
|
||||
try {
|
||||
this.storageService.getChildren("/memories/" + itemName).forEach(s -> {
|
||||
try {
|
||||
// Check if the file is referenced in any memory_block_image_gallery images column
|
||||
boolean isReferenced = this.jdbcTemplate.queryForObject(
|
||||
"SELECT COUNT(*) FROM memory_block_image_gallery WHERE images::text LIKE ? AND block_id IN (SELECT id FROM memory_block WHERE memory_id = ?::int)",
|
||||
Integer.class,
|
||||
"%" + s + "%",
|
||||
itemName
|
||||
) > 0;
|
||||
|
||||
if (!isReferenced) {
|
||||
String pathToDelete = "/memories/" + itemName + "/" + s;
|
||||
this.storageService.remove(pathToDelete);
|
||||
log.info("deleted file [{}] since it is not referenced in any memory block image gallery", pathToDelete);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("Failed to process file [{}] in memory [{}] during cleanup", s, itemName, e);
|
||||
}
|
||||
});
|
||||
} catch (Exception e) {
|
||||
log.error("Failed to get files for memory [{}] during cleanup", itemName, e);
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("Failed to process memory [{}] during cleanup", itemName, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,7 @@ import org.springframework.stereotype.Service;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.file.*;
|
||||
import java.util.List;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
@Service
|
||||
@@ -54,6 +55,44 @@ public class StorageService {
|
||||
}
|
||||
}
|
||||
|
||||
public List<String> getChildren(String path) {
|
||||
Path basePath = Paths.get(storagePath, path);
|
||||
try (Stream<Path> paths = Files.walk(basePath, 1)) {
|
||||
return paths
|
||||
.map(basePath::relativize)
|
||||
.map(Path::toString)
|
||||
.filter(istr -> !istr.isEmpty() && !istr.equals(".") && !istr.equals(".."))
|
||||
.toList();
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException("Failed to read item '" + path + "': " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
public void remove(String itemName) {
|
||||
Path filePath = Paths.get(storagePath, itemName);
|
||||
try {
|
||||
if (Files.exists(filePath)) {
|
||||
if (Files.isDirectory(filePath)) {
|
||||
try (Stream<Path> paths = Files.walk(filePath)) {
|
||||
paths.sorted(java.util.Comparator.reverseOrder())
|
||||
.forEach(path -> {
|
||||
try {
|
||||
Files.delete(path);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException("Failed to delete path '" + path + "': " + e.getMessage(), e);
|
||||
}
|
||||
});
|
||||
}
|
||||
} else {
|
||||
// Delete single file
|
||||
Files.delete(filePath);
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException("Failed to remove item '" + itemName + "': " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
public static class StorageContent {
|
||||
private final InputStream inputStream;
|
||||
private final String contentType;
|
||||
|
||||
@@ -15,3 +15,5 @@ reitti.server.advertise-uri=http://localhost:8080
|
||||
reitti.security.local-login.disable=false
|
||||
|
||||
reitti.storage.path=data/
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user