]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/helpers/image-utils.ts
Add ability to save live replay
[github/Chocobozzz/PeerTube.git] / server / helpers / image-utils.ts
CommitLineData
18782242
C
1import { remove, rename } from 'fs-extra'
2import { convertWebPToJPG } from './ffmpeg-utils'
74577825 3import { logger } from './logger'
18782242 4
e6dfa586 5const Jimp = require('jimp')
ac81d1a0
C
6
7async function processImage (
2fb5b3a5 8 path: string,
ac81d1a0 9 destination: string,
e8bafea3
C
10 newSize: { width: number, height: number },
11 keepOriginal = false
ac81d1a0 12) {
2fb5b3a5 13 if (path === destination) {
e6dfa586 14 throw new Error('Jimp needs an input path different that the output path.')
a8a63227
C
15 }
16
2fb5b3a5 17 logger.debug('Processing image %s to %s.', path, destination)
a8a63227 18
18782242
C
19 let jimpInstance: any
20
21 try {
22 jimpInstance = await Jimp.read(path)
23 } catch (err) {
b5b68755 24 logger.debug('Cannot read %s with jimp. Try to convert the image using ffmpeg first.', path, { err })
18782242
C
25
26 const newName = path + '.jpg'
27 await convertWebPToJPG(path, newName)
28 await rename(newName, path)
29
30 jimpInstance = await Jimp.read(path)
31 }
a8a63227
C
32
33 await remove(destination)
34
e6dfa586 35 await jimpInstance
ac81d1a0 36 .resize(newSize.width, newSize.height)
e6dfa586
RK
37 .quality(80)
38 .writeAsync(destination)
ac81d1a0 39
2fb5b3a5 40 if (keepOriginal !== true) await remove(path)
ac81d1a0
C
41}
42
43// ---------------------------------------------------------------------------
44
45export {
46 processImage
47}