]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame_incremental - server/helpers/image-utils.ts
Fix thumbnail processing
[github/Chocobozzz/PeerTube.git] / server / helpers / image-utils.ts
... / ...
CommitLineData
1import 'multer'
2import * as sharp from 'sharp'
3import { readFile, remove } from 'fs-extra'
4import { logger } from './logger'
5
6async function processImage (
7 physicalFile: { path: string },
8 destination: string,
9 newSize: { width: number, height: number }
10) {
11 if (physicalFile.path === destination) {
12 throw new Error('Sharp needs an input path different that the output path.')
13 }
14
15 logger.debug('Processing image %s to %s.', physicalFile.path, destination)
16
17 // Avoid sharp cache
18 const buf = await readFile(physicalFile.path)
19 const sharpInstance = sharp(buf)
20
21 await remove(destination)
22
23 await sharpInstance
24 .resize(newSize.width, newSize.height)
25 .toFile(destination)
26
27 await remove(physicalFile.path)
28}
29
30// ---------------------------------------------------------------------------
31
32export {
33 processImage
34}