mirror of
https://github.com/FranP-code/Baileys.git
synced 2025-10-13 00:32:22 +00:00
add media cache option
This commit is contained in:
@@ -135,6 +135,8 @@ export type WAMediaUploadFunction = (readStream: ReadStream, opts: { fileEncSha2
|
|||||||
export type MediaGenerationOptions = {
|
export type MediaGenerationOptions = {
|
||||||
logger?: Logger
|
logger?: Logger
|
||||||
upload: WAMediaUploadFunction
|
upload: WAMediaUploadFunction
|
||||||
|
/** cache media so it does not have to be uploaded again */
|
||||||
|
mediaCache?: (url: string) => Promise<WAGenericMediaMessage> | WAGenericMediaMessage
|
||||||
}
|
}
|
||||||
export type MessageContentGenerationOptions = MediaGenerationOptions & {
|
export type MessageContentGenerationOptions = MediaGenerationOptions & {
|
||||||
getUrlInfo?: (text: string) => Promise<WAUrlInfo>
|
getUrlInfo?: (text: string) => Promise<WAUrlInfo>
|
||||||
|
|||||||
@@ -63,6 +63,15 @@ export const prepareWAMessageMedia = async(
|
|||||||
[mediaType]: undefined,
|
[mediaType]: undefined,
|
||||||
media: message[mediaType]
|
media: message[mediaType]
|
||||||
}
|
}
|
||||||
|
// check for cache hit
|
||||||
|
if(typeof uploadData.media === 'object' && 'url' in uploadData.media) {
|
||||||
|
const result = !!options.mediaCache && await options.mediaCache!(uploadData.media.url?.toString())
|
||||||
|
if(result) {
|
||||||
|
return WAMessageProto.Message.fromObject({
|
||||||
|
[`${mediaType}Message`]: result
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
if(mediaType === 'document' && !uploadData.fileName) {
|
if(mediaType === 'document' && !uploadData.fileName) {
|
||||||
uploadData.fileName = 'file'
|
uploadData.fileName = 'file'
|
||||||
}
|
}
|
||||||
@@ -71,7 +80,7 @@ export const prepareWAMessageMedia = async(
|
|||||||
}
|
}
|
||||||
const requiresDurationComputation = mediaType === 'audio' && typeof uploadData.seconds === 'undefined'
|
const requiresDurationComputation = mediaType === 'audio' && typeof uploadData.seconds === 'undefined'
|
||||||
const requiresThumbnailComputation = (mediaType === 'image' || mediaType === 'video') &&
|
const requiresThumbnailComputation = (mediaType === 'image' || mediaType === 'video') &&
|
||||||
!('jpegThumbnail' in uploadData)
|
(typeof uploadData['jpegThumbnail'] === 'undefined')
|
||||||
const requiresOriginalForSomeProcessing = requiresDurationComputation || requiresThumbnailComputation
|
const requiresOriginalForSomeProcessing = requiresDurationComputation || requiresThumbnailComputation
|
||||||
const {
|
const {
|
||||||
mediaKey,
|
mediaKey,
|
||||||
|
|||||||
Reference in New Issue
Block a user