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