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