From 4916ce0d9bd98029bca05811eaf237da46647e5d Mon Sep 17 00:00:00 2001 From: "aditya.chandel" <> Date: Thu, 13 Feb 2025 10:14:19 -0700 Subject: [PATCH] Unregister paths on library delete --- .../adityachandel/booklore/service/LibraryService.java | 10 ++++------ .../booklore/service/monitoring/MonitoringService.java | 4 ++-- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/booklore-api/src/main/java/com/adityachandel/booklore/service/LibraryService.java b/booklore-api/src/main/java/com/adityachandel/booklore/service/LibraryService.java index 2e459ade4..6810e0dfa 100644 --- a/booklore-api/src/main/java/com/adityachandel/booklore/service/LibraryService.java +++ b/booklore-api/src/main/java/com/adityachandel/booklore/service/LibraryService.java @@ -6,7 +6,6 @@ import com.adityachandel.booklore.mapper.LibraryMapper; import com.adityachandel.booklore.model.dto.Book; import com.adityachandel.booklore.model.dto.Library; import com.adityachandel.booklore.model.dto.LibraryPath; -import com.adityachandel.booklore.model.dto.Sort; import com.adityachandel.booklore.model.dto.request.CreateLibraryRequest; import com.adityachandel.booklore.model.entity.BookEntity; import com.adityachandel.booklore.model.entity.LibraryEntity; @@ -31,7 +30,6 @@ import java.util.Collections; import java.util.List; import java.util.Set; import java.util.stream.Collectors; -import java.util.stream.Stream; @Slf4j @Service @@ -108,7 +106,7 @@ public class LibraryService { if (request.isWatch()) { for (LibraryPathEntity pathEntity : savedLibrary.getLibraryPaths()) { Path path = Paths.get(pathEntity.getPath()); - monitoringService.registerPathWithLibraryId(path, libraryId); + monitoringService.registerPath(path, libraryId); } } else { for (LibraryPathEntity pathEntity : savedLibrary.getLibraryPaths()) { @@ -152,7 +150,7 @@ public class LibraryService { if (request.isWatch()) { for (LibraryPathEntity pathEntity : libraryEntity.getLibraryPaths()) { Path path = Paths.get(pathEntity.getPath()); - monitoringService.registerPathWithLibraryId(path, libraryId); + monitoringService.registerPath(path, libraryId); } } @@ -196,10 +194,10 @@ public class LibraryService { public void deleteLibrary(long id) { LibraryEntity library = libraryRepository.findById(id).orElseThrow(() -> ApiError.LIBRARY_NOT_FOUND.createException(id)); - /*library.getLibraryPaths().forEach(libraryPath -> { + library.getLibraryPaths().forEach(libraryPath -> { Path path = Paths.get(libraryPath.getPath()); monitoringService.unregisterPath(path.toString()); - });*/ + }); Set bookIds = library.getBookEntities().stream().map(BookEntity::getId).collect(Collectors.toSet()); fileProcessingUtils.deleteBookCovers(bookIds); libraryRepository.deleteById(id); diff --git a/booklore-api/src/main/java/com/adityachandel/booklore/service/monitoring/MonitoringService.java b/booklore-api/src/main/java/com/adityachandel/booklore/service/monitoring/MonitoringService.java index 301d03a4f..cac814f50 100644 --- a/booklore-api/src/main/java/com/adityachandel/booklore/service/monitoring/MonitoringService.java +++ b/booklore-api/src/main/java/com/adityachandel/booklore/service/monitoring/MonitoringService.java @@ -45,14 +45,14 @@ public class MonitoringService { library.getPaths().forEach(libraryPath -> { Path path = Paths.get(libraryPath.getPath()); if (Files.isDirectory(path)) { - registerPathWithLibraryId(path, library.getId()); + registerPath(path, library.getId()); } }); }); log.info("Registered libraries for monitoring: {}", libraries.size()); } - public synchronized void registerPathWithLibraryId(Path path, Long libraryId) { + public synchronized void registerPath(Path path, Long libraryId) { try { if (monitoredPaths.add(path)) { path.register(watchService,