]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame_incremental - server/helpers/image-utils.ts
Process remaining segment hashes on live ending
[github/Chocobozzz/PeerTube.git] / server / helpers / image-utils.ts
... / ...
CommitLineData
1import { remove, rename } from 'fs-extra'
2import { convertWebPToJPG } from './ffmpeg-utils'
3import { logger } from './logger'
4
5const Jimp = require('jimp')
6
7async 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
45export {
46 processImage
47}