]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/helpers/image-utils.ts
space optimizations for `node_modules` and client stats removal
[github/Chocobozzz/PeerTube.git] / server / helpers / image-utils.ts
CommitLineData
ac81d1a0 1import 'multer'
74577825
C
2import { readFile, remove } from 'fs-extra'
3import { logger } from './logger'
e6dfa586 4const Jimp = require('jimp')
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) {
e6dfa586 13 throw new Error('Jimp needs an input path different that the output path.')
a8a63227
C
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)
e6dfa586 20 const jimpInstance = await Jimp.read(buf)
a8a63227
C
21
22 await remove(destination)
23
e6dfa586 24 await jimpInstance
ac81d1a0 25 .resize(newSize.width, newSize.height)
e6dfa586
RK
26 .quality(80)
27 .writeAsync(destination)
ac81d1a0 28
2fb5b3a5 29 if (keepOriginal !== true) await remove(path)
ac81d1a0
C
30}
31
32// ---------------------------------------------------------------------------
33
34export {
35 processImage
36}