Delete book covers on library deletion

This commit is contained in:
aditya.chandel
2025-01-22 22:57:18 -07:00
parent 788dc9796e
commit 136c957221
2 changed files with 38 additions and 1 deletions

View File

@@ -15,6 +15,7 @@ import com.adityachandel.booklore.model.websocket.Topic;
import com.adityachandel.booklore.repository.BookRepository;
import com.adityachandel.booklore.repository.LibraryPathRepository;
import com.adityachandel.booklore.repository.LibraryRepository;
import com.adityachandel.booklore.service.fileprocessor.FileProcessingUtils;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.InvalidDataAccessApiUsageException;
@@ -38,6 +39,7 @@ public class LibraryService {
private final BookMapper bookMapper;
private final LibraryMapper libraryMapper;
private final NotificationService notificationService;
private final FileProcessingUtils fileProcessingUtils;
public Library updateLibrary(CreateLibraryRequest request, Long libraryId) {
LibraryEntity library = libraryRepository.findById(libraryId).orElseThrow(() -> ApiError.LIBRARY_NOT_FOUND.createException(libraryId));
@@ -144,7 +146,9 @@ public class LibraryService {
}
public void deleteLibrary(long id) {
libraryRepository.findById(id).orElseThrow(() -> ApiError.LIBRARY_NOT_FOUND.createException(id));
LibraryEntity library = libraryRepository.findById(id).orElseThrow(() -> ApiError.LIBRARY_NOT_FOUND.createException(id));
Set<Long> bookIds = library.getBookEntities().stream().map(BookEntity::getId).collect(Collectors.toSet());
fileProcessingUtils.deleteBookCovers(bookIds);
libraryRepository.deleteById(id);
}

View File

@@ -2,7 +2,9 @@ package com.adityachandel.booklore.service.fileprocessor;
import com.adityachandel.booklore.config.AppProperties;
import com.adityachandel.booklore.model.entity.BookMetadataEntity;
import com.adityachandel.booklore.repository.BookRepository;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import javax.imageio.ImageIO;
@@ -10,10 +12,17 @@ import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.Instant;
import java.util.Comparator;
import java.util.Set;
import java.util.stream.Stream;
@AllArgsConstructor
@Service
@Slf4j
public class FileProcessingUtils {
private final AppProperties appProperties;
@@ -45,4 +54,28 @@ public class FileProcessingUtils {
g2d.dispose();
return resizedImage;
}
public void deleteBookCovers(Set<Long> bookIds) {
for (Long bookId : bookIds) {
String bookCoverFolder = appProperties.getPathConfig() + "/thumbs/" + bookId;
Path folderPath = Paths.get(bookCoverFolder);
try {
if (Files.exists(folderPath) && Files.isDirectory(folderPath)) {
try (Stream<Path> walk = Files.walk(folderPath)) {
walk.sorted(Comparator.reverseOrder())
.forEach(path -> {
try {
Files.delete(path);
} catch (IOException e) {
log.error("Failed to delete file: {} - {}", path, e.getMessage());
}
});
}
}
} catch (IOException e) {
log.error("Error processing folder: {} - {}", folderPath, e.getMessage());
}
}
log.info("Deleted {} book covers", bookIds.size());
}
}