2021-11-26 01:39:02 +01:00
|
|
|
const Path = require('path')
|
|
|
|
const fs = require('fs-extra')
|
|
|
|
const date = require('date-and-time')
|
2021-11-23 02:58:20 +01:00
|
|
|
|
2021-11-26 01:39:02 +01:00
|
|
|
const Logger = require('../Logger')
|
|
|
|
const Folder = require('../objects/Folder')
|
|
|
|
const { LogLevel } = require('../utils/constants')
|
2021-11-25 03:15:50 +01:00
|
|
|
const { getId, secondsToTimestamp } = require('../utils/index')
|
2021-11-23 02:58:20 +01:00
|
|
|
|
|
|
|
class LibraryScan {
|
|
|
|
constructor() {
|
|
|
|
this.id = null
|
2022-02-16 01:33:33 +01:00
|
|
|
this.type = null
|
2021-11-23 02:58:20 +01:00
|
|
|
this.libraryId = null
|
|
|
|
this.libraryName = null
|
2022-03-13 00:45:32 +01:00
|
|
|
this.libraryMediaType = null
|
2021-11-23 02:58:20 +01:00
|
|
|
this.folders = null
|
2021-11-26 01:39:02 +01:00
|
|
|
this.verbose = false
|
2021-11-23 02:58:20 +01:00
|
|
|
|
|
|
|
this.scanOptions = null
|
|
|
|
|
|
|
|
this.startedAt = null
|
|
|
|
this.finishedAt = null
|
2021-11-25 03:15:50 +01:00
|
|
|
this.elapsed = null
|
2021-11-23 02:58:20 +01:00
|
|
|
|
2021-11-25 03:15:50 +01:00
|
|
|
this.resultsMissing = 0
|
|
|
|
this.resultsAdded = 0
|
|
|
|
this.resultsUpdated = 0
|
2021-11-26 01:39:02 +01:00
|
|
|
|
|
|
|
this.logs = []
|
2021-11-23 02:58:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
get _scanOptions() { return this.scanOptions || {} }
|
|
|
|
get forceRescan() { return !!this._scanOptions.forceRescan }
|
2021-11-26 01:39:02 +01:00
|
|
|
get preferAudioMetadata() { return !!this._scanOptions.preferAudioMetadata }
|
|
|
|
get preferOpfMetadata() { return !!this._scanOptions.preferOpfMetadata }
|
2022-06-12 10:54:58 +02:00
|
|
|
get preferOverdriveMediaMarker() { return !!this._scanOptions.preferOverdriveMediaMarker }
|
2021-11-26 01:39:02 +01:00
|
|
|
get findCovers() { return !!this._scanOptions.findCovers }
|
|
|
|
get timestamp() {
|
|
|
|
return (new Date()).toISOString()
|
|
|
|
}
|
2021-11-23 02:58:20 +01:00
|
|
|
|
2021-11-25 03:15:50 +01:00
|
|
|
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,
|
2021-11-25 03:15:50 +01:00
|
|
|
name: this.libraryName,
|
|
|
|
results: {
|
|
|
|
added: this.resultsAdded,
|
|
|
|
updated: this.resultsUpdated,
|
|
|
|
missing: this.resultsMissing
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-11-26 01:39:02 +01:00
|
|
|
get totalResults() {
|
|
|
|
return this.resultsAdded + this.resultsUpdated + this.resultsMissing
|
|
|
|
}
|
|
|
|
get getLogFilename() {
|
|
|
|
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,
|
2021-11-26 01:39:02 +01:00
|
|
|
libraryId: this.libraryId,
|
|
|
|
libraryName: this.libraryName,
|
2022-03-13 00:45:32 +01:00
|
|
|
libraryMediaType: this.libraryMediaType,
|
2021-11-26 01:39:02 +01:00
|
|
|
folders: this.folders.map(f => f.toJSON()),
|
2022-02-16 01:33:33 +01:00
|
|
|
scanOptions: this.scanOptions ? this.scanOptions.toJSON() : null,
|
2021-11-26 01:39:02 +01:00
|
|
|
startedAt: this.startedAt,
|
|
|
|
finishedAt: this.finishedAt,
|
|
|
|
elapsed: this.elapsed,
|
|
|
|
resultsAdded: this.resultsAdded,
|
|
|
|
resultsUpdated: this.resultsUpdated,
|
|
|
|
resultsMissing: this.resultsMissing
|
|
|
|
}
|
|
|
|
}
|
2021-11-25 03:15:50 +01:00
|
|
|
|
2022-02-16 01:33:33 +01:00
|
|
|
setData(library, scanOptions, type = 'scan') {
|
2021-11-23 02:58:20 +01:00
|
|
|
this.id = getId('lscan')
|
2022-02-16 01:33:33 +01:00
|
|
|
this.type = type
|
2021-11-23 02:58:20 +01:00
|
|
|
this.libraryId = library.id
|
|
|
|
this.libraryName = library.name
|
2022-03-13 00:45:32 +01:00
|
|
|
this.libraryMediaType = library.mediaType
|
2021-11-25 03:15:50 +01:00
|
|
|
this.folders = library.folders.map(folder => new Folder(folder.toJSON()))
|
2021-11-23 02:58:20 +01:00
|
|
|
|
|
|
|
this.scanOptions = scanOptions
|
|
|
|
|
|
|
|
this.startedAt = Date.now()
|
|
|
|
}
|
2021-11-25 03:15:50 +01:00
|
|
|
|
|
|
|
setComplete() {
|
|
|
|
this.finishedAt = Date.now()
|
|
|
|
this.elapsed = this.finishedAt - this.startedAt
|
|
|
|
}
|
2021-11-26 01:39:02 +01:00
|
|
|
|
|
|
|
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(logDir) {
|
|
|
|
await fs.ensureDir(logDir)
|
|
|
|
var outputPath = Path.join(logDir, this.getLogFilename)
|
|
|
|
var logLines = [JSON.stringify(this.toJSON())]
|
|
|
|
this.logs.forEach(l => {
|
|
|
|
logLines.push(JSON.stringify(l))
|
|
|
|
})
|
|
|
|
await fs.writeFile(outputPath, logLines.join('\n') + '\n')
|
|
|
|
Logger.info(`[LibraryScan] Scan log saved "${outputPath}"`)
|
|
|
|
}
|
2021-11-23 02:58:20 +01:00
|
|
|
}
|
|
|
|
module.exports = LibraryScan
|