Skip to content

Commit

Permalink
fix getImageProcessingLibrary usage
Browse files Browse the repository at this point in the history
  • Loading branch information
kaatt authored and adiwajshing committed Nov 14, 2022
1 parent 47900ea commit 5c950da
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/Utils/messages-media.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export const extractImageThumb = async(bufferOrFilePath: Readable | Buffer | str
}

const lib = await getImageProcessingLibrary()
if('sharp' in lib && lib.sharp?.default) {
if('sharp' in lib && typeof lib.sharp?.default === 'function') {
const img = lib.sharp!.default(bufferOrFilePath)
const dimensions = await img.metadata()

Expand All @@ -111,7 +111,7 @@ export const extractImageThumb = async(bufferOrFilePath: Readable | Buffer | str
height: dimensions.height,
},
}
} else if('jimp' in lib && lib.jimp) {
} 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 any)
Expand Down Expand Up @@ -153,14 +153,14 @@ export const generateProfilePicture = async(mediaUpload: WAMediaUpload) => {

const lib = await getImageProcessingLibrary()
let img: Promise<Buffer>
if('sharp' in lib) {
if('sharp' in lib && typeof lib.sharp?.default === 'function') {
img = lib.sharp!.default(bufferOrFilePath)
.resize(640, 640)
.jpeg({
quality: 50,
})
.toBuffer()
} else {
} else if('jimp' in lib && typeof lib.jimp.read === 'function') {
const { read, MIME_JPEG, RESIZE_BILINEAR } = lib.jimp
const jimp = await read(bufferOrFilePath as any)
const min = Math.min(jimp.getWidth(), jimp.getHeight())
Expand All @@ -170,6 +170,8 @@ export const generateProfilePicture = async(mediaUpload: WAMediaUpload) => {
.quality(50)
.resize(640, 640, RESIZE_BILINEAR)
.getBufferAsync(MIME_JPEG)
} else {
throw new Boom('No image processing library available')
}

return {
Expand Down

0 comments on commit 5c950da

Please sign in to comment.