Fix multiple bugs

This commit is contained in:
Tiberiu Ichim 2026-02-06 22:35:03 +02:00
parent ef3f39191d
commit f511e0046d
5 changed files with 198 additions and 188 deletions

View file

@ -139,9 +139,12 @@ Following the initial implementation, several critical areas were improved:
- **Issue**: Metadata `save()` calls for newly created series/authors were not awaited. - **Issue**: Metadata `save()` calls for newly created series/authors were not awaited.
- **Fix**: Ensured all `.save()` calls are properly awaited. - **Fix**: Ensured all `.save()` calls are properly awaited.
### 4. Transactional Integrity - FIXED ### 4. Transactional Integrity & Lock Optimization - FIXED
- **Issue**: The move logic was not wrapped in a DB transaction. - **Issue**: The move logic was not wrapped in a DB transaction, and long-running file moves inside transactions would lock the SQLite database for several seconds (causing `SQLITE_BUSY`).
- **Fix**: Wrapped the DB update portion of `handleMoveLibraryItem` in a Sequelize transaction that is committed only if all updates succeed. - **Fix**:
- Wrapped the DB update portion of `handleMoveLibraryItem` in a Sequelize transaction.
- **Optimization**: Moved the `fs.move` operation **outside** the transaction. The files are moved first, then the transaction handles the lightning-fast DB updates. If the DB update fails, the files are moved back to their original location.
- **Transaction Propagation**: Updated `Series`, `Author`, and `BookAuthor` model helpers to correctly accept and propagate the transaction object.
### 5. Scanner "ENOTDIR" Error - FIXED ### 5. Scanner "ENOTDIR" Error - FIXED
- **Issue**: Single-file items (e.g., `.m4b`) were being scanned as directories, leading to `ENOTDIR` errors and causing items to appear with the "has issues" icon. - **Issue**: Single-file items (e.g., `.m4b`) were being scanned as directories, leading to `ENOTDIR` errors and causing items to appear with the "has issues" icon.

View file

@ -46,9 +46,8 @@ const ShareManager = require('../managers/ShareManager')
* @param {import('../models/LibraryItem')} libraryItem * @param {import('../models/LibraryItem')} libraryItem
* @param {import('../models/Library')} targetLibrary * @param {import('../models/Library')} targetLibrary
* @param {import('../models/LibraryFolder')} targetFolder * @param {import('../models/LibraryFolder')} targetFolder
* @param {import('sequelize').Transaction} [transaction]
*/ */
async function handleMoveLibraryItem(libraryItem, targetLibrary, targetFolder, transaction = null) { async function handleMoveLibraryItem(libraryItem, targetLibrary, targetFolder) {
const oldPath = libraryItem.path const oldPath = libraryItem.path
const oldLibraryId = libraryItem.libraryId const oldLibraryId = libraryItem.libraryId
@ -73,171 +72,180 @@ async function handleMoveLibraryItem(libraryItem, targetLibrary, targetFolder, t
Logger.info(`[LibraryItemController] Moving item "${libraryItem.media.title}" from "${oldPath}" to "${newPath}"`) Logger.info(`[LibraryItemController] Moving item "${libraryItem.media.title}" from "${oldPath}" to "${newPath}"`)
await fs.move(oldPath, newPath) await fs.move(oldPath, newPath)
// Update library item in database // Update database within a transaction
libraryItem.libraryId = targetLibrary.id const transaction = await Database.sequelize.transaction()
libraryItem.libraryFolderId = targetFolder.id try {
libraryItem.path = newPath // Update library item in database
libraryItem.relPath = newRelPath libraryItem.libraryId = targetLibrary.id
libraryItem.isMissing = false libraryItem.libraryFolderId = targetFolder.id
libraryItem.isInvalid = false libraryItem.path = newPath
libraryItem.changed('updatedAt', true) libraryItem.relPath = newRelPath
await libraryItem.save({ transaction }) libraryItem.isMissing = false
libraryItem.isInvalid = false
// Update library files paths libraryItem.changed('updatedAt', true)
if (libraryItem.libraryFiles?.length) {
libraryItem.libraryFiles = libraryItem.libraryFiles.map((lf) => {
if (lf.metadata?.path) {
lf.metadata.path = lf.metadata.path.replace(oldPath, newPath)
}
if (lf.metadata?.relPath) {
lf.metadata.relPath = lf.metadata.relPath.replace(oldRelPath, newRelPath)
}
return lf
})
libraryItem.changed('libraryFiles', true)
await libraryItem.save({ transaction }) await libraryItem.save({ transaction })
}
// Update media file paths (audioFiles, ebookFile for books; podcastEpisodes for podcasts) // Update library files paths
if (libraryItem.isBook) { if (libraryItem.libraryFiles?.length) {
// Update audioFiles paths libraryItem.libraryFiles = libraryItem.libraryFiles.map((lf) => {
if (libraryItem.media.audioFiles?.length) { if (lf.metadata?.path) {
libraryItem.media.audioFiles = libraryItem.media.audioFiles.map((af) => { lf.metadata.path = lf.metadata.path.replace(oldPath, newPath)
if (af.metadata?.path) {
af.metadata.path = af.metadata.path.replace(oldPath, newPath)
} }
if (af.metadata?.relPath) { if (lf.metadata?.relPath) {
af.metadata.relPath = af.metadata.relPath.replace(oldRelPath, newRelPath) lf.metadata.relPath = lf.metadata.relPath.replace(oldRelPath, newRelPath)
} }
return af return lf
}) })
libraryItem.media.changed('audioFiles', true) libraryItem.changed('libraryFiles', true)
await libraryItem.save({ transaction })
} }
// Update ebookFile path
if (libraryItem.media.ebookFile?.metadata?.path) {
libraryItem.media.ebookFile.metadata.path = libraryItem.media.ebookFile.metadata.path.replace(oldPath, newPath)
if (libraryItem.media.ebookFile.metadata?.relPath) {
libraryItem.media.ebookFile.metadata.relPath = libraryItem.media.ebookFile.metadata.relPath.replace(oldRelPath, newRelPath)
}
libraryItem.media.changed('ebookFile', true)
}
// Update coverPath
if (libraryItem.media.coverPath) {
libraryItem.media.coverPath = libraryItem.media.coverPath.replace(oldPath, newPath)
}
await libraryItem.media.save({ transaction })
} else if (libraryItem.isPodcast) {
// Update coverPath
if (libraryItem.media.coverPath) {
libraryItem.media.coverPath = libraryItem.media.coverPath.replace(oldPath, newPath)
}
await libraryItem.media.save({ transaction })
// Update podcast episode audio file paths
for (const episode of libraryItem.media.podcastEpisodes || []) {
let episodeUpdated = false
if (episode.audioFile?.metadata?.path) {
episode.audioFile.metadata.path = episode.audioFile.metadata.path.replace(oldPath, newPath)
episodeUpdated = true
}
if (episode.audioFile?.metadata?.relPath) {
episode.audioFile.metadata.relPath = episode.audioFile.metadata.relPath.replace(oldRelPath, newRelPath)
episodeUpdated = true
}
if (episodeUpdated) {
episode.changed('audioFile', true)
await episode.save({ transaction })
}
}
}
// Handle Series and Authors when moving a book // Update media file paths (audioFiles, ebookFile for books; podcastEpisodes for podcasts)
if (libraryItem.isBook) { if (libraryItem.isBook) {
// Handle Series // Update audioFiles paths
const bookSeries = await Database.bookSeriesModel.findAll({ if (libraryItem.media.audioFiles?.length) {
where: { bookId: libraryItem.media.id }, libraryItem.media.audioFiles = libraryItem.media.audioFiles.map((af) => {
transaction if (af.metadata?.path) {
}) af.metadata.path = af.metadata.path.replace(oldPath, newPath)
for (const bs of bookSeries) { }
const sourceSeries = await Database.seriesModel.findByPk(bs.seriesId, { transaction }) if (af.metadata?.relPath) {
if (sourceSeries) { af.metadata.relPath = af.metadata.relPath.replace(oldRelPath, newRelPath)
const targetSeries = await Database.seriesModel.findOrCreateByNameAndLibrary(sourceSeries.name, targetLibrary.id, transaction) }
return af
// If target series doesn't have a description but source does, copy it })
if (!targetSeries.description && sourceSeries.description) { libraryItem.media.changed('audioFiles', true)
targetSeries.description = sourceSeries.description }
await targetSeries.save({ transaction }) // Update ebookFile path
if (libraryItem.media.ebookFile?.metadata?.path) {
libraryItem.media.ebookFile.metadata.path = libraryItem.media.ebookFile.metadata.path.replace(oldPath, newPath)
if (libraryItem.media.ebookFile.metadata?.relPath) {
libraryItem.media.ebookFile.metadata.relPath = libraryItem.media.ebookFile.metadata.relPath.replace(oldRelPath, newRelPath)
} }
libraryItem.media.changed('ebookFile', true)
// Update link }
bs.seriesId = targetSeries.id // Update coverPath
await bs.save({ transaction }) if (libraryItem.media.coverPath) {
libraryItem.media.coverPath = libraryItem.media.coverPath.replace(oldPath, newPath)
// Check if source series is now empty }
const sourceSeriesBooksCount = await Database.bookSeriesModel.count({ where: { seriesId: sourceSeries.id }, transaction }) await libraryItem.media.save({ transaction })
if (sourceSeriesBooksCount === 0) { } else if (libraryItem.isPodcast) {
Logger.info(`[LibraryItemController] Source series "${sourceSeries.name}" in library ${oldLibraryId} is now empty. Deleting.`) // Update coverPath
await sourceSeries.destroy({ transaction }) if (libraryItem.media.coverPath) {
Database.removeSeriesFromFilterData(oldLibraryId, sourceSeries.id) libraryItem.media.coverPath = libraryItem.media.coverPath.replace(oldPath, newPath)
}
SocketAuthority.emitter('series_removed', { id: sourceSeries.id, libraryId: oldLibraryId }) await libraryItem.media.save({ transaction })
// Update podcast episode audio file paths
for (const episode of libraryItem.media.podcastEpisodes || []) {
let episodeUpdated = false
if (episode.audioFile?.metadata?.path) {
episode.audioFile.metadata.path = episode.audioFile.metadata.path.replace(oldPath, newPath)
episodeUpdated = true
}
if (episode.audioFile?.metadata?.relPath) {
episode.audioFile.metadata.relPath = episode.audioFile.metadata.relPath.replace(oldRelPath, newRelPath)
episodeUpdated = true
}
if (episodeUpdated) {
episode.changed('audioFile', true)
await episode.save({ transaction })
} }
} }
} }
// Handle Authors // Handle Series and Authors when moving a book
const bookAuthors = await Database.bookAuthorModel.findAll({ if (libraryItem.isBook) {
where: { bookId: libraryItem.media.id }, // Handle Series
transaction const bookSeries = await Database.bookSeriesModel.findAll({
}) where: { bookId: libraryItem.media.id },
for (const ba of bookAuthors) { transaction
const sourceAuthor = await Database.authorModel.findByPk(ba.authorId, { transaction }) })
if (sourceAuthor) { for (const bs of bookSeries) {
const targetAuthor = await Database.authorModel.findOrCreateByNameAndLibrary(sourceAuthor.name, targetLibrary.id, transaction) const sourceSeries = await Database.seriesModel.findByPk(bs.seriesId, { transaction })
if (sourceSeries) {
const targetSeries = await Database.seriesModel.findOrCreateByNameAndLibrary(sourceSeries.name, targetLibrary.id, transaction)
// Copy description and ASIN if target doesn't have them // If target series doesn't have a description but source does, copy it
let targetAuthorUpdated = false if (!targetSeries.description && sourceSeries.description) {
if (!targetAuthor.description && sourceAuthor.description) { targetSeries.description = sourceSeries.description
targetAuthor.description = sourceAuthor.description await targetSeries.save({ transaction })
targetAuthorUpdated = true }
}
if (!targetAuthor.asin && sourceAuthor.asin) {
targetAuthor.asin = sourceAuthor.asin
targetAuthorUpdated = true
}
// Copy image if target doesn't have one // Update link
if (!targetAuthor.imagePath && sourceAuthor.imagePath && (await fs.pathExists(sourceAuthor.imagePath))) { bs.seriesId = targetSeries.id
const ext = Path.extname(sourceAuthor.imagePath) await bs.save({ transaction })
const newImagePath = Path.posix.join(Path.join(global.MetadataPath, 'authors'), targetAuthor.id + ext)
try { // Check if source series is now empty
await fs.copy(sourceAuthor.imagePath, newImagePath) const sourceSeriesBooksCount = await Database.bookSeriesModel.count({ where: { seriesId: sourceSeries.id }, transaction })
targetAuthor.imagePath = newImagePath if (sourceSeriesBooksCount === 0) {
Logger.info(`[LibraryItemController] Source series "${sourceSeries.name}" in library ${oldLibraryId} is now empty. Deleting.`)
await sourceSeries.destroy({ transaction })
Database.removeSeriesFromFilterData(oldLibraryId, sourceSeries.id)
SocketAuthority.emitter('series_removed', { id: sourceSeries.id, libraryId: oldLibraryId })
}
}
}
// Handle Authors
const bookAuthors = await Database.bookAuthorModel.findAll({
where: { bookId: libraryItem.media.id },
transaction
})
for (const ba of bookAuthors) {
const sourceAuthor = await Database.authorModel.findByPk(ba.authorId, { transaction })
if (sourceAuthor) {
const targetAuthor = await Database.authorModel.findOrCreateByNameAndLibrary(sourceAuthor.name, targetLibrary.id, transaction)
// Copy description and ASIN if target doesn't have them
let targetAuthorUpdated = false
if (!targetAuthor.description && sourceAuthor.description) {
targetAuthor.description = sourceAuthor.description
targetAuthorUpdated = true targetAuthorUpdated = true
} catch (err) {
Logger.error(`[LibraryItemController] Failed to copy author image`, err)
} }
} if (!targetAuthor.asin && sourceAuthor.asin) {
targetAuthor.asin = sourceAuthor.asin
if (targetAuthorUpdated) await targetAuthor.save({ transaction }) targetAuthorUpdated = true
// Update link
ba.authorId = targetAuthor.id
await ba.save({ transaction })
// Check if source author is now empty
const sourceAuthorBooksCount = await Database.bookAuthorModel.getCountForAuthor(sourceAuthor.id, transaction)
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({ transaction })
Database.removeAuthorFromFilterData(oldLibraryId, sourceAuthor.id)
SocketAuthority.emitter('author_removed', { id: sourceAuthor.id, libraryId: oldLibraryId }) // 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({ transaction })
// Update link
ba.authorId = targetAuthor.id
await ba.save({ transaction })
// Check if source author is now empty
const sourceAuthorBooksCount = await Database.bookAuthorModel.getCountForAuthor(sourceAuthor.id, transaction)
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({ transaction })
Database.removeAuthorFromFilterData(oldLibraryId, sourceAuthor.id)
SocketAuthority.emitter('author_removed', { id: sourceAuthor.id, libraryId: oldLibraryId })
}
} }
} }
} }
await transaction.commit()
} catch (dbError) {
if (transaction) await transaction.rollback()
throw dbError
} }
// Emit socket events for UI updates // Emit socket events for UI updates
@ -1107,19 +1115,11 @@ class LibraryItemController {
continue continue
} }
const transaction = await Database.sequelize.transaction() await handleMoveLibraryItem(libraryItem, targetLibrary, targetFolder)
try { successCount++
await handleMoveLibraryItem(libraryItem, targetLibrary, targetFolder, transaction)
await transaction.commit()
successCount++
} catch (error) {
if (transaction) await transaction.rollback()
failCount++
errors.push({ id: libraryItem.id, error: error.message || 'Failed to move item' })
}
} catch (error) { } catch (error) {
failCount++ failCount++
errors.push({ id: libraryItem.id, error: error.message || 'Unknown error' }) errors.push({ id: libraryItem.id, error: error.message || 'Failed to move item' })
} }
} }
@ -1565,14 +1565,7 @@ class LibraryItemController {
} }
try { try {
const transaction = await Database.sequelize.transaction() await handleMoveLibraryItem(req.libraryItem, targetLibrary, targetFolder)
try {
await handleMoveLibraryItem(req.libraryItem, targetLibrary, targetFolder, transaction)
await transaction.commit()
} catch (error) {
await transaction.rollback()
throw error
}
await Database.resetLibraryIssuesFilterData(sourceLibrary.id) await Database.resetLibraryIssuesFilterData(sourceLibrary.id)
await Database.resetLibraryIssuesFilterData(targetLibrary.id) await Database.resetLibraryIssuesFilterData(targetLibrary.id)

View file

@ -50,16 +50,18 @@ class Author extends Model {
* *
* @param {string} authorName * @param {string} authorName
* @param {string} libraryId * @param {string} libraryId
* @param {import('sequelize').Transaction} [transaction]
* @returns {Promise<Author>} * @returns {Promise<Author>}
*/ */
static async getByNameAndLibrary(authorName, libraryId) { static async getByNameAndLibrary(authorName, libraryId, transaction = null) {
return this.findOne({ return this.findOne({
where: [ where: [
where(fn('lower', col('name')), authorName.toLowerCase()), where(fn('lower', col('name')), authorName.toLowerCase()),
{ {
libraryId libraryId
} }
] ],
transaction
}) })
} }
@ -111,16 +113,20 @@ class Author extends Model {
* *
* @param {string} name * @param {string} name
* @param {string} libraryId * @param {string} libraryId
* @param {import('sequelize').Transaction} [transaction]
* @returns {Promise<Author>} * @returns {Promise<Author>}
*/ */
static async findOrCreateByNameAndLibrary(name, libraryId) { static async findOrCreateByNameAndLibrary(name, libraryId, transaction = null) {
const author = await this.getByNameAndLibrary(name, libraryId) const author = await this.getByNameAndLibrary(name, libraryId, transaction)
if (author) return author if (author) return author
return this.create({ return this.create(
name, {
lastFirst: this.getLastFirst(name), name,
libraryId lastFirst: this.getLastFirst(name),
}) libraryId
},
{ transaction }
)
} }
/** /**

View file

@ -27,13 +27,15 @@ class BookAuthor extends Model {
* Get number of books for author * Get number of books for author
* *
* @param {string} authorId * @param {string} authorId
* @param {import('sequelize').Transaction} [transaction]
* @returns {Promise<number>} * @returns {Promise<number>}
*/ */
static getCountForAuthor(authorId) { static getCountForAuthor(authorId, transaction = null) {
return this.count({ return this.count({
where: { where: {
authorId authorId
} },
transaction
}) })
} }

View file

@ -41,16 +41,18 @@ class Series extends Model {
* *
* @param {string} seriesName * @param {string} seriesName
* @param {string} libraryId * @param {string} libraryId
* @param {import('sequelize').Transaction} [transaction]
* @returns {Promise<Series>} * @returns {Promise<Series>}
*/ */
static async getByNameAndLibrary(seriesName, libraryId) { static async getByNameAndLibrary(seriesName, libraryId, transaction = null) {
return this.findOne({ return this.findOne({
where: [ where: [
where(fn('lower', col('name')), seriesName.toLowerCase()), where(fn('lower', col('name')), seriesName.toLowerCase()),
{ {
libraryId libraryId
} }
] ],
transaction
}) })
} }
@ -70,16 +72,20 @@ class Series extends Model {
* *
* @param {string} seriesName * @param {string} seriesName
* @param {string} libraryId * @param {string} libraryId
* @param {import('sequelize').Transaction} [transaction]
* @returns {Promise<Series>} * @returns {Promise<Series>}
*/ */
static async findOrCreateByNameAndLibrary(seriesName, libraryId) { static async findOrCreateByNameAndLibrary(seriesName, libraryId, transaction = null) {
const series = await this.getByNameAndLibrary(seriesName, libraryId) const series = await this.getByNameAndLibrary(seriesName, libraryId, transaction)
if (series) return series if (series) return series
return this.create({ return this.create(
name: seriesName, {
nameIgnorePrefix: getTitleIgnorePrefix(seriesName), name: seriesName,
libraryId nameIgnorePrefix: getTitleIgnorePrefix(seriesName),
}) libraryId
},
{ transaction }
)
} }
/** /**