2022-12-19 22:06:43 +01:00
|
|
|
|
|
|
|
const fs = require('../libs/fsExtra')
|
|
|
|
const { createNewSortInstance } = require('../libs/fastSort')
|
|
|
|
|
2022-03-13 12:42:43 +01:00
|
|
|
const Logger = require('../Logger')
|
2022-11-24 22:53:58 +01:00
|
|
|
const SocketAuthority = require('../SocketAuthority')
|
|
|
|
|
2022-03-13 16:35:35 +01:00
|
|
|
const { reqSupportsWebp } = require('../utils/index')
|
2022-03-13 12:42:43 +01:00
|
|
|
|
2022-05-09 01:21:46 +02:00
|
|
|
const naturalSort = createNewSortInstance({
|
|
|
|
comparer: new Intl.Collator(undefined, { numeric: true, sensitivity: 'base' }).compare
|
|
|
|
})
|
2022-03-13 12:42:43 +01:00
|
|
|
class AuthorController {
|
|
|
|
constructor() { }
|
|
|
|
|
|
|
|
async findOne(req, res) {
|
2022-10-07 00:06:06 +02:00
|
|
|
const libraryId = req.query.library
|
2022-05-09 01:21:46 +02:00
|
|
|
const include = (req.query.include || '').split(',')
|
|
|
|
|
|
|
|
const authorJson = req.author.toJSON()
|
|
|
|
|
|
|
|
// Used on author landing page to include library items and items grouped in series
|
|
|
|
if (include.includes('items')) {
|
|
|
|
authorJson.libraryItems = this.db.libraryItems.filter(li => {
|
2022-10-07 00:06:06 +02:00
|
|
|
if (libraryId && li.libraryId !== libraryId) return false
|
2022-05-09 01:48:57 +02:00
|
|
|
if (!req.user.checkCanAccessLibraryItem(li)) return false // filter out library items user cannot access
|
2022-05-09 01:21:46 +02:00
|
|
|
return li.media.metadata.hasAuthor && li.media.metadata.hasAuthor(req.author.id)
|
|
|
|
})
|
|
|
|
|
|
|
|
if (include.includes('series')) {
|
|
|
|
const seriesMap = {}
|
|
|
|
// Group items into series
|
|
|
|
authorJson.libraryItems.forEach((li) => {
|
|
|
|
if (li.media.metadata.series) {
|
|
|
|
li.media.metadata.series.forEach((series) => {
|
|
|
|
|
|
|
|
const itemWithSeries = li.toJSONMinified()
|
|
|
|
itemWithSeries.media.metadata.series = series
|
|
|
|
|
|
|
|
if (seriesMap[series.id]) {
|
|
|
|
seriesMap[series.id].items.push(itemWithSeries)
|
|
|
|
} else {
|
|
|
|
seriesMap[series.id] = {
|
|
|
|
id: series.id,
|
|
|
|
name: series.name,
|
|
|
|
items: [itemWithSeries]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
// Sort series items
|
|
|
|
for (const key in seriesMap) {
|
|
|
|
seriesMap[key].items = naturalSort(seriesMap[key].items).asc(li => li.media.metadata.series.sequence)
|
|
|
|
}
|
|
|
|
|
|
|
|
authorJson.series = Object.values(seriesMap)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Minify library items
|
|
|
|
authorJson.libraryItems = authorJson.libraryItems.map(li => li.toJSONMinified())
|
|
|
|
}
|
|
|
|
|
|
|
|
return res.json(authorJson)
|
2022-03-13 12:42:43 +01:00
|
|
|
}
|
|
|
|
|
2022-03-15 00:53:49 +01:00
|
|
|
async update(req, res) {
|
2022-11-27 22:35:47 +01:00
|
|
|
const payload = req.body
|
|
|
|
let hasUpdated = false
|
|
|
|
|
|
|
|
// Updating/removing cover image
|
|
|
|
if (payload.imagePath !== undefined && payload.imagePath !== req.author.imagePath) {
|
|
|
|
if (!payload.imagePath && req.author.imagePath) { // If removing image then remove file
|
|
|
|
await this.cacheManager.purgeImageCache(req.author.id) // Purge cache
|
|
|
|
await this.coverManager.removeFile(req.author.imagePath)
|
2022-06-18 19:05:30 +02:00
|
|
|
} else if (payload.imagePath.startsWith('http')) { // Check if image path is a url
|
2022-11-27 22:35:47 +01:00
|
|
|
const imageData = await this.authorFinder.saveAuthorImage(req.author.id, payload.imagePath)
|
2022-06-18 19:05:30 +02:00
|
|
|
if (imageData) {
|
2022-11-27 22:35:47 +01:00
|
|
|
if (req.author.imagePath) {
|
|
|
|
await this.cacheManager.purgeImageCache(req.author.id) // Purge cache
|
|
|
|
}
|
|
|
|
payload.imagePath = imageData.path
|
|
|
|
payload.relImagePath = imageData.relPath
|
|
|
|
hasUpdated = true
|
2022-06-18 19:05:30 +02:00
|
|
|
}
|
2022-03-15 00:53:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-27 22:35:47 +01:00
|
|
|
const authorNameUpdate = payload.name !== undefined && payload.name !== req.author.name
|
2022-03-18 15:38:36 +01:00
|
|
|
|
2022-07-24 19:00:36 +02:00
|
|
|
// Check if author name matches another author and merge the authors
|
2022-11-27 22:35:47 +01:00
|
|
|
const existingAuthor = authorNameUpdate ? this.db.authors.find(au => au.id !== req.author.id && payload.name === au.name) : false
|
2022-07-24 19:00:36 +02:00
|
|
|
if (existingAuthor) {
|
2022-11-27 22:35:47 +01:00
|
|
|
const itemsWithAuthor = this.db.libraryItems.filter(li => li.mediaType === 'book' && li.media.metadata.hasAuthor(req.author.id))
|
2022-07-24 19:00:36 +02:00
|
|
|
itemsWithAuthor.forEach(libraryItem => { // Replace old author with merging author for each book
|
|
|
|
libraryItem.media.metadata.replaceAuthor(req.author, existingAuthor)
|
|
|
|
})
|
|
|
|
if (itemsWithAuthor.length) {
|
|
|
|
await this.db.updateLibraryItems(itemsWithAuthor)
|
2022-11-24 22:53:58 +01:00
|
|
|
SocketAuthority.emitter('items_updated', itemsWithAuthor.map(li => li.toJSONExpanded()))
|
2022-03-18 15:38:36 +01:00
|
|
|
}
|
|
|
|
|
2022-07-24 19:00:36 +02:00
|
|
|
// Remove old author
|
|
|
|
await this.db.removeEntity('author', req.author.id)
|
2022-11-24 22:53:58 +01:00
|
|
|
SocketAuthority.emitter('author_removed', req.author.toJSON())
|
2022-07-24 19:00:36 +02:00
|
|
|
|
|
|
|
// Send updated num books for merged author
|
2022-11-27 22:35:47 +01:00
|
|
|
const numBooks = this.db.libraryItems.filter(li => {
|
2022-07-24 19:00:36 +02:00
|
|
|
return li.media.metadata.hasAuthor && li.media.metadata.hasAuthor(existingAuthor.id)
|
2022-03-18 15:38:36 +01:00
|
|
|
}).length
|
2022-11-24 22:53:58 +01:00
|
|
|
SocketAuthority.emitter('author_updated', existingAuthor.toJSONExpanded(numBooks))
|
2022-05-09 01:21:46 +02:00
|
|
|
|
2022-07-24 19:00:36 +02:00
|
|
|
res.json({
|
|
|
|
author: existingAuthor.toJSON(),
|
|
|
|
merged: true
|
|
|
|
})
|
|
|
|
} else { // Regular author update
|
2022-11-27 22:35:47 +01:00
|
|
|
if (req.author.update(payload)) {
|
|
|
|
hasUpdated = true
|
|
|
|
}
|
2022-07-24 19:00:36 +02:00
|
|
|
|
|
|
|
if (hasUpdated) {
|
|
|
|
if (authorNameUpdate) { // Update author name on all books
|
2022-11-27 22:35:47 +01:00
|
|
|
const itemsWithAuthor = this.db.libraryItems.filter(li => li.mediaType === 'book' && li.media.metadata.hasAuthor(req.author.id))
|
2022-07-24 19:00:36 +02:00
|
|
|
itemsWithAuthor.forEach(libraryItem => {
|
|
|
|
libraryItem.media.metadata.updateAuthor(req.author)
|
|
|
|
})
|
|
|
|
if (itemsWithAuthor.length) {
|
|
|
|
await this.db.updateLibraryItems(itemsWithAuthor)
|
2022-11-24 22:53:58 +01:00
|
|
|
SocketAuthority.emitter('items_updated', itemsWithAuthor.map(li => li.toJSONExpanded()))
|
2022-07-24 19:00:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
await this.db.updateEntity('author', req.author)
|
2022-11-27 22:35:47 +01:00
|
|
|
const numBooks = this.db.libraryItems.filter(li => {
|
2022-07-24 19:00:36 +02:00
|
|
|
return li.media.metadata.hasAuthor && li.media.metadata.hasAuthor(req.author.id)
|
|
|
|
}).length
|
2022-11-24 22:53:58 +01:00
|
|
|
SocketAuthority.emitter('author_updated', req.author.toJSONExpanded(numBooks))
|
2022-07-24 19:00:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
res.json({
|
|
|
|
author: req.author.toJSON(),
|
|
|
|
updated: hasUpdated
|
|
|
|
})
|
|
|
|
}
|
2022-03-15 00:53:49 +01:00
|
|
|
}
|
|
|
|
|
2022-03-13 12:42:43 +01:00
|
|
|
async search(req, res) {
|
|
|
|
var q = (req.query.q || '').toLowerCase()
|
|
|
|
if (!q) return res.json([])
|
|
|
|
var limit = (req.query.limit && !isNaN(req.query.limit)) ? Number(req.query.limit) : 25
|
|
|
|
var authors = this.db.authors.filter(au => au.name.toLowerCase().includes(q))
|
|
|
|
authors = authors.slice(0, limit)
|
2022-11-29 19:04:45 +01:00
|
|
|
res.json({
|
|
|
|
results: authors
|
|
|
|
})
|
2022-03-13 12:42:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
async match(req, res) {
|
2022-05-14 01:11:54 +02:00
|
|
|
var authorData = null
|
|
|
|
if (req.body.asin) {
|
|
|
|
authorData = await this.authorFinder.findAuthorByASIN(req.body.asin)
|
|
|
|
} else {
|
|
|
|
authorData = await this.authorFinder.findAuthorByName(req.body.q)
|
|
|
|
}
|
2022-03-13 12:42:43 +01:00
|
|
|
if (!authorData) {
|
|
|
|
return res.status(404).send('Author not found')
|
|
|
|
}
|
2022-05-14 01:11:54 +02:00
|
|
|
Logger.debug(`[AuthorController] match author with "${req.body.q || req.body.asin}"`, authorData)
|
2022-03-13 16:35:35 +01:00
|
|
|
|
|
|
|
var hasUpdates = false
|
|
|
|
if (authorData.asin && req.author.asin !== authorData.asin) {
|
|
|
|
req.author.asin = authorData.asin
|
|
|
|
hasUpdates = true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only updates image if there was no image before or the author ASIN was updated
|
|
|
|
if (authorData.image && (!req.author.imagePath || hasUpdates)) {
|
2022-05-14 01:11:54 +02:00
|
|
|
this.cacheManager.purgeImageCache(req.author.id)
|
|
|
|
|
2022-03-13 12:42:43 +01:00
|
|
|
var imageData = await this.authorFinder.saveAuthorImage(req.author.id, authorData.image)
|
|
|
|
if (imageData) {
|
|
|
|
req.author.imagePath = imageData.path
|
|
|
|
req.author.relImagePath = imageData.relPath
|
2022-03-13 16:35:35 +01:00
|
|
|
hasUpdates = true
|
2022-03-13 12:42:43 +01:00
|
|
|
}
|
|
|
|
}
|
2022-03-13 16:35:35 +01:00
|
|
|
|
|
|
|
if (authorData.description && req.author.description !== authorData.description) {
|
2022-03-13 12:42:43 +01:00
|
|
|
req.author.description = authorData.description
|
2022-03-13 16:35:35 +01:00
|
|
|
hasUpdates = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hasUpdates) {
|
|
|
|
req.author.updatedAt = Date.now()
|
|
|
|
|
|
|
|
await this.db.updateEntity('author', req.author)
|
|
|
|
var numBooks = this.db.libraryItems.filter(li => {
|
|
|
|
return li.media.metadata.hasAuthor && li.media.metadata.hasAuthor(req.author.id)
|
|
|
|
}).length
|
2022-11-24 22:53:58 +01:00
|
|
|
SocketAuthority.emitter('author_updated', req.author.toJSONExpanded(numBooks))
|
2022-03-13 16:35:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
res.json({
|
|
|
|
updated: hasUpdates,
|
|
|
|
author: req.author
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// GET api/authors/:id/image
|
|
|
|
async getImage(req, res) {
|
2022-12-19 22:06:43 +01:00
|
|
|
const { query: { width, height, format, raw }, author } = req
|
|
|
|
|
|
|
|
if (raw) { // any value
|
|
|
|
if (!author.imagePath || !await fs.pathExists(author.imagePath)) {
|
|
|
|
return res.sendStatus(404)
|
|
|
|
}
|
|
|
|
|
|
|
|
return res.sendFile(author.imagePath)
|
|
|
|
}
|
2022-03-13 16:35:35 +01:00
|
|
|
|
|
|
|
const options = {
|
|
|
|
format: format || (reqSupportsWebp(req) ? 'webp' : 'jpeg'),
|
|
|
|
height: height ? parseInt(height) : null,
|
|
|
|
width: width ? parseInt(width) : null
|
2022-03-13 12:42:43 +01:00
|
|
|
}
|
2022-03-13 16:35:35 +01:00
|
|
|
return this.cacheManager.handleAuthorCache(res, author, options)
|
2022-03-13 12:42:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
middleware(req, res, next) {
|
|
|
|
var author = this.db.authors.find(au => au.id === req.params.id)
|
|
|
|
if (!author) return res.sendStatus(404)
|
|
|
|
|
|
|
|
if (req.method == 'DELETE' && !req.user.canDelete) {
|
|
|
|
Logger.warn(`[AuthorController] User attempted to delete without permission`, req.user)
|
|
|
|
return res.sendStatus(403)
|
|
|
|
} else if ((req.method == 'PATCH' || req.method == 'POST') && !req.user.canUpdate) {
|
|
|
|
Logger.warn('[AuthorController] User attempted to update without permission', req.user)
|
|
|
|
return res.sendStatus(403)
|
|
|
|
}
|
|
|
|
|
|
|
|
req.author = author
|
|
|
|
next()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
module.exports = new AuthorController()
|