Fix: book id length & check duplicate ids, Change: library to lazy load book cards

This commit is contained in:
advplyr 2021-11-15 20:09:42 -06:00
parent ca6f2c01f6
commit 72f9732b67
18 changed files with 466 additions and 86 deletions

View file

@ -4,7 +4,7 @@ const fs = require('fs-extra')
const date = require('date-and-time')
const Logger = require('./Logger')
const { isObject } = require('./utils/index')
const { isObject, getId } = require('./utils/index')
const audioFileScanner = require('./utils/audioFileScanner')
const BookFinder = require('./BookFinder')
@ -702,7 +702,7 @@ class ApiController {
return res.status(500).send('Username already taken')
}
account.id = (Math.trunc(Math.random() * 1000) + Date.now()).toString(36)
account.id = getId('usr')
account.pash = await this.auth.hashPass(account.password)
delete account.password
account.token = await this.auth.generateAccessToken({ userId: account.id })

View file

@ -3,6 +3,7 @@ const njodb = require("njodb")
const fs = require('fs-extra')
const jwt = require('jsonwebtoken')
const Logger = require('./Logger')
const { version } = require('../package.json')
const Audiobook = require('./objects/Audiobook')
const User = require('./objects/User')
const UserCollection = require('./objects/UserCollection')
@ -36,6 +37,9 @@ class Db {
this.collections = []
this.serverSettings = null
// Stores previous version only if upgraded
this.previousVersion = null
}
getEntityDb(entityName) {
@ -138,6 +142,11 @@ class Db {
var serverSettings = this.settings.find(s => s.id === 'server-settings')
if (serverSettings) {
this.serverSettings = new ServerSettings(serverSettings)
// Check if server was upgraded
if (!this.serverSettings.version || this.serverSettings.version !== version) {
this.previousVersion = this.serverSettings.version || '1.0.0'
}
}
}
})
@ -146,6 +155,12 @@ class Db {
Logger.info(`[DB] ${this.collections.length} Collections Loaded`)
})
await Promise.all([p1, p2, p3, p4, p5])
// Update server version in server settings
if (this.previousVersion) {
this.serverSettings.version = version
await this.updateEntity('settings', this.serverSettings)
}
}
updateAudiobook(audiobook) {

View file

@ -5,6 +5,7 @@ const archiver = require('archiver')
const workerThreads = require('worker_threads')
const Logger = require('./Logger')
const Download = require('./objects/Download')
const { getId } = require('./utils/index')
const { writeConcatFile, writeMetadataFile } = require('./utils/ffmpegHelpers')
const { getFileSize } = require('./utils/fileUtils')
const TAG = 'DownloadManager'
@ -61,7 +62,7 @@ class DownloadManager {
}
async prepareDownload(client, audiobook, options = {}) {
var downloadId = (Math.trunc(Math.random() * 1000) + Date.now()).toString(36)
var downloadId = getId('dl')
var dlpath = Path.join(this.downloadDirPath, downloadId)
Logger.info(`Start Download for ${audiobook.id} - DownloadId: ${downloadId} - ${dlpath}`)

View file

@ -6,7 +6,7 @@ const Logger = require('./Logger')
const { version } = require('../package.json')
const audioFileScanner = require('./utils/audioFileScanner')
const { groupFilesIntoAudiobookPaths, getAudiobookFileData, scanRootDir } = require('./utils/scandir')
const { comparePaths, getIno } = require('./utils/index')
const { comparePaths, getIno, getId } = require('./utils/index')
const { secondsToTimestamp } = require('./utils/fileUtils')
const { ScanResult, CoverDestination } = require('./utils/constants')
@ -752,5 +752,29 @@ class Scanner {
var result = await this.bookFinder.findCovers(query.provider, query.title, query.author || null, options)
res.json(result)
}
async fixDuplicateIds() {
var ids = {}
var audiobooksUpdated = 0
for (let i = 0; i < this.db.audiobooks.length; i++) {
var ab = this.db.audiobooks[i]
if (ids[ab.id]) {
var abCopy = new Audiobook(ab.toJSON())
abCopy.id = getId('ab')
if (abCopy.book.cover) {
abCopy.book.cover = abCopy.book.cover.replace(ab.id, abCopy.id)
}
Logger.warn('Found duplicate ID - updating from', ab.id, 'to', abCopy.id)
await this.db.removeEntity('audiobook', ab.id)
await this.db.insertEntity('audiobook', abCopy)
audiobooksUpdated++
} else {
ids[ab.id] = true
}
}
if (audiobooksUpdated) {
Logger.info(`[Scanner] Updated ${audiobooksUpdated} audiobook IDs`)
}
}
}
module.exports = Scanner

View file

@ -118,6 +118,12 @@ class Server {
await this.backupManager.init()
await this.logManager.init()
// Only fix duplicate ids once on upgrade
if (this.db.previousVersion === '1.0.0') {
Logger.info(`[Server] Running scan for duplicate book IDs`)
await this.scanner.fixDuplicateIds()
}
this.watcher.initWatcher(this.libraries)
this.watcher.on('files', this.filesChanged.bind(this))
}

View file

@ -1,7 +1,7 @@
const Path = require('path')
const fs = require('fs-extra')
const { bytesPretty, elapsedPretty, readTextFile } = require('../utils/fileUtils')
const { comparePaths, getIno } = require('../utils/index')
const { comparePaths, getIno, getId } = require('../utils/index')
const { parseOpfMetadataXML } = require('../utils/parseOpfMetadata')
const { extractCoverArt } = require('../utils/ffmpegHelpers')
const nfoGenerator = require('../utils/nfoGenerator')
@ -317,7 +317,7 @@ class Audiobook {
}
setData(data) {
this.id = (Math.trunc(Math.random() * 1000) + Date.now()).toString(36)
this.id = getId('ab')
this.libraryId = data.libraryId || 'main'
this.folderId = data.folderId || 'audiobooks'
this.ino = data.ino || null

View file

@ -1,3 +1,5 @@
const { getId } = require("../utils")
class Folder {
constructor(folder = null) {
this.id = null
@ -27,7 +29,7 @@ class Folder {
}
setData(data) {
this.id = data.id ? data.id : 'fol' + (Math.trunc(Math.random() * 1000) + Date.now()).toString(36)
this.id = data.id ? data.id : getId('fol')
this.fullPath = data.fullPath
this.libraryId = data.libraryId
this.addedAt = Date.now()

View file

@ -1,4 +1,5 @@
const Folder = require('./Folder')
const { getId } = require('../utils/index')
class Library {
constructor(library = null) {
@ -46,7 +47,7 @@ class Library {
}
setData(data) {
this.id = data.id ? data.id : 'lib' + (Math.trunc(Math.random() * 1000) + Date.now()).toString(36)
this.id = data.id ? data.id : getId('lib')
this.name = data.name
if (data.folder) {
this.folders = [

View file

@ -32,6 +32,7 @@ class ServerSettings {
this.loggerScannerLogsToKeep = 2
this.logLevel = Logger.logLevel
this.version = null
if (settings) {
this.construct(settings)
@ -56,6 +57,7 @@ class ServerSettings {
this.loggerScannerLogsToKeep = settings.loggerScannerLogsToKeep || 2
this.logLevel = settings.logLevel || Logger.logLevel
this.version = settings.version || null
if (this.logLevel !== Logger.logLevel) {
Logger.setLogLevel(this.logLevel)
@ -78,7 +80,8 @@ class ServerSettings {
backupMetadataCovers: this.backupMetadataCovers,
loggerDailyLogsToKeep: this.loggerDailyLogsToKeep,
loggerScannerLogsToKeep: this.loggerScannerLogsToKeep,
logLevel: this.logLevel
logLevel: this.logLevel,
version: this.version
}
}

View file

@ -3,6 +3,7 @@ const EventEmitter = require('events')
const Path = require('path')
const fs = require('fs-extra')
const Logger = require('../Logger')
const { getId } = require('../utils/index')
const { secondsToTimestamp } = require('../utils/fileUtils')
const { writeConcatFile } = require('../utils/ffmpegHelpers')
const hlsPlaylistGenerator = require('../utils/hlsPlaylistGenerator')
@ -13,7 +14,7 @@ class Stream extends EventEmitter {
constructor(streamPath, client, audiobook, transcodeOptions = {}) {
super()
this.id = (Date.now() + Math.trunc(Math.random() * 1000)).toString(36)
this.id = getId('str')
this.client = client
this.audiobook = audiobook

View file

@ -1,4 +1,5 @@
const Logger = require('../Logger')
const { getId } = require('../utils/index')
class UserCollection {
constructor(collection) {
@ -62,7 +63,7 @@ class UserCollection {
if (!data.userId || !data.libraryId || !data.name) {
return false
}
this.id = (Math.trunc(Math.random() * 1000) + Date.now()).toString(36)
this.id = getId('usr')
this.userId = data.userId
this.libraryId = data.libraryId
this.name = data.name

View file

@ -1,5 +1,6 @@
const Logger = require('../Logger')
const date = require('date-and-time')
const { getId } = require('../utils/index')
class UserListeningSession {
constructor(session) {
@ -58,7 +59,7 @@ class UserListeningSession {
}
setData(audiobook, user) {
this.id = 'ls_' + (Math.trunc(Math.random() * 1000) + Date.now()).toString(36)
this.id = getId('ls')
this.userId = user.id
this.audiobookId = audiobook.id
this.audiobookTitle = audiobook.title || ''

View file

@ -58,3 +58,9 @@ const xmlToJSON = (xml) => {
})
}
module.exports.xmlToJSON = xmlToJSON
module.exports.getId = (prepend = '') => {
var _id = Math.random().toString(36).substring(2, 8) + Math.random().toString(36).substring(2, 8) + Math.random().toString(36).substring(2, 8)
if (prepend) return prepend + '_' + _id
return _id
}