mirror of
https://github.com/advplyr/audiobookshelf.git
synced 2025-07-14 11:25:02 +02:00
Add custom metadata provider controller, update model, move to item metadata utils
This commit is contained in:
parent
ddf4b2646c
commit
0cf2f8885e
21 changed files with 496 additions and 373 deletions
117
server/controllers/CustomMetadataProviderController.js
Normal file
117
server/controllers/CustomMetadataProviderController.js
Normal file
|
@ -0,0 +1,117 @@
|
|||
const Logger = require('../Logger')
|
||||
const SocketAuthority = require('../SocketAuthority')
|
||||
const Database = require('../Database')
|
||||
|
||||
const { validateUrl } = require('../utils/index')
|
||||
|
||||
//
|
||||
// This is a controller for routes that don't have a home yet :(
|
||||
//
|
||||
class CustomMetadataProviderController {
|
||||
constructor() { }
|
||||
|
||||
/**
|
||||
* GET: /api/custom-metadata-providers
|
||||
*
|
||||
* @param {import('express').Request} req
|
||||
* @param {import('express').Response} res
|
||||
*/
|
||||
async getAll(req, res) {
|
||||
const providers = await Database.customMetadataProviderModel.findAll()
|
||||
|
||||
res.json({
|
||||
providers
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* POST: /api/custom-metadata-providers
|
||||
*
|
||||
* @param {import('express').Request} req
|
||||
* @param {import('express').Response} res
|
||||
*/
|
||||
async create(req, res) {
|
||||
const { name, url, mediaType, authHeaderValue } = req.body
|
||||
|
||||
if (!name || !url || !mediaType) {
|
||||
return res.status(400).send('Invalid request body')
|
||||
}
|
||||
|
||||
const validUrl = validateUrl(url)
|
||||
if (!validUrl) {
|
||||
Logger.error(`[CustomMetadataProviderController] Invalid url "${url}"`)
|
||||
return res.status(400).send('Invalid url')
|
||||
}
|
||||
|
||||
const provider = await Database.customMetadataProviderModel.create({
|
||||
name,
|
||||
mediaType,
|
||||
url,
|
||||
authHeaderValue: !authHeaderValue ? null : authHeaderValue,
|
||||
})
|
||||
|
||||
// TODO: Necessary to emit to all clients?
|
||||
SocketAuthority.emitter('custom_metadata_provider_added', provider.toClientJson())
|
||||
|
||||
res.json({
|
||||
provider
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* DELETE: /api/custom-metadata-providers/:id
|
||||
*
|
||||
* @param {import('express').Request} req
|
||||
* @param {import('express').Response} res
|
||||
*/
|
||||
async delete(req, res) {
|
||||
const slug = `custom-${req.params.id}`
|
||||
|
||||
/** @type {import('../models/CustomMetadataProvider')} */
|
||||
const provider = req.customMetadataProvider
|
||||
const providerClientJson = provider.toClientJson()
|
||||
|
||||
const fallbackProvider = provider.mediaType === 'book' ? 'google' : 'itunes'
|
||||
|
||||
await provider.destroy()
|
||||
|
||||
// Libraries using this provider fallback to default provider
|
||||
await Database.libraryModel.update({
|
||||
provider: fallbackProvider
|
||||
}, {
|
||||
where: {
|
||||
provider: slug
|
||||
}
|
||||
})
|
||||
|
||||
// TODO: Necessary to emit to all clients?
|
||||
SocketAuthority.emitter('custom_metadata_provider_removed', providerClientJson)
|
||||
|
||||
res.sendStatus(200)
|
||||
}
|
||||
|
||||
/**
|
||||
* Middleware that requires admin or up
|
||||
*
|
||||
* @param {import('express').Request} req
|
||||
* @param {import('express').Response} res
|
||||
* @param {import('express').NextFunction} next
|
||||
*/
|
||||
async middleware(req, res, next) {
|
||||
if (!req.user.isAdminOrUp) {
|
||||
Logger.warn(`[CustomMetadataProviderController] Non-admin user "${req.user.username}" attempted access route "${req.path}"`)
|
||||
return res.sendStatus(403)
|
||||
}
|
||||
|
||||
// If id param then add req.customMetadataProvider
|
||||
if (req.params.id) {
|
||||
req.customMetadataProvider = await Database.customMetadataProviderModel.findByPk(req.params.id)
|
||||
if (!req.customMetadataProvider) {
|
||||
return res.sendStatus(404)
|
||||
}
|
||||
}
|
||||
|
||||
next()
|
||||
}
|
||||
}
|
||||
module.exports = new CustomMetadataProviderController()
|
|
@ -33,6 +33,14 @@ class LibraryController {
|
|||
return res.status(500).send('Invalid request')
|
||||
}
|
||||
|
||||
// Validate that the custom provider exists if given any
|
||||
if (newLibraryPayload.provider?.startsWith('custom-')) {
|
||||
if (!await Database.customMetadataProviderModel.checkExistsBySlug(newLibraryPayload.provider)) {
|
||||
Logger.error(`[LibraryController] Custom metadata provider "${newLibraryPayload.provider}" does not exist`)
|
||||
return res.status(400).send('Custom metadata provider does not exist')
|
||||
}
|
||||
}
|
||||
|
||||
// Validate folder paths exist or can be created & resolve rel paths
|
||||
// returns 400 if a folder fails to access
|
||||
newLibraryPayload.folders = newLibraryPayload.folders.map(f => {
|
||||
|
@ -51,11 +59,6 @@ class LibraryController {
|
|||
}
|
||||
}
|
||||
|
||||
// Validate that the custom provider exists if given any
|
||||
if (newLibraryPayload.provider && newLibraryPayload.provider.startsWith("custom-")) {
|
||||
await Database.doesCustomProviderExistWithSlug(newLibraryPayload.provider)
|
||||
}
|
||||
|
||||
const library = new Library()
|
||||
|
||||
let currentLargestDisplayOrder = await Database.libraryModel.getMaxDisplayOrder()
|
||||
|
@ -91,19 +94,27 @@ class LibraryController {
|
|||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* GET: /api/libraries/:id
|
||||
*
|
||||
* @param {import('express').Request} req
|
||||
* @param {import('express').Response} res
|
||||
*/
|
||||
async findOne(req, res) {
|
||||
const includeArray = (req.query.include || '').split(',')
|
||||
if (includeArray.includes('filterdata')) {
|
||||
const filterdata = await libraryFilters.getFilterData(req.library.mediaType, req.library.id)
|
||||
const customMetadataProviders = await Database.customMetadataProviderModel.getForClientByMediaType(req.library.mediaType)
|
||||
|
||||
return res.json({
|
||||
filterdata,
|
||||
issues: filterdata.numIssues,
|
||||
numUserPlaylists: await Database.playlistModel.getNumPlaylistsForUserAndLibrary(req.user.id, req.library.id),
|
||||
customMetadataProviders,
|
||||
library: req.library
|
||||
})
|
||||
}
|
||||
return res.json(req.library)
|
||||
res.json(req.library)
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -120,6 +131,14 @@ class LibraryController {
|
|||
async update(req, res) {
|
||||
const library = req.library
|
||||
|
||||
// Validate that the custom provider exists if given any
|
||||
if (req.body.provider?.startsWith('custom-')) {
|
||||
if (!await Database.customMetadataProviderModel.checkExistsBySlug(req.body.provider)) {
|
||||
Logger.error(`[LibraryController] Custom metadata provider "${req.body.provider}" does not exist`)
|
||||
return res.status(400).send('Custom metadata provider does not exist')
|
||||
}
|
||||
}
|
||||
|
||||
// Validate new folder paths exist or can be created & resolve rel paths
|
||||
// returns 400 if a new folder fails to access
|
||||
if (req.body.folders) {
|
||||
|
@ -180,11 +199,6 @@ class LibraryController {
|
|||
}
|
||||
}
|
||||
|
||||
// Validate that the custom provider exists if given any
|
||||
if (req.body.provider && req.body.provider.startsWith("custom-")) {
|
||||
await Database.doesCustomProviderExistWithSlug(req.body.provider)
|
||||
}
|
||||
|
||||
const hasUpdates = library.update(req.body)
|
||||
// TODO: Should check if this is an update to folder paths or name only
|
||||
if (hasUpdates) {
|
||||
|
|
|
@ -717,95 +717,5 @@ class MiscController {
|
|||
const stats = await adminStats.getStatsForYear(year)
|
||||
res.json(stats)
|
||||
}
|
||||
|
||||
/**
|
||||
* GET: /api/custom-metadata-providers
|
||||
*
|
||||
* @param {import('express').Request} req
|
||||
* @param {import('express').Response} res
|
||||
*/
|
||||
async getCustomMetadataProviders(req, res) {
|
||||
const providers = await Database.customMetadataProviderModel.findAll()
|
||||
|
||||
res.json({
|
||||
providers: providers.map((p) => p.toUserJson()),
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* GET: /api/custom-metadata-providers/admin
|
||||
*
|
||||
* @param {import('express').Request} req
|
||||
* @param {import('express').Response} res
|
||||
*/
|
||||
async getAdminCustomMetadataProviders(req, res) {
|
||||
if (!req.user.isAdminOrUp) {
|
||||
Logger.error(`[MiscController] Non-admin user "${req.user.username}" attempted to get admin custom metadata providers`)
|
||||
return res.sendStatus(403)
|
||||
}
|
||||
|
||||
const providers = await Database.customMetadataProviderModel.findAll()
|
||||
|
||||
res.json({
|
||||
providers,
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* PATCH: /api/custom-metadata-providers/admin
|
||||
*
|
||||
* @param {import('express').Request} req
|
||||
* @param {import('express').Response} res
|
||||
*/
|
||||
async addCustomMetadataProviders(req, res) {
|
||||
if (!req.user.isAdminOrUp) {
|
||||
Logger.error(`[MiscController] Non-admin user "${req.user.username}" attempted to add admin custom metadata providers`)
|
||||
return res.sendStatus(403)
|
||||
}
|
||||
|
||||
const { name, url, apiKey } = req.body
|
||||
|
||||
if (!name || !url || !apiKey) {
|
||||
return res.status(500).send(`Invalid patch data`)
|
||||
}
|
||||
|
||||
const provider = await Database.customMetadataProviderModel.create({
|
||||
name,
|
||||
url,
|
||||
apiKey,
|
||||
})
|
||||
|
||||
SocketAuthority.adminEmitter('custom_metadata_provider_added', provider)
|
||||
|
||||
res.json({
|
||||
provider,
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* DELETE: /api/custom-metadata-providers/admin/:id
|
||||
*
|
||||
* @param {import('express').Request} req
|
||||
* @param {import('express').Response} res
|
||||
*/
|
||||
async deleteCustomMetadataProviders(req, res) {
|
||||
if (!req.user.isAdminOrUp) {
|
||||
Logger.error(`[MiscController] Non-admin user "${req.user.username}" attempted to delete admin custom metadata providers`)
|
||||
return res.sendStatus(403)
|
||||
}
|
||||
|
||||
const { id } = req.params
|
||||
|
||||
if (!id) {
|
||||
return res.status(500).send(`Invalid delete data`)
|
||||
}
|
||||
|
||||
const provider = await Database.customMetadataProviderModel.findByPk(id)
|
||||
await Database.removeCustomMetadataProviderById(id)
|
||||
|
||||
SocketAuthority.adminEmitter('custom_metadata_provider_removed', provider)
|
||||
|
||||
res.sendStatus(200)
|
||||
}
|
||||
}
|
||||
module.exports = new MiscController()
|
||||
|
|
|
@ -161,7 +161,7 @@ class SessionController {
|
|||
* @typedef batchDeleteReqBody
|
||||
* @property {string[]} sessions
|
||||
*
|
||||
* @param {import('express').Request<{}, {}, batchDeleteReqBody, {}} req
|
||||
* @param {import('express').Request<{}, {}, batchDeleteReqBody, {}>} req
|
||||
* @param {import('express').Response} res
|
||||
*/
|
||||
async batchDelete(req, res) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue