mirror of
https://github.com/advplyr/audiobookshelf.git
synced 2025-06-22 00:49:10 +02:00
Update:Only load libraries from db when needed
This commit is contained in:
parent
1c40af3eef
commit
1d974375a0
13 changed files with 189 additions and 60 deletions
|
@ -44,7 +44,9 @@ class LibraryController {
|
|||
|
||||
const library = new Library()
|
||||
|
||||
newLibraryPayload.displayOrder = Database.libraries.map(li => li.displayOrder).sort((a, b) => a - b).pop() + 1
|
||||
let currentLargestDisplayOrder = await Database.models.library.getMaxDisplayOrder()
|
||||
if (isNaN(currentLargestDisplayOrder)) currentLargestDisplayOrder = 0
|
||||
newLibraryPayload.displayOrder = currentLargestDisplayOrder + 1
|
||||
library.setData(newLibraryPayload)
|
||||
await Database.createLibrary(library)
|
||||
|
||||
|
@ -60,17 +62,18 @@ class LibraryController {
|
|||
res.json(library)
|
||||
}
|
||||
|
||||
findAll(req, res) {
|
||||
async findAll(req, res) {
|
||||
const libraries = await Database.models.library.getAllOldLibraries()
|
||||
|
||||
const librariesAccessible = req.user.librariesAccessible || []
|
||||
if (librariesAccessible.length) {
|
||||
return res.json({
|
||||
libraries: Database.libraries.filter(lib => librariesAccessible.includes(lib.id)).map(lib => lib.toJSON())
|
||||
libraries: libraries.filter(lib => librariesAccessible.includes(lib.id)).map(lib => lib.toJSON())
|
||||
})
|
||||
}
|
||||
|
||||
res.json({
|
||||
libraries: Database.libraries.map(lib => lib.toJSON())
|
||||
// libraries: Database.libraries.map(lib => lib.toJSON())
|
||||
libraries: libraries.map(lib => lib.toJSON())
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -151,6 +154,12 @@ class LibraryController {
|
|||
return res.json(library.toJSON())
|
||||
}
|
||||
|
||||
/**
|
||||
* DELETE: /api/libraries/:id
|
||||
* Delete a library
|
||||
* @param {*} req
|
||||
* @param {*} res
|
||||
*/
|
||||
async delete(req, res) {
|
||||
const library = req.library
|
||||
|
||||
|
@ -173,6 +182,10 @@ class LibraryController {
|
|||
|
||||
const libraryJson = library.toJSON()
|
||||
await Database.removeLibrary(library.id)
|
||||
|
||||
// Re-order libraries
|
||||
await Database.models.library.resetDisplayOrder()
|
||||
|
||||
SocketAuthority.emitter('library_removed', libraryJson)
|
||||
return res.json(libraryJson)
|
||||
}
|
||||
|
@ -601,17 +614,23 @@ class LibraryController {
|
|||
res.json(categories)
|
||||
}
|
||||
|
||||
// PATCH: Change the order of libraries
|
||||
/**
|
||||
* POST: /api/libraries/order
|
||||
* Change the display order of libraries
|
||||
* @param {*} req
|
||||
* @param {*} res
|
||||
*/
|
||||
async reorder(req, res) {
|
||||
if (!req.user.isAdminOrUp) {
|
||||
Logger.error('[LibraryController] ReorderLibraries invalid user', req.user)
|
||||
return res.sendStatus(403)
|
||||
}
|
||||
const libraries = await Database.models.library.getAllOldLibraries()
|
||||
|
||||
var orderdata = req.body
|
||||
var hasUpdates = false
|
||||
const orderdata = req.body
|
||||
let hasUpdates = false
|
||||
for (let i = 0; i < orderdata.length; i++) {
|
||||
var library = Database.libraries.find(lib => lib.id === orderdata[i].id)
|
||||
const library = libraries.find(lib => lib.id === orderdata[i].id)
|
||||
if (!library) {
|
||||
Logger.error(`[LibraryController] Invalid library not found in reorder ${orderdata[i].id}`)
|
||||
return res.sendStatus(500)
|
||||
|
@ -623,14 +642,14 @@ class LibraryController {
|
|||
}
|
||||
|
||||
if (hasUpdates) {
|
||||
Database.libraries.sort((a, b) => a.displayOrder - b.displayOrder)
|
||||
libraries.sort((a, b) => a.displayOrder - b.displayOrder)
|
||||
Logger.debug(`[LibraryController] Updated library display orders`)
|
||||
} else {
|
||||
Logger.debug(`[LibraryController] Library orders were up to date`)
|
||||
}
|
||||
|
||||
res.json({
|
||||
libraries: Database.libraries.map(lib => lib.toJSON())
|
||||
libraries: libraries.map(lib => lib.toJSON())
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -902,13 +921,13 @@ class LibraryController {
|
|||
res.send(opmlText)
|
||||
}
|
||||
|
||||
middleware(req, res, next) {
|
||||
async middleware(req, res, next) {
|
||||
if (!req.user.checkCanAccessLibrary(req.params.id)) {
|
||||
Logger.warn(`[LibraryController] Library ${req.params.id} not accessible to user ${req.user.username}`)
|
||||
return res.sendStatus(403)
|
||||
}
|
||||
|
||||
const library = Database.libraries.find(lib => lib.id === req.params.id)
|
||||
const library = await Database.models.library.getOldById(req.params.id)
|
||||
if (!library) {
|
||||
return res.status(404).send('Library not found')
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue