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