2021-08-21 20:02:24 +02:00
|
|
|
const Path = require('path')
|
2021-09-04 21:17:26 +02:00
|
|
|
const Logger = require('../Logger')
|
|
|
|
const parseAuthors = require('../utils/parseAuthors')
|
|
|
|
|
2021-08-18 00:01:11 +02:00
|
|
|
class Book {
|
|
|
|
constructor(book = null) {
|
|
|
|
this.olid = null
|
|
|
|
this.title = null
|
2021-09-05 02:58:39 +02:00
|
|
|
this.subtitle = null
|
2021-08-18 00:01:11 +02:00
|
|
|
this.author = null
|
2021-08-25 03:24:40 +02:00
|
|
|
this.authorFL = null
|
|
|
|
this.authorLF = null
|
2021-09-05 02:58:39 +02:00
|
|
|
this.narrarator = null
|
2021-08-19 18:31:03 +02:00
|
|
|
this.series = null
|
2021-08-25 03:24:40 +02:00
|
|
|
this.volumeNumber = null
|
2021-08-18 00:01:11 +02:00
|
|
|
this.publishYear = null
|
|
|
|
this.publisher = null
|
|
|
|
this.description = null
|
|
|
|
this.cover = null
|
2021-08-20 00:29:36 +02:00
|
|
|
this.genres = []
|
2021-08-18 00:01:11 +02:00
|
|
|
|
|
|
|
if (book) {
|
|
|
|
this.construct(book)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-21 23:23:35 +02:00
|
|
|
get _title() { return this.title || '' }
|
2021-09-05 02:58:39 +02:00
|
|
|
get _subtitle() { return this.subtitle || '' }
|
|
|
|
get _narrarator() { return this.narrarator || '' }
|
2021-08-21 23:23:35 +02:00
|
|
|
get _author() { return this.author || '' }
|
|
|
|
get _series() { return this.series || '' }
|
|
|
|
|
2021-08-18 00:01:11 +02:00
|
|
|
construct(book) {
|
|
|
|
this.olid = book.olid
|
|
|
|
this.title = book.title
|
2021-09-05 02:58:39 +02:00
|
|
|
this.subtitle = book.subtitle || null
|
2021-08-18 00:01:11 +02:00
|
|
|
this.author = book.author
|
2021-08-25 03:24:40 +02:00
|
|
|
this.authorFL = book.authorFL || null
|
|
|
|
this.authorLF = book.authorLF || null
|
2021-09-05 02:58:39 +02:00
|
|
|
this.narrarator = book.narrarator || null
|
2021-08-19 18:31:03 +02:00
|
|
|
this.series = book.series
|
2021-08-25 03:24:40 +02:00
|
|
|
this.volumeNumber = book.volumeNumber || null
|
2021-08-20 02:14:24 +02:00
|
|
|
this.publishYear = book.publishYear
|
2021-08-18 00:01:11 +02:00
|
|
|
this.publisher = book.publisher
|
|
|
|
this.description = book.description
|
|
|
|
this.cover = book.cover
|
2021-08-20 00:29:36 +02:00
|
|
|
this.genres = book.genres
|
2021-08-18 00:01:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
toJSON() {
|
|
|
|
return {
|
|
|
|
olid: this.olid,
|
|
|
|
title: this.title,
|
2021-09-05 02:58:39 +02:00
|
|
|
subtitle: this.subtitle,
|
2021-08-18 00:01:11 +02:00
|
|
|
author: this.author,
|
2021-08-25 03:24:40 +02:00
|
|
|
authorFL: this.authorFL,
|
|
|
|
authorLF: this.authorLF,
|
2021-09-05 02:58:39 +02:00
|
|
|
narrarator: this.narrarator,
|
2021-08-19 18:31:03 +02:00
|
|
|
series: this.series,
|
2021-08-25 03:24:40 +02:00
|
|
|
volumeNumber: this.volumeNumber,
|
2021-08-20 02:14:24 +02:00
|
|
|
publishYear: this.publishYear,
|
2021-08-18 00:01:11 +02:00
|
|
|
publisher: this.publisher,
|
|
|
|
description: this.description,
|
|
|
|
cover: this.cover,
|
2021-08-20 00:29:36 +02:00
|
|
|
genres: this.genres
|
2021-08-18 00:01:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-25 13:38:32 +02:00
|
|
|
setParseAuthor(author) {
|
|
|
|
if (!author) {
|
|
|
|
var hasUpdated = this.authorFL || this.authorLF
|
|
|
|
this.authorFL = null
|
|
|
|
this.authorLF = null
|
|
|
|
return hasUpdated
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
var { authorLF, authorFL } = parseAuthors(author)
|
|
|
|
var hasUpdated = authorLF !== this.authorLF || authorFL !== this.authorFL
|
|
|
|
this.authorFL = authorFL || null
|
|
|
|
this.authorLF = authorLF || null
|
|
|
|
return hasUpdated
|
|
|
|
} catch (err) {
|
|
|
|
Logger.error('[Book] Parse authors failed', err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-18 00:01:11 +02:00
|
|
|
setData(data) {
|
|
|
|
this.olid = data.olid || null
|
|
|
|
this.title = data.title || null
|
2021-09-05 02:58:39 +02:00
|
|
|
this.subtitle = data.subtitle || null
|
2021-08-18 00:01:11 +02:00
|
|
|
this.author = data.author || null
|
2021-09-05 02:58:39 +02:00
|
|
|
this.narrarator = data.narrarator || null
|
2021-08-19 18:31:03 +02:00
|
|
|
this.series = data.series || null
|
2021-08-25 03:24:40 +02:00
|
|
|
this.volumeNumber = data.volumeNumber || null
|
2021-08-20 02:14:24 +02:00
|
|
|
this.publishYear = data.publishYear || null
|
2021-08-18 00:01:11 +02:00
|
|
|
this.description = data.description || null
|
|
|
|
this.cover = data.cover || null
|
2021-08-20 00:29:36 +02:00
|
|
|
this.genres = data.genres || []
|
2021-08-21 20:02:24 +02:00
|
|
|
|
2021-08-25 13:38:32 +02:00
|
|
|
if (data.author) {
|
|
|
|
this.setParseAuthor(this.author)
|
|
|
|
}
|
|
|
|
|
2021-08-21 20:02:24 +02:00
|
|
|
// Use first image file as cover
|
|
|
|
if (data.otherFiles && data.otherFiles.length) {
|
|
|
|
var imageFile = data.otherFiles.find(f => f.filetype === 'image')
|
|
|
|
if (imageFile) {
|
2021-08-26 02:15:00 +02:00
|
|
|
this.cover = Path.normalize(Path.join('/local', imageFile.path))
|
2021-08-21 20:02:24 +02:00
|
|
|
}
|
|
|
|
}
|
2021-08-18 00:01:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
update(payload) {
|
|
|
|
var hasUpdates = false
|
2021-08-26 02:15:00 +02:00
|
|
|
|
|
|
|
if (payload.cover) {
|
|
|
|
// If updating to local cover then normalize path
|
|
|
|
if (!payload.cover.startsWith('http:') && !payload.cover.startsWith('https:')) {
|
|
|
|
payload.cover = Path.normalize(payload.cover)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-18 00:01:11 +02:00
|
|
|
for (const key in payload) {
|
|
|
|
if (payload[key] === undefined) continue;
|
|
|
|
|
2021-08-20 00:29:36 +02:00
|
|
|
if (key === 'genres') {
|
|
|
|
if (payload['genres'] === null && this.genres !== null) {
|
|
|
|
this.genres = []
|
2021-08-18 00:01:11 +02:00
|
|
|
hasUpdates = true
|
2021-08-20 00:29:36 +02:00
|
|
|
} else if (payload['genres'].join(',') !== this.genres.join(',')) {
|
|
|
|
this.genres = payload['genres']
|
2021-08-18 00:01:11 +02:00
|
|
|
hasUpdates = true
|
|
|
|
}
|
2021-08-25 13:38:32 +02:00
|
|
|
} else if (key === 'author') {
|
|
|
|
if (this.author !== payload.author) {
|
|
|
|
this.author = payload.author || null
|
|
|
|
hasUpdates = true
|
|
|
|
}
|
|
|
|
if (this.setParseAuthor(this.author)) {
|
|
|
|
hasUpdates = true
|
|
|
|
}
|
2021-08-18 00:01:11 +02:00
|
|
|
} else if (this[key] !== undefined && payload[key] !== this[key]) {
|
|
|
|
this[key] = payload[key]
|
|
|
|
hasUpdates = true
|
|
|
|
}
|
|
|
|
}
|
2021-08-25 03:24:40 +02:00
|
|
|
return hasUpdates
|
|
|
|
}
|
|
|
|
|
2021-08-26 00:36:54 +02:00
|
|
|
// If audiobook directory path was changed, check and update properties set from dirnames
|
|
|
|
// May be worthwhile checking if these were manually updated and not override manual updates
|
|
|
|
syncPathsUpdated(audiobookData) {
|
|
|
|
var keysToSync = ['author', 'title', 'series', 'publishYear']
|
|
|
|
var syncPayload = {}
|
|
|
|
keysToSync.forEach((key) => {
|
|
|
|
if (audiobookData[key]) syncPayload[key] = audiobookData[key]
|
|
|
|
})
|
|
|
|
if (!Object.keys(syncPayload).length) return false
|
|
|
|
return this.update(syncPayload)
|
|
|
|
}
|
|
|
|
|
2021-08-21 23:23:35 +02:00
|
|
|
isSearchMatch(search) {
|
2021-09-05 02:58:39 +02:00
|
|
|
return this._title.toLowerCase().includes(search) || this._subtitle.toLowerCase().includes(search) || this._author.toLowerCase().includes(search) || this._series.toLowerCase().includes(search)
|
2021-08-21 23:23:35 +02:00
|
|
|
}
|
2021-08-18 00:01:11 +02:00
|
|
|
}
|
|
|
|
module.exports = Book
|