diff --git a/client/components/modals/libraries/EditModal.vue b/client/components/modals/libraries/EditModal.vue
index a6d8a4d59..8fe4c2ec1 100644
--- a/client/components/modals/libraries/EditModal.vue
+++ b/client/components/modals/libraries/EditModal.vue
@@ -125,6 +125,7 @@ export default {
skipMatchingMediaWithAsin: false,
skipMatchingMediaWithIsbn: false,
autoScanCronExpression: null,
+ matchAfterScan: false,
hideSingleBookSeries: false,
onlyShowLaterBooksInContinueSeries: false,
metadataPrecedence: ['folderStructure', 'audioMetatags', 'nfoFile', 'txtFiles', 'opfFile', 'absMetadata'],
diff --git a/client/components/modals/libraries/ScheduleScan.vue b/client/components/modals/libraries/ScheduleScan.vue
index e4f4810e5..25a6b298e 100644
--- a/client/components/modals/libraries/ScheduleScan.vue
+++ b/client/components/modals/libraries/ScheduleScan.vue
@@ -4,7 +4,12 @@
{{ $strings.HeaderScheduleLibraryScans }}
-
+
{{ $strings.MessageScheduleLibraryScanNote }}
@@ -23,7 +28,8 @@ export default {
data() {
return {
cronExpression: null,
- enableAutoScan: false
+ enableAutoScan: false,
+ matchAfterScan: false
}
},
computed: {},
@@ -47,9 +53,17 @@ export default {
}
})
},
+ updateMatchAfterScan(value) {
+ this.$emit('update', {
+ settings: {
+ matchAfterScan: value
+ }
+ })
+ },
init() {
this.cronExpression = this.library.settings.autoScanCronExpression
this.enableAutoScan = !!this.cronExpression
+ this.matchAfterScan = this.library.settings.matchAfterScan
}
},
mounted() {
diff --git a/client/strings/en-us.json b/client/strings/en-us.json
index 83acb5a69..06866ced3 100644
--- a/client/strings/en-us.json
+++ b/client/strings/en-us.json
@@ -448,6 +448,7 @@
"LabelLookForNewEpisodesAfterDate": "Look for new episodes after this date",
"LabelLowestPriority": "Lowest Priority",
"LabelMatchConfidence": "Confidence",
+ "LabelMatchAfterScan": "Run 'Match Books' after scan",
"LabelMatchExistingUsersBy": "Match existing users by",
"LabelMatchExistingUsersByDescription": "Used for connecting existing users. Once connected, users will be matched by a unique id from your SSO provider",
"LabelMaxEpisodesToDownload": "Max # of episodes to download. Use 0 for unlimited.",
diff --git a/server/controllers/LibraryController.js b/server/controllers/LibraryController.js
index 55ef45690..8daeb6a89 100644
--- a/server/controllers/LibraryController.js
+++ b/server/controllers/LibraryController.js
@@ -354,6 +354,15 @@ class LibraryController {
updatedSettings[key] = req.body.settings[key] === null ? null : Number(req.body.settings[key])
Logger.debug(`[LibraryController] Library "${req.library.name}" updating setting "${key}" to "${updatedSettings[key]}"`)
}
+ } else if (key === 'matchAfterScan') {
+ if (typeof req.body.settings[key] !== 'boolean') {
+ return res.status(400).send('Invalid request. Setting "matchAfterScan" must be a boolean')
+ }
+ if (req.body.settings[key] !== updatedSettings[key]) {
+ hasUpdates = true
+ updatedSettings[key] = req.body.settings[key]
+ Logger.debug(`[LibraryController] Library "${req.library.name}" updating setting "${key}" to "${updatedSettings[key]}"`)
+ }
} else {
if (typeof req.body.settings[key] !== typeof updatedSettings[key]) {
Logger.error(`[LibraryController] Invalid request. Setting "${key}" must be of type ${typeof updatedSettings[key]}`)
diff --git a/server/managers/CronManager.js b/server/managers/CronManager.js
index d3e652129..8849d8491 100644
--- a/server/managers/CronManager.js
+++ b/server/managers/CronManager.js
@@ -3,6 +3,8 @@ const cron = require('../libs/nodeCron')
const Logger = require('../Logger')
const Database = require('../Database')
const LibraryScanner = require('../scanner/LibraryScanner')
+const Scanner = require('../scanner/Scanner')
+const { checkRemoveEmptySeries, checkRemoveAuthorsWithNoBooks } = require('../utils/cleanup')
const ShareManager = require('./ShareManager')
@@ -74,7 +76,16 @@ class CronManager {
Logger.error(`[CronManager] Library not found for scan cron ${_library.id}`)
} else {
Logger.debug(`[CronManager] Library scan cron executing for ${library.name}`)
- LibraryScanner.scan(library)
+ await LibraryScanner.scan(library)
+
+ if (library.settings.matchAfterScan) {
+ Logger.debug(`[CronManager] Library scan cron matching books for ${library.name}`)
+ const apiRouterCtx = {
+ checkRemoveEmptySeries,
+ checkRemoveAuthorsWithNoBooks
+ }
+ Scanner.matchLibraryItems(apiRouterCtx, library)
+ }
}
})
this.libraryScanCrons.push({
diff --git a/server/models/Library.js b/server/models/Library.js
index 708880aad..e10f442dd 100644
--- a/server/models/Library.js
+++ b/server/models/Library.js
@@ -68,6 +68,7 @@ class Library extends Model {
coverAspectRatio: 1, // Square
disableWatcher: false,
autoScanCronExpression: null,
+ matchAfterScan: false,
skipMatchingMediaWithAsin: false,
skipMatchingMediaWithIsbn: false,
audiobooksOnly: false,
diff --git a/server/routers/ApiRouter.js b/server/routers/ApiRouter.js
index db04bf5ec..00bfb240c 100644
--- a/server/routers/ApiRouter.js
+++ b/server/routers/ApiRouter.js
@@ -35,6 +35,7 @@ const MiscController = require('../controllers/MiscController')
const ShareController = require('../controllers/ShareController')
const StatsController = require('../controllers/StatsController')
const ApiKeyController = require('../controllers/ApiKeyController')
+const { checkRemoveEmptySeries, checkRemoveAuthorsWithNoBooks } = require('../utils/cleanup')
class ApiRouter {
constructor(Server) {
@@ -405,54 +406,7 @@ class ApiRouter {
* @param {string[]} seriesIds
*/
async checkRemoveEmptySeries(seriesIds) {
- if (!seriesIds?.length) return
-
- const transaction = await Database.sequelize.transaction()
- try {
- const seriesToRemove = (
- await Database.seriesModel.findAll({
- where: [
- {
- id: seriesIds
- },
- sequelize.where(sequelize.literal('(SELECT count(*) FROM bookSeries bs WHERE bs.seriesId = series.id)'), 0)
- ],
- attributes: ['id', 'name', 'libraryId'],
- include: {
- model: Database.bookModel,
- attributes: ['id'],
- required: false // Ensure it includes series even if no books exist
- },
- transaction
- })
- ).map((s) => ({ id: s.id, name: s.name, libraryId: s.libraryId }))
-
- if (seriesToRemove.length) {
- await Database.seriesModel.destroy({
- where: {
- id: seriesToRemove.map((s) => s.id)
- },
- transaction
- })
- }
-
- await transaction.commit()
-
- seriesToRemove.forEach(({ id, name, libraryId }) => {
- Logger.info(`[ApiRouter] Series "${name}" is now empty. Removing series`)
-
- // Remove series from library filter data
- Database.removeSeriesFromFilterData(libraryId, id)
- SocketAuthority.emitter('series_removed', { id: id, libraryId: libraryId })
- })
- // Close rss feeds - remove from db and emit socket event
- if (seriesToRemove.length) {
- await RssFeedManager.closeFeedsForEntityIds(seriesToRemove.map((s) => s.id))
- }
- } catch (error) {
- await transaction.rollback()
- Logger.error(`[ApiRouter] Error removing empty series: ${error.message}`)
- }
+ return checkRemoveEmptySeries(seriesIds)
}
/**
@@ -463,56 +417,7 @@ class ApiRouter {
* @returns {Promise}
*/
async checkRemoveAuthorsWithNoBooks(authorIds) {
- if (!authorIds?.length) return
-
- const transaction = await Database.sequelize.transaction()
- try {
- // Select authors with locking to prevent concurrent updates
- const bookAuthorsToRemove = (
- await Database.authorModel.findAll({
- where: [
- {
- id: authorIds,
- asin: {
- [sequelize.Op.or]: [null, '']
- },
- description: {
- [sequelize.Op.or]: [null, '']
- },
- imagePath: {
- [sequelize.Op.or]: [null, '']
- }
- },
- sequelize.where(sequelize.literal('(SELECT count(*) FROM bookAuthors ba WHERE ba.authorId = author.id)'), 0)
- ],
- attributes: ['id', 'name', 'libraryId'],
- raw: true,
- transaction
- })
- ).map((au) => ({ id: au.id, name: au.name, libraryId: au.libraryId }))
-
- if (bookAuthorsToRemove.length) {
- await Database.authorModel.destroy({
- where: {
- id: bookAuthorsToRemove.map((au) => au.id)
- },
- transaction
- })
- }
-
- await transaction.commit()
-
- // Remove all book authors after completing remove from database
- bookAuthorsToRemove.forEach(({ id, name, libraryId }) => {
- Database.removeAuthorFromFilterData(libraryId, id)
- // TODO: Clients were expecting full author in payload but its unnecessary
- SocketAuthority.emitter('author_removed', { id, libraryId })
- Logger.info(`[ApiRouter] Removed author "${name}" with no books`)
- })
- } catch (error) {
- await transaction.rollback()
- Logger.error(`[ApiRouter] Error removing authors: ${error.message}`)
- }
+ return checkRemoveAuthorsWithNoBooks(authorIds)
}
async getUserListeningSessionsHelper(userId) {
diff --git a/server/utils/cleanup.js b/server/utils/cleanup.js
new file mode 100644
index 000000000..2b1354d00
--- /dev/null
+++ b/server/utils/cleanup.js
@@ -0,0 +1,126 @@
+const sequelize = require('sequelize')
+const Logger = require('../Logger')
+const Database = require('../Database')
+const SocketAuthority = require('../SocketAuthority')
+const RssFeedManager = require('../managers/RssFeedManager')
+
+/**
+ * After deleting book(s), remove empty series
+ *
+ * @param {string[]} seriesIds
+ */
+async function checkRemoveEmptySeries(seriesIds) {
+ if (!seriesIds?.length) return
+
+ const transaction = await Database.sequelize.transaction()
+ try {
+ const seriesToRemove = (
+ await Database.seriesModel.findAll({
+ where: [
+ {
+ id: seriesIds
+ },
+ sequelize.where(sequelize.literal('(SELECT count(*) FROM bookSeries bs WHERE bs.seriesId = series.id)'), 0)
+ ],
+ attributes: ['id', 'name', 'libraryId'],
+ include: {
+ model: Database.bookModel,
+ attributes: ['id'],
+ required: false // Ensure it includes series even if no books exist
+ },
+ transaction
+ })
+ ).map((s) => ({ id: s.id, name: s.name, libraryId: s.libraryId }))
+
+ if (seriesToRemove.length) {
+ await Database.seriesModel.destroy({
+ where: {
+ id: seriesToRemove.map((s) => s.id)
+ },
+ transaction
+ })
+ }
+
+ await transaction.commit()
+
+ seriesToRemove.forEach(({ id, name, libraryId }) => {
+ Logger.info(`[ApiRouter] Series "${name}" is now empty. Removing series`)
+
+ // Remove series from library filter data
+ Database.removeSeriesFromFilterData(libraryId, id)
+ SocketAuthority.emitter('series_removed', { id: id, libraryId: libraryId })
+ })
+ // Close rss feeds - remove from db and emit socket event
+ if (seriesToRemove.length) {
+ await RssFeedManager.closeFeedsForEntityIds(seriesToRemove.map((s) => s.id))
+ }
+ } catch (error) {
+ await transaction.rollback()
+ Logger.error(`[ApiRouter] Error removing empty series: ${error.message}`)
+ }
+}
+
+/**
+ * Remove authors with no books and unset asin, description and imagePath
+ * Note: Other implementation is in BookScanner.checkAuthorsRemovedFromBooks (can be merged)
+ *
+ * @param {string[]} authorIds
+ * @returns {Promise}
+ */
+async function checkRemoveAuthorsWithNoBooks(authorIds) {
+ if (!authorIds?.length) return
+
+ const transaction = await Database.sequelize.transaction()
+ try {
+ // Select authors with locking to prevent concurrent updates
+ const bookAuthorsToRemove = (
+ await Database.authorModel.findAll({
+ where: [
+ {
+ id: authorIds,
+ asin: {
+ [sequelize.Op.or]: [null, '']
+ },
+ description: {
+ [sequelize.Op.or]: [null, '']
+ },
+ imagePath: {
+ [sequelize.Op.or]: [null, '']
+ }
+ },
+ sequelize.where(sequelize.literal('(SELECT count(*) FROM bookAuthors ba WHERE ba.authorId = author.id)'), 0)
+ ],
+ attributes: ['id', 'name', 'libraryId'],
+ raw: true,
+ transaction
+ })
+ ).map((au) => ({ id: au.id, name: au.name, libraryId: au.libraryId }))
+
+ if (bookAuthorsToRemove.length) {
+ await Database.authorModel.destroy({
+ where: {
+ id: bookAuthorsToRemove.map((au) => au.id)
+ },
+ transaction
+ })
+ }
+
+ await transaction.commit()
+
+ // Remove all book authors after completing remove from database
+ bookAuthorsToRemove.forEach(({ id, name, libraryId }) => {
+ Database.removeAuthorFromFilterData(libraryId, id)
+ // TODO: Clients were expecting full author in payload but its unnecessary
+ SocketAuthority.emitter('author_removed', { id, libraryId })
+ Logger.info(`[ApiRouter] Removed author "${name}" with no books`)
+ })
+ } catch (error) {
+ await transaction.rollback()
+ Logger.error(`[ApiRouter] Error removing authors: ${error.message}`)
+ }
+}
+
+module.exports = {
+ checkRemoveEmptySeries,
+ checkRemoveAuthorsWithNoBooks
+}