Skip to content

Commit f827aa9

Browse files
committed
Update library scanner findLibraryItemByItemToFileInoMatch query to iterate through json objects comparing inodes
1 parent 68276fe commit f827aa9

File tree

3 files changed

+23
-17
lines changed

3 files changed

+23
-17
lines changed

server/Logger.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ class Logger {
9393

9494
// Save log to file
9595
if (level >= this.logLevel) {
96-
await this.logManager.logToFile(logObj)
96+
await this.logManager?.logToFile(logObj)
9797
}
9898
}
9999

server/models/LibraryItem.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { DataTypes, Model, WhereOptions } = require('sequelize')
1+
const { DataTypes, Model } = require('sequelize')
22
const Logger = require('../Logger')
33
const oldLibraryItem = require('../objects/LibraryItem')
44
const libraryFilters = require('../utils/queries/libraryFilters')
@@ -116,7 +116,7 @@ class LibraryItem extends Model {
116116

117117
/**
118118
* Currently unused because this is too slow and uses too much mem
119-
* @param {[WhereOptions]} where
119+
* @param {import('sequelize').WhereOptions} [where]
120120
* @returns {Array<objects.LibraryItem>} old library items
121121
*/
122122
static async getAllOldLibraryItems(where = null) {
@@ -773,12 +773,14 @@ class LibraryItem extends Model {
773773

774774
/**
775775
*
776-
* @param {WhereOptions} where
776+
* @param {import('sequelize').WhereOptions} where
777+
* @param {import('sequelize').BindOrReplacements} replacements
777778
* @returns {Object} oldLibraryItem
778779
*/
779-
static async findOneOld(where) {
780+
static async findOneOld(where, replacements = {}) {
780781
const libraryItem = await this.findOne({
781782
where,
783+
replacements,
782784
include: [
783785
{
784786
model: this.sequelize.models.book,

server/scanner/LibraryScanner.js

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -155,10 +155,10 @@ class LibraryScanner {
155155
if (!libraryItemData) {
156156
// Fallback to finding matching library item with matching inode value
157157
libraryItemData = libraryItemDataFound.find(lid =>
158-
ItemToItemInoMatch(lid, existingLibraryItem) ||
159-
ItemToFileInoMatch(lid, existingLibraryItem) ||
158+
ItemToItemInoMatch(lid, existingLibraryItem) ||
159+
ItemToFileInoMatch(lid, existingLibraryItem) ||
160160
ItemToFileInoMatch(existingLibraryItem, lid)
161-
)
161+
)
162162
if (libraryItemData) {
163163
libraryScan.addLog(LogLevel.INFO, `Library item with path "${existingLibraryItem.path}" was not found, but library item inode "${existingLibraryItem.ino}" was found at path "${libraryItemData.path}"`)
164164
}
@@ -533,8 +533,8 @@ class LibraryScanner {
533533
let updatedLibraryItemDetails = {}
534534
if (!existingLibraryItem) {
535535
const isSingleMedia = isSingleMediaFile(fileUpdateGroup, itemDir)
536-
existingLibraryItem =
537-
await findLibraryItemByItemToItemInoMatch(library.id, fullPath) ||
536+
existingLibraryItem =
537+
await findLibraryItemByItemToItemInoMatch(library.id, fullPath) ||
538538
await findLibraryItemByItemToFileInoMatch(library.id, fullPath, isSingleMedia) ||
539539
await findLibraryItemByFileToItemInoMatch(library.id, fullPath, isSingleMedia, fileUpdateGroup[itemDir])
540540
if (existingLibraryItem) {
@@ -630,16 +630,20 @@ async function findLibraryItemByItemToItemInoMatch(libraryId, fullPath) {
630630
return existingLibraryItem
631631
}
632632

633-
async function findLibraryItemByItemToFileInoMatch(libraryId, fullPath, isSingleMedia) {
633+
async function findLibraryItemByItemToFileInoMatch(libraryId, fullPath, isSingleMedia) {
634634
if (!isSingleMedia) return null
635635
// check if it was moved from another folder by comparing the ino to the library files
636636
const ino = await fileUtils.getIno(fullPath)
637637
if (!ino) return null
638-
const existingLibraryItem = await Database.libraryItemModel.findOneOld({
639-
libraryId: libraryId,
640-
libraryFiles: {
641-
[ sequelize.Op.substring ]: ino
642-
}
638+
const existingLibraryItem = await Database.libraryItemModel.findOneOld([
639+
{
640+
libraryId: libraryId
641+
},
642+
sequelize.where(sequelize.literal('(SELECT count(*) FROM json_each(libraryFiles) WHERE json_valid(json_each.value) AND json_each.value->>"$.ino" = :inode)'), {
643+
[sequelize.Op.gt]: 0
644+
})
645+
], {
646+
inode: ino
643647
})
644648
if (existingLibraryItem)
645649
Logger.debug(`[LibraryScanner] Found library item with a library file matching inode "${ino}" at path "${existingLibraryItem.path}"`)
@@ -658,7 +662,7 @@ async function findLibraryItemByFileToItemInoMatch(libraryId, fullPath, isSingle
658662
const existingLibraryItem = await Database.libraryItemModel.findOneOld({
659663
libraryId: libraryId,
660664
ino: {
661-
[ sequelize.Op.in ] : itemFileInos
665+
[sequelize.Op.in]: itemFileInos
662666
}
663667
})
664668
if (existingLibraryItem)

0 commit comments

Comments
 (0)