2021-10-05 05:11:42 +02:00
|
|
|
const Folder = require('./Folder')
|
2021-11-16 03:09:42 +01:00
|
|
|
const { getId } = require('../utils/index')
|
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
|
2021-10-09 18:09:06 +02:00
|
|
|
this.icon = 'database'
|
2022-03-04 02:03:34 +01:00
|
|
|
this.mediaType = 'default'
|
2022-02-15 21:59:46 +01:00
|
|
|
this.provider = 'google'
|
2022-02-26 23:34:54 +01:00
|
|
|
this.disableWatcher = false
|
2021-10-05 05:11:42 +02:00
|
|
|
|
2021-10-06 04:10:49 +02:00
|
|
|
this.lastScan = 0
|
|
|
|
|
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-03-13 18:39:12 +01:00
|
|
|
get itemMediaType() {
|
|
|
|
return this.mediaType === 'podcast' ? 'podcast' : '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-04 02:03:34 +01:00
|
|
|
this.mediaType = library.mediaType || 'default'
|
2022-02-15 21:59:46 +01:00
|
|
|
this.provider = library.provider || 'google'
|
2022-02-26 23:34:54 +01:00
|
|
|
this.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
|
|
|
|
}
|
|
|
|
|
|
|
|
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-02-26 23:34:54 +01:00
|
|
|
disableWatcher: this.disableWatcher,
|
2021-10-05 05:11:42 +02:00
|
|
|
createdAt: this.createdAt,
|
|
|
|
lastUpdate: this.lastUpdate
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
setData(data) {
|
2021-11-16 03:09:42 +01:00
|
|
|
this.id = data.id ? data.id : getId('lib')
|
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-04 02:03:34 +01:00
|
|
|
this.mediaType = data.mediaType || 'default'
|
2022-02-26 23:34:54 +01:00
|
|
|
this.disableWatcher = !!data.disableWatcher
|
2021-10-05 05:11:42 +02:00
|
|
|
this.createdAt = Date.now()
|
|
|
|
this.lastUpdate = Date.now()
|
|
|
|
}
|
|
|
|
|
|
|
|
update(payload) {
|
|
|
|
var hasUpdates = false
|
|
|
|
if (payload.name && payload.name !== this.name) {
|
|
|
|
this.name = payload.name
|
|
|
|
hasUpdates = true
|
2021-10-13 03:07:42 +02:00
|
|
|
}
|
2022-02-15 23:15:09 +01:00
|
|
|
if (payload.provider && payload.provider !== this.provider) {
|
|
|
|
this.provider = payload.provider
|
|
|
|
hasUpdates = true
|
|
|
|
}
|
2022-03-04 02:03:34 +01:00
|
|
|
if (payload.mediaType && payload.mediaType !== this.mediaType) {
|
|
|
|
this.mediaType = payload.mediaType
|
|
|
|
hasUpdates = true
|
|
|
|
}
|
|
|
|
if (payload.icon && payload.icon !== this.icon) {
|
|
|
|
this.icon = payload.icon
|
|
|
|
hasUpdates = true
|
|
|
|
}
|
|
|
|
|
2022-02-26 23:34:54 +01:00
|
|
|
if (payload.disableWatcher !== this.disableWatcher) {
|
|
|
|
this.disableWatcher = !!payload.disableWatcher
|
|
|
|
hasUpdates = true
|
|
|
|
}
|
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) {
|
2022-02-03 23:39:05 +01:00
|
|
|
fullPath = fullPath.replace(/\\/g, '/')
|
|
|
|
return this.folders.find(folder => fullPath.startsWith(folder.fullPath.replace(/\\/g, '/')))
|
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
|