Unregister paths on library delete
This commit is contained in:
@@ -6,7 +6,6 @@ import com.adityachandel.booklore.mapper.LibraryMapper;
|
|||||||
import com.adityachandel.booklore.model.dto.Book;
|
import com.adityachandel.booklore.model.dto.Book;
|
||||||
import com.adityachandel.booklore.model.dto.Library;
|
import com.adityachandel.booklore.model.dto.Library;
|
||||||
import com.adityachandel.booklore.model.dto.LibraryPath;
|
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.dto.request.CreateLibraryRequest;
|
||||||
import com.adityachandel.booklore.model.entity.BookEntity;
|
import com.adityachandel.booklore.model.entity.BookEntity;
|
||||||
import com.adityachandel.booklore.model.entity.LibraryEntity;
|
import com.adityachandel.booklore.model.entity.LibraryEntity;
|
||||||
@@ -31,7 +30,6 @@ import java.util.Collections;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
import java.util.stream.Stream;
|
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@Service
|
@Service
|
||||||
@@ -108,7 +106,7 @@ public class LibraryService {
|
|||||||
if (request.isWatch()) {
|
if (request.isWatch()) {
|
||||||
for (LibraryPathEntity pathEntity : savedLibrary.getLibraryPaths()) {
|
for (LibraryPathEntity pathEntity : savedLibrary.getLibraryPaths()) {
|
||||||
Path path = Paths.get(pathEntity.getPath());
|
Path path = Paths.get(pathEntity.getPath());
|
||||||
monitoringService.registerPathWithLibraryId(path, libraryId);
|
monitoringService.registerPath(path, libraryId);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
for (LibraryPathEntity pathEntity : savedLibrary.getLibraryPaths()) {
|
for (LibraryPathEntity pathEntity : savedLibrary.getLibraryPaths()) {
|
||||||
@@ -152,7 +150,7 @@ public class LibraryService {
|
|||||||
if (request.isWatch()) {
|
if (request.isWatch()) {
|
||||||
for (LibraryPathEntity pathEntity : libraryEntity.getLibraryPaths()) {
|
for (LibraryPathEntity pathEntity : libraryEntity.getLibraryPaths()) {
|
||||||
Path path = Paths.get(pathEntity.getPath());
|
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) {
|
public void deleteLibrary(long id) {
|
||||||
LibraryEntity library = libraryRepository.findById(id).orElseThrow(() -> ApiError.LIBRARY_NOT_FOUND.createException(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());
|
Path path = Paths.get(libraryPath.getPath());
|
||||||
monitoringService.unregisterPath(path.toString());
|
monitoringService.unregisterPath(path.toString());
|
||||||
});*/
|
});
|
||||||
Set<Long> bookIds = library.getBookEntities().stream().map(BookEntity::getId).collect(Collectors.toSet());
|
Set<Long> bookIds = library.getBookEntities().stream().map(BookEntity::getId).collect(Collectors.toSet());
|
||||||
fileProcessingUtils.deleteBookCovers(bookIds);
|
fileProcessingUtils.deleteBookCovers(bookIds);
|
||||||
libraryRepository.deleteById(id);
|
libraryRepository.deleteById(id);
|
||||||
|
|||||||
+2
-2
@@ -45,14 +45,14 @@ public class MonitoringService {
|
|||||||
library.getPaths().forEach(libraryPath -> {
|
library.getPaths().forEach(libraryPath -> {
|
||||||
Path path = Paths.get(libraryPath.getPath());
|
Path path = Paths.get(libraryPath.getPath());
|
||||||
if (Files.isDirectory(path)) {
|
if (Files.isDirectory(path)) {
|
||||||
registerPathWithLibraryId(path, library.getId());
|
registerPath(path, library.getId());
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
log.info("Registered libraries for monitoring: {}", libraries.size());
|
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 {
|
try {
|
||||||
if (monitoredPaths.add(path)) {
|
if (monitoredPaths.add(path)) {
|
||||||
path.register(watchService,
|
path.register(watchService,
|
||||||
|
|||||||
Reference in New Issue
Block a user