mirror of
https://github.com/advplyr/audiobookshelf.git
synced 2025-12-10 05:49:37 +00:00
Lazy bookshelf
This commit is contained in:
parent
3941da1144
commit
4587916c8e
11 changed files with 669 additions and 31 deletions
|
|
@ -53,6 +53,7 @@ class ApiController {
|
|||
this.router.patch('/libraries/:id', LibraryController.update.bind(this))
|
||||
this.router.delete('/libraries/:id', LibraryController.delete.bind(this))
|
||||
|
||||
this.router.get('/libraries/:id/books/all', LibraryController.getBooksForLibrary2.bind(this))
|
||||
this.router.get('/libraries/:id/books', LibraryController.getBooksForLibrary.bind(this))
|
||||
this.router.get('/libraries/:id/search', LibraryController.search.bind(this))
|
||||
this.router.patch('/libraries/order', LibraryController.reorder.bind(this))
|
||||
|
|
@ -488,5 +489,45 @@ class ApiController {
|
|||
})
|
||||
return listeningStats
|
||||
}
|
||||
|
||||
|
||||
decode(text) {
|
||||
return Buffer.from(decodeURIComponent(text), 'base64').toString()
|
||||
}
|
||||
|
||||
getFiltered(audiobooks, filterBy, user) {
|
||||
var filtered = audiobooks
|
||||
|
||||
var searchGroups = ['genres', 'tags', 'series', 'authors', 'progress', 'narrators']
|
||||
var group = searchGroups.find(_group => filterBy.startsWith(_group + '.'))
|
||||
if (group) {
|
||||
var filterVal = filterBy.replace(`${group}.`, '')
|
||||
var filter = this.decode(filterVal)
|
||||
if (group === 'genres') filtered = filtered.filter(ab => ab.book && ab.book.genres.includes(filter))
|
||||
else if (group === 'tags') filtered = filtered.filter(ab => ab.tags.includes(filter))
|
||||
else if (group === 'series') {
|
||||
if (filter === 'No Series') filtered = filtered.filter(ab => ab.book && !ab.book.series)
|
||||
else filtered = filtered.filter(ab => ab.book && ab.book.series === filter)
|
||||
}
|
||||
else if (group === 'authors') filtered = filtered.filter(ab => ab.book && ab.book.authorFL && ab.book.authorFL.split(', ').includes(filter))
|
||||
else if (group === 'narrators') filtered = filtered.filter(ab => ab.book && ab.book.narratorFL && ab.book.narratorFL.split(', ').includes(filter))
|
||||
else if (group === 'progress') {
|
||||
filtered = filtered.filter(ab => {
|
||||
var userAudiobook = user.getAudiobookJSON(ab.id)
|
||||
var isRead = userAudiobook && userAudiobook.isRead
|
||||
if (filter === 'Read' && isRead) return true
|
||||
if (filter === 'Unread' && !isRead) return true
|
||||
if (filter === 'In Progress' && (userAudiobook && !userAudiobook.isRead && userAudiobook.progress > 0)) return true
|
||||
return false
|
||||
})
|
||||
}
|
||||
} else if (filterBy === 'issues') {
|
||||
filtered = filtered.filter(ab => {
|
||||
return ab.hasMissingParts || ab.hasInvalidParts || ab.isMissing || ab.isIncomplete
|
||||
})
|
||||
}
|
||||
|
||||
return filtered
|
||||
}
|
||||
}
|
||||
module.exports = ApiController
|
||||
|
|
@ -1,16 +1,11 @@
|
|||
const Logger = require('../Logger')
|
||||
|
||||
class BookController {
|
||||
constructor(db, emitter, clientEmitter, streamManager, coverController) {
|
||||
this.db = db
|
||||
this.emitter = emitter
|
||||
this.clientEmitter = clientEmitter
|
||||
this.streamManager = streamManager
|
||||
this.coverController = coverController
|
||||
}
|
||||
constructor() { }
|
||||
|
||||
findAll(req, res) {
|
||||
var audiobooks = []
|
||||
|
||||
if (req.query.q) {
|
||||
audiobooks = this.db.audiobooks.filter(ab => {
|
||||
return ab.isSearchMatch(req.query.q)
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
const Logger = require('../Logger')
|
||||
const Library = require('../objects/Library')
|
||||
const { sort } = require('fast-sort')
|
||||
|
||||
class LibraryController {
|
||||
constructor() { }
|
||||
|
|
@ -91,18 +92,84 @@ class LibraryController {
|
|||
if (!library) {
|
||||
return res.status(400).send('Library does not exist')
|
||||
}
|
||||
var audiobooks = this.db.audiobooks.filter(ab => ab.libraryId === libraryId)
|
||||
// if (req.query.q) {
|
||||
// audiobooks = this.db.audiobooks.filter(ab => {
|
||||
// return ab.libraryId === libraryId && ab.isSearchMatch(req.query.q)
|
||||
// }).map(ab => ab.toJSONMinified())
|
||||
// } else {
|
||||
// audiobooks = this.db.audiobooks.filter(ab => ab.libraryId === libraryId).map(ab => ab.toJSONMinified())
|
||||
// }
|
||||
|
||||
var audiobooks = []
|
||||
if (req.query.q) {
|
||||
audiobooks = this.db.audiobooks.filter(ab => {
|
||||
return ab.libraryId === libraryId && ab.isSearchMatch(req.query.q)
|
||||
}).map(ab => ab.toJSONMinified())
|
||||
} else {
|
||||
audiobooks = this.db.audiobooks.filter(ab => ab.libraryId === libraryId).map(ab => ab.toJSONMinified())
|
||||
if (req.query.filter) {
|
||||
audiobooks = this.getFiltered(this.db.audiobooks, req.query.filter, req.user)
|
||||
}
|
||||
|
||||
|
||||
if (req.query.sort) {
|
||||
var orderByNumber = req.query.sort === 'book.volumeNumber'
|
||||
var direction = req.query.desc === '1' ? 'desc' : 'asc'
|
||||
audiobooks = sort(audiobooks)[direction]((ab) => {
|
||||
// Supports dot notation strings i.e. "book.title"
|
||||
var value = req.query.sort.split('.').reduce((a, b) => a[b], ab)
|
||||
if (orderByNumber && !isNaN(value)) return Number(value)
|
||||
return value
|
||||
})
|
||||
}
|
||||
|
||||
if (req.query.limit && !isNaN(req.query.limit)) {
|
||||
var page = req.query.page && !isNaN(req.query.page) ? Number(req.query.page) : 0
|
||||
var limit = Number(req.query.limit)
|
||||
var startIndex = page * limit
|
||||
audiobooks = audiobooks.slice(startIndex, startIndex + limit)
|
||||
}
|
||||
res.json(audiobooks)
|
||||
}
|
||||
|
||||
// api/libraries/:id/books/fs
|
||||
getBooksForLibrary2(req, res) {
|
||||
var libraryId = req.params.id
|
||||
var library = this.db.libraries.find(lib => lib.id === libraryId)
|
||||
if (!library) {
|
||||
return res.status(400).send('Library does not exist')
|
||||
}
|
||||
|
||||
var audiobooks = this.db.audiobooks.filter(ab => ab.libraryId === libraryId)
|
||||
var payload = {
|
||||
results: [],
|
||||
total: audiobooks.length,
|
||||
limit: req.query.limit && !isNaN(req.query.limit) ? Number(req.query.limit) : 0,
|
||||
page: req.query.page && !isNaN(req.query.page) ? Number(req.query.page) : 0,
|
||||
sortBy: req.query.sort,
|
||||
sortDesc: req.query.desc === '1',
|
||||
filterBy: req.query.filter
|
||||
}
|
||||
|
||||
if (payload.filterBy) {
|
||||
audiobooks = this.getFiltered(this.db.audiobooks, payload.filterBy, req.user)
|
||||
}
|
||||
|
||||
if (payload.sortBy) {
|
||||
var orderByNumber = payload.sortBy === 'book.volumeNumber'
|
||||
var direction = payload.sortDesc ? 'desc' : 'asc'
|
||||
audiobooks = sort(audiobooks)[direction]((ab) => {
|
||||
// Supports dot notation strings i.e. "book.title"
|
||||
var value = payload.sortBy.split('.').reduce((a, b) => a[b], ab)
|
||||
if (orderByNumber && !isNaN(value)) return Number(value)
|
||||
return value
|
||||
})
|
||||
}
|
||||
|
||||
if (payload.limit) {
|
||||
var startIndex = payload.page * payload.limit
|
||||
audiobooks = audiobooks.slice(startIndex, startIndex + payload.limit)
|
||||
}
|
||||
payload.results = audiobooks.map(ab => ab.toJSONExpanded())
|
||||
console.log('returning books', audiobooks.length)
|
||||
|
||||
res.json(payload)
|
||||
}
|
||||
|
||||
// PATCH: Change the order of libraries
|
||||
async reorder(req, res) {
|
||||
if (!req.user.isRoot) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue