audiobookshelf/server/scanner/LibraryScan.js

142 lines
3.8 KiB
JavaScript
Raw Normal View History

const Path = require('path')
2022-07-06 02:53:01 +02:00
const fs = require('../libs/fsExtra')
2022-07-07 02:18:27 +02:00
const date = require('../libs/dateAndTime')
const Logger = require('../Logger')
const Folder = require('../objects/Folder')
const { LogLevel } = require('../utils/constants')
const filePerms = require('../utils/filePerms')
const { getId, secondsToTimestamp } = require('../utils/index')
class LibraryScan {
constructor() {
this.id = null
2022-02-16 01:33:33 +01:00
this.type = null
this.libraryId = null
this.libraryName = null
this.libraryMediaType = null
this.folders = null
this.verbose = false
this.scanOptions = null
this.startedAt = null
this.finishedAt = null
this.elapsed = null
this.resultsMissing = 0
this.resultsAdded = 0
this.resultsUpdated = 0
this.logs = []
}
get _scanOptions() { return this.scanOptions || {} }
get forceRescan() { return !!this._scanOptions.forceRescan }
get preferAudioMetadata() { return !!this._scanOptions.preferAudioMetadata }
get preferOpfMetadata() { return !!this._scanOptions.preferOpfMetadata }
get preferOverdriveMediaMarker() { return !!this._scanOptions.preferOverdriveMediaMarker }
get findCovers() { return !!this._scanOptions.findCovers }
get timestamp() {
return (new Date()).toISOString()
}
get resultStats() {
return `${this.resultsAdded} Added | ${this.resultsUpdated} Updated | ${this.resultsMissing} Missing`
}
get elapsedTimestamp() {
return secondsToTimestamp(this.elapsed / 1000)
}
get getScanEmitData() {
return {
id: this.libraryId,
2022-02-16 01:33:33 +01:00
type: this.type,
name: this.libraryName,
results: {
added: this.resultsAdded,
updated: this.resultsUpdated,
missing: this.resultsMissing
}
}
}
get totalResults() {
return this.resultsAdded + this.resultsUpdated + this.resultsMissing
}
get logFilename() {
return date.format(new Date(), 'YYYY-MM-DD') + '_' + this.id + '.txt'
}
toJSON() {
return {
id: this.id,
2022-02-16 01:33:33 +01:00
type: this.type,
libraryId: this.libraryId,
libraryName: this.libraryName,
libraryMediaType: this.libraryMediaType,
folders: this.folders.map(f => f.toJSON()),
2022-02-16 01:33:33 +01:00
scanOptions: this.scanOptions ? this.scanOptions.toJSON() : null,
startedAt: this.startedAt,
finishedAt: this.finishedAt,
elapsed: this.elapsed,
resultsAdded: this.resultsAdded,
resultsUpdated: this.resultsUpdated,
resultsMissing: this.resultsMissing
}
}
2022-02-16 01:33:33 +01:00
setData(library, scanOptions, type = 'scan') {
this.id = getId('lscan')
2022-02-16 01:33:33 +01:00
this.type = type
this.libraryId = library.id
this.libraryName = library.name
this.libraryMediaType = library.mediaType
this.folders = library.folders.map(folder => new Folder(folder.toJSON()))
this.scanOptions = scanOptions
this.startedAt = Date.now()
}
setComplete() {
this.finishedAt = Date.now()
this.elapsed = this.finishedAt - this.startedAt
}
getLogLevelString(level) {
for (const key in LogLevel) {
if (LogLevel[key] === level) {
return key
}
}
return 'UNKNOWN'
}
addLog(level, ...args) {
const logObj = {
timestamp: this.timestamp,
message: args.join(' '),
levelName: this.getLogLevelString(level),
level
}
if (this.verbose) {
Logger.debug(`[LibraryScan] "${this.libraryName}":`, args)
}
this.logs.push(logObj)
}
async saveLog() {
await Logger.logManager.ensureScanLogDir()
const outputPath = Path.join(logDir, this.logFilename)
const logLines = [JSON.stringify(this.toJSON())]
this.logs.forEach(l => {
logLines.push(JSON.stringify(l))
})
await fs.writeFile(outputPath, logLines.join('\n') + '\n')
await filePerms.setDefault(outputPath)
Logger.info(`[LibraryScan] Scan log saved "${outputPath}"`)
}
}
module.exports = LibraryScan