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)
|
||||
|
||||
Reference in New Issue
Block a user