Bump Jimp && Sharp (#1575)

* Bump Jimp && Sharp

Upgrade and use the latest version of jimp and sharp libraries

* lint
This commit is contained in:
DevAstro
2025-07-05 21:58:57 +01:00
committed by GitHub
parent 719a57b275
commit 3cdb0f4dd9
3 changed files with 494 additions and 648 deletions

View File

@@ -4,6 +4,7 @@ import { exec } from 'child_process'
import * as Crypto from 'crypto'
import { once } from 'events'
import { createReadStream, createWriteStream, promises as fs, WriteStream } from 'fs'
import { ResizeStrategy } from 'jimp'
import type { IAudioMetadata } from 'music-metadata'
import { tmpdir } from 'os'
import { join } from 'path'
@@ -32,22 +33,12 @@ import { ILogger } from './logger'
const getTmpFilesDirectory = () => tmpdir()
const getImageProcessingLibrary = async () => {
const [_jimp, sharp] = await Promise.all([
(async () => {
const jimp = await import('jimp').catch(() => {})
return jimp
})(),
(async () => {
const sharp = await import('sharp').catch(() => {})
return sharp
})()
])
const [jimp, sharp] = await Promise.all([import('jimp').catch(() => {}), import('sharp').catch(() => {})])
if (sharp) {
return { sharp }
}
const jimp = _jimp?.default || _jimp
if (jimp) {
return { jimp }
}
@@ -159,15 +150,15 @@ export const extractImageThumb = async (bufferOrFilePath: Readable | Buffer | st
height: dimensions.height
}
}
} else if ('jimp' in lib && typeof lib.jimp?.read === 'function') {
const { read, MIME_JPEG, RESIZE_BILINEAR, AUTO } = lib.jimp
const jimp = await read(bufferOrFilePath as string)
} else if ('jimp' in lib && typeof lib.jimp?.Jimp === 'object') {
const jimp = await lib.jimp.default.Jimp.read(bufferOrFilePath)
const dimensions = {
width: jimp.getWidth(),
height: jimp.getHeight()
width: jimp.width,
height: jimp.height
}
const buffer = await jimp.quality(50).resize(width, AUTO, RESIZE_BILINEAR).getBufferAsync(MIME_JPEG)
const buffer = await jimp
.resize({ w: width, mode: ResizeStrategy.BILINEAR })
.getBuffer('image/jpeg', { quality: 50 })
return {
buffer,
original: dimensions
@@ -207,13 +198,12 @@ export const generateProfilePicture = async (
quality: 50
})
.toBuffer()
} else if ('jimp' in lib && typeof lib.jimp?.read === 'function') {
const { read, MIME_JPEG, RESIZE_BILINEAR } = lib.jimp
const jimp = await read(buffer)
const min = Math.min(jimp.getWidth(), jimp.getHeight())
const cropped = jimp.crop(0, 0, min, min)
} else if ('jimp' in lib && typeof lib.jimp?.Jimp === 'object') {
const jimp = await lib.jimp.default.Jimp.read(buffer)
const min = Math.min(jimp.width, jimp.height)
const cropped = jimp.crop({ x: 0, y: 0, w: min, h: min })
img = cropped.quality(50).resize(w, h, RESIZE_BILINEAR).getBufferAsync(MIME_JPEG)
img = cropped.resize({ w, h, mode: ResizeStrategy.BILINEAR }).getBuffer('image/jpeg', { quality: 50 })
} else {
throw new Boom('No image processing library available')
}