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