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:
+15
-2
@@ -19,8 +19,6 @@ import java.util.Set;
|
|||||||
public interface BookRepository extends JpaRepository<BookEntity, Long>, JpaSpecificationExecutor<BookEntity> {
|
public interface BookRepository extends JpaRepository<BookEntity, Long>, JpaSpecificationExecutor<BookEntity> {
|
||||||
Optional<BookEntity> findBookByIdAndLibraryId(long id, long libraryId);
|
Optional<BookEntity> findBookByIdAndLibraryId(long id, long libraryId);
|
||||||
|
|
||||||
Optional<BookEntity> findBookByFileNameAndLibraryId(String fileName, long libraryId);
|
|
||||||
|
|
||||||
Optional<BookEntity> findByCurrentHash(String currentHash);
|
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)")
|
@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("fileName") String fileName,
|
||||||
@Param("libraryId") Long libraryId,
|
@Param("libraryId") Long libraryId,
|
||||||
@Param("libraryPath") LibraryPathEntity libraryPath);
|
@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) {
|
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);
|
log.info("Fetching release notes since version: {}", currentVersion);
|
||||||
|
|
||||||
List<ReleaseNote> updates = new ArrayList<>();
|
List<ReleaseNote> updates = new ArrayList<>();
|
||||||
|
|||||||
+27
-2
@@ -4,9 +4,11 @@ import com.adityachandel.booklore.model.dto.settings.LibraryFile;
|
|||||||
import com.adityachandel.booklore.model.entity.*;
|
import com.adityachandel.booklore.model.entity.*;
|
||||||
import com.adityachandel.booklore.model.enums.BookFileType;
|
import com.adityachandel.booklore.model.enums.BookFileType;
|
||||||
import com.adityachandel.booklore.repository.*;
|
import com.adityachandel.booklore.repository.*;
|
||||||
|
import com.adityachandel.booklore.service.file.FileFingerprint;
|
||||||
import com.adityachandel.booklore.util.FileUtils;
|
import com.adityachandel.booklore.util.FileUtils;
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.dao.DataIntegrityViolationException;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
@@ -23,8 +25,26 @@ public class BookCreatorService {
|
|||||||
private final BookMetadataRepository bookMetadataRepository;
|
private final BookMetadataRepository bookMetadataRepository;
|
||||||
|
|
||||||
public BookEntity createShellBook(LibraryFile libraryFile, BookFileType bookFileType) {
|
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());
|
long fileSizeKb = FileUtils.getFileSizeInKb(libraryFile.getFullPath());
|
||||||
BookMetadataEntity metadata = BookMetadataEntity.builder().build();
|
|
||||||
BookEntity bookEntity = BookEntity.builder()
|
BookEntity bookEntity = BookEntity.builder()
|
||||||
.library(libraryFile.getLibraryEntity())
|
.library(libraryFile.getLibraryEntity())
|
||||||
.libraryPath(libraryFile.getLibraryPathEntity())
|
.libraryPath(libraryFile.getLibraryPathEntity())
|
||||||
@@ -33,8 +53,13 @@ public class BookCreatorService {
|
|||||||
.bookType(bookFileType)
|
.bookType(bookFileType)
|
||||||
.fileSizeKb(fileSizeKb)
|
.fileSizeKb(fileSizeKb)
|
||||||
.addedOn(Instant.now())
|
.addedOn(Instant.now())
|
||||||
.metadata(metadata)
|
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
|
BookMetadataEntity metadata = BookMetadataEntity.builder()
|
||||||
|
.book(bookEntity)
|
||||||
|
.build();
|
||||||
|
bookEntity.setMetadata(metadata);
|
||||||
|
|
||||||
return bookRepository.saveAndFlush(bookEntity);
|
return bookRepository.saveAndFlush(bookEntity);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+1
-3
@@ -5,7 +5,6 @@ import com.adityachandel.booklore.model.dto.settings.LibraryFile;
|
|||||||
import com.adityachandel.booklore.model.entity.LibraryEntity;
|
import com.adityachandel.booklore.model.entity.LibraryEntity;
|
||||||
import com.adityachandel.booklore.model.enums.BookFileType;
|
import com.adityachandel.booklore.model.enums.BookFileType;
|
||||||
import com.adityachandel.booklore.model.enums.LibraryScanMode;
|
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.event.BookEventBroadcaster;
|
||||||
import com.adityachandel.booklore.service.fileprocessor.BookFileProcessor;
|
import com.adityachandel.booklore.service.fileprocessor.BookFileProcessor;
|
||||||
import com.adityachandel.booklore.service.fileprocessor.BookFileProcessorRegistry;
|
import com.adityachandel.booklore.service.fileprocessor.BookFileProcessorRegistry;
|
||||||
@@ -23,7 +22,6 @@ public class FileAsBookProcessor implements LibraryFileProcessor {
|
|||||||
|
|
||||||
private final BookEventBroadcaster bookEventBroadcaster;
|
private final BookEventBroadcaster bookEventBroadcaster;
|
||||||
private final BookFileProcessorRegistry processorRegistry;
|
private final BookFileProcessorRegistry processorRegistry;
|
||||||
private final NotificationService notificationService;
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public LibraryScanMode getScanMode() {
|
public LibraryScanMode getScanMode() {
|
||||||
@@ -40,7 +38,7 @@ public class FileAsBookProcessor implements LibraryFileProcessor {
|
|||||||
|
|
||||||
if (result != null) {
|
if (result != null) {
|
||||||
bookEventBroadcaster.broadcastBookAddEvent(result.getBook());
|
bookEventBroadcaster.broadcastBookAddEvent(result.getBook());
|
||||||
log.info("Processed file: {}", libraryFile.getFileName());
|
log.debug("Processed file: {}", libraryFile.getFileName());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
app:
|
app:
|
||||||
path-config: '/app/data'
|
path-config: '/app/data'
|
||||||
bookdrop-folder: '/bookdrop'
|
bookdrop-folder: '/bookdrop'
|
||||||
version: 'v0.0.40'
|
version: 'development'
|
||||||
swagger:
|
swagger:
|
||||||
enabled: ${SWAGGER_ENABLED:false}
|
enabled: ${SWAGGER_ENABLED:false}
|
||||||
remote-auth:
|
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',
|
header: 'Edit Library',
|
||||||
modal: true,
|
modal: true,
|
||||||
closable: true,
|
closable: true,
|
||||||
style: {
|
|
||||||
position: 'absolute',
|
|
||||||
top: '15%',
|
|
||||||
},
|
|
||||||
data: {
|
data: {
|
||||||
mode: 'edit',
|
mode: 'edit',
|
||||||
libraryId: entity?.id
|
libraryId: entity?.id
|
||||||
|
|||||||
Reference in New Issue
Block a user