fix(file): allow physical books to be moved between libraries (#296)

* chore(release): 2.3.0 [skip ci]

## [2.3.0](https://github.com/grimmory-tools/grimmory/compare/v2.2.6...v2.3.0) (2026-03-21)

### Features

* **release:** document develop-based stable release previews ([930e526](https://github.com/grimmory-tools/grimmory/commit/930e5262285540b3b65b9ef4f7be328a05dfb5b4))

### Bug Fixes

* **api:** fix potential memory leaks in file processing ([031e8ae](https://github.com/grimmory-tools/grimmory/commit/031e8ae257c3354cacce2d17c8c6bc35ce80badb))
* **ci:** correct artifact download action pin ([37ca101](https://github.com/grimmory-tools/grimmory/commit/37ca101dd4bd08ccfe6d4d2395ecc71298cc323d))
* **ci:** publish PR test results from workflow_run ([11a76bf](https://github.com/grimmory-tools/grimmory/commit/11a76bffe12f80d0271e3626291cfbd275346727))
* **ci:** repair release preview and test result publishing ([afa5b81](https://github.com/grimmory-tools/grimmory/commit/afa5b818ebe612e66acc819aa398ac0c6184d21b))
* drop telemetry from app ([#52](https://github.com/grimmory-tools/grimmory/issues/52)) ([4d82cb7](https://github.com/grimmory-tools/grimmory/commit/4d82cb718833a2a4e08ee2f18b2ff3ab9b043dd6))
* **ui:** repair frontend compile after rebrand ([fea1ec6](https://github.com/grimmory-tools/grimmory/commit/fea1ec6930ae64445c6d7e7b38bdc4e7925b51c1))

### Refactors

* **build:** rename frontend dist output to grimmory ([ecf388f](https://github.com/grimmory-tools/grimmory/commit/ecf388f7a2086fbd1d8737a972926d70ec3190d4))
* **i18n:** rename booklore translation keys to grimmory ([eb94afa](https://github.com/grimmory-tools/grimmory/commit/eb94afa7ad600eaa2535802583f21937bd1ae2c0))
* **metadata:** move default parser from Amazon to Goodreads ([e252122](https://github.com/grimmory-tools/grimmory/commit/e252122bc5a89c75af85ddbaae66a14d368479f9))
* pull kepubify & ffprobe during build ([#50](https://github.com/grimmory-tools/grimmory/issues/50)) ([1c15629](https://github.com/grimmory-tools/grimmory/commit/1c15629a10ba2e2ad78455f118e8c937b585b157))
* **ui:** rebrand frontend surfaces to grimmory ([d786dd8](https://github.com/grimmory-tools/grimmory/commit/d786dd8ccbebaa360385dce834816bcb6aaf3b2f))

### Chores

* **api:** remove the custom startup banner ([98c9b1a](https://github.com/grimmory-tools/grimmory/commit/98c9b1ae653c2140b7b2ab86b5511be3376bfe43))
* **deps:** bump flatted from 3.4.1 to 3.4.2 in /booklore-ui ([#73](https://github.com/grimmory-tools/grimmory/issues/73)) ([c4bd0c7](https://github.com/grimmory-tools/grimmory/commit/c4bd0c779ae398968dbdea6d566693ed57eab3eb))
* **funding:** point support links at opencollective ([55c0ac0](https://github.com/grimmory-tools/grimmory/commit/55c0ac089966b8586e7d7bdef1fc469049903d08))
* **release:** 2.2.7 [skip ci] ([0b5e24c](https://github.com/grimmory-tools/grimmory/commit/0b5e24c23848c9e97b04f4221303d0b4bf0b2dd7))
* remove old verbose PR template, replace with temporary more low-key one. ([#84](https://github.com/grimmory-tools/grimmory/issues/84)) ([b868526](https://github.com/grimmory-tools/grimmory/commit/b8685268a9a9730416a209201a70c284cee590c5))
* **ui:** drop financial support dialog ([#21](https://github.com/grimmory-tools/grimmory/issues/21)) ([62be6b1](https://github.com/grimmory-tools/grimmory/commit/62be6b152cbd42910620fdeae1f35783ea258b27))

### Documentation

* updated supported file formats in README.md ([#68](https://github.com/grimmory-tools/grimmory/issues/68)) ([f912e80](https://github.com/grimmory-tools/grimmory/commit/f912e802f97263206308817acd5bab84a5321dcf))

### Style

* **i18n:** normalize translation json formatting ([#89](https://github.com/grimmory-tools/grimmory/issues/89)) ([857290d](https://github.com/grimmory-tools/grimmory/commit/857290d215b518e61c9b3a058ae5d40d5e214672))
* **ui:** simplify the topbar logo branding ([0416d48](https://github.com/grimmory-tools/grimmory/commit/0416d48a6c441792b4c9607b3e472159c879d439))

* fix(file): deduplicate bookFiles and protect source library root in FileMoveService

* fix(shelf): include public shelves in filterShelvesByUserId

* fix(shelf): centralize shelf visibility predicate and deduplicate book files by ID

* fix(shelf): controll for null userid

---------

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: msmouse <git@dsmouse.com>
Co-authored-by: brios <127139797+balazs-szucs@users.noreply.github.com>
This commit is contained in:
Peri Diane Jones
2026-04-22 17:27:02 -04:00
committed by Zach Yale
co-authored by github-actions[bot] msmouse brios
parent 4d2b803905
commit 4ce918891b
6 changed files with 59 additions and 23 deletions
@@ -19,6 +19,7 @@ import org.booklore.service.audit.AuditService;
import org.booklore.service.metadata.sidecar.SidecarMetadataWriter;
import org.booklore.service.monitoring.MonitoringRegistrationService;
import org.booklore.service.progress.ReadingProgressService;
import org.booklore.util.BookUtils;
import org.booklore.util.FileService;
import org.booklore.util.FileUtils;
import org.springframework.core.io.ByteArrayResource;
@@ -543,10 +544,7 @@ public class BookService {
}
public Set<Shelf> filterShelvesByUserId(Set<Shelf> shelves, Long userId) {
if (shelves == null) return Collections.emptySet();
return shelves.stream()
.filter(shelf -> userId.equals(shelf.getUserId()))
.collect(Collectors.toSet());
return BookUtils.filterShelvesByUserId(shelves, userId);
}
}
@@ -1,5 +1,6 @@
package org.booklore.service.book;
import org.booklore.util.BookUtils;
import org.booklore.config.security.service.AuthenticationService;
import org.booklore.exception.ApiError;
import org.booklore.mapper.BookMapper;
@@ -362,9 +363,6 @@ public class BookUpdateService {
}
private Set<Shelf> filterShelvesByUserId(Set<Shelf> shelves, Long userId) {
if (shelves == null) return Collections.emptySet();
return shelves.stream()
.filter(shelf -> userId.equals(shelf.getUserId()))
.collect(Collectors.toSet());
return BookUtils.filterShelvesByUserId(shelves, userId);
}
}
@@ -123,11 +123,22 @@ public class FileMoveService {
LibraryPathEntity libraryPathEntity = optionalLibraryPathEntity.get();
if (bookEntity.getBookFiles() == null || bookEntity.getBookFiles().isEmpty()) {
log.warn("Book has no files to move: bookId={}", bookId);
if (Boolean.TRUE.equals(bookEntity.getIsPhysical())) {
log.info("Moving physical book (no files): bookId={}", bookId);
bookRepository.updateLibrary(bookEntity.getId(), targetLibrary.getId(), libraryPathEntity);
entityManager.clear();
BookEntity fresh = bookRepository.findByIdWithBookFiles(bookId).orElseThrow();
notificationService.sendMessage(Topic.BOOK_UPDATE, bookMapper.toBookWithDescription(fresh, false));
} else {
log.warn("Book has no files to move: bookId={}", bookId);
}
return;
}
List<BookFileEntity> bookFiles = bookEntity.getBookFiles().stream().distinct().toList();
List<BookFileEntity> bookFiles = bookEntity.getBookFiles().stream()
.filter(f -> f.getId() != null)
.collect(Collectors.toMap(BookFileEntity::getId, f -> f, (a, b) -> a, LinkedHashMap::new))
.values().stream().toList();
Path currentPrimaryFilePath = bookEntity.getFullFilePath();
String pattern = fileMoveHelper.getFileNamingPattern(targetLibrary);
@@ -281,7 +292,10 @@ public class FileMoveService {
return FileMoveResult.builder().moved(false).build();
}
List<BookFileEntity> bookFiles = bookWithFiles.getBookFiles().stream().distinct().toList();
List<BookFileEntity> bookFiles = bookWithFiles.getBookFiles().stream()
.filter(f -> f.getId() != null)
.collect(Collectors.toMap(BookFileEntity::getId, f -> f, (a, b) -> a, LinkedHashMap::new))
.values().stream().toList();
String pattern = fileMoveHelper.getFileNamingPattern(bookWithFiles.getLibraryPath().getLibrary());
Path currentPrimaryFilePath = bookWithFiles.getFullFilePath();
@@ -12,6 +12,7 @@ import org.booklore.model.dto.request.FetchMetadataRequest;
import org.booklore.model.dto.request.MetadataRefreshOptions;
import org.booklore.model.dto.request.MetadataRefreshRequest;
import org.booklore.model.dto.settings.AppSettings;
import org.booklore.util.BookUtils;
import org.booklore.model.entity.BookEntity;
import org.booklore.model.entity.LibraryEntity;
import org.booklore.model.entity.MetadataFetchJobEntity;
@@ -847,9 +848,6 @@ public class MetadataRefreshService {
}
private Set<Shelf> filterShelvesByUserId(Set<Shelf> shelves, Long userId) {
if (shelves == null) return Collections.emptySet();
return shelves.stream()
.filter(shelf -> userId.equals(shelf.getUserId()))
.collect(Collectors.toSet());
return BookUtils.filterShelvesByUserId(shelves, userId);
}
}
@@ -1,14 +1,25 @@
package org.booklore.util;
import org.booklore.model.dto.Shelf;
import org.booklore.model.entity.AuthorEntity;
import org.booklore.model.entity.BookMetadataEntity;
import lombok.experimental.UtilityClass;
import java.util.Collections;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.regex.Pattern;
@UtilityClass
public class BookUtils {
public static Set<Shelf> filterShelvesByUserId(Set<Shelf> shelves, Long userId) {
if (shelves == null) return Collections.emptySet();
return shelves.stream()
.filter(shelf -> shelf.isPublicShelf() || userId.equals(shelf.getUserId()))
.collect(Collectors.toSet());
}
private static final Pattern WHITESPACE_PATTERN = Pattern.compile("\\s+");
private static final Pattern SPECIAL_CHARACTERS_PATTERN = Pattern.compile("[!@$%^&*_=|~`<>?/\"]");
private static final Pattern DIACRITICAL_MARKS_PATTERN = Pattern.compile("\\p{InCombiningDiacriticalMarks}+");
@@ -437,16 +437,33 @@ class BookServiceTest {
}
@Test
void filterShelvesByUserId_returnsOnlyUserShelves() {
Shelf shelf1 = Shelf.builder().id(1L).userId(1L).build();
Shelf shelf2 = Shelf.builder().id(2L).userId(2L).build();
Set<Shelf> shelves = Set.of(shelf1, shelf2);
void filterShelvesByUserId_returnsOwnShelvesAndPublicShelves() {
Shelf ownShelf = Shelf.builder().id(1L).userId(1L).publicShelf(false).build();
Shelf otherPrivateShelf = Shelf.builder().id(2L).userId(2L).publicShelf(false).build();
Shelf otherPublicShelf = Shelf.builder().id(3L).userId(2L).publicShelf(true).build();
Set<Shelf> shelves = Set.of(ownShelf, otherPrivateShelf, otherPublicShelf);
Set<Shelf> result = org.springframework.test.util.ReflectionTestUtils.invokeMethod(
bookService, "filterShelvesByUserId", shelves, 1L);
Set<Shelf> result = bookService.filterShelvesByUserId(shelves, 1L);
assertEquals(1, result.size());
assertTrue(result.contains(shelf1));
assertEquals(2, result.size());
assertTrue(result.contains(ownShelf));
assertTrue(result.contains(otherPublicShelf));
assertFalse(result.contains(otherPrivateShelf));
}
@Test
void filterShelvesByUserId_excludesPrivateShelvesFromOtherUsers() {
Shelf otherPrivateShelf = Shelf.builder().id(2L).userId(2L).publicShelf(false).build();
Set<Shelf> result = bookService.filterShelvesByUserId(Set.of(otherPrivateShelf), 1L);
assertTrue(result.isEmpty());
}
@Test
void filterShelvesByUserId_nullInput_returnsEmpty() {
Set<Shelf> result = bookService.filterShelvesByUserId(null, 1L);
assertTrue(result.isEmpty());
}
}