feat(library): add validation for library path accessibility during rescan (#2347)
* feat(library): add validation for library path accessibility during rescan Signed-off-by: Balázs Szücs <bszucs1209@gmail.com> * feat(BookMapper): add mappings for primary file attributes in BookMapper Signed-off-by: Balázs Szücs <bszucs1209@gmail.com> --------- Signed-off-by: Balázs Szücs <bszucs1209@gmail.com>
This commit is contained in:
@@ -59,7 +59,8 @@ public enum ApiError {
|
||||
TASK_ALREADY_RUNNING(HttpStatus.CONFLICT, "Task is already running: %s"),
|
||||
ICON_ALREADY_EXISTS(HttpStatus.CONFLICT, "SVG icon with name '%s' already exists"),
|
||||
DEMO_USER_PASSWORD_CHANGE_NOT_ALLOWED(HttpStatus.FORBIDDEN, "Demo user password change not allowed."),
|
||||
PERMISSION_DENIED(HttpStatus.FORBIDDEN, "Permission denied: %s");
|
||||
PERMISSION_DENIED(HttpStatus.FORBIDDEN, "Permission denied: %s"),
|
||||
LIBRARY_PATH_NOT_ACCESSIBLE(HttpStatus.SERVICE_UNAVAILABLE, "Library scan aborted: path not accessible or empty: %s");
|
||||
|
||||
private final HttpStatus status;
|
||||
private final String message;
|
||||
|
||||
@@ -24,6 +24,10 @@ public interface BookMapper {
|
||||
@Mapping(source = "metadata", target = "metadata")
|
||||
@Mapping(source = "shelves", target = "shelves")
|
||||
@Mapping(source = "bookFiles", target = "bookType", qualifiedByName = "mapPrimaryBookType")
|
||||
@Mapping(source = "bookFiles", target = "fileName", qualifiedByName = "mapPrimaryFileName")
|
||||
@Mapping(source = "bookFiles", target = "filePath", qualifiedByName = "mapPrimaryFilePath")
|
||||
@Mapping(source = "bookFiles", target = "fileSubPath", qualifiedByName = "mapPrimaryFileSubPath")
|
||||
@Mapping(source = "bookFiles", target = "fileSizeKb", qualifiedByName = "mapPrimaryFileSizeKb")
|
||||
@Mapping(source = "bookFiles", target = "alternativeFormats", qualifiedByName = "mapAlternativeFormats")
|
||||
@Mapping(source = "bookFiles", target = "supplementaryFiles", qualifiedByName = "mapSupplementaryFiles")
|
||||
Book toBook(BookEntity bookEntity);
|
||||
@@ -34,6 +38,10 @@ public interface BookMapper {
|
||||
@Mapping(source = "metadata", target = "metadata")
|
||||
@Mapping(source = "shelves", target = "shelves")
|
||||
@Mapping(source = "bookFiles", target = "bookType", qualifiedByName = "mapPrimaryBookType")
|
||||
@Mapping(source = "bookFiles", target = "fileName", qualifiedByName = "mapPrimaryFileName")
|
||||
@Mapping(source = "bookFiles", target = "filePath", qualifiedByName = "mapPrimaryFilePath")
|
||||
@Mapping(source = "bookFiles", target = "fileSubPath", qualifiedByName = "mapPrimaryFileSubPath")
|
||||
@Mapping(source = "bookFiles", target = "fileSizeKb", qualifiedByName = "mapPrimaryFileSizeKb")
|
||||
@Mapping(source = "bookFiles", target = "alternativeFormats", qualifiedByName = "mapAlternativeFormats")
|
||||
@Mapping(source = "bookFiles", target = "supplementaryFiles", qualifiedByName = "mapSupplementaryFiles")
|
||||
Book toBookWithDescription(BookEntity bookEntity, @Context boolean includeDescription);
|
||||
@@ -80,6 +88,30 @@ public interface BookMapper {
|
||||
return primary == null ? null : primary.getBookType();
|
||||
}
|
||||
|
||||
@Named("mapPrimaryFileName")
|
||||
default String mapPrimaryFileName(List<BookFileEntity> bookFiles) {
|
||||
BookFileEntity primary = getPrimaryBookFile(bookFiles);
|
||||
return primary == null ? null : primary.getFileName();
|
||||
}
|
||||
|
||||
@Named("mapPrimaryFilePath")
|
||||
default String mapPrimaryFilePath(List<BookFileEntity> bookFiles) {
|
||||
BookFileEntity primary = getPrimaryBookFile(bookFiles);
|
||||
return primary == null ? null : primary.getFullFilePath().toString();
|
||||
}
|
||||
|
||||
@Named("mapPrimaryFileSubPath")
|
||||
default String mapPrimaryFileSubPath(List<BookFileEntity> bookFiles) {
|
||||
BookFileEntity primary = getPrimaryBookFile(bookFiles);
|
||||
return primary == null ? null : primary.getFileSubPath();
|
||||
}
|
||||
|
||||
@Named("mapPrimaryFileSizeKb")
|
||||
default Long mapPrimaryFileSizeKb(List<BookFileEntity> bookFiles) {
|
||||
BookFileEntity primary = getPrimaryBookFile(bookFiles);
|
||||
return primary == null ? null : primary.getFileSizeKb();
|
||||
}
|
||||
|
||||
@Named("mapAlternativeFormats")
|
||||
default List<BookFile> mapAlternativeFormats(List<BookFileEntity> bookFiles) {
|
||||
if (bookFiles == null) return null;
|
||||
|
||||
+25
@@ -20,6 +20,7 @@ import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.UncheckedIOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
@@ -60,7 +61,21 @@ public class LibraryProcessingService {
|
||||
LibraryEntity libraryEntity = libraryRepository.findById(context.getLibraryId()).orElseThrow(() -> ApiError.LIBRARY_NOT_FOUND.createException(context.getLibraryId()));
|
||||
notificationService.sendMessage(Topic.LOG, LogNotification.info("Started refreshing library: " + libraryEntity.getName()));
|
||||
LibraryFileProcessor processor = fileProcessorRegistry.getProcessor(libraryEntity);
|
||||
|
||||
validateLibraryPathsAccessible(libraryEntity);
|
||||
|
||||
List<LibraryFile> libraryFiles = libraryFileHelper.getLibraryFiles(libraryEntity, processor);
|
||||
|
||||
int existingBookCount = libraryEntity.getBookEntities().size();
|
||||
if (existingBookCount > 0 && libraryFiles.isEmpty()) {
|
||||
String paths = libraryEntity.getLibraryPaths().stream()
|
||||
.map(p -> p.getPath())
|
||||
.collect(Collectors.joining(", "));
|
||||
log.error("Library '{}' has {} existing books but scan found 0 files. Paths may be offline: {}",
|
||||
libraryEntity.getName(), existingBookCount, paths);
|
||||
throw ApiError.LIBRARY_PATH_NOT_ACCESSIBLE.createException(paths);
|
||||
}
|
||||
|
||||
List<Long> additionalFileIds = detectDeletedAdditionalFiles(libraryFiles, libraryEntity, processor);
|
||||
if (!additionalFileIds.isEmpty()) {
|
||||
log.info("Detected {} removed additional files in library: {}", additionalFileIds.size(), libraryEntity.getName());
|
||||
@@ -83,6 +98,16 @@ public class LibraryProcessingService {
|
||||
processor.processLibraryFiles(libraryFiles, libraryEntity);
|
||||
}
|
||||
|
||||
private void validateLibraryPathsAccessible(LibraryEntity libraryEntity) {
|
||||
for (var pathEntity : libraryEntity.getLibraryPaths()) {
|
||||
Path path = Path.of(pathEntity.getPath());
|
||||
if (!Files.exists(path) || !Files.isDirectory(path) || !Files.isReadable(path)) {
|
||||
log.error("Library path not accessible: {}", path);
|
||||
throw ApiError.LIBRARY_PATH_NOT_ACCESSIBLE.createException(path.toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected static List<Long> detectDeletedBookIds(List<LibraryFile> libraryFiles, LibraryEntity libraryEntity) {
|
||||
Set<Path> currentFullPaths = libraryFiles.stream()
|
||||
.map(LibraryFile::getFullPath)
|
||||
|
||||
+152
-2
@@ -1,5 +1,6 @@
|
||||
package com.adityachandel.booklore.service.library;
|
||||
|
||||
import com.adityachandel.booklore.exception.APIException;
|
||||
import com.adityachandel.booklore.model.dto.settings.LibraryFile;
|
||||
import com.adityachandel.booklore.model.entity.BookEntity;
|
||||
import com.adityachandel.booklore.model.entity.BookFileEntity;
|
||||
@@ -14,16 +15,20 @@ import jakarta.persistence.EntityManager;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
@@ -281,8 +286,10 @@ class LibraryProcessingServiceTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
void rescanLibrary_shouldNotDeleteNonBookFiles_whenProcessorDoesNotSupportSupplementaryFiles() throws IOException {
|
||||
void rescanLibrary_shouldNotDeleteNonBookFiles_whenProcessorDoesNotSupportSupplementaryFiles(@TempDir Path tempDir) throws IOException {
|
||||
long libraryId = 1L;
|
||||
Path accessiblePath = tempDir.resolve("library");
|
||||
Files.createDirectory(accessiblePath);
|
||||
|
||||
LibraryEntity libraryEntity = new LibraryEntity();
|
||||
libraryEntity.setId(libraryId);
|
||||
@@ -291,7 +298,7 @@ class LibraryProcessingServiceTest {
|
||||
|
||||
LibraryPathEntity pathEntity = new LibraryPathEntity();
|
||||
pathEntity.setId(10L);
|
||||
pathEntity.setPath("/library");
|
||||
pathEntity.setPath(accessiblePath.toString());
|
||||
libraryEntity.setLibraryPaths(List.of(pathEntity));
|
||||
|
||||
BookEntity book = new BookEntity();
|
||||
@@ -348,4 +355,147 @@ class LibraryProcessingServiceTest {
|
||||
|
||||
verify(bookDeletionService, never()).deleteRemovedAdditionalFiles(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void rescanLibrary_shouldAbortWhenPathNotAccessible(@TempDir Path tempDir) throws IOException {
|
||||
long libraryId = 1L;
|
||||
Path nonExistentPath = tempDir.resolve("non_existent_path");
|
||||
|
||||
LibraryEntity libraryEntity = new LibraryEntity();
|
||||
libraryEntity.setId(libraryId);
|
||||
libraryEntity.setName("Test Library");
|
||||
libraryEntity.setScanMode(LibraryScanMode.FILE_AS_BOOK);
|
||||
|
||||
LibraryPathEntity pathEntity = new LibraryPathEntity();
|
||||
pathEntity.setId(10L);
|
||||
pathEntity.setPath(nonExistentPath.toString());
|
||||
libraryEntity.setLibraryPaths(List.of(pathEntity));
|
||||
libraryEntity.setBookEntities(Collections.emptyList());
|
||||
|
||||
when(libraryRepository.findById(libraryId)).thenReturn(Optional.of(libraryEntity));
|
||||
when(fileProcessorRegistry.getProcessor(libraryEntity)).thenReturn(libraryFileProcessor);
|
||||
|
||||
RescanLibraryContext context = RescanLibraryContext.builder().libraryId(libraryId).build();
|
||||
|
||||
assertThatThrownBy(() -> libraryProcessingService.rescanLibrary(context))
|
||||
.isInstanceOf(APIException.class)
|
||||
.hasMessageContaining("not accessible");
|
||||
|
||||
verify(bookDeletionService, never()).processDeletedLibraryFiles(any(), any());
|
||||
verify(bookDeletionService, never()).deleteRemovedAdditionalFiles(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void rescanLibrary_shouldAbortWhenLibraryHasBooksButScanReturnsEmpty(@TempDir Path tempDir) throws IOException {
|
||||
long libraryId = 1L;
|
||||
Path accessiblePath = tempDir.resolve("accessible");
|
||||
Files.createDirectory(accessiblePath);
|
||||
|
||||
LibraryEntity libraryEntity = new LibraryEntity();
|
||||
libraryEntity.setId(libraryId);
|
||||
libraryEntity.setName("Test Library");
|
||||
libraryEntity.setScanMode(LibraryScanMode.FILE_AS_BOOK);
|
||||
|
||||
LibraryPathEntity pathEntity = new LibraryPathEntity();
|
||||
pathEntity.setId(10L);
|
||||
pathEntity.setPath(accessiblePath.toString());
|
||||
libraryEntity.setLibraryPaths(List.of(pathEntity));
|
||||
|
||||
BookEntity existingBook = new BookEntity();
|
||||
existingBook.setId(1L);
|
||||
existingBook.setLibraryPath(pathEntity);
|
||||
BookFileEntity existingBookFile = new BookFileEntity();
|
||||
existingBookFile.setBook(existingBook);
|
||||
existingBook.setBookFiles(List.of(existingBookFile));
|
||||
existingBook.getPrimaryBookFile().setFileSubPath("");
|
||||
existingBook.getPrimaryBookFile().setFileName("book1.epub");
|
||||
libraryEntity.setBookEntities(List.of(existingBook));
|
||||
|
||||
when(libraryRepository.findById(libraryId)).thenReturn(Optional.of(libraryEntity));
|
||||
when(fileProcessorRegistry.getProcessor(libraryEntity)).thenReturn(libraryFileProcessor);
|
||||
when(libraryFileHelper.getLibraryFiles(libraryEntity, libraryFileProcessor)).thenReturn(Collections.emptyList());
|
||||
|
||||
RescanLibraryContext context = RescanLibraryContext.builder().libraryId(libraryId).build();
|
||||
|
||||
assertThatThrownBy(() -> libraryProcessingService.rescanLibrary(context))
|
||||
.isInstanceOf(APIException.class)
|
||||
.hasMessageContaining("not accessible");
|
||||
|
||||
verify(bookDeletionService, never()).processDeletedLibraryFiles(any(), any());
|
||||
verify(bookDeletionService, never()).deleteRemovedAdditionalFiles(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void rescanLibrary_shouldProceedWhenLibraryHasBooksAndScanFindsThem(@TempDir Path tempDir) throws IOException {
|
||||
long libraryId = 1L;
|
||||
Path accessiblePath = tempDir.resolve("accessible");
|
||||
Files.createDirectory(accessiblePath);
|
||||
|
||||
LibraryEntity libraryEntity = new LibraryEntity();
|
||||
libraryEntity.setId(libraryId);
|
||||
libraryEntity.setName("Test Library");
|
||||
libraryEntity.setScanMode(LibraryScanMode.FILE_AS_BOOK);
|
||||
|
||||
LibraryPathEntity pathEntity = new LibraryPathEntity();
|
||||
pathEntity.setId(10L);
|
||||
pathEntity.setPath(accessiblePath.toString());
|
||||
libraryEntity.setLibraryPaths(List.of(pathEntity));
|
||||
|
||||
BookEntity existingBook = new BookEntity();
|
||||
existingBook.setId(1L);
|
||||
existingBook.setLibraryPath(pathEntity);
|
||||
BookFileEntity existingBookFile = new BookFileEntity();
|
||||
existingBookFile.setBook(existingBook);
|
||||
existingBook.setBookFiles(List.of(existingBookFile));
|
||||
existingBook.getPrimaryBookFile().setFileSubPath("");
|
||||
existingBook.getPrimaryBookFile().setFileName("book1.epub");
|
||||
libraryEntity.setBookEntities(List.of(existingBook));
|
||||
|
||||
LibraryFile fileOnDisk = LibraryFile.builder()
|
||||
.libraryEntity(libraryEntity)
|
||||
.libraryPathEntity(pathEntity)
|
||||
.fileSubPath("")
|
||||
.fileName("book1.epub")
|
||||
.build();
|
||||
|
||||
when(libraryRepository.findById(libraryId)).thenReturn(Optional.of(libraryEntity));
|
||||
when(fileProcessorRegistry.getProcessor(libraryEntity)).thenReturn(libraryFileProcessor);
|
||||
when(libraryFileHelper.getLibraryFiles(libraryEntity, libraryFileProcessor)).thenReturn(List.of(fileOnDisk));
|
||||
when(bookAdditionalFileRepository.findByLibraryId(libraryId)).thenReturn(Collections.emptyList());
|
||||
|
||||
RescanLibraryContext context = RescanLibraryContext.builder().libraryId(libraryId).build();
|
||||
|
||||
libraryProcessingService.rescanLibrary(context);
|
||||
|
||||
verify(bookDeletionService, never()).processDeletedLibraryFiles(any(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void rescanLibrary_shouldProceedForEmptyLibraryWithNoFilesFound(@TempDir Path tempDir) throws IOException {
|
||||
long libraryId = 1L;
|
||||
Path accessiblePath = tempDir.resolve("accessible");
|
||||
Files.createDirectory(accessiblePath);
|
||||
|
||||
LibraryEntity libraryEntity = new LibraryEntity();
|
||||
libraryEntity.setId(libraryId);
|
||||
libraryEntity.setName("Test Library");
|
||||
libraryEntity.setScanMode(LibraryScanMode.FILE_AS_BOOK);
|
||||
|
||||
LibraryPathEntity pathEntity = new LibraryPathEntity();
|
||||
pathEntity.setId(10L);
|
||||
pathEntity.setPath(accessiblePath.toString());
|
||||
libraryEntity.setLibraryPaths(List.of(pathEntity));
|
||||
libraryEntity.setBookEntities(Collections.emptyList());
|
||||
|
||||
when(libraryRepository.findById(libraryId)).thenReturn(Optional.of(libraryEntity));
|
||||
when(fileProcessorRegistry.getProcessor(libraryEntity)).thenReturn(libraryFileProcessor);
|
||||
when(libraryFileHelper.getLibraryFiles(libraryEntity, libraryFileProcessor)).thenReturn(Collections.emptyList());
|
||||
when(bookAdditionalFileRepository.findByLibraryId(libraryId)).thenReturn(Collections.emptyList());
|
||||
|
||||
RescanLibraryContext context = RescanLibraryContext.builder().libraryId(libraryId).build();
|
||||
|
||||
libraryProcessingService.rescanLibrary(context);
|
||||
|
||||
verify(bookDeletionService, never()).processDeletedLibraryFiles(any(), any());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user