Added an endpoint to serve different cover sizes

This commit is contained in:
Keagan Hilliard 2021-12-12 08:52:27 -08:00
parent c92175cdc7
commit 69fa00608f
3 changed files with 42 additions and 0 deletions

View File

@ -45,6 +45,7 @@
"podcast": "^1.3.0",
"read-chunk": "^3.1.0",
"recursive-readdir-async": "^1.1.8",
"sharp": "^0.29.3",
"socket.io": "^4.1.3",
"string-strip-html": "^8.3.0",
"watcher": "^1.2.0",

View File

@ -5,6 +5,7 @@ const date = require('date-and-time')
const Logger = require('./Logger')
const { isObject } = require('./utils/index')
const resize = require('./utils/resizeImage')
const audioFileScanner = require('./utils/audioFileScanner')
const BookController = require('./controllers/BookController')
@ -82,6 +83,7 @@ class ApiController {
this.router.patch('/books/:id/tracks', BookController.updateTracks.bind(this))
this.router.get('/books/:id/stream', BookController.openStream.bind(this))
this.router.post('/books/:id/cover', BookController.uploadCover.bind(this))
this.router.get('/books/:id/cover', this.resizeCover.bind(this))
this.router.patch('/books/:id/coverfile', BookController.updateCoverFromFile.bind(this))
// TEMP: Support old syntax for mobile app
@ -176,6 +178,29 @@ class ApiController {
this.router.post('/syncUserAudiobookData', this.syncUserAudiobookData.bind(this))
}
async resizeCover(req, res) {
let { query: { width, height }, params: { id }, user } = req;
if (!user) {
return res.sendStatus(403)
}
var audiobook = this.db.audiobooks.find(a => a.id === id)
if (!audiobook) return res.sendStatus(404)
// Check user can access this audiobooks library
if (!req.user.checkCanAccessLibrary(audiobook.libraryId)) {
return res.sendStatus(403)
}
res.type('image/jpeg');
if (width) width = parseInt(width)
if (height) height = parseInt(height)
return resize(audiobook.book.coverFullPath, width, height).pipe(res)
}
async findBooks(req, res) {
var provider = req.query.provider || 'google'
var title = req.query.title || ''

View File

@ -0,0 +1,16 @@
const sharp = require('sharp')
const fs = require('fs')
function resize(filePath, width, height) {
const readStream = fs.createReadStream(filePath);
let sharpie = sharp()
sharpie.toFormat('jpeg')
if (width || height) {
sharpie.resize(width, height)
}
return readStream.pipe(sharpie)
}
module.exports = resize;