mirror of
https://github.com/FranP-code/Baileys.git
synced 2025-10-13 00:32:22 +00:00
Add support for stream as media message input (#905)
* Add support for stream as media message input * refactor: use async/await on readable toBuffer * refactor: be more explicit about using a readable stream Co-authored-by: Adhiraj Singh <adhirajsingh1001@gmail.com>
This commit is contained in:
@@ -100,7 +100,7 @@ export const makeChatsSocket = (config: SocketConfig) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const updateProfilePicture = async(jid: string, content: WAMediaUpload) => {
|
const updateProfilePicture = async(jid: string, content: WAMediaUpload) => {
|
||||||
const { img } = await generateProfilePicture('url' in content ? content.url.toString() : content)
|
const { img } = await generateProfilePicture(content)
|
||||||
await query({
|
await query({
|
||||||
tag: 'iq',
|
tag: 'iq',
|
||||||
attrs: {
|
attrs: {
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ import type { Logger } from "pino"
|
|||||||
import type { URL } from "url"
|
import type { URL } from "url"
|
||||||
import type NodeCache from "node-cache"
|
import type NodeCache from "node-cache"
|
||||||
import type { GroupMetadata } from "./GroupMetadata"
|
import type { GroupMetadata } from "./GroupMetadata"
|
||||||
|
import type { Readable } from "stream"
|
||||||
import { proto } from '../../WAProto'
|
import { proto } from '../../WAProto'
|
||||||
|
|
||||||
// export the WAMessage Prototypes
|
// export the WAMessage Prototypes
|
||||||
@@ -18,7 +19,7 @@ export type WALocationMessage = proto.ILocationMessage
|
|||||||
export type WAGenericMediaMessage = proto.IVideoMessage | proto.IImageMessage | proto.IAudioMessage | proto.IDocumentMessage | proto.IStickerMessage
|
export type WAGenericMediaMessage = proto.IVideoMessage | proto.IImageMessage | proto.IAudioMessage | proto.IDocumentMessage | proto.IStickerMessage
|
||||||
export import WAMessageStubType = proto.WebMessageInfo.WebMessageInfoStubType
|
export import WAMessageStubType = proto.WebMessageInfo.WebMessageInfoStubType
|
||||||
export import WAMessageStatus = proto.WebMessageInfo.WebMessageInfoStatus
|
export import WAMessageStatus = proto.WebMessageInfo.WebMessageInfoStatus
|
||||||
export type WAMediaUpload = Buffer | { url: URL | string }
|
export type WAMediaUpload = Buffer | { url: URL | string } | { stream: Readable }
|
||||||
/** Set of message types that are supported by the library */
|
/** Set of message types that are supported by the library */
|
||||||
export type MessageType = keyof proto.Message
|
export type MessageType = keyof proto.Message
|
||||||
|
|
||||||
|
|||||||
@@ -51,13 +51,25 @@ const extractVideoThumb = async (
|
|||||||
})
|
})
|
||||||
}) as Promise<void>
|
}) as Promise<void>
|
||||||
|
|
||||||
export const compressImage = async (bufferOrFilePath: Buffer | string) => {
|
export const compressImage = async (bufferOrFilePath: Readable | Buffer | string) => {
|
||||||
|
if(bufferOrFilePath instanceof Readable) {
|
||||||
|
bufferOrFilePath = await toBuffer(bufferOrFilePath)
|
||||||
|
}
|
||||||
const { read, MIME_JPEG } = await import('jimp')
|
const { read, MIME_JPEG } = await import('jimp')
|
||||||
const jimp = await read(bufferOrFilePath as any)
|
const jimp = await read(bufferOrFilePath as any)
|
||||||
const result = await jimp.resize(32, 32).getBufferAsync(MIME_JPEG)
|
const result = await jimp.resize(32, 32).getBufferAsync(MIME_JPEG)
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
export const generateProfilePicture = async (bufferOrFilePath: Buffer | string) => {
|
export const generateProfilePicture = async (mediaUpload: WAMediaUpload) => {
|
||||||
|
let bufferOrFilePath: Buffer | string
|
||||||
|
if(Buffer.isBuffer(mediaUpload)) {
|
||||||
|
bufferOrFilePath = mediaUpload
|
||||||
|
} else if('url' in mediaUpload) {
|
||||||
|
bufferOrFilePath = mediaUpload.url.toString()
|
||||||
|
} else {
|
||||||
|
bufferOrFilePath = await toBuffer(mediaUpload.stream)
|
||||||
|
}
|
||||||
|
|
||||||
const { read, MIME_JPEG } = await import('jimp')
|
const { read, MIME_JPEG } = await import('jimp')
|
||||||
const jimp = await read(bufferOrFilePath as any)
|
const jimp = await read(bufferOrFilePath as any)
|
||||||
const min = Math.min(jimp.getWidth (), jimp.getHeight ())
|
const min = Math.min(jimp.getWidth (), jimp.getHeight ())
|
||||||
@@ -89,8 +101,16 @@ export const toReadable = (buffer: Buffer) => {
|
|||||||
readable.push(null)
|
readable.push(null)
|
||||||
return readable
|
return readable
|
||||||
}
|
}
|
||||||
|
export const toBuffer = async(stream: Readable) => {
|
||||||
|
let buff = Buffer.alloc(0)
|
||||||
|
for await(const chunk of stream) {
|
||||||
|
buff = Buffer.concat([ buff, chunk ])
|
||||||
|
}
|
||||||
|
return buff
|
||||||
|
}
|
||||||
export const getStream = async (item: WAMediaUpload) => {
|
export const getStream = async (item: WAMediaUpload) => {
|
||||||
if(Buffer.isBuffer(item)) return { stream: toReadable(item), type: 'buffer' }
|
if(Buffer.isBuffer(item)) return { stream: toReadable(item), type: 'buffer' }
|
||||||
|
if('stream' in item) return { stream: item.stream, type: 'readable' }
|
||||||
if(item.url.toString().startsWith('http://') || item.url.toString().startsWith('https://')) {
|
if(item.url.toString().startsWith('http://') || item.url.toString().startsWith('https://')) {
|
||||||
return { stream: await getGotStream(item.url), type: 'remote' }
|
return { stream: await getGotStream(item.url), type: 'remote' }
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user