]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/helpers/image-utils.ts
Translated using Weblate (Russian)
[github/Chocobozzz/PeerTube.git] / server / helpers / image-utils.ts
CommitLineData
1664bc60
C
1import { copy, readFile, remove, rename } from 'fs-extra'
2import * as Jimp from 'jimp'
f619de0e 3import { extname } from 'path'
123f6193 4import { convertWebPToJPG, processGIF } from './ffmpeg-utils'
74577825 5import { logger } from './logger'
18782242 6
ac81d1a0 7async function processImage (
2fb5b3a5 8 path: string,
ac81d1a0 9 destination: string,
e8bafea3
C
10 newSize: { width: number, height: number },
11 keepOriginal = false
ac81d1a0 12) {
123f6193
K
13 const extension = extname(path)
14
f619de0e
C
15 if (path === destination) {
16 throw new Error('Jimp/FFmpeg needs an input path different that the output path.')
17 }
18
19 logger.debug('Processing image %s to %s.', path, destination)
20
123f6193
K
21 // Use FFmpeg to process GIF
22 if (extension === '.gif') {
f619de0e
C
23 await processGIF(path, destination, newSize)
24 } else {
1664bc60 25 await jimpProcessor(path, destination, newSize, extension)
123f6193
K
26 }
27
f619de0e
C
28 if (keepOriginal !== true) await remove(path)
29}
a8a63227 30
f619de0e 31// ---------------------------------------------------------------------------
a8a63227 32
f619de0e
C
33export {
34 processImage
35}
36
37// ---------------------------------------------------------------------------
38
1664bc60
C
39async function jimpProcessor (path: string, destination: string, newSize: { width: number, height: number }, inputExt: string) {
40 let jimpInstance: Jimp
41 const inputBuffer = await readFile(path)
18782242
C
42
43 try {
1664bc60 44 jimpInstance = await Jimp.read(inputBuffer)
18782242 45 } catch (err) {
b5b68755 46 logger.debug('Cannot read %s with jimp. Try to convert the image using ffmpeg first.', path, { err })
18782242
C
47
48 const newName = path + '.jpg'
49 await convertWebPToJPG(path, newName)
50 await rename(newName, path)
51
52 jimpInstance = await Jimp.read(path)
53 }
a8a63227
C
54
55 await remove(destination)
56
1664bc60
C
57 // Optimization if the source file has the appropriate size
58 if (await skipProcessing({ jimpInstance, newSize, imageBytes: inputBuffer.byteLength, inputExt, outputExt: extname(destination) })) {
59 return copy(path, destination)
60 }
61
e6dfa586 62 await jimpInstance
ac81d1a0 63 .resize(newSize.width, newSize.height)
e6dfa586
RK
64 .quality(80)
65 .writeAsync(destination)
ac81d1a0 66}
1664bc60
C
67
68function skipProcessing (options: {
69 jimpInstance: Jimp
70 newSize: { width: number, height: number }
71 imageBytes: number
72 inputExt: string
73 outputExt: string
74}) {
75 const { jimpInstance, newSize, imageBytes, inputExt, outputExt } = options
76 const { width, height } = newSize
77
78 if (jimpInstance.getWidth() > width || jimpInstance.getHeight() > height) return false
79 if (inputExt !== outputExt) return false
80
81 const kB = 1000
82
83 if (height >= 1000) return imageBytes <= 200 * kB
84 if (height >= 500) return imageBytes <= 100 * kB
85
86 return imageBytes <= 15 * kB
87}