2021-08-24 14:15:56 +02:00
|
|
|
const Path = require('path')
|
2021-09-04 21:17:26 +02:00
|
|
|
const { bytesPretty, elapsedPretty } = require('../utils/fileUtils')
|
|
|
|
const { comparePaths, getIno } = require('../utils/index')
|
2021-09-29 17:16:38 +02:00
|
|
|
const nfoGenerator = require('../utils/nfoGenerator')
|
2021-09-04 21:17:26 +02:00
|
|
|
const Logger = require('../Logger')
|
2021-08-18 00:01:11 +02:00
|
|
|
const Book = require('./Book')
|
|
|
|
const AudioTrack = require('./AudioTrack')
|
2021-08-26 00:36:54 +02:00
|
|
|
const AudioFile = require('./AudioFile')
|
|
|
|
const AudiobookFile = require('./AudiobookFile')
|
2021-08-18 00:01:11 +02:00
|
|
|
|
|
|
|
class Audiobook {
|
|
|
|
constructor(audiobook = null) {
|
|
|
|
this.id = null
|
2021-08-26 00:36:54 +02:00
|
|
|
this.ino = null // Inode
|
|
|
|
|
2021-08-18 00:01:11 +02:00
|
|
|
this.path = null
|
|
|
|
this.fullPath = null
|
2021-08-26 00:36:54 +02:00
|
|
|
|
2021-08-18 00:01:11 +02:00
|
|
|
this.addedAt = null
|
2021-08-24 14:15:56 +02:00
|
|
|
this.lastUpdate = null
|
2021-08-18 00:01:11 +02:00
|
|
|
|
|
|
|
this.tracks = []
|
|
|
|
this.missingParts = []
|
|
|
|
|
|
|
|
this.audioFiles = []
|
|
|
|
this.otherFiles = []
|
|
|
|
|
|
|
|
this.tags = []
|
|
|
|
this.book = null
|
2021-09-08 16:15:54 +02:00
|
|
|
this.chapters = []
|
2021-08-18 00:01:11 +02:00
|
|
|
|
2021-09-18 01:40:30 +02:00
|
|
|
// Audiobook was scanned and not found
|
|
|
|
this.isMissing = false
|
|
|
|
|
2021-08-18 00:01:11 +02:00
|
|
|
if (audiobook) {
|
|
|
|
this.construct(audiobook)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
construct(audiobook) {
|
|
|
|
this.id = audiobook.id
|
2021-08-26 00:36:54 +02:00
|
|
|
this.ino = audiobook.ino || null
|
|
|
|
|
2021-08-18 00:01:11 +02:00
|
|
|
this.path = audiobook.path
|
|
|
|
this.fullPath = audiobook.fullPath
|
|
|
|
this.addedAt = audiobook.addedAt
|
2021-08-24 14:15:56 +02:00
|
|
|
this.lastUpdate = audiobook.lastUpdate || this.addedAt
|
2021-08-18 00:01:11 +02:00
|
|
|
|
2021-08-26 00:36:54 +02:00
|
|
|
this.tracks = audiobook.tracks.map(track => new AudioTrack(track))
|
2021-08-18 00:01:11 +02:00
|
|
|
this.missingParts = audiobook.missingParts
|
|
|
|
|
2021-08-26 00:36:54 +02:00
|
|
|
this.audioFiles = audiobook.audioFiles.map(file => new AudioFile(file))
|
|
|
|
this.otherFiles = audiobook.otherFiles.map(file => new AudiobookFile(file))
|
2021-08-18 00:01:11 +02:00
|
|
|
|
|
|
|
this.tags = audiobook.tags
|
|
|
|
if (audiobook.book) {
|
|
|
|
this.book = new Book(audiobook.book)
|
|
|
|
}
|
2021-09-08 16:15:54 +02:00
|
|
|
if (audiobook.chapters) {
|
|
|
|
this.chapters = audiobook.chapters.map(c => ({ ...c }))
|
|
|
|
}
|
2021-09-18 01:40:30 +02:00
|
|
|
|
|
|
|
this.isMissing = !!audiobook.isMissing
|
2021-08-18 00:01:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
get title() {
|
|
|
|
return this.book ? this.book.title : 'No Title'
|
|
|
|
}
|
|
|
|
|
|
|
|
get author() {
|
|
|
|
return this.book ? this.book.author : 'Unknown'
|
|
|
|
}
|
|
|
|
|
2021-09-05 01:02:42 +02:00
|
|
|
get cover() {
|
|
|
|
return this.book ? this.book.cover : ''
|
|
|
|
}
|
|
|
|
|
2021-08-25 03:24:40 +02:00
|
|
|
get authorLF() {
|
|
|
|
return this.book ? this.book.authorLF : null
|
|
|
|
}
|
|
|
|
|
2021-08-20 00:29:36 +02:00
|
|
|
get genres() {
|
|
|
|
return this.book ? this.book.genres || [] : []
|
|
|
|
}
|
|
|
|
|
2021-08-18 00:01:11 +02:00
|
|
|
get totalDuration() {
|
|
|
|
var total = 0
|
|
|
|
this.tracks.forEach((track) => total += track.duration)
|
|
|
|
return total
|
|
|
|
}
|
|
|
|
|
|
|
|
get totalSize() {
|
|
|
|
var total = 0
|
|
|
|
this.tracks.forEach((track) => total += track.size)
|
|
|
|
return total
|
|
|
|
}
|
|
|
|
|
|
|
|
get sizePretty() {
|
|
|
|
return bytesPretty(this.totalSize)
|
|
|
|
}
|
|
|
|
|
|
|
|
get durationPretty() {
|
|
|
|
return elapsedPretty(this.totalDuration)
|
|
|
|
}
|
|
|
|
|
2021-08-26 14:09:23 +02:00
|
|
|
get invalidParts() {
|
|
|
|
return (this.audioFiles || []).filter(af => af.invalid).map(af => ({ filename: af.filename, error: af.error || 'Unknown Error' }))
|
|
|
|
}
|
|
|
|
|
2021-09-27 13:52:21 +02:00
|
|
|
get ebooks() {
|
|
|
|
return this.otherFiles.filter(file => file.filetype === 'ebook')
|
|
|
|
}
|
|
|
|
|
|
|
|
get hasMissingIno() {
|
|
|
|
return !this.ino || (this.audioFiles || []).find(abf => !abf.ino) || (this.otherFiles || []).find(f => !f.ino) || (this.tracks || []).find(t => !t.ino)
|
|
|
|
}
|
|
|
|
|
2021-08-18 00:01:11 +02:00
|
|
|
bookToJSON() {
|
|
|
|
return this.book ? this.book.toJSON() : null
|
|
|
|
}
|
|
|
|
|
|
|
|
tracksToJSON() {
|
|
|
|
if (!this.tracks || !this.tracks.length) return []
|
|
|
|
return this.tracks.map(t => t.toJSON())
|
|
|
|
}
|
|
|
|
|
|
|
|
toJSON() {
|
|
|
|
return {
|
|
|
|
id: this.id,
|
2021-08-26 00:36:54 +02:00
|
|
|
ino: this.ino,
|
2021-08-18 00:01:11 +02:00
|
|
|
title: this.title,
|
|
|
|
author: this.author,
|
|
|
|
cover: this.cover,
|
|
|
|
path: this.path,
|
|
|
|
fullPath: this.fullPath,
|
|
|
|
addedAt: this.addedAt,
|
2021-08-24 14:15:56 +02:00
|
|
|
lastUpdate: this.lastUpdate,
|
2021-08-18 00:01:11 +02:00
|
|
|
missingParts: this.missingParts,
|
|
|
|
tags: this.tags,
|
|
|
|
book: this.bookToJSON(),
|
|
|
|
tracks: this.tracksToJSON(),
|
2021-08-26 00:36:54 +02:00
|
|
|
audioFiles: (this.audioFiles || []).map(audioFile => audioFile.toJSON()),
|
2021-09-08 16:15:54 +02:00
|
|
|
otherFiles: (this.otherFiles || []).map(otherFile => otherFile.toJSON()),
|
2021-09-18 01:40:30 +02:00
|
|
|
chapters: this.chapters || [],
|
|
|
|
isMissing: !!this.isMissing
|
2021-08-18 00:01:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
toJSONMinified() {
|
|
|
|
return {
|
|
|
|
id: this.id,
|
2021-08-26 00:36:54 +02:00
|
|
|
ino: this.ino,
|
2021-08-18 00:01:11 +02:00
|
|
|
book: this.bookToJSON(),
|
|
|
|
tags: this.tags,
|
|
|
|
path: this.path,
|
|
|
|
fullPath: this.fullPath,
|
|
|
|
addedAt: this.addedAt,
|
2021-08-24 14:15:56 +02:00
|
|
|
lastUpdate: this.lastUpdate,
|
2021-08-18 00:01:11 +02:00
|
|
|
duration: this.totalDuration,
|
|
|
|
size: this.totalSize,
|
|
|
|
hasBookMatch: !!this.book,
|
|
|
|
hasMissingParts: this.missingParts ? this.missingParts.length : 0,
|
|
|
|
hasInvalidParts: this.invalidParts ? this.invalidParts.length : 0,
|
2021-09-27 13:52:21 +02:00
|
|
|
numEbooks: this.ebooks.length,
|
2021-09-08 16:15:54 +02:00
|
|
|
numTracks: this.tracks.length,
|
2021-09-18 01:40:30 +02:00
|
|
|
chapters: this.chapters || [],
|
|
|
|
isMissing: !!this.isMissing
|
2021-08-18 00:01:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
toJSONExpanded() {
|
|
|
|
return {
|
|
|
|
id: this.id,
|
|
|
|
path: this.path,
|
|
|
|
fullPath: this.fullPath,
|
|
|
|
addedAt: this.addedAt,
|
2021-08-24 14:15:56 +02:00
|
|
|
lastUpdate: this.lastUpdate,
|
2021-08-18 00:01:11 +02:00
|
|
|
duration: this.totalDuration,
|
|
|
|
durationPretty: this.durationPretty,
|
|
|
|
size: this.totalSize,
|
|
|
|
sizePretty: this.sizePretty,
|
|
|
|
missingParts: this.missingParts,
|
|
|
|
invalidParts: this.invalidParts,
|
2021-08-26 00:36:54 +02:00
|
|
|
audioFiles: (this.audioFiles || []).map(audioFile => audioFile.toJSON()),
|
|
|
|
otherFiles: (this.otherFiles || []).map(otherFile => otherFile.toJSON()),
|
2021-09-27 13:52:21 +02:00
|
|
|
ebooks: (this.ebooks || []).map(ebook => ebook.toJSON()),
|
2021-08-18 00:01:11 +02:00
|
|
|
tags: this.tags,
|
|
|
|
book: this.bookToJSON(),
|
2021-09-08 16:15:54 +02:00
|
|
|
tracks: this.tracksToJSON(),
|
2021-09-18 01:40:30 +02:00
|
|
|
chapters: this.chapters || [],
|
|
|
|
isMissing: !!this.isMissing
|
2021-08-18 00:01:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-24 14:50:36 +02:00
|
|
|
// Scanner had a bug that was saving a file path as the audiobook path.
|
|
|
|
// audiobook path should be a directory.
|
|
|
|
// fixing this before a scan prevents audiobooks being removed and re-added
|
|
|
|
fixRelativePath(abRootPath) {
|
|
|
|
var pathExt = Path.extname(this.path)
|
|
|
|
if (pathExt) {
|
|
|
|
this.path = Path.dirname(this.path)
|
|
|
|
this.fullPath = Path.join(abRootPath, this.path)
|
|
|
|
Logger.warn('Audiobook path has extname', pathExt, 'fixed path:', this.path)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-09-27 13:52:21 +02:00
|
|
|
// Originally files did not store the inode value
|
|
|
|
// this function checks all files and sets the inode
|
2021-08-26 00:36:54 +02:00
|
|
|
async checkUpdateInos() {
|
|
|
|
var hasUpdates = false
|
|
|
|
if (!this.ino) {
|
|
|
|
this.ino = await getIno(this.fullPath)
|
|
|
|
hasUpdates = true
|
|
|
|
}
|
|
|
|
for (let i = 0; i < this.audioFiles.length; i++) {
|
|
|
|
var af = this.audioFiles[i]
|
2021-09-27 13:52:21 +02:00
|
|
|
var at = this.tracks.find(t => t.ino === af.ino)
|
|
|
|
if (!at) {
|
|
|
|
at = this.tracks.find(t => comparePaths(t.path, af.path))
|
|
|
|
}
|
2021-08-26 00:36:54 +02:00
|
|
|
if (!af.ino || af.ino === this.ino) {
|
|
|
|
af.ino = await getIno(af.fullPath)
|
|
|
|
if (!af.ino) {
|
|
|
|
Logger.error('[Audiobook] checkUpdateInos: Failed to set ino for audio file', af.fullPath)
|
|
|
|
} else {
|
2021-09-27 13:52:21 +02:00
|
|
|
Logger.debug(`[Audiobook] Set INO For audio file ${af.path}`)
|
|
|
|
if (at) at.ino = af.ino
|
|
|
|
}
|
|
|
|
hasUpdates = true
|
|
|
|
} else if (at && at.ino !== af.ino) {
|
|
|
|
at.ino = af.ino
|
|
|
|
hasUpdates = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (let i = 0; i < this.tracks.length; i++) {
|
|
|
|
var at = this.tracks[i]
|
|
|
|
if (!at.ino) {
|
|
|
|
Logger.debug(`[Audiobook] Track ${at.filename} still does not have ino`)
|
|
|
|
var atino = await getIno(at.fullPath)
|
|
|
|
var af = this.audioFiles.find(_af => _af.ino === atino)
|
|
|
|
if (!af) {
|
|
|
|
Logger.debug(`[Audiobook] Track ${at.filename} no matching audio file with ino ${atino}`)
|
|
|
|
af = this.audioFiles.find(_af => _af.filename === at.filename)
|
|
|
|
if (!af) {
|
|
|
|
Logger.debug(`[Audiobook] Track ${at.filename} no matching audio file with filename`)
|
|
|
|
} else {
|
|
|
|
Logger.debug(`[Audiobook] Track ${at.filename} found matching filename but mismatch ino ${atino}/${af.ino}`)
|
|
|
|
// at.ino = af.ino
|
|
|
|
// at.path = af.path
|
|
|
|
// at.fullPath = af.fullPath
|
|
|
|
// hasUpdates = true
|
2021-08-26 00:36:54 +02:00
|
|
|
}
|
2021-09-27 13:52:21 +02:00
|
|
|
} else {
|
|
|
|
Logger.debug(`[Audiobook] Track ${at.filename} found audio file with matching ino ${at.path}/${af.path}`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (let i = 0; i < this.otherFiles.length; i++) {
|
|
|
|
var file = this.otherFiles[i]
|
|
|
|
if (!file.ino || file.ino === this.ino) {
|
|
|
|
file.ino = await getIno(file.fullPath)
|
|
|
|
if (!file.ino) {
|
|
|
|
Logger.error('[Audiobook] checkUpdateInos: Failed to set ino for other file', file.fullPath)
|
|
|
|
} else {
|
|
|
|
Logger.debug(`[Audiobook] Set INO For other file ${file.path}`)
|
2021-08-26 00:36:54 +02:00
|
|
|
}
|
|
|
|
hasUpdates = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return hasUpdates
|
|
|
|
}
|
|
|
|
|
2021-08-18 00:01:11 +02:00
|
|
|
setData(data) {
|
|
|
|
this.id = (Math.trunc(Math.random() * 1000) + Date.now()).toString(36)
|
2021-08-26 00:36:54 +02:00
|
|
|
this.ino = data.ino || null
|
|
|
|
|
2021-08-18 00:01:11 +02:00
|
|
|
this.path = data.path
|
|
|
|
this.fullPath = data.fullPath
|
|
|
|
this.addedAt = Date.now()
|
2021-08-24 14:15:56 +02:00
|
|
|
this.lastUpdate = this.addedAt
|
2021-08-18 00:01:11 +02:00
|
|
|
|
2021-08-26 00:36:54 +02:00
|
|
|
if (data.otherFiles) {
|
|
|
|
data.otherFiles.forEach((file) => {
|
|
|
|
this.addOtherFile(file)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-08-18 00:01:11 +02:00
|
|
|
this.setBook(data)
|
|
|
|
}
|
|
|
|
|
|
|
|
setBook(data) {
|
|
|
|
this.book = new Book()
|
|
|
|
this.book.setData(data)
|
|
|
|
}
|
|
|
|
|
|
|
|
addTrack(trackData) {
|
|
|
|
var track = new AudioTrack()
|
|
|
|
track.setData(trackData)
|
|
|
|
this.tracks.push(track)
|
|
|
|
return track
|
|
|
|
}
|
|
|
|
|
2021-08-26 00:36:54 +02:00
|
|
|
addAudioFile(audioFileData) {
|
|
|
|
var audioFile = new AudioFile()
|
|
|
|
audioFile.setData(audioFileData)
|
|
|
|
this.audioFiles.push(audioFile)
|
|
|
|
return audioFile
|
|
|
|
}
|
|
|
|
|
|
|
|
addOtherFile(fileData) {
|
|
|
|
var file = new AudiobookFile()
|
|
|
|
file.setData(fileData)
|
|
|
|
this.otherFiles.push(file)
|
|
|
|
return file
|
|
|
|
}
|
|
|
|
|
2021-08-18 00:01:11 +02:00
|
|
|
update(payload) {
|
|
|
|
var hasUpdates = false
|
|
|
|
|
|
|
|
if (payload.tags && payload.tags.join(',') !== this.tags.join(',')) {
|
|
|
|
this.tags = payload.tags
|
|
|
|
hasUpdates = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if (payload.book) {
|
|
|
|
if (!this.book) {
|
|
|
|
this.setBook(payload.book)
|
|
|
|
hasUpdates = true
|
|
|
|
} else if (this.book.update(payload.book)) {
|
|
|
|
hasUpdates = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-24 14:15:56 +02:00
|
|
|
if (hasUpdates) {
|
|
|
|
this.lastUpdate = Date.now()
|
|
|
|
}
|
|
|
|
|
2021-08-18 00:01:11 +02:00
|
|
|
return hasUpdates
|
|
|
|
}
|
|
|
|
|
2021-09-22 03:57:33 +02:00
|
|
|
// Cover Url may be the same, this ensures the lastUpdate is updated
|
|
|
|
updateBookCover(cover) {
|
|
|
|
if (!this.book) return false
|
|
|
|
return this.book.updateCover(cover)
|
|
|
|
}
|
|
|
|
|
2021-09-05 01:02:42 +02:00
|
|
|
updateAudioTracks(orderedFileData) {
|
2021-08-18 00:01:11 +02:00
|
|
|
var index = 1
|
2021-09-05 01:02:42 +02:00
|
|
|
this.audioFiles = orderedFileData.map((fileData) => {
|
|
|
|
var audioFile = this.audioFiles.find(af => af.ino === fileData.ino)
|
|
|
|
audioFile.manuallyVerified = true
|
|
|
|
audioFile.invalid = false
|
|
|
|
audioFile.error = null
|
|
|
|
if (fileData.exclude !== undefined) {
|
|
|
|
audioFile.exclude = !!fileData.exclude
|
|
|
|
}
|
|
|
|
if (audioFile.exclude) {
|
|
|
|
audioFile.index = -1
|
|
|
|
} else {
|
|
|
|
audioFile.index = index++
|
|
|
|
}
|
|
|
|
return audioFile
|
2021-08-18 00:01:11 +02:00
|
|
|
})
|
2021-09-05 01:02:42 +02:00
|
|
|
|
|
|
|
this.audioFiles.sort((a, b) => a.index - b.index)
|
|
|
|
|
2021-08-18 00:01:11 +02:00
|
|
|
this.tracks = []
|
|
|
|
this.missingParts = []
|
|
|
|
this.audioFiles.forEach((file) => {
|
2021-09-05 01:02:42 +02:00
|
|
|
if (!file.exclude) {
|
|
|
|
this.addTrack(file)
|
|
|
|
}
|
2021-08-18 00:01:11 +02:00
|
|
|
})
|
2021-08-24 14:15:56 +02:00
|
|
|
this.lastUpdate = Date.now()
|
|
|
|
}
|
|
|
|
|
|
|
|
removeAudioFile(audioFile) {
|
2021-08-26 00:36:54 +02:00
|
|
|
this.tracks = this.tracks.filter(t => t.ino !== audioFile.ino)
|
|
|
|
this.audioFiles = this.audioFiles.filter(f => f.ino !== audioFile.ino)
|
2021-08-24 14:15:56 +02:00
|
|
|
}
|
|
|
|
|
2021-09-27 13:52:21 +02:00
|
|
|
removeAudioTrack(track) {
|
|
|
|
this.tracks = this.tracks.filter(t => t.ino !== track.ino)
|
|
|
|
this.audioFiles = this.audioFiles.filter(f => f.ino !== track.ino)
|
|
|
|
}
|
|
|
|
|
2021-08-24 14:15:56 +02:00
|
|
|
checkUpdateMissingParts() {
|
2021-08-26 00:36:54 +02:00
|
|
|
var currMissingParts = (this.missingParts || []).join(',') || ''
|
2021-08-24 14:15:56 +02:00
|
|
|
|
|
|
|
var current_index = 1
|
|
|
|
var missingParts = []
|
|
|
|
for (let i = 0; i < this.tracks.length; i++) {
|
|
|
|
var _track = this.tracks[i]
|
|
|
|
if (_track.index > current_index) {
|
|
|
|
var num_parts_missing = _track.index - current_index
|
|
|
|
for (let x = 0; x < num_parts_missing; x++) {
|
|
|
|
missingParts.push(current_index + x)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
current_index = _track.index + 1
|
|
|
|
}
|
|
|
|
|
|
|
|
this.missingParts = missingParts
|
|
|
|
|
2021-08-26 00:36:54 +02:00
|
|
|
var newMissingParts = (this.missingParts || []).join(',') || ''
|
|
|
|
var wasUpdated = newMissingParts !== currMissingParts
|
2021-08-24 14:15:56 +02:00
|
|
|
if (wasUpdated && this.missingParts.length) {
|
|
|
|
Logger.info(`[Audiobook] "${this.title}" has ${missingParts.length} missing parts`)
|
|
|
|
}
|
|
|
|
|
|
|
|
return wasUpdated
|
|
|
|
}
|
|
|
|
|
|
|
|
// On scan check other files found with other files saved
|
|
|
|
syncOtherFiles(newOtherFiles) {
|
|
|
|
var currOtherFileNum = this.otherFiles.length
|
|
|
|
|
|
|
|
var newOtherFilePaths = newOtherFiles.map(f => f.path)
|
|
|
|
this.otherFiles = this.otherFiles.filter(f => newOtherFilePaths.includes(f.path))
|
2021-08-26 00:36:54 +02:00
|
|
|
|
2021-09-27 13:52:21 +02:00
|
|
|
// TODO: Should use inode
|
2021-08-24 14:15:56 +02:00
|
|
|
newOtherFiles.forEach((file) => {
|
|
|
|
var existingOtherFile = this.otherFiles.find(f => f.path === file.path)
|
|
|
|
if (!existingOtherFile) {
|
2021-08-26 00:36:54 +02:00
|
|
|
Logger.debug(`[Audiobook] New other file found on sync ${file.filename}/${file.filetype} | "${this.title}"`)
|
|
|
|
this.addOtherFile(file)
|
2021-08-24 14:15:56 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
var hasUpdates = currOtherFileNum !== this.otherFiles.length
|
|
|
|
|
2021-08-26 00:36:54 +02:00
|
|
|
// Check if cover was a local image and that it still exists
|
2021-08-24 14:15:56 +02:00
|
|
|
var imageFiles = this.otherFiles.filter(f => f.filetype === 'image')
|
|
|
|
if (this.book.cover && this.book.cover.substr(1).startsWith('local')) {
|
|
|
|
var coverStillExists = imageFiles.find(f => comparePaths(f.path, this.book.cover.substr('/local/'.length)))
|
|
|
|
if (!coverStillExists) {
|
|
|
|
Logger.info(`[Audiobook] Local cover was removed | "${this.title}"`)
|
|
|
|
this.book.cover = null
|
|
|
|
hasUpdates = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-26 00:36:54 +02:00
|
|
|
// If no cover set and image file exists then use it
|
2021-08-24 14:15:56 +02:00
|
|
|
if (!this.book.cover && imageFiles.length) {
|
|
|
|
this.book.cover = Path.join('/local', imageFiles[0].path)
|
|
|
|
Logger.info(`[Audiobook] Local cover was set | "${this.title}"`)
|
|
|
|
hasUpdates = true
|
|
|
|
}
|
|
|
|
return hasUpdates
|
2021-08-18 00:01:11 +02:00
|
|
|
}
|
2021-08-21 23:23:35 +02:00
|
|
|
|
2021-08-26 00:36:54 +02:00
|
|
|
syncAudioFile(audioFile, fileScanData) {
|
|
|
|
var hasUpdates = audioFile.syncFile(fileScanData)
|
2021-08-26 02:32:17 +02:00
|
|
|
var track = this.tracks.find(t => t.ino === audioFile.ino)
|
|
|
|
if (track && track.syncFile(fileScanData)) {
|
|
|
|
hasUpdates = true
|
2021-08-26 00:36:54 +02:00
|
|
|
}
|
|
|
|
return hasUpdates
|
|
|
|
}
|
|
|
|
|
|
|
|
syncPaths(audiobookData) {
|
|
|
|
var hasUpdates = false
|
|
|
|
var keysToSync = ['path', 'fullPath']
|
|
|
|
keysToSync.forEach((key) => {
|
|
|
|
if (audiobookData[key] !== undefined && audiobookData[key] !== this[key]) {
|
|
|
|
hasUpdates = true
|
|
|
|
this[key] = audiobookData[key]
|
|
|
|
}
|
|
|
|
})
|
|
|
|
if (hasUpdates) {
|
|
|
|
this.book.syncPathsUpdated(audiobookData)
|
|
|
|
}
|
|
|
|
return hasUpdates
|
|
|
|
}
|
|
|
|
|
2021-08-21 23:23:35 +02:00
|
|
|
isSearchMatch(search) {
|
|
|
|
return this.book.isSearchMatch(search.toLowerCase().trim())
|
|
|
|
}
|
2021-08-26 00:36:54 +02:00
|
|
|
|
|
|
|
getAudioFileByIno(ino) {
|
|
|
|
return this.audioFiles.find(af => af.ino === ino)
|
|
|
|
}
|
2021-09-08 16:15:54 +02:00
|
|
|
|
2021-09-24 23:14:33 +02:00
|
|
|
getAudioFileByPath(fullPath) {
|
|
|
|
return this.audioFiles.find(af => af.fullPath === fullPath)
|
|
|
|
}
|
|
|
|
|
2021-09-08 16:15:54 +02:00
|
|
|
setChapters() {
|
2021-09-15 03:45:00 +02:00
|
|
|
// If 1 audio file without chapters, then no chapters will be set
|
|
|
|
var includedAudioFiles = this.audioFiles.filter(af => !af.exclude)
|
|
|
|
if (includedAudioFiles.length === 1) {
|
|
|
|
// 1 audio file with chapters
|
|
|
|
if (includedAudioFiles[0].chapters) {
|
|
|
|
this.chapters = includedAudioFiles[0].chapters.map(c => ({ ...c }))
|
2021-09-08 16:15:54 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this.chapters = []
|
2021-09-15 03:45:00 +02:00
|
|
|
var currChapterId = 0
|
2021-09-08 16:15:54 +02:00
|
|
|
var currStartTime = 0
|
2021-09-15 03:45:00 +02:00
|
|
|
includedAudioFiles.forEach((file) => {
|
|
|
|
// If audio file has chapters use chapters
|
|
|
|
if (file.chapters && file.chapters.length) {
|
|
|
|
file.chapters.forEach((chapter) => {
|
|
|
|
var chapterDuration = chapter.end - chapter.start
|
|
|
|
if (chapterDuration > 0) {
|
|
|
|
var title = `Chapter ${currChapterId}`
|
|
|
|
if (chapter.title) {
|
|
|
|
title += ` (${chapter.title})`
|
|
|
|
}
|
|
|
|
this.chapters.push({
|
|
|
|
id: currChapterId++,
|
|
|
|
start: currStartTime,
|
|
|
|
end: currStartTime + chapterDuration,
|
|
|
|
title
|
|
|
|
})
|
|
|
|
currStartTime += chapterDuration
|
|
|
|
}
|
|
|
|
})
|
|
|
|
} else if (file.duration) {
|
|
|
|
// Otherwise just use track has chapter
|
|
|
|
this.chapters.push({
|
|
|
|
id: currChapterId++,
|
|
|
|
start: currStartTime,
|
|
|
|
end: currStartTime + file.duration,
|
2021-09-29 00:36:41 +02:00
|
|
|
title: file.filename ? Path.basename(file.filename, Path.extname(file.filename)) : `Chapter ${currChapterId}`
|
2021-09-15 03:45:00 +02:00
|
|
|
})
|
|
|
|
currStartTime += file.duration
|
|
|
|
}
|
2021-09-08 16:15:54 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2021-09-29 17:16:38 +02:00
|
|
|
|
|
|
|
writeNfoFile(nfoFilename = 'metadata.nfo') {
|
|
|
|
return nfoGenerator(this, nfoFilename)
|
|
|
|
}
|
2021-08-18 00:01:11 +02:00
|
|
|
}
|
|
|
|
module.exports = Audiobook
|