Fix:Update author updatedAt when downloading new image, fixes author image refresh #2934

This commit is contained in:
advplyr 2024-05-06 17:17:35 -05:00
parent ce98bcc989
commit 941c798d78
4 changed files with 39 additions and 26 deletions

View File

@ -84,4 +84,4 @@ export default {
}, },
mounted() {} mounted() {}
} }
</script> </script>

View File

@ -242,4 +242,4 @@ export default {
mounted() {}, mounted() {},
beforeDestroy() {} beforeDestroy() {}
} }
</script> </script>

View File

@ -108,4 +108,4 @@ export default {
this.$root.socket.off('author_removed', this.authorRemoved) this.$root.socket.off('author_removed', this.authorRemoved)
} }
} }
</script> </script>

View File

@ -15,7 +15,7 @@ const naturalSort = createNewSortInstance({
comparer: new Intl.Collator(undefined, { numeric: true, sensitivity: 'base' }).compare comparer: new Intl.Collator(undefined, { numeric: true, sensitivity: 'base' }).compare
}) })
class AuthorController { class AuthorController {
constructor() { } constructor() {}
async findOne(req, res) { async findOne(req, res) {
const include = (req.query.include || '').split(',') const include = (req.query.include || '').split(',')
@ -32,7 +32,6 @@ class AuthorController {
authorJson.libraryItems.forEach((li) => { authorJson.libraryItems.forEach((li) => {
if (li.media.metadata.series) { if (li.media.metadata.series) {
li.media.metadata.series.forEach((series) => { li.media.metadata.series.forEach((series) => {
const itemWithSeries = li.toJSONMinified() const itemWithSeries = li.toJSONMinified()
itemWithSeries.media.metadata.series = series itemWithSeries.media.metadata.series = series
@ -50,14 +49,14 @@ class AuthorController {
}) })
// Sort series items // Sort series items
for (const key in seriesMap) { for (const key in seriesMap) {
seriesMap[key].items = naturalSort(seriesMap[key].items).asc(li => li.media.metadata.series.sequence) seriesMap[key].items = naturalSort(seriesMap[key].items).asc((li) => li.media.metadata.series.sequence)
} }
authorJson.series = Object.values(seriesMap) authorJson.series = Object.values(seriesMap)
} }
// Minify library items // Minify library items
authorJson.libraryItems = authorJson.libraryItems.map(li => li.toJSONMinified()) authorJson.libraryItems = authorJson.libraryItems.map((li) => li.toJSONMinified())
} }
return res.json(authorJson) return res.json(authorJson)
@ -91,7 +90,8 @@ class AuthorController {
if (existingAuthor) { if (existingAuthor) {
const bookAuthorsToCreate = [] const bookAuthorsToCreate = []
const itemsWithAuthor = await Database.libraryItemModel.getForAuthor(req.author) const itemsWithAuthor = await Database.libraryItemModel.getForAuthor(req.author)
itemsWithAuthor.forEach(libraryItem => { // Replace old author with merging author for each book itemsWithAuthor.forEach((libraryItem) => {
// Replace old author with merging author for each book
libraryItem.media.metadata.replaceAuthor(req.author, existingAuthor) libraryItem.media.metadata.replaceAuthor(req.author, existingAuthor)
bookAuthorsToCreate.push({ bookAuthorsToCreate.push({
bookId: libraryItem.media.id, bookId: libraryItem.media.id,
@ -101,7 +101,10 @@ class AuthorController {
if (itemsWithAuthor.length) { if (itemsWithAuthor.length) {
await Database.removeBulkBookAuthors(req.author.id) // Remove all old BookAuthor await Database.removeBulkBookAuthors(req.author.id) // Remove all old BookAuthor
await Database.createBulkBookAuthors(bookAuthorsToCreate) // Create all new BookAuthor await Database.createBulkBookAuthors(bookAuthorsToCreate) // Create all new BookAuthor
SocketAuthority.emitter('items_updated', itemsWithAuthor.map(li => li.toJSONExpanded())) SocketAuthority.emitter(
'items_updated',
itemsWithAuthor.map((li) => li.toJSONExpanded())
)
} }
// Remove old author // Remove old author
@ -118,7 +121,8 @@ class AuthorController {
author: existingAuthor.toJSON(), author: existingAuthor.toJSON(),
merged: true merged: true
}) })
} else { // Regular author update } else {
// Regular author update
if (req.author.update(payload)) { if (req.author.update(payload)) {
hasUpdated = true hasUpdated = true
} }
@ -127,12 +131,16 @@ class AuthorController {
req.author.updatedAt = Date.now() req.author.updatedAt = Date.now()
const itemsWithAuthor = await Database.libraryItemModel.getForAuthor(req.author) const itemsWithAuthor = await Database.libraryItemModel.getForAuthor(req.author)
if (authorNameUpdate) { // Update author name on all books if (authorNameUpdate) {
itemsWithAuthor.forEach(libraryItem => { // Update author name on all books
itemsWithAuthor.forEach((libraryItem) => {
libraryItem.media.metadata.updateAuthor(req.author) libraryItem.media.metadata.updateAuthor(req.author)
}) })
if (itemsWithAuthor.length) { if (itemsWithAuthor.length) {
SocketAuthority.emitter('items_updated', itemsWithAuthor.map(li => li.toJSONExpanded())) SocketAuthority.emitter(
'items_updated',
itemsWithAuthor.map((li) => li.toJSONExpanded())
)
} }
} }
@ -150,9 +158,9 @@ class AuthorController {
/** /**
* DELETE: /api/authors/:id * DELETE: /api/authors/:id
* Remove author from all books and delete * Remove author from all books and delete
* *
* @param {import('express').Request} req * @param {import('express').Request} req
* @param {import('express').Response} res * @param {import('express').Response} res
*/ */
async delete(req, res) { async delete(req, res) {
Logger.info(`[AuthorController] Removing author "${req.author.name}"`) Logger.info(`[AuthorController] Removing author "${req.author.name}"`)
@ -174,9 +182,9 @@ class AuthorController {
/** /**
* POST: /api/authors/:id/image * POST: /api/authors/:id/image
* Upload author image from web URL * Upload author image from web URL
* *
* @param {import('express').Request} req * @param {import('express').Request} req
* @param {import('express').Response} res * @param {import('express').Response} res
*/ */
async uploadImage(req, res) { async uploadImage(req, res) {
if (!req.user.canUpload) { if (!req.user.canUpload) {
@ -206,6 +214,7 @@ class AuthorController {
} }
req.author.imagePath = result.path req.author.imagePath = result.path
req.author.updatedAt = Date.now()
await Database.authorModel.updateFromOld(req.author) await Database.authorModel.updateFromOld(req.author)
const numBooks = (await Database.libraryItemModel.getForAuthor(req.author)).length const numBooks = (await Database.libraryItemModel.getForAuthor(req.author)).length
@ -218,9 +227,9 @@ class AuthorController {
/** /**
* DELETE: /api/authors/:id/image * DELETE: /api/authors/:id/image
* Remove author image & delete image file * Remove author image & delete image file
* *
* @param {import('express').Request} req * @param {import('express').Request} req
* @param {import('express').Response} res * @param {import('express').Response} res
*/ */
async deleteImage(req, res) { async deleteImage(req, res) {
if (!req.author.imagePath) { if (!req.author.imagePath) {
@ -292,10 +301,14 @@ class AuthorController {
// GET api/authors/:id/image // GET api/authors/:id/image
async getImage(req, res) { async getImage(req, res) {
const { query: { width, height, format, raw }, author } = req const {
query: { width, height, format, raw },
author
} = req
if (raw) { // any value if (raw) {
if (!author.imagePath || !await fs.pathExists(author.imagePath)) { // any value
if (!author.imagePath || !(await fs.pathExists(author.imagePath))) {
return res.sendStatus(404) return res.sendStatus(404)
} }
@ -326,4 +339,4 @@ class AuthorController {
next() next()
} }
} }
module.exports = new AuthorController() module.exports = new AuthorController()