2021-11-23 02:58:20 +01:00
|
|
|
const fs = require('fs-extra')
|
|
|
|
const Path = require('path')
|
|
|
|
|
|
|
|
// Utils
|
|
|
|
const Logger = require('../Logger')
|
|
|
|
const { version } = require('../../package.json')
|
|
|
|
const { groupFilesIntoAudiobookPaths, getAudiobookFileData, scanRootDir } = require('../utils/scandir')
|
2021-11-25 03:15:50 +01:00
|
|
|
const { comparePaths, getIno, getId, msToTimestamp } = require('../utils/index')
|
2021-11-26 01:39:02 +01:00
|
|
|
const { ScanResult, CoverDestination, LogLevel } = require('../utils/constants')
|
2021-11-23 02:58:20 +01:00
|
|
|
|
|
|
|
const AudioFileScanner = require('./AudioFileScanner')
|
|
|
|
const BookFinder = require('../BookFinder')
|
|
|
|
const Audiobook = require('../objects/Audiobook')
|
|
|
|
const LibraryScan = require('./LibraryScan')
|
|
|
|
const ScanOptions = require('./ScanOptions')
|
|
|
|
|
|
|
|
class Scanner {
|
|
|
|
constructor(AUDIOBOOK_PATH, METADATA_PATH, db, coverController, emitter) {
|
|
|
|
this.AudiobookPath = AUDIOBOOK_PATH
|
|
|
|
this.MetadataPath = METADATA_PATH
|
|
|
|
this.BookMetadataPath = Path.posix.join(this.MetadataPath.replace(/\\/g, '/'), 'books')
|
2021-11-26 01:39:02 +01:00
|
|
|
var LogDirPath = Path.join(this.MetadataPath, 'logs')
|
|
|
|
this.ScanLogPath = Path.join(LogDirPath, 'scans')
|
2021-11-23 02:58:20 +01:00
|
|
|
|
|
|
|
this.db = db
|
|
|
|
this.coverController = coverController
|
|
|
|
this.emitter = emitter
|
|
|
|
|
|
|
|
this.cancelLibraryScan = {}
|
|
|
|
this.librariesScanning = []
|
|
|
|
|
|
|
|
this.bookFinder = new BookFinder()
|
|
|
|
}
|
|
|
|
|
2021-11-25 03:15:50 +01:00
|
|
|
getCoverDirectory(audiobook) {
|
|
|
|
if (this.db.serverSettings.coverDestination === CoverDestination.AUDIOBOOK) {
|
|
|
|
return {
|
|
|
|
fullPath: audiobook.fullPath,
|
|
|
|
relPath: '/s/book/' + audiobook.id
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return {
|
|
|
|
fullPath: Path.posix.join(this.BookMetadataPath, audiobook.id),
|
|
|
|
relPath: Path.posix.join('/metadata', 'books', audiobook.id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-26 01:39:02 +01:00
|
|
|
isLibraryScanning(libraryId) {
|
|
|
|
return this.librariesScanning.find(ls => ls.id === libraryId)
|
|
|
|
}
|
|
|
|
|
2021-11-26 03:25:44 +01:00
|
|
|
setCancelLibraryScan(libraryId) {
|
|
|
|
var libraryScanning = this.librariesScanning.find(ls => ls.id === libraryId)
|
|
|
|
if (!libraryScanning) return
|
|
|
|
this.cancelLibraryScan[libraryId] = true
|
|
|
|
}
|
|
|
|
|
2021-11-26 01:39:02 +01:00
|
|
|
async scanAudiobookById(audiobookId) {
|
|
|
|
var audiobook = this.db.audiobooks.find(ab => ab.id === audiobookId)
|
|
|
|
if (!audiobook) {
|
|
|
|
Logger.error(`[Scanner] Scan audiobook by id not found ${audiobookId}`)
|
|
|
|
return ScanResult.NOTHING
|
|
|
|
}
|
|
|
|
const library = this.db.libraries.find(lib => lib.id === audiobook.libraryId)
|
|
|
|
if (!library) {
|
|
|
|
Logger.error(`[Scanner] Scan audiobook by id library not found "${audiobook.libraryId}"`)
|
|
|
|
return ScanResult.NOTHING
|
|
|
|
}
|
|
|
|
const folder = library.folders.find(f => f.id === audiobook.folderId)
|
|
|
|
if (!folder) {
|
|
|
|
Logger.error(`[Scanner] Scan audiobook by id folder not found "${audiobook.folderId}" in library "${library.name}"`)
|
|
|
|
return ScanResult.NOTHING
|
|
|
|
}
|
|
|
|
Logger.info(`[Scanner] Scanning Audiobook "${audiobook.title}"`)
|
|
|
|
return this.scanAudiobook(folder, audiobook)
|
|
|
|
}
|
|
|
|
|
|
|
|
async scanAudiobook(folder, audiobook) {
|
|
|
|
var audiobookData = await getAudiobookFileData(folder, audiobook.fullPath, this.db.serverSettings)
|
|
|
|
if (!audiobookData) {
|
|
|
|
return ScanResult.NOTHING
|
|
|
|
}
|
|
|
|
var hasUpdated = false
|
|
|
|
|
|
|
|
var checkRes = audiobook.checkScanData(audiobookData, version)
|
|
|
|
if (checkRes.updated) hasUpdated = true
|
|
|
|
|
|
|
|
// Sync other files first so that local images are used as cover art
|
|
|
|
// TODO: Cleanup other file sync
|
|
|
|
var allOtherFiles = checkRes.newOtherFileData.concat(audiobook._otherFiles)
|
|
|
|
if (await audiobook.syncOtherFiles(allOtherFiles, this.MetadataPath, this.db.serverSettings.scannerPreferOpfMetadata)) {
|
|
|
|
hasUpdated = true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Scan all audio files
|
|
|
|
if (audiobookData.audioFiles.length) {
|
|
|
|
if (await AudioFileScanner.scanAudioFiles(audiobookData.audioFiles, audiobookData, audiobook, this.db.serverSettings.scannerPreferAudioMetadata)) {
|
|
|
|
hasUpdated = true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Extract embedded cover art if cover is not already in directory
|
|
|
|
if (audiobook.hasEmbeddedCoverArt && !audiobook.cover) {
|
|
|
|
var outputCoverDirs = this.getCoverDirectory(audiobook)
|
|
|
|
var relativeDir = await audiobook.saveEmbeddedCoverArt(outputCoverDirs.fullPath, outputCoverDirs.relPath)
|
|
|
|
if (relativeDir) {
|
|
|
|
Logger.debug(`[Scanner] Saved embedded cover art "${relativeDir}"`)
|
|
|
|
hasUpdated = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!audiobook.audioFilesToInclude.length && !audiobook.ebooks.length) { // Audiobook is invalid
|
|
|
|
audiobook.setInvalid()
|
|
|
|
hasUpdated = true
|
|
|
|
} else if (audiobook.isInvalid) {
|
|
|
|
audiobook.isInvalid = false
|
|
|
|
hasUpdated = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hasUpdated) {
|
|
|
|
this.emitter('audiobook_updated', audiobook.toJSONExpanded())
|
|
|
|
await this.db.updateEntity('audiobook', audiobook)
|
|
|
|
return ScanResult.UPDATED
|
|
|
|
}
|
|
|
|
return ScanResult.UPTODATE
|
|
|
|
}
|
|
|
|
|
2021-11-23 02:58:20 +01:00
|
|
|
async scan(libraryId, options = {}) {
|
2021-11-26 01:39:02 +01:00
|
|
|
if (this.isLibraryScanning(libraryId)) {
|
2021-11-23 02:58:20 +01:00
|
|
|
Logger.error(`[Scanner] Already scanning ${libraryId}`)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var library = this.db.libraries.find(lib => lib.id === libraryId)
|
|
|
|
if (!library) {
|
|
|
|
Logger.error(`[Scanner] Library not found for scan ${libraryId}`)
|
|
|
|
return
|
|
|
|
} else if (!library.folders.length) {
|
|
|
|
Logger.warn(`[Scanner] Library has no folders to scan "${library.name}"`)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var scanOptions = new ScanOptions()
|
|
|
|
scanOptions.setData(options, this.db.serverSettings)
|
|
|
|
|
|
|
|
var libraryScan = new LibraryScan()
|
|
|
|
libraryScan.setData(library, scanOptions)
|
2021-11-26 01:39:02 +01:00
|
|
|
libraryScan.verbose = false
|
|
|
|
this.librariesScanning.push(libraryScan.getScanEmitData)
|
2021-11-25 03:15:50 +01:00
|
|
|
|
|
|
|
this.emitter('scan_start', libraryScan.getScanEmitData)
|
2021-11-23 02:58:20 +01:00
|
|
|
|
|
|
|
Logger.info(`[Scanner] Starting library scan ${libraryScan.id} for ${libraryScan.libraryName}`)
|
|
|
|
|
2021-11-26 01:39:02 +01:00
|
|
|
var canceled = await this.scanLibrary(libraryScan)
|
|
|
|
|
|
|
|
if (canceled) {
|
|
|
|
Logger.info(`[Scanner] Library scan canceled for "${libraryScan.libraryName}"`)
|
|
|
|
delete this.cancelLibraryScan[libraryScan.libraryId]
|
|
|
|
}
|
2021-11-23 02:58:20 +01:00
|
|
|
|
2021-11-25 03:15:50 +01:00
|
|
|
libraryScan.setComplete()
|
2021-11-23 02:58:20 +01:00
|
|
|
|
2021-11-26 01:39:02 +01:00
|
|
|
Logger.info(`[Scanner] Library scan ${libraryScan.id} completed in ${libraryScan.elapsedTimestamp} | ${libraryScan.resultStats}`)
|
2021-11-25 03:15:50 +01:00
|
|
|
this.librariesScanning = this.librariesScanning.filter(ls => ls.id !== library.id)
|
2021-11-26 01:39:02 +01:00
|
|
|
|
|
|
|
if (canceled && !libraryScan.totalResults) {
|
|
|
|
var emitData = libraryScan.getScanEmitData
|
|
|
|
emitData.results = null
|
|
|
|
this.emitter('scan_complete', emitData)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-11-25 03:15:50 +01:00
|
|
|
this.emitter('scan_complete', libraryScan.getScanEmitData)
|
2021-11-26 01:39:02 +01:00
|
|
|
|
|
|
|
if (libraryScan.totalResults) {
|
|
|
|
libraryScan.saveLog(this.ScanLogPath)
|
|
|
|
}
|
2021-11-23 02:58:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
async scanLibrary(libraryScan) {
|
|
|
|
var audiobookDataFound = []
|
2021-11-26 01:39:02 +01:00
|
|
|
|
|
|
|
// Scan each library
|
2021-11-23 02:58:20 +01:00
|
|
|
for (let i = 0; i < libraryScan.folders.length; i++) {
|
|
|
|
var folder = libraryScan.folders[i]
|
|
|
|
var abDataFoundInFolder = await scanRootDir(folder, this.db.serverSettings)
|
2021-11-26 01:39:02 +01:00
|
|
|
libraryScan.addLog(LogLevel.INFO, `${abDataFoundInFolder.length} ab data found in folder "${folder.fullPath}"`)
|
2021-11-23 02:58:20 +01:00
|
|
|
audiobookDataFound = audiobookDataFound.concat(abDataFoundInFolder)
|
|
|
|
}
|
|
|
|
|
2021-11-26 01:39:02 +01:00
|
|
|
if (this.cancelLibraryScan[libraryScan.libraryId]) return true
|
|
|
|
|
2021-11-23 02:58:20 +01:00
|
|
|
// Remove audiobooks with no inode
|
|
|
|
audiobookDataFound = audiobookDataFound.filter(abd => abd.ino)
|
|
|
|
|
|
|
|
var audiobooksInLibrary = this.db.audiobooks.filter(ab => ab.libraryId === libraryScan.libraryId)
|
|
|
|
|
2021-11-26 01:39:02 +01:00
|
|
|
const NumScansPerChunk = 25
|
|
|
|
const audiobooksToUpdateChunks = []
|
|
|
|
const audiobookDataToRescanChunks = []
|
|
|
|
const newAudiobookDataToScanChunks = []
|
2021-11-25 03:15:50 +01:00
|
|
|
var audiobooksToUpdate = []
|
2021-11-26 01:39:02 +01:00
|
|
|
var audiobookDataToRescan = []
|
|
|
|
var newAudiobookDataToScan = []
|
|
|
|
var audiobooksToFindCovers = []
|
2021-11-23 02:58:20 +01:00
|
|
|
|
|
|
|
// Check for existing & removed audiobooks
|
|
|
|
for (let i = 0; i < audiobooksInLibrary.length; i++) {
|
|
|
|
var audiobook = audiobooksInLibrary[i]
|
|
|
|
var dataFound = audiobookDataFound.find(abd => abd.ino === audiobook.ino || comparePaths(abd.path, audiobook.path))
|
|
|
|
if (!dataFound) {
|
2021-11-26 01:39:02 +01:00
|
|
|
libraryScan.addLog(LogLevel.WARN, `Audiobook "${audiobook.title}" is missing`)
|
|
|
|
libraryScan.resultsMissing++
|
2021-11-25 03:15:50 +01:00
|
|
|
audiobook.setMissing()
|
2021-11-23 02:58:20 +01:00
|
|
|
audiobooksToUpdate.push(audiobook)
|
2021-11-26 01:39:02 +01:00
|
|
|
if (audiobooksToUpdate.length === NumScansPerChunk) {
|
|
|
|
audiobooksToUpdateChunks.push(audiobooksToUpdate)
|
|
|
|
audiobooksToUpdate = []
|
|
|
|
}
|
2021-11-23 02:58:20 +01:00
|
|
|
} else {
|
2021-11-26 01:39:02 +01:00
|
|
|
var checkRes = audiobook.checkScanData(dataFound, version)
|
|
|
|
if (checkRes.newAudioFileData.length || checkRes.newOtherFileData.length) { // Audiobook has new files
|
2021-11-23 02:58:20 +01:00
|
|
|
checkRes.audiobook = audiobook
|
2021-11-25 03:15:50 +01:00
|
|
|
checkRes.bookScanData = dataFound
|
2021-11-26 01:39:02 +01:00
|
|
|
audiobookDataToRescan.push(checkRes)
|
|
|
|
if (audiobookDataToRescan.length === NumScansPerChunk) {
|
|
|
|
audiobookDataToRescanChunks.push(audiobookDataToRescan)
|
|
|
|
audiobookDataToRescan = []
|
|
|
|
}
|
|
|
|
} else if (libraryScan.findCovers && audiobook.book.shouldSearchForCover) {
|
|
|
|
libraryScan.resultsUpdated++
|
|
|
|
audiobooksToFindCovers.push(audiobook)
|
2021-11-23 02:58:20 +01:00
|
|
|
audiobooksToUpdate.push(audiobook)
|
2021-11-26 01:39:02 +01:00
|
|
|
if (audiobooksToUpdate.length === NumScansPerChunk) {
|
|
|
|
audiobooksToUpdateChunks.push(audiobooksToUpdate)
|
|
|
|
audiobooksToUpdate = []
|
|
|
|
}
|
|
|
|
} else if (checkRes.updated) { // Updated but no scan required
|
2021-11-25 03:15:50 +01:00
|
|
|
libraryScan.resultsUpdated++
|
2021-11-26 01:39:02 +01:00
|
|
|
audiobooksToUpdate.push(audiobook)
|
|
|
|
if (audiobooksToUpdate.length === NumScansPerChunk) {
|
|
|
|
audiobooksToUpdateChunks.push(audiobooksToUpdate)
|
|
|
|
audiobooksToUpdate = []
|
|
|
|
}
|
2021-11-23 02:58:20 +01:00
|
|
|
}
|
|
|
|
audiobookDataFound = audiobookDataFound.filter(abf => abf.ino !== dataFound.ino)
|
|
|
|
}
|
|
|
|
}
|
2021-11-26 01:39:02 +01:00
|
|
|
if (audiobooksToUpdate.length) audiobooksToUpdateChunks.push(audiobooksToUpdate)
|
|
|
|
if (audiobookDataToRescan.length) audiobookDataToRescanChunks.push(audiobookDataToRescan)
|
2021-11-23 02:58:20 +01:00
|
|
|
|
|
|
|
// Potential NEW Audiobooks
|
|
|
|
for (let i = 0; i < audiobookDataFound.length; i++) {
|
|
|
|
var dataFound = audiobookDataFound[i]
|
|
|
|
var hasEbook = dataFound.otherFiles.find(otherFile => otherFile.filetype === 'ebook')
|
|
|
|
if (!hasEbook && !dataFound.audioFiles.length) {
|
2021-11-26 01:39:02 +01:00
|
|
|
libraryScan.addLog(LogLevel.WARN, `Directory found "${audiobookDataFound.path}" has no ebook or audio files`)
|
2021-11-23 02:58:20 +01:00
|
|
|
} else {
|
2021-11-26 01:39:02 +01:00
|
|
|
newAudiobookDataToScan.push(dataFound)
|
|
|
|
if (newAudiobookDataToScan.length === NumScansPerChunk) {
|
|
|
|
newAudiobookDataToScanChunks.push(newAudiobookDataToScan)
|
|
|
|
newAudiobookDataToScan = []
|
|
|
|
}
|
2021-11-23 02:58:20 +01:00
|
|
|
}
|
|
|
|
}
|
2021-11-26 01:39:02 +01:00
|
|
|
if (newAudiobookDataToScan.length) newAudiobookDataToScanChunks.push(newAudiobookDataToScan)
|
2021-11-23 02:58:20 +01:00
|
|
|
|
2021-11-26 01:39:02 +01:00
|
|
|
// console.log('Num chunks to update', audiobooksToUpdateChunks.length)
|
|
|
|
// console.log('Num chunks to rescan', audiobookDataToRescanChunks.length)
|
|
|
|
// console.log('Num chunks to new scan', newAudiobookDataToScanChunks.length)
|
|
|
|
|
|
|
|
// Audiobooks not requiring a scan but require a search for cover
|
|
|
|
for (let i = 0; i < audiobooksToFindCovers.length; i++) {
|
|
|
|
var audiobook = audiobooksToFindCovers[i]
|
|
|
|
var updatedCover = await this.searchForCover(audiobook, libraryScan)
|
|
|
|
audiobook.book.updateLastCoverSearch(updatedCover)
|
2021-11-23 02:58:20 +01:00
|
|
|
}
|
|
|
|
|
2021-11-26 01:39:02 +01:00
|
|
|
for (let i = 0; i < audiobooksToUpdateChunks.length; i++) {
|
|
|
|
await this.updateAudiobooksChunk(audiobooksToUpdateChunks[i])
|
|
|
|
if (this.cancelLibraryScan[libraryScan.libraryId]) return true
|
|
|
|
// console.log('Update chunk done', i, 'of', audiobooksToUpdateChunks.length)
|
|
|
|
}
|
|
|
|
for (let i = 0; i < audiobookDataToRescanChunks.length; i++) {
|
|
|
|
await this.rescanAudiobookDataChunk(audiobookDataToRescanChunks[i], libraryScan)
|
|
|
|
if (this.cancelLibraryScan[libraryScan.libraryId]) return true
|
|
|
|
// console.log('Rescan chunk done', i, 'of', audiobookDataToRescanChunks.length)
|
|
|
|
}
|
|
|
|
for (let i = 0; i < newAudiobookDataToScanChunks.length; i++) {
|
|
|
|
await this.scanNewAudiobookDataChunk(newAudiobookDataToScanChunks[i], libraryScan)
|
|
|
|
// console.log('New scan chunk done', i, 'of', newAudiobookDataToScanChunks.length)
|
|
|
|
if (this.cancelLibraryScan[libraryScan.libraryId]) return true
|
2021-11-23 02:58:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-26 01:39:02 +01:00
|
|
|
async updateAudiobooksChunk(audiobooksToUpdate) {
|
|
|
|
await this.db.updateEntities('audiobook', audiobooksToUpdate)
|
|
|
|
this.emitter('audiobooks_updated', audiobooksToUpdate.map(ab => ab.toJSONExpanded()))
|
|
|
|
}
|
|
|
|
|
|
|
|
async rescanAudiobookDataChunk(audiobookDataToRescan, libraryScan) {
|
|
|
|
var audiobooksUpdated = await Promise.all(audiobookDataToRescan.map((abd) => {
|
|
|
|
return this.rescanAudiobook(abd, libraryScan)
|
|
|
|
}))
|
|
|
|
audiobooksUpdated = audiobooksUpdated.filter(ab => ab) // Filter out nulls
|
|
|
|
libraryScan.resultsUpdated += audiobooksUpdated.length
|
|
|
|
await this.db.updateEntities('audiobook', audiobooksUpdated)
|
|
|
|
this.emitter('audiobooks_updated', audiobooksUpdated.map(ab => ab.toJSONExpanded()))
|
|
|
|
}
|
|
|
|
|
|
|
|
async scanNewAudiobookDataChunk(newAudiobookDataToScan, libraryScan) {
|
|
|
|
var newAudiobooks = await Promise.all(newAudiobookDataToScan.map((abd) => {
|
|
|
|
return this.scanNewAudiobook(abd, libraryScan.preferAudioMetadata, libraryScan.preferOpfMetadata, libraryScan.findCovers, libraryScan)
|
|
|
|
}))
|
|
|
|
newAudiobooks = newAudiobooks.filter(ab => ab) // Filter out nulls
|
|
|
|
libraryScan.resultsAdded += newAudiobooks.length
|
|
|
|
await this.db.insertEntities('audiobook', newAudiobooks)
|
|
|
|
this.emitter('audiobooks_added', newAudiobooks.map(ab => ab.toJSONExpanded()))
|
|
|
|
}
|
|
|
|
|
2021-11-25 03:15:50 +01:00
|
|
|
async rescanAudiobook(audiobookCheckData, libraryScan) {
|
|
|
|
const { newAudioFileData, newOtherFileData, audiobook, bookScanData } = audiobookCheckData
|
2021-11-26 01:39:02 +01:00
|
|
|
libraryScan.addLog(LogLevel.DEBUG, `Library "${libraryScan.libraryName}" Re-scanning "${audiobook.path}"`)
|
|
|
|
|
|
|
|
// Sync other files first to use local images as cover before extracting audio file cover
|
|
|
|
if (newOtherFileData.length) {
|
|
|
|
// TODO: Cleanup other file sync
|
|
|
|
var allOtherFiles = newOtherFileData.concat(audiobook._otherFiles)
|
|
|
|
await audiobook.syncOtherFiles(allOtherFiles, this.MetadataPath, libraryScan.preferOpfMetadata)
|
|
|
|
}
|
2021-11-25 03:15:50 +01:00
|
|
|
|
2021-11-23 02:58:20 +01:00
|
|
|
if (newAudioFileData.length) {
|
2021-11-26 01:39:02 +01:00
|
|
|
await AudioFileScanner.scanAudioFiles(newAudioFileData, bookScanData, audiobook, libraryScan.preferAudioMetadata, libraryScan)
|
|
|
|
|
|
|
|
// Extract embedded cover art if cover is not already in directory
|
|
|
|
if (audiobook.hasEmbeddedCoverArt && !audiobook.cover) {
|
|
|
|
var outputCoverDirs = this.getCoverDirectory(audiobook)
|
|
|
|
var relativeDir = await audiobook.saveEmbeddedCoverArt(outputCoverDirs.fullPath, outputCoverDirs.relPath)
|
|
|
|
if (relativeDir) {
|
|
|
|
libraryScan.addLog(LogLevel.DEBUG, `Saved embedded cover art "${relativeDir}"`)
|
2021-11-25 03:15:50 +01:00
|
|
|
}
|
|
|
|
}
|
2021-11-23 02:58:20 +01:00
|
|
|
}
|
2021-11-26 01:39:02 +01:00
|
|
|
|
|
|
|
if (!audiobook.audioFilesToInclude.length && !audiobook.ebooks.length) { // Audiobook is invalid
|
|
|
|
audiobook.setInvalid()
|
|
|
|
} else if (audiobook.isInvalid) {
|
|
|
|
audiobook.isInvalid = false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Scan for cover if enabled and has no cover
|
|
|
|
if (audiobook && libraryScan.findCovers && !audiobook.cover && audiobook.book.shouldSearchForCover) {
|
|
|
|
var updatedCover = await this.searchForCover(audiobook, libraryScan)
|
|
|
|
audiobook.book.updateLastCoverSearch(updatedCover)
|
2021-11-23 02:58:20 +01:00
|
|
|
}
|
2021-11-26 01:39:02 +01:00
|
|
|
|
2021-11-25 03:15:50 +01:00
|
|
|
return audiobook
|
|
|
|
}
|
2021-11-23 02:58:20 +01:00
|
|
|
|
2021-11-26 01:39:02 +01:00
|
|
|
async scanNewAudiobook(audiobookData, preferAudioMetadata, preferOpfMetadata, findCovers, libraryScan = null) {
|
|
|
|
if (libraryScan) libraryScan.addLog(LogLevel.DEBUG, `Scanning new book "${audiobookData.path}"`)
|
|
|
|
else Logger.debug(`[Scanner] Scanning new book "${audiobookData.path}"`)
|
|
|
|
|
2021-11-25 03:15:50 +01:00
|
|
|
var audiobook = new Audiobook()
|
|
|
|
audiobook.setData(audiobookData)
|
|
|
|
|
|
|
|
if (audiobookData.audioFiles.length) {
|
2021-11-26 01:39:02 +01:00
|
|
|
await AudioFileScanner.scanAudioFiles(audiobookData.audioFiles, audiobookData, audiobook, preferAudioMetadata, libraryScan)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!audiobook.audioFilesToInclude.length && !audiobook.ebooks.length) {
|
|
|
|
// Audiobook has no ebooks and no valid audio tracks do not continue
|
|
|
|
Logger.warn(`[Scanner] Audiobook has no ebooks and no valid audio tracks "${audiobook.path}"`)
|
|
|
|
return null
|
2021-11-23 02:58:20 +01:00
|
|
|
}
|
|
|
|
|
2021-11-25 03:15:50 +01:00
|
|
|
// Look for desc.txt and reader.txt and update
|
2021-11-26 01:39:02 +01:00
|
|
|
await audiobook.saveDataFromTextFiles(preferOpfMetadata)
|
2021-11-23 02:58:20 +01:00
|
|
|
|
2021-11-25 03:15:50 +01:00
|
|
|
// Extract embedded cover art if cover is not already in directory
|
|
|
|
if (audiobook.hasEmbeddedCoverArt && !audiobook.cover) {
|
|
|
|
var outputCoverDirs = this.getCoverDirectory(audiobook)
|
|
|
|
var relativeDir = await audiobook.saveEmbeddedCoverArt(outputCoverDirs.fullPath, outputCoverDirs.relPath)
|
|
|
|
if (relativeDir) {
|
2021-11-26 01:39:02 +01:00
|
|
|
if (libraryScan) libraryScan.addLog(LogLevel.DEBUG, `Saved embedded cover art "${relativeDir}"`)
|
|
|
|
else Logger.debug(`[Scanner] Saved embedded cover art "${relativeDir}"`)
|
2021-11-25 03:15:50 +01:00
|
|
|
}
|
2021-11-23 02:58:20 +01:00
|
|
|
}
|
2021-11-25 03:15:50 +01:00
|
|
|
|
2021-11-26 01:39:02 +01:00
|
|
|
// Scan for cover if enabled and has no cover
|
|
|
|
if (audiobook && findCovers && !audiobook.cover && audiobook.book.shouldSearchForCover) {
|
|
|
|
var updatedCover = await this.searchForCover(audiobook, libraryScan)
|
|
|
|
audiobook.book.updateLastCoverSearch(updatedCover)
|
|
|
|
}
|
|
|
|
|
2021-11-25 03:15:50 +01:00
|
|
|
return audiobook
|
2021-11-23 02:58:20 +01:00
|
|
|
}
|
2021-11-26 01:39:02 +01:00
|
|
|
|
|
|
|
getFileUpdatesGrouped(fileUpdates) {
|
|
|
|
var folderGroups = {}
|
|
|
|
fileUpdates.forEach((file) => {
|
|
|
|
if (folderGroups[file.folderId]) {
|
|
|
|
folderGroups[file.folderId].fileUpdates.push(file)
|
|
|
|
} else {
|
|
|
|
folderGroups[file.folderId] = {
|
|
|
|
libraryId: file.libraryId,
|
|
|
|
folderId: file.folderId,
|
|
|
|
fileUpdates: [file]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return folderGroups
|
|
|
|
}
|
|
|
|
|
|
|
|
async scanFilesChanged(fileUpdates) {
|
|
|
|
if (!fileUpdates.length) return
|
|
|
|
// files grouped by folder
|
|
|
|
var folderGroups = this.getFileUpdatesGrouped(fileUpdates)
|
|
|
|
|
|
|
|
for (const folderId in folderGroups) {
|
|
|
|
var libraryId = folderGroups[folderId].libraryId
|
|
|
|
var library = this.db.libraries.find(lib => lib.id === libraryId)
|
|
|
|
if (!library) {
|
|
|
|
Logger.error(`[Scanner] Library not found in files changed ${libraryId}`)
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
var folder = library.getFolderById(folderId)
|
|
|
|
if (!folder) {
|
|
|
|
Logger.error(`[Scanner] Folder is not in library in files changed "${folderId}", Library "${library.name}"`)
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
var relFilePaths = folderGroups[folderId].fileUpdates.map(fileUpdate => fileUpdate.relPath)
|
|
|
|
var fileUpdateBookGroup = groupFilesIntoAudiobookPaths(relFilePaths, true)
|
|
|
|
var folderScanResults = await this.scanFolderUpdates(library, folder, fileUpdateBookGroup)
|
|
|
|
Logger.debug(`[Scanner] Folder scan results`, folderScanResults)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async scanFolderUpdates(library, folder, fileUpdateBookGroup) {
|
|
|
|
Logger.debug(`[Scanner] Scanning file update groups in folder "${folder.id}" of library "${library.name}"`)
|
|
|
|
|
|
|
|
var bookGroupingResults = {}
|
|
|
|
for (const bookDir in fileUpdateBookGroup) {
|
|
|
|
var fullPath = Path.posix.join(folder.fullPath.replace(/\\/g, '/'), bookDir)
|
|
|
|
|
|
|
|
// Check if book dir group is already an audiobook or in a subdir of an audiobook
|
|
|
|
var existingAudiobook = this.db.audiobooks.find(ab => fullPath.startsWith(ab.fullPath))
|
|
|
|
if (existingAudiobook) {
|
|
|
|
|
|
|
|
// Is the audiobook exactly - check if was deleted
|
|
|
|
if (existingAudiobook.fullPath === fullPath) {
|
|
|
|
var exists = await fs.pathExists(fullPath)
|
|
|
|
if (!exists) {
|
|
|
|
Logger.info(`[Scanner] Scanning file update group and audiobook was deleted "${existingAudiobook.title}" - marking as missing`)
|
|
|
|
existingAudiobook.setMissing()
|
|
|
|
await this.db.updateAudiobook(existingAudiobook)
|
|
|
|
this.emitter('audiobook_updated', existingAudiobook.toJSONExpanded())
|
|
|
|
|
|
|
|
bookGroupingResults[bookDir] = ScanResult.REMOVED
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Scan audiobook for updates
|
|
|
|
Logger.debug(`[Scanner] Folder update for relative path "${bookDir}" is in audiobook "${existingAudiobook.title}" - scan for updates`)
|
|
|
|
bookGroupingResults[bookDir] = await this.scanAudiobook(folder, existingAudiobook)
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if an audiobook is a subdirectory of this dir
|
|
|
|
var childAudiobook = this.db.audiobooks.find(ab => ab.fullPath.startsWith(fullPath))
|
|
|
|
if (childAudiobook) {
|
|
|
|
Logger.warn(`[Scanner] Files were modified in a parent directory of an audiobook "${childAudiobook.title}" - ignoring`)
|
|
|
|
bookGroupingResults[bookDir] = ScanResult.NOTHING
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
Logger.debug(`[Scanner] Folder update group must be a new book "${bookDir}" in library "${library.name}"`)
|
|
|
|
var newAudiobook = await this.scanPotentialNewAudiobook(folder, fullPath)
|
|
|
|
if (newAudiobook) {
|
|
|
|
await this.db.insertEntity('audiobook', newAudiobook)
|
|
|
|
this.emitter('audiobook_added', newAudiobook.toJSONExpanded())
|
|
|
|
}
|
|
|
|
bookGroupingResults[bookDir] = newAudiobook ? ScanResult.ADDED : ScanResult.NOTHING
|
|
|
|
}
|
|
|
|
|
|
|
|
return bookGroupingResults
|
|
|
|
}
|
|
|
|
|
|
|
|
async scanPotentialNewAudiobook(folder, fullPath) {
|
|
|
|
var audiobookData = await getAudiobookFileData(folder, fullPath, this.db.serverSettings)
|
|
|
|
if (!audiobookData) return null
|
|
|
|
var serverSettings = this.db.serverSettings
|
|
|
|
return this.scanNewAudiobook(audiobookData, serverSettings.scannerPreferAudioMetadata, serverSettings.scannerPreferOpfMetadata, serverSettings.scannerFindCovers)
|
|
|
|
}
|
|
|
|
|
|
|
|
async searchForCover(audiobook, libraryScan = null) {
|
|
|
|
var options = {
|
|
|
|
titleDistance: 2,
|
|
|
|
authorDistance: 2
|
|
|
|
}
|
|
|
|
var results = await this.bookFinder.findCovers('google', audiobook.title, audiobook.authorFL, options)
|
|
|
|
if (results.length) {
|
|
|
|
if (libraryScan) libraryScan.addLog(LogLevel.DEBUG, `Found best cover for "${audiobook.title}"`)
|
|
|
|
else Logger.debug(`[Scanner] Found best cover for "${audiobook.title}"`)
|
|
|
|
|
|
|
|
// If the first cover result fails, attempt to download the second
|
|
|
|
for (let i = 0; i < results.length && i < 2; i++) {
|
|
|
|
|
|
|
|
// Downloads and updates the book cover
|
|
|
|
var result = await this.coverController.downloadCoverFromUrl(audiobook, results[i])
|
|
|
|
|
|
|
|
if (result.error) {
|
|
|
|
Logger.error(`[Scanner] Failed to download cover from url "${results[i]}" | Attempt ${i + 1}`, result.error)
|
|
|
|
} else {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2021-11-23 02:58:20 +01:00
|
|
|
}
|
|
|
|
module.exports = Scanner
|