2023-07-05 01:14:44 +02:00
|
|
|
const uuidv4 = require("uuid").v4
|
2021-10-05 05:11:42 +02:00
|
|
|
const Folder = require('./Folder')
|
2022-04-15 00:15:52 +02:00
|
|
|
const LibrarySettings = require('./settings/LibrarySettings')
|
2023-01-06 00:45:27 +01:00
|
|
|
const { filePathToPOSIX } = require('../utils/fileUtils')
|
2021-10-05 05:11:42 +02:00
|
|
|
|
|
|
|
class Library {
|
|
|
|
constructor(library = null) {
|
|
|
|
this.id = null
|
|
|
|
this.name = null
|
|
|
|
this.folders = []
|
2021-10-13 03:07:42 +02:00
|
|
|
this.displayOrder = 1
|
2022-10-18 19:09:36 +02:00
|
|
|
this.icon = 'database'
|
2022-03-14 15:56:24 +01:00
|
|
|
this.mediaType = 'book' // book, podcast
|
2022-02-15 21:59:46 +01:00
|
|
|
this.provider = 'google'
|
2021-10-05 05:11:42 +02:00
|
|
|
|
2021-10-06 04:10:49 +02:00
|
|
|
this.lastScan = 0
|
2022-04-15 00:15:52 +02:00
|
|
|
this.settings = null
|
2021-10-06 04:10:49 +02:00
|
|
|
|
2021-10-05 05:11:42 +02:00
|
|
|
this.createdAt = null
|
|
|
|
this.lastUpdate = null
|
|
|
|
|
|
|
|
if (library) {
|
|
|
|
this.construct(library)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
get folderPaths() {
|
|
|
|
return this.folders.map(f => f.fullPath)
|
|
|
|
}
|
2022-04-15 01:24:24 +02:00
|
|
|
get isPodcast() {
|
|
|
|
return this.mediaType === 'podcast'
|
|
|
|
}
|
2023-01-03 01:02:04 +01:00
|
|
|
get isMusic() {
|
|
|
|
return this.mediaType === 'music'
|
|
|
|
}
|
2023-06-10 19:46:57 +02:00
|
|
|
get isBook() {
|
|
|
|
return this.mediaType === 'book'
|
|
|
|
}
|
2021-10-05 05:11:42 +02:00
|
|
|
|
|
|
|
construct(library) {
|
|
|
|
this.id = library.id
|
|
|
|
this.name = library.name
|
|
|
|
this.folders = (library.folders || []).map(f => new Folder(f))
|
2021-10-13 03:07:42 +02:00
|
|
|
this.displayOrder = library.displayOrder || 1
|
2021-10-09 18:09:06 +02:00
|
|
|
this.icon = library.icon || 'database'
|
2022-03-14 15:56:24 +01:00
|
|
|
this.mediaType = library.mediaType
|
2022-02-15 21:59:46 +01:00
|
|
|
this.provider = library.provider || 'google'
|
2022-04-15 00:15:52 +02:00
|
|
|
|
|
|
|
this.settings = new LibrarySettings(library.settings)
|
|
|
|
if (library.settings === undefined) { // LibrarySettings added in v2, migrate settings
|
|
|
|
this.settings.disableWatcher = !!library.disableWatcher
|
|
|
|
}
|
2021-10-09 18:09:06 +02:00
|
|
|
|
2021-10-05 05:11:42 +02:00
|
|
|
this.createdAt = library.createdAt
|
|
|
|
this.lastUpdate = library.lastUpdate
|
2022-10-18 19:09:36 +02:00
|
|
|
this.cleanOldValues() // mediaType changed for v2 and icon change for v2.2.2
|
2022-03-14 15:56:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
cleanOldValues() {
|
2022-10-18 19:09:36 +02:00
|
|
|
const availableIcons = ['database', 'audiobookshelf', 'books-1', 'books-2', 'book-1', 'microphone-1', 'microphone-3', 'radio', 'podcast', 'rss', 'headphones', 'music', 'file-picture', 'rocket', 'power', 'star', 'heart']
|
2022-03-18 23:09:17 +01:00
|
|
|
if (!availableIcons.includes(this.icon)) {
|
2022-10-18 19:09:36 +02:00
|
|
|
if (this.icon === 'audiobook') this.icon = 'audiobookshelf'
|
|
|
|
else if (this.icon === 'book') this.icon = 'books-1'
|
|
|
|
else if (this.icon === 'comic') this.icon = 'file-picture'
|
2022-03-18 23:09:17 +01:00
|
|
|
else this.icon = 'database'
|
2022-03-14 15:56:24 +01:00
|
|
|
}
|
2022-12-22 23:38:55 +01:00
|
|
|
|
|
|
|
const mediaTypes = ['podcast', 'book', 'video', 'music']
|
|
|
|
if (!this.mediaType || !mediaTypes.includes(this.mediaType)) {
|
2022-03-18 23:09:17 +01:00
|
|
|
this.mediaType = 'book'
|
2022-03-14 15:56:24 +01:00
|
|
|
}
|
2021-10-05 05:11:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
toJSON() {
|
|
|
|
return {
|
|
|
|
id: this.id,
|
|
|
|
name: this.name,
|
|
|
|
folders: (this.folders || []).map(f => f.toJSON()),
|
2021-10-13 03:07:42 +02:00
|
|
|
displayOrder: this.displayOrder,
|
2021-10-09 18:09:06 +02:00
|
|
|
icon: this.icon,
|
2022-03-04 02:03:34 +01:00
|
|
|
mediaType: this.mediaType,
|
2022-02-15 21:59:46 +01:00
|
|
|
provider: this.provider,
|
2022-04-15 00:15:52 +02:00
|
|
|
settings: this.settings.toJSON(),
|
2021-10-05 05:11:42 +02:00
|
|
|
createdAt: this.createdAt,
|
|
|
|
lastUpdate: this.lastUpdate
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
setData(data) {
|
2023-07-05 01:14:44 +02:00
|
|
|
this.id = data.id || uuidv4()
|
2021-10-05 05:11:42 +02:00
|
|
|
this.name = data.name
|
|
|
|
if (data.folder) {
|
|
|
|
this.folders = [
|
|
|
|
new Folder(data.folder)
|
|
|
|
]
|
|
|
|
} else if (data.folders) {
|
|
|
|
this.folders = data.folders.map(folder => {
|
|
|
|
var newFolder = new Folder()
|
|
|
|
newFolder.setData({
|
|
|
|
fullPath: folder.fullPath,
|
|
|
|
libraryId: this.id
|
|
|
|
})
|
|
|
|
return newFolder
|
|
|
|
})
|
|
|
|
}
|
2021-10-13 03:07:42 +02:00
|
|
|
this.displayOrder = data.displayOrder || 1
|
2021-10-09 18:09:06 +02:00
|
|
|
this.icon = data.icon || 'database'
|
2022-03-14 15:56:24 +01:00
|
|
|
this.mediaType = data.mediaType || 'book'
|
2022-03-19 12:41:54 +01:00
|
|
|
this.provider = data.provider || 'google'
|
2022-04-15 00:15:52 +02:00
|
|
|
this.settings = new LibrarySettings(data.settings)
|
2021-10-05 05:11:42 +02:00
|
|
|
this.createdAt = Date.now()
|
|
|
|
this.lastUpdate = Date.now()
|
|
|
|
}
|
|
|
|
|
|
|
|
update(payload) {
|
|
|
|
var hasUpdates = false
|
2022-03-14 15:56:24 +01:00
|
|
|
|
2022-03-18 23:09:17 +01:00
|
|
|
var keysToCheck = ['name', 'provider', 'mediaType', 'icon']
|
2022-03-14 15:56:24 +01:00
|
|
|
keysToCheck.forEach((key) => {
|
|
|
|
if (payload[key] && payload[key] !== this[key]) {
|
|
|
|
this[key] = payload[key]
|
|
|
|
hasUpdates = true
|
|
|
|
}
|
|
|
|
})
|
2022-03-04 02:03:34 +01:00
|
|
|
|
2022-04-15 00:15:52 +02:00
|
|
|
if (payload.settings && this.settings.update(payload.settings)) {
|
2022-02-26 23:34:54 +01:00
|
|
|
hasUpdates = true
|
|
|
|
}
|
2022-04-15 00:15:52 +02:00
|
|
|
|
2021-10-13 03:07:42 +02:00
|
|
|
if (!isNaN(payload.displayOrder) && payload.displayOrder !== this.displayOrder) {
|
|
|
|
this.displayOrder = Number(payload.displayOrder)
|
|
|
|
hasUpdates = true
|
2021-10-05 05:11:42 +02:00
|
|
|
}
|
|
|
|
if (payload.folders) {
|
|
|
|
var newFolders = payload.folders.filter(f => !f.id)
|
|
|
|
var removedFolders = this.folders.filter(f => !payload.folders.find(_f => _f.id === f.id))
|
|
|
|
|
|
|
|
if (removedFolders.length) {
|
|
|
|
var removedFolderIds = removedFolders.map(f => f.id)
|
|
|
|
this.folders = this.folders.filter(f => !removedFolderIds.includes(f.id))
|
|
|
|
}
|
|
|
|
|
|
|
|
if (newFolders.length) {
|
|
|
|
newFolders.forEach((folderData) => {
|
2021-10-06 04:10:49 +02:00
|
|
|
folderData.libraryId = this.id
|
2021-10-05 05:11:42 +02:00
|
|
|
var newFolder = new Folder()
|
|
|
|
newFolder.setData(folderData)
|
|
|
|
this.folders.push(newFolder)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-02-03 23:39:05 +01:00
|
|
|
if (newFolders.length || removedFolders.length) {
|
|
|
|
hasUpdates = true
|
|
|
|
}
|
2021-10-05 05:11:42 +02:00
|
|
|
}
|
|
|
|
if (hasUpdates) {
|
|
|
|
this.lastUpdate = Date.now()
|
|
|
|
}
|
|
|
|
return hasUpdates
|
|
|
|
}
|
|
|
|
|
|
|
|
checkFullPathInLibrary(fullPath) {
|
2023-01-06 00:45:27 +01:00
|
|
|
fullPath = filePathToPOSIX(fullPath)
|
|
|
|
return this.folders.find(folder => fullPath.startsWith(filePathToPOSIX(folder.fullPath)))
|
2021-10-05 05:11:42 +02:00
|
|
|
}
|
2021-10-06 04:10:49 +02:00
|
|
|
|
|
|
|
getFolderById(id) {
|
|
|
|
return this.folders.find(folder => folder.id === id)
|
|
|
|
}
|
2021-10-05 05:11:42 +02:00
|
|
|
}
|
|
|
|
module.exports = Library
|