2023-09-03 17:04:14 +02:00
|
|
|
const sequelize = require('sequelize')
|
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')
|
2023-07-05 01:14:44 +02:00
|
|
|
const Database = require('../Database')
|
2023-09-07 00:48:50 +02:00
|
|
|
const CacheManager = require('../managers/CacheManager')
|
|
|
|
const CoverManager = require('../managers/CoverManager')
|
2023-09-04 23:33:55 +02:00
|
|
|
const AuthorFinder = require('../finders/AuthorFinder')
|
2022-11-24 22:53:58 +01:00
|
|
|
|
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-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')) {
|
2023-08-20 20:34:03 +02:00
|
|
|
authorJson.libraryItems = await Database.libraryItemModel.getForAuthor(req.author, req.user)
|
2022-05-09 01:21:46 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2023-10-14 00:37:37 +02:00
|
|
|
// author imagePath must be set through other endpoints as of v2.4.5
|
|
|
|
if (payload.imagePath !== undefined) {
|
|
|
|
Logger.warn(`[AuthorController] Updating local author imagePath is not supported`)
|
|
|
|
delete payload.imagePath
|
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
|
2023-09-03 17:04:14 +02:00
|
|
|
let existingAuthor = null
|
|
|
|
if (authorNameUpdate) {
|
|
|
|
const author = await Database.authorModel.findOne({
|
|
|
|
where: {
|
|
|
|
id: {
|
|
|
|
[sequelize.Op.not]: req.author.id
|
|
|
|
},
|
|
|
|
name: payload.name
|
|
|
|
}
|
|
|
|
})
|
|
|
|
existingAuthor = author?.getOldAuthor()
|
|
|
|
}
|
2022-07-24 19:00:36 +02:00
|
|
|
if (existingAuthor) {
|
2023-07-05 01:14:44 +02:00
|
|
|
const bookAuthorsToCreate = []
|
2023-08-20 20:34:03 +02:00
|
|
|
const itemsWithAuthor = await Database.libraryItemModel.getForAuthor(req.author)
|
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)
|
2023-07-05 01:14:44 +02:00
|
|
|
bookAuthorsToCreate.push({
|
|
|
|
bookId: libraryItem.media.id,
|
|
|
|
authorId: existingAuthor.id
|
|
|
|
})
|
2022-07-24 19:00:36 +02:00
|
|
|
})
|
|
|
|
if (itemsWithAuthor.length) {
|
2023-07-05 01:14:44 +02:00
|
|
|
await Database.removeBulkBookAuthors(req.author.id) // Remove all old BookAuthor
|
|
|
|
await Database.createBulkBookAuthors(bookAuthorsToCreate) // Create all new BookAuthor
|
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
|
2023-07-05 01:14:44 +02:00
|
|
|
await Database.removeAuthor(req.author.id)
|
2022-11-24 22:53:58 +01:00
|
|
|
SocketAuthority.emitter('author_removed', req.author.toJSON())
|
2023-08-18 21:40:36 +02:00
|
|
|
// Update filter data
|
|
|
|
Database.removeAuthorFromFilterData(req.author.libraryId, req.author.id)
|
2022-07-24 19:00:36 +02:00
|
|
|
|
|
|
|
// Send updated num books for merged author
|
2023-10-14 00:37:37 +02:00
|
|
|
const numBooks = (await Database.libraryItemModel.getForAuthor(existingAuthor)).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) {
|
2022-12-26 22:45:42 +01:00
|
|
|
req.author.updatedAt = Date.now()
|
|
|
|
|
2023-08-20 20:34:03 +02:00
|
|
|
const itemsWithAuthor = await Database.libraryItemModel.getForAuthor(req.author)
|
2022-07-24 19:00:36 +02:00
|
|
|
if (authorNameUpdate) { // Update author name on all books
|
|
|
|
itemsWithAuthor.forEach(libraryItem => {
|
|
|
|
libraryItem.media.metadata.updateAuthor(req.author)
|
|
|
|
})
|
|
|
|
if (itemsWithAuthor.length) {
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-05 01:14:44 +02:00
|
|
|
await Database.updateAuthor(req.author)
|
2023-08-06 22:06:45 +02:00
|
|
|
SocketAuthority.emitter('author_updated', req.author.toJSONExpanded(itemsWithAuthor.length))
|
2022-07-24 19:00:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
res.json({
|
|
|
|
author: req.author.toJSON(),
|
|
|
|
updated: hasUpdated
|
|
|
|
})
|
|
|
|
}
|
2022-03-15 00:53:49 +01:00
|
|
|
}
|
|
|
|
|
2023-09-25 00:06:32 +02:00
|
|
|
/**
|
|
|
|
* DELETE: /api/authors/:id
|
|
|
|
* Remove author from all books and delete
|
|
|
|
*
|
|
|
|
* @param {import('express').Request} req
|
|
|
|
* @param {import('express').Response} res
|
|
|
|
*/
|
|
|
|
async delete(req, res) {
|
|
|
|
Logger.info(`[AuthorController] Removing author "${req.author.name}"`)
|
|
|
|
|
|
|
|
await Database.authorModel.removeById(req.author.id)
|
|
|
|
|
|
|
|
if (req.author.imagePath) {
|
|
|
|
await CacheManager.purgeImageCache(req.author.id) // Purge cache
|
|
|
|
}
|
|
|
|
|
|
|
|
SocketAuthority.emitter('author_removed', req.author.toJSON())
|
|
|
|
|
|
|
|
// Update filter data
|
|
|
|
Database.removeAuthorFromFilterData(req.author.libraryId, req.author.id)
|
|
|
|
|
|
|
|
res.sendStatus(200)
|
|
|
|
}
|
|
|
|
|
2023-10-14 00:37:37 +02:00
|
|
|
/**
|
|
|
|
* POST: /api/authors/:id/image
|
|
|
|
* Upload author image from web URL
|
|
|
|
*
|
|
|
|
* @param {import('express').Request} req
|
|
|
|
* @param {import('express').Response} res
|
|
|
|
*/
|
|
|
|
async uploadImage(req, res) {
|
|
|
|
if (!req.user.canUpload) {
|
|
|
|
Logger.warn('User attempted to upload an image without permission', req.user)
|
|
|
|
return res.sendStatus(403)
|
|
|
|
}
|
|
|
|
if (!req.body.url) {
|
|
|
|
Logger.error(`[AuthorController] Invalid request payload. 'url' not in request body`)
|
|
|
|
return res.status(400).send(`Invalid request payload. 'url' not in request body`)
|
|
|
|
}
|
|
|
|
if (!req.body.url.startsWith?.('http:') && !req.body.url.startsWith?.('https:')) {
|
|
|
|
Logger.error(`[AuthorController] Invalid request payload. Invalid url "${req.body.url}"`)
|
|
|
|
return res.status(400).send(`Invalid request payload. Invalid url "${req.body.url}"`)
|
|
|
|
}
|
|
|
|
|
|
|
|
Logger.debug(`[AuthorController] Requesting download author image from url "${req.body.url}"`)
|
|
|
|
const result = await AuthorFinder.saveAuthorImage(req.author.id, req.body.url)
|
|
|
|
|
|
|
|
if (result?.error) {
|
|
|
|
return res.status(400).send(result.error)
|
|
|
|
} else if (!result?.path) {
|
|
|
|
return res.status(500).send('Unknown error occurred')
|
|
|
|
}
|
|
|
|
|
|
|
|
if (req.author.imagePath) {
|
|
|
|
await CacheManager.purgeImageCache(req.author.id) // Purge cache
|
|
|
|
}
|
|
|
|
|
|
|
|
req.author.imagePath = result.path
|
|
|
|
await Database.authorModel.updateFromOld(req.author)
|
|
|
|
|
|
|
|
const numBooks = (await Database.libraryItemModel.getForAuthor(req.author)).length
|
|
|
|
SocketAuthority.emitter('author_updated', req.author.toJSONExpanded(numBooks))
|
|
|
|
res.json({
|
|
|
|
author: req.author.toJSON()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* DELETE: /api/authors/:id/image
|
|
|
|
* Remove author image & delete image file
|
|
|
|
*
|
|
|
|
* @param {import('express').Request} req
|
|
|
|
* @param {import('express').Response} res
|
|
|
|
*/
|
|
|
|
async deleteImage(req, res) {
|
|
|
|
if (!req.author.imagePath) {
|
|
|
|
Logger.error(`[AuthorController] Author "${req.author.imagePath}" has no imagePath set`)
|
|
|
|
return res.status(400).send('Author has no image path set')
|
|
|
|
}
|
|
|
|
Logger.info(`[AuthorController] Removing image for author "${req.author.name}" at "${req.author.imagePath}"`)
|
|
|
|
await CacheManager.purgeImageCache(req.author.id) // Purge cache
|
|
|
|
await CoverManager.removeFile(req.author.imagePath)
|
|
|
|
req.author.imagePath = null
|
|
|
|
await Database.authorModel.updateFromOld(req.author)
|
|
|
|
|
|
|
|
const numBooks = (await Database.libraryItemModel.getForAuthor(req.author)).length
|
|
|
|
SocketAuthority.emitter('author_updated', req.author.toJSONExpanded(numBooks))
|
|
|
|
res.json({
|
|
|
|
author: req.author.toJSON()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-03-13 12:42:43 +01:00
|
|
|
async match(req, res) {
|
2023-04-16 22:53:46 +02:00
|
|
|
let authorData = null
|
|
|
|
const region = req.body.region || 'us'
|
2022-05-14 01:11:54 +02:00
|
|
|
if (req.body.asin) {
|
2023-09-04 23:33:55 +02:00
|
|
|
authorData = await AuthorFinder.findAuthorByASIN(req.body.asin, region)
|
2022-05-14 01:11:54 +02:00
|
|
|
} else {
|
2023-09-04 23:33:55 +02:00
|
|
|
authorData = await AuthorFinder.findAuthorByName(req.body.q, region)
|
2022-05-14 01:11:54 +02:00
|
|
|
}
|
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
|
|
|
|
2023-04-16 22:53:46 +02:00
|
|
|
let hasUpdates = false
|
2022-03-13 16:35:35 +01:00
|
|
|
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)) {
|
2023-09-07 00:48:50 +02:00
|
|
|
await CacheManager.purgeImageCache(req.author.id)
|
2022-05-14 01:11:54 +02:00
|
|
|
|
2023-09-04 23:33:55 +02:00
|
|
|
const imageData = await AuthorFinder.saveAuthorImage(req.author.id, authorData.image)
|
2023-10-14 00:37:37 +02:00
|
|
|
if (imageData?.path) {
|
2022-03-13 12:42:43 +01:00
|
|
|
req.author.imagePath = imageData.path
|
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()
|
|
|
|
|
2023-07-05 01:14:44 +02:00
|
|
|
await Database.updateAuthor(req.author)
|
2023-08-06 22:06:45 +02:00
|
|
|
|
2023-10-14 00:37:37 +02:00
|
|
|
const numBooks = (await Database.libraryItemModel.getForAuthor(req.author)).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
|
|
|
}
|
2023-09-07 00:48:50 +02:00
|
|
|
return CacheManager.handleAuthorCache(res, author, options)
|
2022-03-13 12:42:43 +01:00
|
|
|
}
|
|
|
|
|
2023-09-03 00:49:28 +02:00
|
|
|
async middleware(req, res, next) {
|
|
|
|
const author = await Database.authorModel.getOldById(req.params.id)
|
2022-03-13 12:42:43 +01:00
|
|
|
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()
|