fix(reader): resolve epub preview runtime and audiobook download failures (#463)
* fix(reader): resolve epub preview runtime and audiobook download failures * fix(files): load book library path for additional file downloads * fix(files): scope additional file operations to book-owned non-primary files
This commit is contained in:
@@ -84,7 +84,7 @@ public class AdditionalFileController {
|
||||
public ResponseEntity<Resource> downloadAdditionalFile(
|
||||
@PathVariable Long bookId,
|
||||
@PathVariable Long fileId) throws IOException {
|
||||
return additionalFileService.downloadAdditionalFile(fileId);
|
||||
return additionalFileService.downloadAdditionalFile(bookId, fileId);
|
||||
}
|
||||
|
||||
@Operation(
|
||||
@@ -98,7 +98,7 @@ public class AdditionalFileController {
|
||||
public ResponseEntity<Void> deleteAdditionalFile(
|
||||
@PathVariable Long bookId,
|
||||
@PathVariable Long fileId) {
|
||||
additionalFileService.deleteAdditionalFile(fileId);
|
||||
additionalFileService.deleteAdditionalFile(bookId, fileId);
|
||||
return ResponseEntity.noContent().build();
|
||||
}
|
||||
|
||||
|
||||
@@ -28,6 +28,20 @@ public interface BookAdditionalFileRepository extends JpaRepository<BookFileEnti
|
||||
@Query("SELECT bf FROM BookFileEntity bf WHERE bf.book.library.id = :libraryId")
|
||||
List<BookFileEntity> findByLibraryId(@Param("libraryId") Long libraryId);
|
||||
|
||||
@Query("""
|
||||
SELECT DISTINCT bf FROM BookFileEntity bf
|
||||
LEFT JOIN FETCH bf.book b
|
||||
LEFT JOIN FETCH b.libraryPath
|
||||
LEFT JOIN FETCH b.library
|
||||
LEFT JOIN FETCH b.bookFiles
|
||||
WHERE bf.id = :id
|
||||
AND b.id = :bookId
|
||||
""")
|
||||
Optional<BookFileEntity> findByIdAndBookIdWithBookAndLibraryPath(
|
||||
@Param("id") Long id,
|
||||
@Param("bookId") Long bookId
|
||||
);
|
||||
|
||||
@Modifying
|
||||
@Transactional
|
||||
@Query("""
|
||||
|
||||
@@ -5,7 +5,6 @@ import org.booklore.model.dto.BookFile;
|
||||
import org.booklore.model.entity.BookEntity;
|
||||
import org.booklore.model.entity.BookFileEntity;
|
||||
import org.booklore.repository.BookAdditionalFileRepository;
|
||||
import org.booklore.repository.BookRepository;
|
||||
import org.booklore.service.monitoring.MonitoringRegistrationService;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
@@ -39,7 +38,6 @@ public class AdditionalFileService {
|
||||
private static final Pattern NON_ASCII = Pattern.compile("[^\\x00-\\x7F]");
|
||||
|
||||
private final BookAdditionalFileRepository additionalFileRepository;
|
||||
private final BookRepository bookRepository;
|
||||
private final AdditionalFileMapper additionalFileMapper;
|
||||
private final MonitoringRegistrationService monitoringRegistrationService;
|
||||
|
||||
@@ -54,14 +52,15 @@ public class AdditionalFileService {
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void deleteAdditionalFile(Long fileId) {
|
||||
Optional<BookFileEntity> fileOpt = additionalFileRepository.findById(fileId);
|
||||
public void deleteAdditionalFile(Long bookId, Long fileId) {
|
||||
Optional<BookFileEntity> fileOpt = additionalFileRepository.findByIdAndBookIdWithBookAndLibraryPath(fileId, bookId);
|
||||
if (fileOpt.isEmpty()) {
|
||||
throw new IllegalArgumentException("Additional file not found with id: " + fileId);
|
||||
}
|
||||
|
||||
BookFileEntity file = fileOpt.get();
|
||||
BookEntity book = file.getBook();
|
||||
validateAdditionalFile(file, book);
|
||||
|
||||
try {
|
||||
monitoringRegistrationService.unregisterSpecificPath(file.getFullFilePath().getParent());
|
||||
@@ -96,13 +95,14 @@ public class AdditionalFileService {
|
||||
}
|
||||
}
|
||||
|
||||
public ResponseEntity<Resource> downloadAdditionalFile(Long fileId) throws IOException {
|
||||
Optional<BookFileEntity> fileOpt = additionalFileRepository.findById(fileId);
|
||||
public ResponseEntity<Resource> downloadAdditionalFile(Long bookId, Long fileId) throws IOException {
|
||||
Optional<BookFileEntity> fileOpt = additionalFileRepository.findByIdAndBookIdWithBookAndLibraryPath(fileId, bookId);
|
||||
if (fileOpt.isEmpty()) {
|
||||
return ResponseEntity.notFound().build();
|
||||
}
|
||||
|
||||
BookFileEntity file = fileOpt.get();
|
||||
validateAdditionalFile(file, file.getBook());
|
||||
Path filePath = file.getFullFilePath();
|
||||
|
||||
if (!Files.exists(filePath)) {
|
||||
@@ -159,4 +159,10 @@ public class AdditionalFileService {
|
||||
.header(HttpHeaders.CONTENT_LENGTH, String.valueOf(zipBytes.length))
|
||||
.body(resource);
|
||||
}
|
||||
|
||||
private void validateAdditionalFile(BookFileEntity file, BookEntity book) {
|
||||
if (book != null && book.getPrimaryBookFile() != null && file.getId().equals(book.getPrimaryBookFile().getId())) {
|
||||
throw new IllegalArgumentException("Primary book file cannot be processed as an additional file: " + file.getId());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user