mirror of
https://github.com/advplyr/audiobookshelf.git
synced 2025-06-25 18:29:10 +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
|
@ -1,4 +1,12 @@
|
|||
const { DataTypes, Model, Sequelize } = require('sequelize')
|
||||
const { DataTypes, Model } = require('sequelize')
|
||||
|
||||
/**
|
||||
* @typedef ClientCustomMetadataProvider
|
||||
* @property {UUIDV4} id
|
||||
* @property {string} name
|
||||
* @property {string} url
|
||||
* @property {string} slug
|
||||
*/
|
||||
|
||||
class CustomMetadataProvider extends Model {
|
||||
constructor(values, options) {
|
||||
|
@ -7,26 +15,67 @@ class CustomMetadataProvider extends Model {
|
|||
/** @type {UUIDV4} */
|
||||
this.id
|
||||
/** @type {string} */
|
||||
this.mediaType
|
||||
/** @type {string} */
|
||||
this.name
|
||||
/** @type {string} */
|
||||
this.url
|
||||
/** @type {string} */
|
||||
this.apiKey
|
||||
this.authHeaderValue
|
||||
/** @type {Object} */
|
||||
this.extraData
|
||||
/** @type {Date} */
|
||||
this.createdAt
|
||||
/** @type {Date} */
|
||||
this.updatedAt
|
||||
}
|
||||
|
||||
getSlug() {
|
||||
return `custom-${this.id}`
|
||||
}
|
||||
|
||||
toUserJson() {
|
||||
/**
|
||||
* Safe for clients
|
||||
* @returns {ClientCustomMetadataProvider}
|
||||
*/
|
||||
toClientJson() {
|
||||
return {
|
||||
name: this.name,
|
||||
id: this.id,
|
||||
name: this.name,
|
||||
mediaType: this.mediaType,
|
||||
slug: this.getSlug()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get providers for client by media type
|
||||
* Currently only available for "book" media type
|
||||
*
|
||||
* @param {string} mediaType
|
||||
* @returns {Promise<ClientCustomMetadataProvider[]>}
|
||||
*/
|
||||
static async getForClientByMediaType(mediaType) {
|
||||
if (mediaType !== 'book') return []
|
||||
const customMetadataProviders = await this.findAll({
|
||||
where: {
|
||||
mediaType
|
||||
}
|
||||
})
|
||||
return customMetadataProviders.map(cmp => cmp.toClientJson())
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if provider exists by slug
|
||||
*
|
||||
* @param {string} providerSlug
|
||||
* @returns {Promise<boolean>}
|
||||
*/
|
||||
static async checkExistsBySlug(providerSlug) {
|
||||
const providerId = providerSlug?.split?.('custom-')[1]
|
||||
if (!providerId) return false
|
||||
|
||||
return (await this.count({ where: { id: providerId } })) > 0
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize model
|
||||
|
@ -40,8 +89,10 @@ class CustomMetadataProvider extends Model {
|
|||
primaryKey: true
|
||||
},
|
||||
name: DataTypes.STRING,
|
||||
mediaType: DataTypes.STRING,
|
||||
url: DataTypes.STRING,
|
||||
apiKey: DataTypes.STRING,
|
||||
authHeaderValue: DataTypes.STRING,
|
||||
extraData: DataTypes.JSON
|
||||
}, {
|
||||
sequelize,
|
||||
modelName: 'customMetadataProvider'
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue