2024-08-12 00:01:25 +02:00
|
|
|
const { Request, Response, NextFunction } = require('express')
|
2024-02-11 23:48:16 +01:00
|
|
|
const Logger = require('../Logger')
|
|
|
|
const SocketAuthority = require('../SocketAuthority')
|
|
|
|
const Database = require('../Database')
|
|
|
|
|
|
|
|
const { validateUrl } = require('../utils/index')
|
|
|
|
|
2024-08-12 00:01:25 +02:00
|
|
|
/**
|
|
|
|
* @typedef RequestUserObject
|
|
|
|
* @property {import('../models/User')} user
|
|
|
|
*
|
|
|
|
* @typedef {Request & RequestUserObject} RequestWithUser
|
|
|
|
*/
|
|
|
|
|
2024-02-11 23:48:16 +01:00
|
|
|
class CustomMetadataProviderController {
|
2024-08-11 00:15:21 +02:00
|
|
|
constructor() {}
|
2024-02-11 23:48:16 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* GET: /api/custom-metadata-providers
|
|
|
|
*
|
2024-08-12 00:01:25 +02:00
|
|
|
* @param {RequestWithUser} req
|
|
|
|
* @param {Response} res
|
2024-02-11 23:48:16 +01:00
|
|
|
*/
|
|
|
|
async getAll(req, res) {
|
|
|
|
const providers = await Database.customMetadataProviderModel.findAll()
|
|
|
|
|
|
|
|
res.json({
|
|
|
|
providers
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* POST: /api/custom-metadata-providers
|
|
|
|
*
|
2024-08-12 00:01:25 +02:00
|
|
|
* @param {RequestWithUser} req
|
|
|
|
* @param {Response} res
|
2024-02-11 23:48:16 +01:00
|
|
|
*/
|
|
|
|
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,
|
2024-08-11 00:15:21 +02:00
|
|
|
authHeaderValue: !authHeaderValue ? null : authHeaderValue
|
2024-02-11 23:48:16 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
// TODO: Necessary to emit to all clients?
|
|
|
|
SocketAuthority.emitter('custom_metadata_provider_added', provider.toClientJson())
|
|
|
|
|
|
|
|
res.json({
|
|
|
|
provider
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* DELETE: /api/custom-metadata-providers/:id
|
2024-08-11 00:15:21 +02:00
|
|
|
*
|
2024-08-12 00:01:25 +02:00
|
|
|
* @param {RequestWithUser} req
|
|
|
|
* @param {Response} res
|
2024-02-11 23:48:16 +01:00
|
|
|
*/
|
|
|
|
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
|
2024-08-11 00:15:21 +02:00
|
|
|
await Database.libraryModel.update(
|
|
|
|
{
|
|
|
|
provider: fallbackProvider
|
|
|
|
},
|
|
|
|
{
|
|
|
|
where: {
|
|
|
|
provider: slug
|
|
|
|
}
|
2024-02-11 23:48:16 +01:00
|
|
|
}
|
2024-08-11 00:15:21 +02:00
|
|
|
)
|
2024-02-11 23:48:16 +01:00
|
|
|
|
|
|
|
// TODO: Necessary to emit to all clients?
|
|
|
|
SocketAuthority.emitter('custom_metadata_provider_removed', providerClientJson)
|
|
|
|
|
|
|
|
res.sendStatus(200)
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Middleware that requires admin or up
|
2024-08-11 00:15:21 +02:00
|
|
|
*
|
2024-08-12 00:01:25 +02:00
|
|
|
* @param {RequestWithUser} req
|
|
|
|
* @param {Response} res
|
|
|
|
* @param {NextFunction} next
|
2024-02-11 23:48:16 +01:00
|
|
|
*/
|
|
|
|
async middleware(req, res, next) {
|
2024-08-11 23:07:29 +02:00
|
|
|
if (!req.user.isAdminOrUp) {
|
|
|
|
Logger.warn(`[CustomMetadataProviderController] Non-admin user "${req.user.username}" attempted access route "${req.path}"`)
|
2024-02-11 23:48:16 +01:00
|
|
|
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()
|