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', id: 'config-authentication',
title: this.$strings.HeaderAuthentication, title: this.$strings.HeaderAuthentication,
path: '/config/authentication' path: '/config/authentication'
},
{
id: 'config-custom-metadata-providers',
title: this.$strings.HeaderCustomMetadataProviders,
path: '/config/custom-metadata-providers'
} }
] ]

View File

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

View File

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

View File

@ -328,6 +328,17 @@ export default {
console.error('PersistProvider', error) 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() { getSearchQuery() {
if (this.isPodcast) return `term=${encodeURIComponent(this.searchTitle)}` if (this.isPodcast) return `term=${encodeURIComponent(this.searchTitle)}`
var searchQuery = `provider=${this.provider}&fallbackTitleOnly=1&title=${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.searchTitle = this.libraryItem.media.metadata.title
this.searchAuthor = this.libraryItem.media.metadata.authorName || '' this.searchAuthor = this.libraryItem.media.metadata.authorName || ''
if (this.isPodcast) this.provider = 'itunes' 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 // Prefer using ASIN if set and using audible provider
if (this.provider.startsWith('audible') && this.libraryItem.media.metadata.asin) { if (this.provider.startsWith('audible') && this.libraryItem.media.metadata.asin) {

View File

@ -1,18 +1,17 @@
<template> <template>
<div> <div class="min-h-40">
<div class="text-center"> <table v-if="providers.length" id="providers">
<table id="providers">
<tr> <tr>
<th>{{ $strings.LabelName }}</th> <th>{{ $strings.LabelName }}</th>
<th>{{ $strings.LabelUrl }}</th> <th>URL</th>
<th>{{ $strings.LabelApiKey }}</th> <th>Authorization Header Value</th>
<th class="w-12"></th> <th class="w-12"></th>
</tr> </tr>
<tr v-for="provider in providers" :key="provider.id"> <tr v-for="provider in providers" :key="provider.id">
<td class="text-sm">{{ provider.name }}</td> <td class="text-sm">{{ provider.name }}</td>
<td class="text-sm">{{ provider.url }}</td> <td class="text-sm">{{ provider.url }}</td>
<td class="text-sm"> <td class="text-sm">
<span class="custom-provider-api-key">{{ provider.apiKey }}</span> <span v-if="provider.authHeaderValue" class="custom-provider-api-key">{{ provider.authHeaderValue }}</span>
</td> </td>
<td class="py-0"> <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)"> <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)">
@ -21,76 +20,54 @@
</td> </td>
</tr> </tr>
</table> </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>
</div> </div>
</template> </template>
<script> <script>
export default { export default {
props: {
providers: {
type: Array,
default: () => []
},
processing: Boolean
},
data() { data() {
return { return {}
providers: [],
}
}, },
methods: { 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) { removeProvider(provider) {
const payload = {
message: `Are you sure you want remove custom metadata provider "${provider.name}"?`,
callback: (confirmed) => {
if (confirmed) {
this.$emit('update:processing', true)
this.$axios this.$axios
.$delete(`/api/custom-metadata-providers/admin/${provider.id}`) .$delete(`/api/custom-metadata-providers/${provider.id}`)
.then((data) => { .then(() => {
if (data.error) {
this.$toast.error(`Failed to remove provider: ${data.error}`)
} else {
this.$toast.success('Provider removed') this.$toast.success('Provider removed')
} this.$emit('removed', provider.id)
}) })
.catch((error) => { .catch((error) => {
console.error('Failed to remove provider', error) console.error('Failed to remove provider', error)
this.$toast.error('Failed to remove provider') this.$toast.error('Failed to remove provider')
}) })
}, .finally(() => {
loadProviders() { this.$emit('update:processing', false)
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() { type: 'yesNo'
this.loadProviders()
this.init()
},
beforeDestroy() {
if (this.$refs.addModal) {
this.$refs.addModal.close()
} }
this.$store.commit('globals/setConfirmPrompt', payload)
if (this.$root.socket) {
this.$root.socket.off('custom_metadata_provider_added', this.addedProvider)
this.$root.socket.off('custom_metadata_provider_removed', this.removedProvider)
} }
} }
} }

View File

@ -328,8 +328,13 @@ export default {
this.$store.commit('libraries/setEReaderDevices', data.ereaderDevices) this.$store.commit('libraries/setEReaderDevices', data.ereaderDevices)
}, },
customMetadataProvidersChanged() { customMetadataProviderAdded(provider) {
this.$store.dispatch('scanners/reFetchCustom') if (!provider?.id) return
this.$store.commit('scanners/addCustomMetadataProvider', provider)
},
customMetadataProviderRemoved(provider) {
if (!provider?.id) return
this.$store.commit('scanners/removeCustomMetadataProvider', provider)
}, },
initializeSocket() { initializeSocket() {
this.socket = this.$nuxtSocket({ this.socket = this.$nuxtSocket({
@ -411,8 +416,8 @@ export default {
this.socket.on('admin_message', this.adminMessageEvt) this.socket.on('admin_message', this.adminMessageEvt)
// Custom metadata provider Listeners // Custom metadata provider Listeners
this.socket.on('custom_metadata_provider_added', this.customMetadataProvidersChanged) this.socket.on('custom_metadata_provider_added', this.customMetadataProviderAdded)
this.socket.on('custom_metadata_provider_removed', this.customMetadataProvidersChanged) this.socket.on('custom_metadata_provider_removed', this.customMetadataProviderRemoved)
}, },
showUpdateToast(versionData) { showUpdateToast(versionData) {
var ignoreVersion = localStorage.getItem('ignoreVersion') var ignoreVersion = localStorage.getItem('ignoreVersion')
@ -548,7 +553,6 @@ export default {
window.addEventListener('keydown', this.keyDown) window.addEventListener('keydown', this.keyDown)
this.$store.dispatch('libraries/load') this.$store.dispatch('libraries/load')
this.$store.dispatch('scanners/reFetchCustom')
this.initLocalStorage() 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> <span class="material-icons">arrow_forward</span>
</div> </div>
</nuxt-link> </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> </app-settings-content>
</div> </div>
</template> </template>

View File

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

View File

@ -71,35 +71,56 @@ export const state = () => ({
] ]
}) })
export const getters = {} export const getters = {
checkBookProviderExists: state => (providerValue) => {
export const actions = { return state.providers.some(p => p.value === providerValue)
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
})
}, },
checkPodcastProviderExists: state => (providerValue) => {
return state.podcastProviders.some(p => p.value === providerValue)
} }
}
export const actions = {}
export const mutations = { export const mutations = {
setCustomProviders(state, providers) { 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 // clear previous values, and add new values to the end
state.providers = state.providers.filter((p) => !p.value.startsWith("custom-")); state.providers = state.providers.filter((p) => !p.value.startsWith('custom-'))
state.providers = [ state.providers = [
...state.providers, ...state.providers,
...providers.map((p) => {return { ...providers.map((p) => ({
text: p.name, text: p.name,
value: p.slug, value: p.slug
}}) }))
] ]
}, } else {
// Podcast providers not supported yet
}
}
} }

View File

@ -104,7 +104,7 @@
"HeaderCollectionItems": "Collection Items", "HeaderCollectionItems": "Collection Items",
"HeaderCover": "Cover", "HeaderCover": "Cover",
"HeaderCurrentDownloads": "Current Downloads", "HeaderCurrentDownloads": "Current Downloads",
"HeaderCustomMetadataProviders": "Custom metadata providers", "HeaderCustomMetadataProviders": "Custom Metadata Providers",
"HeaderDetails": "Details", "HeaderDetails": "Details",
"HeaderDownloadQueue": "Download Queue", "HeaderDownloadQueue": "Download Queue",
"HeaderEbookFiles": "Ebook Files", "HeaderEbookFiles": "Ebook Files",
@ -194,7 +194,6 @@
"LabelAllUsersExcludingGuests": "All users excluding guests", "LabelAllUsersExcludingGuests": "All users excluding guests",
"LabelAllUsersIncludingGuests": "All users including guests", "LabelAllUsersIncludingGuests": "All users including guests",
"LabelAlreadyInYourLibrary": "Already in your library", "LabelAlreadyInYourLibrary": "Already in your library",
"LabelApiKey": "API Key",
"LabelAppend": "Append", "LabelAppend": "Append",
"LabelAuthor": "Author", "LabelAuthor": "Author",
"LabelAuthorFirstLast": "Author (First Last)", "LabelAuthorFirstLast": "Author (First Last)",
@ -536,7 +535,6 @@
"LabelUploaderDragAndDrop": "Drag & drop files or folders", "LabelUploaderDragAndDrop": "Drag & drop files or folders",
"LabelUploaderDropFiles": "Drop files", "LabelUploaderDropFiles": "Drop files",
"LabelUploaderItemFetchMetadataHelp": "Automatically fetch title, author, and series", "LabelUploaderItemFetchMetadataHelp": "Automatically fetch title, author, and series",
"LabelUrl": "URL",
"LabelUseChapterTrack": "Use chapter track", "LabelUseChapterTrack": "Use chapter track",
"LabelUseFullTrack": "Use full track", "LabelUseFullTrack": "Use full track",
"LabelUser": "User", "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 * Clean invalid records in database
* Series should have atleast one Book * 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') 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 // Validate folder paths exist or can be created & resolve rel paths
// returns 400 if a folder fails to access // returns 400 if a folder fails to access
newLibraryPayload.folders = newLibraryPayload.folders.map(f => { 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() const library = new Library()
let currentLargestDisplayOrder = await Database.libraryModel.getMaxDisplayOrder() 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) { async findOne(req, res) {
const includeArray = (req.query.include || '').split(',') const includeArray = (req.query.include || '').split(',')
if (includeArray.includes('filterdata')) { if (includeArray.includes('filterdata')) {
const filterdata = await libraryFilters.getFilterData(req.library.mediaType, req.library.id) const filterdata = await libraryFilters.getFilterData(req.library.mediaType, req.library.id)
const customMetadataProviders = await Database.customMetadataProviderModel.getForClientByMediaType(req.library.mediaType)
return res.json({ return res.json({
filterdata, filterdata,
issues: filterdata.numIssues, issues: filterdata.numIssues,
numUserPlaylists: await Database.playlistModel.getNumPlaylistsForUserAndLibrary(req.user.id, req.library.id), numUserPlaylists: await Database.playlistModel.getNumPlaylistsForUserAndLibrary(req.user.id, req.library.id),
customMetadataProviders,
library: req.library library: req.library
}) })
} }
return res.json(req.library) res.json(req.library)
} }
/** /**
@ -120,6 +131,14 @@ class LibraryController {
async update(req, res) { async update(req, res) {
const library = req.library 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 // Validate new folder paths exist or can be created & resolve rel paths
// returns 400 if a new folder fails to access // returns 400 if a new folder fails to access
if (req.body.folders) { 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) const hasUpdates = library.update(req.body)
// TODO: Should check if this is an update to folder paths or name only // TODO: Should check if this is an update to folder paths or name only
if (hasUpdates) { if (hasUpdates) {

View File

@ -717,95 +717,5 @@ class MiscController {
const stats = await adminStats.getStatsForYear(year) const stats = await adminStats.getStatsForYear(year)
res.json(stats) 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() module.exports = new MiscController()

View File

@ -161,7 +161,7 @@ class SessionController {
* @typedef batchDeleteReqBody * @typedef batchDeleteReqBody
* @property {string[]} sessions * @property {string[]} sessions
* *
* @param {import('express').Request<{}, {}, batchDeleteReqBody, {}} req * @param {import('express').Request<{}, {}, batchDeleteReqBody, {}>} req
* @param {import('express').Response} res * @param {import('express').Response} res
*/ */
async batchDelete(req, res) { async batchDelete(req, res) {

View File

@ -149,6 +149,13 @@ class BookFinder {
return books return books
} }
/**
*
* @param {string} title
* @param {string} author
* @param {string} providerSlug
* @returns {Promise<Object[]>}
*/
async getCustomProviderResults(title, author, providerSlug) { async getCustomProviderResults(title, author, providerSlug) {
const books = await this.customProviderAdapter.search(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}`) if (this.verbose) Logger.debug(`Custom provider '${providerSlug}' Search Results: ${books.length || 0}`)
@ -325,8 +332,8 @@ class BookFinder {
let numFuzzySearches = 0 let numFuzzySearches = 0
// Custom providers are assumed to be correct // Custom providers are assumed to be correct
if (provider.startsWith("custom-")) { if (provider.startsWith('custom-')) {
return await this.getCustomProviderResults(title, author, provider) return this.getCustomProviderResults(title, author, provider)
} }
if (!title) 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 { class CustomMetadataProvider extends Model {
constructor(values, options) { constructor(values, options) {
@ -7,26 +15,67 @@ class CustomMetadataProvider extends Model {
/** @type {UUIDV4} */ /** @type {UUIDV4} */
this.id this.id
/** @type {string} */ /** @type {string} */
this.mediaType
/** @type {string} */
this.name this.name
/** @type {string} */ /** @type {string} */
this.url this.url
/** @type {string} */ /** @type {string} */
this.apiKey this.authHeaderValue
/** @type {Object} */
this.extraData
/** @type {Date} */
this.createdAt
/** @type {Date} */
this.updatedAt
} }
getSlug() { getSlug() {
return `custom-${this.id}` return `custom-${this.id}`
} }
toUserJson() { /**
* Safe for clients
* @returns {ClientCustomMetadataProvider}
*/
toClientJson() {
return { return {
name: this.name,
id: this.id, id: this.id,
name: this.name,
mediaType: this.mediaType,
slug: this.getSlug() 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 * Initialize model
@ -40,8 +89,10 @@ class CustomMetadataProvider extends Model {
primaryKey: true primaryKey: true
}, },
name: DataTypes.STRING, name: DataTypes.STRING,
mediaType: DataTypes.STRING,
url: DataTypes.STRING, url: DataTypes.STRING,
apiKey: DataTypes.STRING, authHeaderValue: DataTypes.STRING,
extraData: DataTypes.JSON
}, { }, {
sequelize, sequelize,
modelName: 'customMetadataProvider' modelName: 'customMetadataProvider'

View File

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

View File

@ -28,6 +28,7 @@ const SearchController = require('../controllers/SearchController')
const CacheController = require('../controllers/CacheController') const CacheController = require('../controllers/CacheController')
const ToolsController = require('../controllers/ToolsController') const ToolsController = require('../controllers/ToolsController')
const RSSFeedController = require('../controllers/RSSFeedController') const RSSFeedController = require('../controllers/RSSFeedController')
const CustomMetadataProviderController = require('../controllers/CustomMetadataProviderController')
const MiscController = require('../controllers/MiscController') const MiscController = require('../controllers/MiscController')
const Author = require('../objects/entities/Author') 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/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)) 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 // Misc Routes
// //
@ -318,10 +327,6 @@ class ApiRouter {
this.router.patch('/auth-settings', MiscController.updateAuthSettings.bind(this)) this.router.patch('/auth-settings', MiscController.updateAuthSettings.bind(this))
this.router.post('/watcher/update', MiscController.updateWatchedPath.bind(this)) this.router.post('/watcher/update', MiscController.updateWatchedPath.bind(this))
this.router.get('/stats/year/:year', MiscController.getAdminStatsForYear.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))
} }
// //