2021-10-05 05:11:42 +02:00
|
|
|
const Folder = require('./Folder')
|
|
|
|
|
|
|
|
class Library {
|
|
|
|
constructor(library = null) {
|
|
|
|
this.id = null
|
|
|
|
this.name = null
|
|
|
|
this.folders = []
|
2021-10-09 18:09:06 +02:00
|
|
|
this.icon = 'database'
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
construct(library) {
|
|
|
|
this.id = library.id
|
|
|
|
this.name = library.name
|
|
|
|
this.folders = (library.folders || []).map(f => new Folder(f))
|
2021-10-09 18:09:06 +02:00
|
|
|
this.icon = library.icon || 'database'
|
|
|
|
|
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-09 18:09:06 +02:00
|
|
|
icon: this.icon,
|
2021-10-05 05:11:42 +02:00
|
|
|
createdAt: this.createdAt,
|
|
|
|
lastUpdate: this.lastUpdate
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
setData(data) {
|
|
|
|
this.id = data.id ? data.id : 'lib' + (Math.trunc(Math.random() * 1000) + Date.now()).toString(36)
|
|
|
|
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-09 18:09:06 +02:00
|
|
|
this.icon = data.icon || 'database'
|
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
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
hasUpdates = newFolders.length || removedFolders.length
|
|
|
|
}
|
|
|
|
if (hasUpdates) {
|
|
|
|
this.lastUpdate = Date.now()
|
|
|
|
}
|
|
|
|
return hasUpdates
|
|
|
|
}
|
|
|
|
|
|
|
|
checkFullPathInLibrary(fullPath) {
|
|
|
|
return this.folders.find(folder => fullPath.startsWith(folder.fullPath))
|
|
|
|
}
|
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
|