From 69fa00608f16a3d9c83e494116de501b8b1319fb Mon Sep 17 00:00:00 2001 From: Keagan Hilliard Date: Sun, 12 Dec 2021 08:52:27 -0800 Subject: [PATCH] Added an endpoint to serve different cover sizes --- package.json | 1 + server/ApiController.js | 25 +++++++++++++++++++++++++ server/utils/resizeImage.js | 16 ++++++++++++++++ 3 files changed, 42 insertions(+) create mode 100644 server/utils/resizeImage.js diff --git a/package.json b/package.json index 13c56039..acfeb316 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/server/ApiController.js b/server/ApiController.js index dbe6a053..7f423bf9 100644 --- a/server/ApiController.js +++ b/server/ApiController.js @@ -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 || '' diff --git a/server/utils/resizeImage.js b/server/utils/resizeImage.js new file mode 100644 index 00000000..ec359725 --- /dev/null +++ b/server/utils/resizeImage.js @@ -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; \ No newline at end of file