Add:Authors page match authors and display author image

This commit is contained in:
advplyr 2022-03-13 10:35:35 -05:00
parent dad12537b6
commit 30f15d3575
9 changed files with 432 additions and 285 deletions

View file

@ -164,6 +164,7 @@ class ApiController {
//
this.router.get('/authors/:id', AuthorController.middleware.bind(this), AuthorController.findOne.bind(this))
this.router.post('/authors/:id/match', AuthorController.middleware.bind(this), AuthorController.match.bind(this))
this.router.get('/authors/:id/image', AuthorController.middleware.bind(this), AuthorController.getImage.bind(this))
this.router.get('/authors/search', AuthorController.search.bind(this))
//

View file

@ -8,6 +8,7 @@ class CacheManager {
constructor() {
this.CachePath = Path.join(global.MetadataPath, 'cache')
this.CoverCachePath = Path.join(this.CachePath, 'covers')
this.ImageCachePath = Path.join(this.CachePath, 'images')
}
async handleCoverCache(res, libraryItem, options = {}) {
@ -72,5 +73,37 @@ class CacheManager {
})
}
}
async handleAuthorCache(res, author, options = {}) {
const format = options.format || 'webp'
const width = options.width || 400
const height = options.height || null
res.type(`image/${format}`)
var path = Path.join(this.ImageCachePath, `${author.id}_${width}${height ? `x${height}` : ''}`) + '.' + format
// Cache exists
if (await fs.pathExists(path)) {
const r = fs.createReadStream(path)
const ps = new stream.PassThrough()
stream.pipeline(r, ps, (err) => {
if (err) {
console.log(err)
return res.sendStatus(400)
}
})
return ps.pipe(res)
}
// Write cache
await fs.ensureDir(this.ImageCachePath)
let writtenFile = await resizeImage(author.imagePath, path, width, height)
if (!writtenFile) return res.sendStatus(400)
var readStream = fs.createReadStream(writtenFile)
readStream.pipe(res)
}
}
module.exports = CacheManager

View file

@ -61,28 +61,7 @@ class StreamManager {
}
}
async tempCheckStrayStreams() {
try {
var dirs = await fs.readdir(global.MetadataPath)
if (!dirs || !dirs.length) return true
await Promise.all(dirs.map(async (dirname) => {
if (dirname !== 'streams' && dirname !== 'books' && dirname !== 'downloads' && dirname !== 'backups' && dirname !== 'logs' && dirname !== 'cache') {
var fullPath = Path.join(global.MetadataPath, dirname)
Logger.warn(`Removing OLD Orphan Stream ${dirname}`)
return fs.remove(fullPath)
}
}))
return true
} catch (error) {
Logger.debug('No old orphan streams', error)
return false
}
}
async removeOrphanStreams() {
await this.tempCheckStrayStreams()
try {
var dirs = await fs.readdir(this.StreamsPath)
if (!dirs || !dirs.length) return true

View file

@ -1,4 +1,5 @@
const Logger = require('../Logger')
const { reqSupportsWebp } = require('../utils/index')
class AuthorController {
constructor() { }
@ -21,20 +22,55 @@ class AuthorController {
if (!authorData) {
return res.status(404).send('Author not found')
}
req.author.asin = authorData.asin
if (authorData.image) {
Logger.debug(`[AuthorController] match author with "${req.body.q}"`, authorData)
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)) {
var imageData = await this.authorFinder.saveAuthorImage(req.author.id, authorData.image)
if (imageData) {
req.author.imagePath = imageData.path
req.author.relImagePath = imageData.relPath
hasUpdates = true
}
}
if (authorData.description) {
if (authorData.description && req.author.description !== authorData.description) {
req.author.description = authorData.description
hasUpdates = true
}
await this.db.updateEntity('author', req.author)
this.emitter('author_updated', req.author)
res.json(req.author)
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
this.emitter('author_updated', req.author.toJSONExpanded(numBooks))
}
res.json({
updated: hasUpdates,
author: req.author
})
}
// GET api/authors/:id/image
async getImage(req, res) {
let { query: { width, height, format }, author } = req
const options = {
format: format || (reqSupportsWebp(req) ? 'webp' : 'jpeg'),
height: height ? parseInt(height) : null,
width: width ? parseInt(width) : null
}
return this.cacheManager.handleAuthorCache(res, author, options)
}
middleware(req, res, next) {

View file

@ -40,6 +40,12 @@ class Author {
}
}
toJSONExpanded(numBooks = 0) {
var json = this.toJSON()
json.numBooks = numBooks
return json
}
toJSONMinimal() {
return {
id: this.id,