Add custom metadata provider controller, update model, move to item metadata utils

This commit is contained in:
advplyr 2024-02-11 16:48:16 -06:00
parent ddf4b2646c
commit 0cf2f8885e
21 changed files with 496 additions and 373 deletions

View File

@ -109,11 +109,6 @@ export default {
id: 'config-authentication',
title: this.$strings.HeaderAuthentication,
path: '/config/authentication'
},
{
id: 'config-custom-metadata-providers',
title: this.$strings.HeaderCustomMetadataProviders,
path: '/config/custom-metadata-providers'
}
]

View File

@ -1,6 +1,7 @@
<template>
<div class="bg-bg rounded-md shadow-lg border border-white border-opacity-5 p-4 mb-8">
<div class="flex items-center mb-2">
<slot name="header-prefix"></slot>
<h1 class="text-xl">{{ headerText }}</h1>
<slot name="header-items"></slot>

View File

@ -6,18 +6,23 @@
</div>
</template>
<form @submit.prevent="submitForm">
<div class="px-4 w-full text-sm py-6 rounded-lg bg-bg shadow-lg border border-black-300 overflow-y-auto overflow-x-hidden" style="min-height: 400px; max-height: 80vh">
<div class="px-4 w-full flex items-center text-sm py-6 rounded-lg bg-bg shadow-lg border border-black-300 overflow-y-auto overflow-x-hidden" style="min-height: 400px; max-height: 80vh">
<div class="w-full p-8">
<div class="w-full mb-4">
<ui-text-input-with-label v-model="newName" :label="$strings.LabelName" />
<div class="flex mb-2">
<div class="w-3/4 p-1">
<ui-text-input-with-label v-model="newName" :label="$strings.LabelName" />
</div>
<div class="w-1/4 p-1">
<ui-text-input-with-label value="Book" readonly :label="$strings.LabelMediaType" />
</div>
</div>
<div class="w-full mb-4">
<ui-text-input-with-label v-model="newUrl" :label="$strings.LabelUrl" />
<div class="w-full mb-2 p-1">
<ui-text-input-with-label v-model="newUrl" label="URL" />
</div>
<div class="w-full mb-4">
<ui-text-input-with-label v-model="newApiKey" :label="$strings.LabelApiKey" type="password" />
<div class="w-full mb-2 p-1">
<ui-text-input-with-label v-model="newAuthHeaderValue" :label="'Authorization Header Value'" type="password" />
</div>
<div class="flex pt-4 px-2">
<div class="flex px-1 pt-4">
<div class="flex-grow" />
<ui-btn color="success" type="submit">{{ $strings.ButtonAdd }}</ui-btn>
</div>
@ -30,14 +35,14 @@
<script>
export default {
props: {
value: Boolean,
value: Boolean
},
data() {
return {
processing: false,
newName: "",
newUrl: "",
newApiKey: "",
newName: '',
newUrl: '',
newAuthHeaderValue: ''
}
},
watch: {
@ -57,46 +62,42 @@ export default {
set(val) {
this.$emit('input', val)
}
},
}
},
methods: {
close() {
// Force close when navigating - used in the table
if (this.$refs.modal) this.$refs.modal.setHide()
},
submitForm() {
if (!this.newName || !this.newUrl || !this.newApiKey) {
this.$toast.error('Must add name, url and API key')
if (!this.newName || !this.newUrl) {
this.$toast.error('Must add name and url')
return
}
this.processing = true
this.$axios
.$patch('/api/custom-metadata-providers/admin', {
name: this.newName,
url: this.newUrl,
apiKey: this.newApiKey,
})
.then((data) => {
this.processing = false
if (data.error) {
this.$toast.error(`Failed to add provider: ${data.error}`)
} else {
this.$toast.success('New provider added')
this.show = false
}
})
.catch((error) => {
this.processing = false
console.error('Failed to add provider', error)
this.$toast.error('Failed to add provider')
})
.$post('/api/custom-metadata-providers', {
name: this.newName,
url: this.newUrl,
mediaType: 'book', // Currently only supporting book mediaType
authHeaderValue: this.newAuthHeaderValue
})
.then((data) => {
this.$emit('added', data.provider)
this.$toast.success('New provider added')
this.show = false
})
.catch((error) => {
const errorMsg = error.response?.data || 'Unknown error'
console.error('Failed to add provider', error)
this.$toast.error('Failed to add provider: ' + errorMsg)
})
.finally(() => {
this.processing = false
})
},
init() {
this.processing = false
this.newName = ""
this.newUrl = ""
this.newApiKey = ""
this.newName = ''
this.newUrl = ''
this.newAuthHeaderValue = ''
}
},
mounted() {}

View File

@ -328,6 +328,17 @@ export default {
console.error('PersistProvider', error)
}
},
getDefaultBookProvider() {
let provider = localStorage.getItem('book-provider')
if (!provider) return 'google'
// Validate book provider
if (!this.$store.getters['scanners/checkBookProviderExists'](provider)) {
console.error('Stored book provider does not exist', provider)
localStorage.removeItem('book-provider')
return 'google'
}
return provider
},
getSearchQuery() {
if (this.isPodcast) return `term=${encodeURIComponent(this.searchTitle)}`
var searchQuery = `provider=${this.provider}&fallbackTitleOnly=1&title=${encodeURIComponent(this.searchTitle)}`
@ -434,7 +445,9 @@ export default {
this.searchTitle = this.libraryItem.media.metadata.title
this.searchAuthor = this.libraryItem.media.metadata.authorName || ''
if (this.isPodcast) this.provider = 'itunes'
else this.provider = localStorage.getItem('book-provider') || 'google'
else {
this.provider = this.getDefaultBookProvider()
}
// Prefer using ASIN if set and using audible provider
if (this.provider.startsWith('audible') && this.libraryItem.media.metadata.asin) {

View File

@ -1,96 +1,73 @@
<template>
<div>
<div class="text-center">
<table id="providers">
<tr>
<th>{{ $strings.LabelName }}</th>
<th>{{ $strings.LabelUrl }}</th>
<th>{{ $strings.LabelApiKey }}</th>
<th class="w-12"></th>
</tr>
<tr v-for="provider in providers" :key="provider.id">
<td class="text-sm">{{ provider.name }}</td>
<td class="text-sm">{{ provider.url }}</td>
<td class="text-sm">
<span class="custom-provider-api-key">{{ provider.apiKey }}</span>
</td>
<td class="py-0">
<div class="h-8 w-8 flex items-center justify-center text-white text-opacity-50 hover:text-error cursor-pointer" @click.stop="removeProvider(provider)">
<button type="button" :aria-label="$strings.ButtonDelete" class="material-icons text-base">delete</button>
</div>
</td>
</tr>
</table>
<div class="min-h-40">
<table v-if="providers.length" id="providers">
<tr>
<th>{{ $strings.LabelName }}</th>
<th>URL</th>
<th>Authorization Header Value</th>
<th class="w-12"></th>
</tr>
<tr v-for="provider in providers" :key="provider.id">
<td class="text-sm">{{ provider.name }}</td>
<td class="text-sm">{{ provider.url }}</td>
<td class="text-sm">
<span v-if="provider.authHeaderValue" class="custom-provider-api-key">{{ provider.authHeaderValue }}</span>
</td>
<td class="py-0">
<div class="h-8 w-8 flex items-center justify-center text-white text-opacity-50 hover:text-error cursor-pointer" @click.stop="removeProvider(provider)">
<button type="button" :aria-label="$strings.ButtonDelete" class="material-icons text-base">delete</button>
</div>
</td>
</tr>
</table>
<div v-else-if="!processing" class="text-center py-8">
<p class="text-lg">No custom metadata providers</p>
</div>
<div v-if="processing" class="absolute inset-0 h-full flex items-center justify-center bg-black/40 rounded-md">
<ui-loading-indicator />
</div>
</div>
</template>
<script>
export default {
props: {
providers: {
type: Array,
default: () => []
},
processing: Boolean
},
data() {
return {
providers: [],
}
return {}
},
methods: {
addedProvider(provider) {
if (!Array.isArray(this.providers)) return
this.providers.push(provider)
},
removedProvider(provider) {
this.providers = this.providers.filter((p) => p.id !== provider.id)
},
removeProvider(provider) {
this.$axios
.$delete(`/api/custom-metadata-providers/admin/${provider.id}`)
.then((data) => {
if (data.error) {
this.$toast.error(`Failed to remove provider: ${data.error}`)
} else {
this.$toast.success('Provider removed')
}
})
.catch((error) => {
console.error('Failed to remove provider', error)
this.$toast.error('Failed to remove provider')
})
},
loadProviders() {
this.$axios.$get('/api/custom-metadata-providers/admin')
.then((res) => {
this.providers = res.providers
})
.catch((error) => {
console.error('Failed', error)
})
},
init(attempts = 0) {
if (!this.$root.socket) {
if (attempts > 10) {
return console.error('Failed to setup socket listeners')
}
setTimeout(() => {
this.init(++attempts)
}, 250)
return
}
this.$root.socket.on('custom_metadata_provider_added', this.addedProvider)
this.$root.socket.on('custom_metadata_provider_removed', this.removedProvider)
}
},
mounted() {
this.loadProviders()
this.init()
},
beforeDestroy() {
if (this.$refs.addModal) {
this.$refs.addModal.close()
}
const payload = {
message: `Are you sure you want remove custom metadata provider "${provider.name}"?`,
callback: (confirmed) => {
if (confirmed) {
this.$emit('update:processing', true)
if (this.$root.socket) {
this.$root.socket.off('custom_metadata_provider_added', this.addedProvider)
this.$root.socket.off('custom_metadata_provider_removed', this.removedProvider)
this.$axios
.$delete(`/api/custom-metadata-providers/${provider.id}`)
.then(() => {
this.$toast.success('Provider removed')
this.$emit('removed', provider.id)
})
.catch((error) => {
console.error('Failed to remove provider', error)
this.$toast.error('Failed to remove provider')
})
.finally(() => {
this.$emit('update:processing', false)
})
}
},
type: 'yesNo'
}
this.$store.commit('globals/setConfirmPrompt', payload)
}
}
}

View File

@ -328,8 +328,13 @@ export default {
this.$store.commit('libraries/setEReaderDevices', data.ereaderDevices)
},
customMetadataProvidersChanged() {
this.$store.dispatch('scanners/reFetchCustom')
customMetadataProviderAdded(provider) {
if (!provider?.id) return
this.$store.commit('scanners/addCustomMetadataProvider', provider)
},
customMetadataProviderRemoved(provider) {
if (!provider?.id) return
this.$store.commit('scanners/removeCustomMetadataProvider', provider)
},
initializeSocket() {
this.socket = this.$nuxtSocket({
@ -411,8 +416,8 @@ export default {
this.socket.on('admin_message', this.adminMessageEvt)
// Custom metadata provider Listeners
this.socket.on('custom_metadata_provider_added', this.customMetadataProvidersChanged)
this.socket.on('custom_metadata_provider_removed', this.customMetadataProvidersChanged)
this.socket.on('custom_metadata_provider_added', this.customMetadataProviderAdded)
this.socket.on('custom_metadata_provider_removed', this.customMetadataProviderRemoved)
},
showUpdateToast(versionData) {
var ignoreVersion = localStorage.getItem('ignoreVersion')
@ -548,7 +553,6 @@ export default {
window.addEventListener('keydown', this.keyDown)
this.$store.dispatch('libraries/load')
this.$store.dispatch('scanners/reFetchCustom')
this.initLocalStorage()

View File

@ -1,43 +0,0 @@
<template>
<div id="authentication-settings">
<app-settings-content :header-text="$strings.HeaderCustomMetadataProviders">
<template #header-items>
<ui-tooltip :text="$strings.LabelClickForMoreInfo" class="inline-flex ml-2">
<a href="https://www.audiobookshelf.org/guides/#" target="_blank" class="inline-flex">
<span class="material-icons text-xl w-5 text-gray-200">help_outline</span>
</a>
</ui-tooltip>
<div class="flex-grow" />
<ui-btn color="primary" small @click="setShowAddModal">{{ $strings.ButtonAdd }}</ui-btn>
</template>
<tables-custom-metadata-provider-table class="pt-2" />
<modals-add-custom-metadata-provider-modal ref="addModal" v-model="showAddModal" />
</app-settings-content>
</div>
</template>
<script>
export default {
async asyncData({ store, redirect }) {
if (!store.getters['user/getIsAdminOrUp']) {
redirect('/')
return
}
return {}
},
data() {
return {
showAddModal: false,
}
},
methods: {
setShowAddModal() {
this.showAddModal = true
}
}
}
</script>
<style></style>

View File

@ -0,0 +1,74 @@
<template>
<div class="relative">
<app-settings-content :header-text="$strings.HeaderCustomMetadataProviders">
<template #header-prefix>
<nuxt-link to="/config/item-metadata-utils" class="w-8 h-8 flex items-center justify-center rounded-full cursor-pointer hover:bg-white hover:bg-opacity-10 text-center mr-2">
<span class="material-icons text-2xl">arrow_back</span>
</nuxt-link>
</template>
<template #header-items>
<ui-tooltip :text="$strings.LabelClickForMoreInfo" class="inline-flex ml-2">
<a href="https://www.audiobookshelf.org/guides/#" target="_blank" class="inline-flex">
<span class="material-icons text-xl w-5 text-gray-200">help_outline</span>
</a>
</ui-tooltip>
<div class="flex-grow" />
<ui-btn color="primary" small @click="setShowAddModal">{{ $strings.ButtonAdd }}</ui-btn>
</template>
<tables-custom-metadata-provider-table :providers="providers" :processing.sync="processing" class="pt-2" @removed="providerRemoved" />
<modals-add-custom-metadata-provider-modal ref="addModal" v-model="showAddModal" @added="providerAdded" />
</app-settings-content>
</div>
</template>
<script>
export default {
async asyncData({ store, redirect }) {
if (!store.getters['user/getIsAdminOrUp']) {
redirect('/')
return
}
return {}
},
data() {
return {
showAddModal: false,
processing: false,
providers: []
}
},
methods: {
providerRemoved(providerId) {
this.providers = this.providers.filter((p) => p.id !== providerId)
},
providerAdded(provider) {
this.providers.push(provider)
},
setShowAddModal() {
this.showAddModal = true
},
loadProviders() {
this.processing = true
this.$axios
.$get('/api/custom-metadata-providers')
.then((res) => {
this.providers = res.providers
})
.catch((error) => {
console.error('Failed', error)
this.$toast.error('Failed to load custom metadata providers')
})
.finally(() => {
this.processing = false
})
}
},
mounted() {
this.loadProviders()
}
}
</script>
<style></style>

View File

@ -13,6 +13,12 @@
<span class="material-icons">arrow_forward</span>
</div>
</nuxt-link>
<nuxt-link to="/config/item-metadata-utils/custom-metadata-providers" class="block w-full rounded bg-primary/40 hover:bg-primary/60 text-gray-300 hover:text-white p-4 my-2">
<div class="flex justify-between">
<p>{{ $strings.HeaderCustomMetadataProviders }}</p>
<span class="material-icons">arrow_forward</span>
</div>
</nuxt-link>
</app-settings-content>
</div>
</template>

View File

@ -113,6 +113,7 @@ export const actions = {
const library = data.library
const filterData = data.filterdata
const issues = data.issues || 0
const customMetadataProviders = data.customMetadataProviders || []
const numUserPlaylists = data.numUserPlaylists
dispatch('user/checkUpdateLibrarySortFilter', library.mediaType, { root: true })
@ -126,6 +127,8 @@ export const actions = {
commit('setLibraryIssues', issues)
commit('setLibraryFilterData', filterData)
commit('setNumUserPlaylists', numUserPlaylists)
commit('scanners/setCustomMetadataProviders', customMetadataProviders, { root: true })
commit('setCurrentLibrary', libraryId)
return data
})

View File

@ -71,35 +71,56 @@ export const state = () => ({
]
})
export const getters = {}
export const actions = {
reFetchCustom({ dispatch, commit }) {
return this.$axios
.$get(`/api/custom-metadata-providers`)
.then((data) => {
const providers = data.providers
commit('setCustomProviders', providers)
return data
})
.catch((error) => {
console.error('Failed', error)
return false
})
export const getters = {
checkBookProviderExists: state => (providerValue) => {
return state.providers.some(p => p.value === providerValue)
},
checkPodcastProviderExists: state => (providerValue) => {
return state.podcastProviders.some(p => p.value === providerValue)
}
}
export const actions = {}
export const mutations = {
setCustomProviders(state, providers) {
// clear previous values, and add new values to the end
state.providers = state.providers.filter((p) => !p.value.startsWith("custom-"));
state.providers = [
...state.providers,
...providers.map((p) => {return {
text: p.name,
value: p.slug,
}})
]
addCustomMetadataProvider(state, provider) {
if (provider.mediaType === 'book') {
if (state.providers.some(p => p.value === provider.slug)) return
state.providers.push({
text: provider.name,
value: provider.slug
})
} else {
if (state.podcastProviders.some(p => p.value === provider.slug)) return
state.podcastProviders.push({
text: provider.name,
value: provider.slug
})
}
},
removeCustomMetadataProvider(state, provider) {
if (provider.mediaType === 'book') {
state.providers = state.providers.filter(p => p.value !== provider.slug)
} else {
state.podcastProviders = state.podcastProviders.filter(p => p.value !== provider.slug)
}
},
setCustomMetadataProviders(state, providers) {
if (!providers?.length) return
const mediaType = providers[0].mediaType
if (mediaType === 'book') {
// clear previous values, and add new values to the end
state.providers = state.providers.filter((p) => !p.value.startsWith('custom-'))
state.providers = [
...state.providers,
...providers.map((p) => ({
text: p.name,
value: p.slug
}))
]
} else {
// Podcast providers not supported yet
}
}
}

View File

@ -104,7 +104,7 @@
"HeaderCollectionItems": "Collection Items",
"HeaderCover": "Cover",
"HeaderCurrentDownloads": "Current Downloads",
"HeaderCustomMetadataProviders": "Custom metadata providers",
"HeaderCustomMetadataProviders": "Custom Metadata Providers",
"HeaderDetails": "Details",
"HeaderDownloadQueue": "Download Queue",
"HeaderEbookFiles": "Ebook Files",
@ -194,7 +194,6 @@
"LabelAllUsersExcludingGuests": "All users excluding guests",
"LabelAllUsersIncludingGuests": "All users including guests",
"LabelAlreadyInYourLibrary": "Already in your library",
"LabelApiKey": "API Key",
"LabelAppend": "Append",
"LabelAuthor": "Author",
"LabelAuthorFirstLast": "Author (First Last)",
@ -536,7 +535,6 @@
"LabelUploaderDragAndDrop": "Drag & drop files or folders",
"LabelUploaderDropFiles": "Drop files",
"LabelUploaderItemFetchMetadataHelp": "Automatically fetch title, author, and series",
"LabelUrl": "URL",
"LabelUseChapterTrack": "Use chapter track",
"LabelUseFullTrack": "Use full track",
"LabelUser": "User",

View File

@ -700,45 +700,6 @@ class Database {
})
}
/**
* Returns true if a custom provider with the given slug exists
* @param {string} providerSlug
* @return {boolean}
*/
async doesCustomProviderExistWithSlug(providerSlug) {
const id = providerSlug.split("custom-")[1]
if (!id) {
return false
}
return !!await this.customMetadataProviderModel.findByPk(id)
}
/**
* Removes a custom metadata provider
* @param {string} id
*/
async removeCustomMetadataProviderById(id) {
// destroy metadta provider
await this.customMetadataProviderModel.destroy({
where: {
id,
}
})
const slug = `custom-${id}`;
// fallback libraries using it to google
await this.libraryModel.update({
provider: "google",
}, {
where: {
provider: slug,
}
});
}
/**
* Clean invalid records in database
* Series should have atleast one Book

View 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()

View File

@ -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) {

View File

@ -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()

View File

@ -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) {

View File

@ -149,6 +149,13 @@ class BookFinder {
return books
}
/**
*
* @param {string} title
* @param {string} author
* @param {string} providerSlug
* @returns {Promise<Object[]>}
*/
async getCustomProviderResults(title, author, providerSlug) {
const books = await this.customProviderAdapter.search(title, author, providerSlug)
if (this.verbose) Logger.debug(`Custom provider '${providerSlug}' Search Results: ${books.length || 0}`)
@ -325,8 +332,8 @@ class BookFinder {
let numFuzzySearches = 0
// Custom providers are assumed to be correct
if (provider.startsWith("custom-")) {
return await this.getCustomProviderResults(title, author, provider)
if (provider.startsWith('custom-')) {
return this.getCustomProviderResults(title, author, provider)
}
if (!title)

View File

@ -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'

View File

@ -1,32 +1,40 @@
const Database = require('../Database')
const axios = require("axios")
const Logger = require("../Logger")
const axios = require('axios')
const Logger = require('../Logger')
class CustomProviderAdapter {
constructor() {
}
constructor() { }
/**
*
* @param {string} title
* @param {string} author
* @param {string} providerSlug
* @returns {Promise<Object[]>}
*/
async search(title, author, providerSlug) {
const providerId = providerSlug.split("custom-")[1]
const providerId = providerSlug.split('custom-')[1]
const provider = await Database.customMetadataProviderModel.findByPk(providerId)
if (!provider) {
throw new Error("Custom provider not found for the given id")
}
const matches = await axios.get(`${provider.url}/search?query=${encodeURIComponent(title)}${!!author ? `&author=${encodeURIComponent(author)}` : ""}`, {
headers: {
"Authorization": provider.apiKey,
},
}).then((res) => {
if (!res || !res.data || !Array.isArray(res.data.matches)) return null
const axiosOptions = {}
if (provider.authHeaderValue) {
axiosOptions.headers = {
'Authorization': provider.authHeaderValue
}
}
const matches = await axios.get(`${provider.url}/search?query=${encodeURIComponent(title)}${!!author ? `&author=${encodeURIComponent(author)}` : ""}`, axiosOptions).then((res) => {
if (!res?.data || !Array.isArray(res.data.matches)) return null
return res.data.matches
}).catch(error => {
Logger.error('[CustomMetadataProvider] Search error', error)
return []
})
if (matches === null) {
if (!matches) {
throw new Error("Custom provider returned malformed response")
}
@ -46,7 +54,7 @@ class CustomProviderAdapter {
tags,
series,
language,
duration,
duration
}) => {
return {
title,
@ -60,10 +68,10 @@ class CustomProviderAdapter {
isbn,
asin,
genres,
tags: tags.join(","),
series: series.length ? series : null,
tags: tags?.join(',') || null,
series: series?.length ? series : null,
language,
duration,
duration
}
})
}

View File

@ -28,6 +28,7 @@ const SearchController = require('../controllers/SearchController')
const CacheController = require('../controllers/CacheController')
const ToolsController = require('../controllers/ToolsController')
const RSSFeedController = require('../controllers/RSSFeedController')
const CustomMetadataProviderController = require('../controllers/CustomMetadataProviderController')
const MiscController = require('../controllers/MiscController')
const Author = require('../objects/entities/Author')
@ -299,6 +300,14 @@ class ApiRouter {
this.router.post('/feeds/series/:seriesId/open', RSSFeedController.middleware.bind(this), RSSFeedController.openRSSFeedForSeries.bind(this))
this.router.post('/feeds/:id/close', RSSFeedController.middleware.bind(this), RSSFeedController.closeRSSFeed.bind(this))
//
// Custom Metadata Provider routes
//
this.router.get('/custom-metadata-providers', CustomMetadataProviderController.middleware.bind(this), CustomMetadataProviderController.getAll.bind(this))
this.router.post('/custom-metadata-providers', CustomMetadataProviderController.middleware.bind(this), CustomMetadataProviderController.create.bind(this))
this.router.delete('/custom-metadata-providers/:id', CustomMetadataProviderController.middleware.bind(this), CustomMetadataProviderController.delete.bind(this))
//
// Misc Routes
//
@ -318,10 +327,6 @@ class ApiRouter {
this.router.patch('/auth-settings', MiscController.updateAuthSettings.bind(this))
this.router.post('/watcher/update', MiscController.updateWatchedPath.bind(this))
this.router.get('/stats/year/:year', MiscController.getAdminStatsForYear.bind(this))
this.router.get('/custom-metadata-providers', MiscController.getCustomMetadataProviders.bind(this))
this.router.get('/custom-metadata-providers/admin', MiscController.getAdminCustomMetadataProviders.bind(this))
this.router.patch('/custom-metadata-providers/admin', MiscController.addCustomMetadataProviders.bind(this))
this.router.delete('/custom-metadata-providers/admin/:id', MiscController.deleteCustomMetadataProviders.bind(this))
}
//