mirror of
https://github.com/advplyr/audiobookshelf.git
synced 2026-02-03 16:59:41 +00:00
New data model play media entity, PlaybackSessionManager
This commit is contained in:
parent
1cf9e85272
commit
099ae7c776
54 changed files with 841 additions and 902 deletions
|
|
@ -1,57 +0,0 @@
|
|||
const Logger = require('../Logger')
|
||||
|
||||
class AudiobookController {
|
||||
constructor() { }
|
||||
|
||||
async findOne(req, res) {
|
||||
if (req.query.expanded == 1) return res.json(req.audiobook.toJSONExpanded())
|
||||
return res.json(req.audiobook)
|
||||
}
|
||||
|
||||
async findWithItem(req, res) {
|
||||
if (req.query.expanded == 1) {
|
||||
return res.json({
|
||||
libraryItem: req.libraryItem.toJSONExpanded(),
|
||||
audiobook: req.audiobook.toJSONExpanded()
|
||||
})
|
||||
}
|
||||
res.json({
|
||||
libraryItem: req.libraryItem.toJSON(),
|
||||
audiobook: req.audiobook.toJSON()
|
||||
})
|
||||
}
|
||||
|
||||
// PATCH: api/audiobooks/:id/tracks
|
||||
async updateTracks(req, res) {
|
||||
var libraryItem = req.libraryItem
|
||||
var audiobook = req.audiobook
|
||||
var orderedFileData = req.body.orderedFileData
|
||||
audiobook.updateAudioTracks(orderedFileData)
|
||||
await this.db.updateLibraryItem(libraryItem)
|
||||
this.emitter('item_updated', libraryItem.toJSONExpanded())
|
||||
res.json(libraryItem.toJSON())
|
||||
}
|
||||
|
||||
middleware(req, res, next) {
|
||||
var audiobook = null
|
||||
var libraryItem = this.db.libraryItems.find(li => {
|
||||
if (li.mediaType != 'book') return false
|
||||
audiobook = li.media.getAudiobookById(req.params.id)
|
||||
return !!audiobook
|
||||
})
|
||||
if (!audiobook) return res.sendStatus(404)
|
||||
|
||||
if (req.method == 'DELETE' && !req.user.canDelete) {
|
||||
Logger.warn(`[AudiobookController] User attempted to delete without permission`, req.user)
|
||||
return res.sendStatus(403)
|
||||
} else if ((req.method == 'PATCH' || req.method == 'POST') && !req.user.canUpdate) {
|
||||
Logger.warn('[AudiobookController] User attempted to update without permission', req.user)
|
||||
return res.sendStatus(403)
|
||||
}
|
||||
|
||||
req.libraryItem = libraryItem
|
||||
req.audiobook = audiobook
|
||||
next()
|
||||
}
|
||||
}
|
||||
module.exports = new AudiobookController()
|
||||
|
|
@ -5,7 +5,7 @@ class BackupController {
|
|||
|
||||
async delete(req, res) {
|
||||
if (!req.user.isRoot) {
|
||||
Logger.error(`[ApiController] Non-Root user attempting to delete backup`, req.user)
|
||||
Logger.error(`[BackupController] Non-Root user attempting to delete backup`, req.user)
|
||||
return res.sendStatus(403)
|
||||
}
|
||||
var backup = this.backupManager.backups.find(b => b.id === req.params.id)
|
||||
|
|
@ -18,11 +18,11 @@ class BackupController {
|
|||
|
||||
async upload(req, res) {
|
||||
if (!req.user.isRoot) {
|
||||
Logger.error(`[ApiController] Non-Root user attempting to upload backup`, req.user)
|
||||
Logger.error(`[BackupController] Non-Root user attempting to upload backup`, req.user)
|
||||
return res.sendStatus(403)
|
||||
}
|
||||
if (!req.files.file) {
|
||||
Logger.error('[ApiController] Upload backup invalid')
|
||||
Logger.error('[BackupController] Upload backup invalid')
|
||||
return res.sendStatus(500)
|
||||
}
|
||||
this.backupManager.uploadBackup(req, res)
|
||||
|
|
|
|||
|
|
@ -344,7 +344,7 @@ class LibraryController {
|
|||
// PATCH: Change the order of libraries
|
||||
async reorder(req, res) {
|
||||
if (!req.user.isRoot) {
|
||||
Logger.error('[ApiController] ReorderLibraries invalid user', req.user)
|
||||
Logger.error('[LibraryController] ReorderLibraries invalid user', req.user)
|
||||
return res.sendStatus(403)
|
||||
}
|
||||
|
||||
|
|
@ -353,7 +353,7 @@ class LibraryController {
|
|||
for (let i = 0; i < orderdata.length; i++) {
|
||||
var library = this.db.libraries.find(lib => lib.id === orderdata[i].id)
|
||||
if (!library) {
|
||||
Logger.error(`[ApiController] Invalid library not found in reorder ${orderdata[i].id}`)
|
||||
Logger.error(`[LibraryController] Invalid library not found in reorder ${orderdata[i].id}`)
|
||||
return res.sendStatus(500)
|
||||
}
|
||||
if (library.update({ displayOrder: orderdata[i].newOrder })) {
|
||||
|
|
@ -363,9 +363,9 @@ class LibraryController {
|
|||
}
|
||||
|
||||
if (hasUpdates) {
|
||||
Logger.info(`[ApiController] Updated library display orders`)
|
||||
Logger.info(`[LibraryController] Updated library display orders`)
|
||||
} else {
|
||||
Logger.info(`[ApiController] Library orders were up to date`)
|
||||
Logger.info(`[LibraryController] Library orders were up to date`)
|
||||
}
|
||||
|
||||
var libraries = this.db.libraries.map(lib => lib.toJSON())
|
||||
|
|
|
|||
|
|
@ -142,9 +142,16 @@ class LibraryItemController {
|
|||
res.sendStatus(500)
|
||||
}
|
||||
|
||||
// GET: api/items/:id/play
|
||||
|
||||
// POST: api/items/:id/play
|
||||
startPlaybackSession(req, res) {
|
||||
res.sendStatus(200)
|
||||
var playbackMediaEntity = req.libraryItem.getPlaybackMediaEntity()
|
||||
if (!playbackMediaEntity) {
|
||||
Logger.error(`[LibraryItemController] startPlaybackSession no playback media entity ${req.libraryItem.id}`)
|
||||
return res.sendStatus(404)
|
||||
}
|
||||
const options = req.body || {}
|
||||
this.playbackSessionManager.startSessionRequest(req.user, req.libraryItem, playbackMediaEntity, options, res)
|
||||
}
|
||||
|
||||
// POST api/items/:id/match
|
||||
|
|
|
|||
71
server/controllers/MediaEntityController.js
Normal file
71
server/controllers/MediaEntityController.js
Normal file
|
|
@ -0,0 +1,71 @@
|
|||
const Logger = require('../Logger')
|
||||
|
||||
class MediaEntityController {
|
||||
constructor() { }
|
||||
|
||||
async findOne(req, res) {
|
||||
if (req.query.expanded == 1) return res.json(req.mediaEntity.toJSONExpanded())
|
||||
return res.json(req.mediaEntity)
|
||||
}
|
||||
|
||||
async findWithItem(req, res) {
|
||||
if (req.query.expanded == 1) {
|
||||
return res.json({
|
||||
libraryItem: req.libraryItem.toJSONExpanded(),
|
||||
mediaEntity: req.mediaEntity.toJSONExpanded()
|
||||
})
|
||||
}
|
||||
res.json({
|
||||
libraryItem: req.libraryItem.toJSON(),
|
||||
mediaEntity: req.mediaEntity.toJSON()
|
||||
})
|
||||
}
|
||||
|
||||
// PATCH: api/entities/:id/tracks
|
||||
async updateTracks(req, res) {
|
||||
var libraryItem = req.libraryItem
|
||||
var mediaEntity = req.mediaEntity
|
||||
var orderedFileData = req.body.orderedFileData
|
||||
if (!mediaEntity.updateAudioTracks) {
|
||||
Logger.error(`[MediaEntityController] updateTracks invalid media entity ${mediaEntity.id}`)
|
||||
return res.sendStatus(500)
|
||||
}
|
||||
mediaEntity.updateAudioTracks(orderedFileData)
|
||||
await this.db.updateLibraryItem(libraryItem)
|
||||
this.emitter('item_updated', libraryItem.toJSONExpanded())
|
||||
res.json(libraryItem.toJSON())
|
||||
}
|
||||
|
||||
// POST: api/entities/:id/play
|
||||
startPlaybackSession(req, res) {
|
||||
if (!req.mediaEntity.isPlaybackMediaEntity) {
|
||||
Logger.error(`[MediaEntityController] startPlaybackSession invalid media entity ${req.mediaEntity.id}`)
|
||||
return res.sendStatus(500)
|
||||
}
|
||||
const options = req.body || {}
|
||||
this.playbackSessionManager.startSessionRequest(req.user, req.libraryItem, req.mediaEntity, options, res)
|
||||
}
|
||||
|
||||
middleware(req, res, next) {
|
||||
var mediaEntity = null
|
||||
var libraryItem = this.db.libraryItems.find(li => {
|
||||
if (li.mediaType != 'book') return false
|
||||
mediaEntity = li.media.getMediaEntityById(req.params.id)
|
||||
return !!mediaEntity
|
||||
})
|
||||
if (!mediaEntity) return res.sendStatus(404)
|
||||
|
||||
if (req.method == 'DELETE' && !req.user.canDelete) {
|
||||
Logger.warn(`[MediaEntityController] User attempted to delete without permission`, req.user)
|
||||
return res.sendStatus(403)
|
||||
} else if ((req.method == 'PATCH' || req.method == 'POST') && !req.user.canUpdate) {
|
||||
Logger.warn('[MediaEntityController] User attempted to update without permission', req.user)
|
||||
return res.sendStatus(403)
|
||||
}
|
||||
|
||||
req.mediaEntity = mediaEntity
|
||||
req.libraryItem = libraryItem
|
||||
next()
|
||||
}
|
||||
}
|
||||
module.exports = new MediaEntityController()
|
||||
33
server/controllers/SessionController.js
Normal file
33
server/controllers/SessionController.js
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
const Logger = require('../Logger')
|
||||
|
||||
class SessionController {
|
||||
constructor() { }
|
||||
|
||||
async findOne(req, res) {
|
||||
return res.json(req.session)
|
||||
}
|
||||
|
||||
// POST: api/session/:id/sync
|
||||
sync(req, res) {
|
||||
this.playbackSessionManager.syncSessionRequest(req.user, req.session, req.body, res)
|
||||
}
|
||||
|
||||
// POST: api/session/:id/close
|
||||
close(req, res) {
|
||||
this.playbackSessionManager.closeSessionRequest(req.user, req.session, req.body, res)
|
||||
}
|
||||
|
||||
middleware(req, res, next) {
|
||||
var playbackSession = this.playbackSessionManager.getSession(req.params.id)
|
||||
if (!playbackSession) return res.sendStatus(404)
|
||||
|
||||
if (playbackSession.userId !== req.user.id) {
|
||||
Logger.error(`[SessionController] User "${req.user.username}" attempting to access session belonging to another user "${req.params.id}"`)
|
||||
return res.sendStatus(404)
|
||||
}
|
||||
|
||||
req.session = playbackSession
|
||||
next()
|
||||
}
|
||||
}
|
||||
module.exports = new SessionController()
|
||||
|
|
@ -6,6 +6,26 @@ const { getId } = require('../utils/index')
|
|||
class UserController {
|
||||
constructor() { }
|
||||
|
||||
findAll(req, res) {
|
||||
if (!req.user.isRoot) return res.sendStatus(403)
|
||||
var users = this.db.users.map(u => this.userJsonWithItemProgressDetails(u))
|
||||
res.json(users)
|
||||
}
|
||||
|
||||
findOne(req, res) {
|
||||
if (!req.user.isRoot) {
|
||||
Logger.error('User other than root attempting to get user', req.user)
|
||||
return res.sendStatus(403)
|
||||
}
|
||||
|
||||
var user = this.db.users.find(u => u.id === req.params.id)
|
||||
if (!user) {
|
||||
return res.sendStatus(404)
|
||||
}
|
||||
|
||||
res.json(this.userJsonWithItemProgressDetails(user))
|
||||
}
|
||||
|
||||
async create(req, res) {
|
||||
if (!req.user.isRoot) {
|
||||
Logger.warn('Non-root user attempted to create user', req.user)
|
||||
|
|
@ -36,26 +56,6 @@ class UserController {
|
|||
}
|
||||
}
|
||||
|
||||
findAll(req, res) {
|
||||
if (!req.user.isRoot) return res.sendStatus(403)
|
||||
var users = this.db.users.map(u => this.userJsonWithBookProgressDetails(u))
|
||||
res.json(users)
|
||||
}
|
||||
|
||||
findOne(req, res) {
|
||||
if (!req.user.isRoot) {
|
||||
Logger.error('User other than root attempting to get user', req.user)
|
||||
return res.sendStatus(403)
|
||||
}
|
||||
|
||||
var user = this.db.users.find(u => u.id === req.params.id)
|
||||
if (!user) {
|
||||
return res.sendStatus(404)
|
||||
}
|
||||
|
||||
res.json(this.userJsonWithBookProgressDetails(user))
|
||||
}
|
||||
|
||||
async update(req, res) {
|
||||
if (!req.user.isRoot) {
|
||||
Logger.error('User other than root attempting to update user', req.user)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue