]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/helpers/image-utils.ts
Merge branch 'develop' into pr/1217
[github/Chocobozzz/PeerTube.git] / server / helpers / image-utils.ts
1 import 'multer'
2 import * as sharp from 'sharp'
3 import { readFile, remove } from 'fs-extra'
4 import { logger } from './logger'
5
6 async 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
32 export {
33 processImage
34 }