Fix unique file constraint to allow identical filenames in different sub-paths (#1668)

* Upgrade Spring Boot to version 4.0.0

* Fix flyway config

* Fix unique file constraint to allow identical filenames in different sub-paths
This commit is contained in:
Aditya Chandel
2025-11-28 14:10:51 -07:00
committed by GitHub
parent d647fe401e
commit 3a9cb965e3
7 changed files with 54 additions and 12 deletions
@@ -19,8 +19,6 @@ import java.util.Set;
public interface BookRepository extends JpaRepository<BookEntity, Long>, JpaSpecificationExecutor<BookEntity> {
Optional<BookEntity> findBookByIdAndLibraryId(long id, long libraryId);
Optional<BookEntity> findBookByFileNameAndLibraryId(String fileName, long libraryId);
Optional<BookEntity> findByCurrentHash(String currentHash);
@Query("SELECT b.id FROM BookEntity b WHERE b.library.id = :libraryId AND (b.deleted IS NULL OR b.deleted = false)")
@@ -126,4 +124,19 @@ public interface BookRepository extends JpaRepository<BookEntity, Long>, JpaSpec
@Param("fileName") String fileName,
@Param("libraryId") Long libraryId,
@Param("libraryPath") LibraryPathEntity libraryPath);
@Query(value = """
SELECT *
FROM book
WHERE library_id = :libraryId
AND library_path_id = :libraryPathId
AND file_sub_path = :fileSubPath
AND file_name = :fileName
LIMIT 1
""", nativeQuery = true)
Optional<BookEntity> findByLibraryIdAndLibraryPathIdAndFileSubPathAndFileName(
@Param("libraryId") Long libraryId,
@Param("libraryPathId") Long libraryPathId,
@Param("fileSubPath") String fileSubPath,
@Param("fileName") String fileName);
}
@@ -63,6 +63,10 @@ public class VersionService {
}
public List<ReleaseNote> fetchReleaseNotesSince(String currentVersion) {
if ("development".equals(currentVersion)) {
log.warn("Skipping fetch of release notes because current version is '{}', which is a local development build.", currentVersion);
return new ArrayList<>();
}
log.info("Fetching release notes since version: {}", currentVersion);
List<ReleaseNote> updates = new ArrayList<>();
@@ -4,9 +4,11 @@ import com.adityachandel.booklore.model.dto.settings.LibraryFile;
import com.adityachandel.booklore.model.entity.*;
import com.adityachandel.booklore.model.enums.BookFileType;
import com.adityachandel.booklore.repository.*;
import com.adityachandel.booklore.service.file.FileFingerprint;
import com.adityachandel.booklore.util.FileUtils;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.stereotype.Service;
import java.time.Instant;
@@ -23,8 +25,26 @@ public class BookCreatorService {
private final BookMetadataRepository bookMetadataRepository;
public BookEntity createShellBook(LibraryFile libraryFile, BookFileType bookFileType) {
Optional<BookEntity> existingBookOpt = bookRepository.findByLibraryIdAndLibraryPathIdAndFileSubPathAndFileName(
libraryFile.getLibraryEntity().getId(),
libraryFile.getLibraryPathEntity().getId(),
libraryFile.getFileSubPath(),
libraryFile.getFileName());
if (existingBookOpt.isPresent()) {
log.warn("Book already exists for file: {}", libraryFile.getFileName());
String newHash = FileFingerprint.generateHash(libraryFile.getFullPath());
long fileSizeKb = FileUtils.getFileSizeInKb(libraryFile.getFullPath());
BookEntity existingBook = existingBookOpt.get();
existingBook.setCurrentHash(newHash);
existingBook.setInitialHash(newHash);
existingBook.setDeleted(false);
existingBook.setFileSizeKb(fileSizeKb);
return existingBook;
}
long fileSizeKb = FileUtils.getFileSizeInKb(libraryFile.getFullPath());
BookMetadataEntity metadata = BookMetadataEntity.builder().build();
BookEntity bookEntity = BookEntity.builder()
.library(libraryFile.getLibraryEntity())
.libraryPath(libraryFile.getLibraryPathEntity())
@@ -33,8 +53,13 @@ public class BookCreatorService {
.bookType(bookFileType)
.fileSizeKb(fileSizeKb)
.addedOn(Instant.now())
.metadata(metadata)
.build();
BookMetadataEntity metadata = BookMetadataEntity.builder()
.book(bookEntity)
.build();
bookEntity.setMetadata(metadata);
return bookRepository.saveAndFlush(bookEntity);
}
@@ -5,7 +5,6 @@ import com.adityachandel.booklore.model.dto.settings.LibraryFile;
import com.adityachandel.booklore.model.entity.LibraryEntity;
import com.adityachandel.booklore.model.enums.BookFileType;
import com.adityachandel.booklore.model.enums.LibraryScanMode;
import com.adityachandel.booklore.service.NotificationService;
import com.adityachandel.booklore.service.event.BookEventBroadcaster;
import com.adityachandel.booklore.service.fileprocessor.BookFileProcessor;
import com.adityachandel.booklore.service.fileprocessor.BookFileProcessorRegistry;
@@ -23,7 +22,6 @@ public class FileAsBookProcessor implements LibraryFileProcessor {
private final BookEventBroadcaster bookEventBroadcaster;
private final BookFileProcessorRegistry processorRegistry;
private final NotificationService notificationService;
@Override
public LibraryScanMode getScanMode() {
@@ -40,7 +38,7 @@ public class FileAsBookProcessor implements LibraryFileProcessor {
if (result != null) {
bookEventBroadcaster.broadcastBookAddEvent(result.getBook());
log.info("Processed file: {}", libraryFile.getFileName());
log.debug("Processed file: {}", libraryFile.getFileName());
}
}
@@ -1,7 +1,7 @@
app:
path-config: '/app/data'
bookdrop-folder: '/bookdrop'
version: 'v0.0.40'
version: 'development'
swagger:
enabled: ${SWAGGER_ENABLED:false}
remote-auth:
@@ -0,0 +1,6 @@
ALTER TABLE book
DROP INDEX IF EXISTS unique_file_library;
ALTER TABLE book
ADD CONSTRAINT unique_library_file_path UNIQUE (file_name, library_id, library_path_id, file_sub_path);
@@ -45,10 +45,6 @@ export class LibraryShelfMenuService {
header: 'Edit Library',
modal: true,
closable: true,
style: {
position: 'absolute',
top: '15%',
},
data: {
mode: 'edit',
libraryId: entity?.id