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 = {
|
||||
logger?: Logger
|
||||
upload: WAMediaUploadFunction
|
||||
/** cache media so it does not have to be uploaded again */
|
||||
mediaCache?: (url: string) => Promise<WAGenericMediaMessage> | WAGenericMediaMessage
|
||||
}
|
||||
export type MessageContentGenerationOptions = MediaGenerationOptions & {
|
||||
getUrlInfo?: (text: string) => Promise<WAUrlInfo>
|
||||
|
||||
@@ -63,6 +63,15 @@ export const prepareWAMessageMedia = async(
|
||||
[mediaType]: undefined,
|
||||
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) {
|
||||
uploadData.fileName = 'file'
|
||||
}
|
||||
@@ -71,7 +80,7 @@ export const prepareWAMessageMedia = async(
|
||||
}
|
||||
const requiresDurationComputation = mediaType === 'audio' && typeof uploadData.seconds === 'undefined'
|
||||
const requiresThumbnailComputation = (mediaType === 'image' || mediaType === 'video') &&
|
||||
!('jpegThumbnail' in uploadData)
|
||||
(typeof uploadData['jpegThumbnail'] === 'undefined')
|
||||
const requiresOriginalForSomeProcessing = requiresDurationComputation || requiresThumbnailComputation
|
||||
const {
|
||||
mediaKey,
|
||||
|
||||
Reference in New Issue
Block a user