Handle move better

This commit is contained in:
Tiberiu Ichim 2026-02-06 14:19:13 +02:00
parent 4cfafeb174
commit d358b82b17
2 changed files with 91 additions and 3 deletions

View file

@ -81,9 +81,13 @@ POST /api/items/:id/move
11. Update `audioFiles` paths in Book model (for playback to work)
12. Update `ebookFile` path in Book model (if present)
13. Update `podcastEpisodes` audio file paths for Podcasts
14. Emit socket events: `item_removed` (old library), `item_added` (new library)
15. Reset filter data for both libraries
16. On error: rollback file move if possible
14. Handle Series and Authors:
- Moves/merges series and authors to target library
- Copies metadata (description, ASIN) and images if necessary
- Deletes source series/authors if they become empty
15. Emit socket events: `item_removed` (old library), `item_added` (new library)
16. Reset filter data for both libraries
17. On error: rollback file move if possible
### Frontend Flow

View file

@ -1281,6 +1281,90 @@ class LibraryItemController {
}
}
// Handle Series and Authors when moving a book
if (req.libraryItem.isBook) {
// Handle Series
const bookSeries = await Database.bookSeriesModel.findAll({
where: { bookId: req.libraryItem.media.id }
})
for (const bs of bookSeries) {
const sourceSeries = await Database.seriesModel.findByPk(bs.seriesId)
if (sourceSeries) {
const targetSeries = await Database.seriesModel.findOrCreateByNameAndLibrary(sourceSeries.name, targetLibrary.id)
// If target series doesn't have a description but source does, copy it
if (!targetSeries.description && sourceSeries.description) {
targetSeries.description = sourceSeries.description
await targetSeries.save()
}
// Update link
bs.seriesId = targetSeries.id
await bs.save()
// Check if source series is now empty
const sourceSeriesBooksCount = await Database.bookSeriesModel.count({ where: { seriesId: sourceSeries.id } })
if (sourceSeriesBooksCount === 0) {
Logger.info(`[LibraryItemController] Source series "${sourceSeries.name}" in library ${oldLibraryId} is now empty. Deleting.`)
await sourceSeries.destroy()
Database.removeSeriesFromFilterData(oldLibraryId, sourceSeries.id)
}
}
}
// Handle Authors
const bookAuthors = await Database.bookAuthorModel.findAll({
where: { bookId: req.libraryItem.media.id }
})
for (const ba of bookAuthors) {
const sourceAuthor = await Database.authorModel.findByPk(ba.authorId)
if (sourceAuthor) {
const targetAuthor = await Database.authorModel.findOrCreateByNameAndLibrary(sourceAuthor.name, targetLibrary.id)
// Copy description and ASIN if target doesn't have them
let targetAuthorUpdated = false
if (!targetAuthor.description && sourceAuthor.description) {
targetAuthor.description = sourceAuthor.description
targetAuthorUpdated = true
}
if (!targetAuthor.asin && sourceAuthor.asin) {
targetAuthor.asin = sourceAuthor.asin
targetAuthorUpdated = true
}
// Copy image if target doesn't have one
if (!targetAuthor.imagePath && sourceAuthor.imagePath && (await fs.pathExists(sourceAuthor.imagePath))) {
const ext = Path.extname(sourceAuthor.imagePath)
const newImagePath = Path.posix.join(Path.join(global.MetadataPath, 'authors'), targetAuthor.id + ext)
try {
await fs.copy(sourceAuthor.imagePath, newImagePath)
targetAuthor.imagePath = newImagePath
targetAuthorUpdated = true
} catch (err) {
Logger.error(`[LibraryItemController] Failed to copy author image`, err)
}
}
if (targetAuthorUpdated) await targetAuthor.save()
// Update link
ba.authorId = targetAuthor.id
await ba.save()
// Check if source author is now empty
const sourceAuthorBooksCount = await Database.bookAuthorModel.getCountForAuthor(sourceAuthor.id)
if (sourceAuthorBooksCount === 0) {
Logger.info(`[LibraryItemController] Source author "${sourceAuthor.name}" in library ${oldLibraryId} is now empty. Deleting.`)
if (sourceAuthor.imagePath) {
await fs.remove(sourceAuthor.imagePath).catch((err) => Logger.error(`[LibraryItemController] Failed to remove source author image`, err))
}
await sourceAuthor.destroy()
Database.removeAuthorFromFilterData(oldLibraryId, sourceAuthor.id)
}
}
}
}
// Emit socket events for UI updates
SocketAuthority.emitter('item_removed', {
id: req.libraryItem.id,