mirror of
https://github.com/FranP-code/Baileys.git
synced 2025-10-13 00:32:22 +00:00
chore: pass axios options to downloading remote media
This commit is contained in:
@@ -639,6 +639,7 @@ export const makeMessagesSocket = (config: SocketConfig) => {
|
|||||||
),
|
),
|
||||||
upload: waUploadToServer,
|
upload: waUploadToServer,
|
||||||
mediaCache: config.mediaCache,
|
mediaCache: config.mediaCache,
|
||||||
|
options: config.options,
|
||||||
...options,
|
...options,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import { AxiosRequestConfig } from 'axios'
|
||||||
import type NodeCache from 'node-cache'
|
import type NodeCache from 'node-cache'
|
||||||
import type { Logger } from 'pino'
|
import type { Logger } from 'pino'
|
||||||
import type { Readable } from 'stream'
|
import type { Readable } from 'stream'
|
||||||
@@ -204,6 +205,8 @@ export type MediaGenerationOptions = {
|
|||||||
mediaCache?: NodeCache
|
mediaCache?: NodeCache
|
||||||
|
|
||||||
mediaUploadTimeoutMs?: number
|
mediaUploadTimeoutMs?: number
|
||||||
|
|
||||||
|
options?: AxiosRequestConfig
|
||||||
}
|
}
|
||||||
export type MessageContentGenerationOptions = MediaGenerationOptions & {
|
export type MessageContentGenerationOptions = MediaGenerationOptions & {
|
||||||
getUrlInfo?: (text: string) => Promise<WAUrlInfo | undefined>
|
getUrlInfo?: (text: string) => Promise<WAUrlInfo | undefined>
|
||||||
|
|||||||
@@ -66,7 +66,11 @@ export const getUrlInfo = async(
|
|||||||
if(opts.uploadImage) {
|
if(opts.uploadImage) {
|
||||||
const { imageMessage } = await prepareWAMessageMedia(
|
const { imageMessage } = await prepareWAMessageMedia(
|
||||||
{ image: { url: image } },
|
{ image: { url: image } },
|
||||||
{ upload: opts.uploadImage, mediaTypeOverride: 'thumbnail-link' }
|
{
|
||||||
|
upload: opts.uploadImage,
|
||||||
|
mediaTypeOverride: 'thumbnail-link',
|
||||||
|
options: opts.fetchOpts
|
||||||
|
}
|
||||||
)
|
)
|
||||||
urlInfo.jpegThumbnail = imageMessage?.jpegThumbnail
|
urlInfo.jpegThumbnail = imageMessage?.jpegThumbnail
|
||||||
? Buffer.from(imageMessage.jpegThumbnail)
|
? Buffer.from(imageMessage.jpegThumbnail)
|
||||||
|
|||||||
@@ -221,7 +221,7 @@ export const toBuffer = async(stream: Readable) => {
|
|||||||
return Buffer.concat(chunks)
|
return Buffer.concat(chunks)
|
||||||
}
|
}
|
||||||
|
|
||||||
export const getStream = async(item: WAMediaUpload) => {
|
export const getStream = async(item: WAMediaUpload, opts?: AxiosRequestConfig) => {
|
||||||
if(Buffer.isBuffer(item)) {
|
if(Buffer.isBuffer(item)) {
|
||||||
return { stream: toReadable(item), type: 'buffer' } as const
|
return { stream: toReadable(item), type: 'buffer' } as const
|
||||||
}
|
}
|
||||||
@@ -231,7 +231,7 @@ export const getStream = async(item: WAMediaUpload) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
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 getHttpStream(item.url), type: 'remote' } as const
|
return { stream: await getHttpStream(item.url, opts), type: 'remote' } as const
|
||||||
}
|
}
|
||||||
|
|
||||||
return { stream: createReadStream(item.url), type: 'file' } as const
|
return { stream: createReadStream(item.url), type: 'file' } as const
|
||||||
@@ -281,13 +281,18 @@ export const getHttpStream = async(url: string | URL, options: AxiosRequestConfi
|
|||||||
return fetched.data as Readable
|
return fetched.data as Readable
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type EncryptedStreamOptions = {
|
||||||
|
saveOriginalFileIfRequired?: boolean
|
||||||
|
logger?: Logger
|
||||||
|
opts?: AxiosRequestConfig
|
||||||
|
}
|
||||||
|
|
||||||
export const encryptedStream = async(
|
export const encryptedStream = async(
|
||||||
media: WAMediaUpload,
|
media: WAMediaUpload,
|
||||||
mediaType: MediaType,
|
mediaType: MediaType,
|
||||||
saveOriginalFileIfRequired = true,
|
{ logger, saveOriginalFileIfRequired, opts }: EncryptedStreamOptions = {}
|
||||||
logger?: Logger
|
|
||||||
) => {
|
) => {
|
||||||
const { stream, type } = await getStream(media)
|
const { stream, type } = await getStream(media, opts)
|
||||||
|
|
||||||
logger?.debug('fetched media stream')
|
logger?.debug('fetched media stream')
|
||||||
|
|
||||||
|
|||||||
@@ -149,7 +149,11 @@ export const prepareWAMessageMedia = async(
|
|||||||
} = await encryptedStream(
|
} = await encryptedStream(
|
||||||
uploadData.media,
|
uploadData.media,
|
||||||
options.mediaTypeOverride || mediaType,
|
options.mediaTypeOverride || mediaType,
|
||||||
requiresOriginalForSomeProcessing
|
{
|
||||||
|
logger,
|
||||||
|
saveOriginalFileIfRequired: requiresOriginalForSomeProcessing,
|
||||||
|
opts: options.options
|
||||||
|
}
|
||||||
)
|
)
|
||||||
// url safe Base64 encode the SHA256 hash of the body
|
// url safe Base64 encode the SHA256 hash of the body
|
||||||
const fileEncSha256B64 = fileEncSha256.toString('base64')
|
const fileEncSha256B64 = fileEncSha256.toString('base64')
|
||||||
|
|||||||
Reference in New Issue
Block a user