mirror of
				https://github.com/advplyr/audiobookshelf.git
				synced 2025-10-27 11:18:14 +01:00 
			
		
		
		
	Merge pull request #233 from keaganhilliard/master
Added an endpoint to serve different cover sizes
This commit is contained in:
		
						commit
						d04f3450ec
					
				| @ -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", | ||||
|  | ||||
| @ -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 || '' | ||||
|  | ||||
							
								
								
									
										16
									
								
								server/utils/resizeImage.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										16
									
								
								server/utils/resizeImage.js
									
									
									
									
									
										Normal 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; | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user