feat: add more logging to media

This commit is contained in:
Adhiraj Singh
2021-12-19 14:35:51 +05:30
parent 2696af4da1
commit 9d7aec11da
4 changed files with 34 additions and 15 deletions

View File

@@ -517,12 +517,12 @@ const makeMessagesSocket = (config: LegacySocketConfig) => {
jid, jid,
content, content,
{ {
...options,
logger, logger,
userJid: userJid, userJid: userJid,
getUrlInfo: generateUrlInfo, getUrlInfo: generateUrlInfo,
upload: waUploadToServer, upload: waUploadToServer,
mediaCache: config.mediaCache mediaCache: config.mediaCache,
...options,
} }
) )

View File

@@ -451,13 +451,13 @@ export const makeMessagesSocket = (config: SocketConfig) => {
jid, jid,
content, content,
{ {
...options,
logger, logger,
userJid, userJid,
// multi-device does not have this yet // multi-device does not have this yet
//getUrlInfo: generateUrlInfo, //getUrlInfo: generateUrlInfo,
upload: waUploadToServer, upload: waUploadToServer,
mediaCache: config.mediaCache, mediaCache: config.mediaCache,
...options,
} }
) )
const isDeleteMsg = 'delete' in content && !!content.delete const isDeleteMsg = 'delete' in content && !!content.delete

View File

@@ -222,10 +222,13 @@ export const getGotStream = async(url: string | URL, options: Options & { isStre
export const encryptedStream = async( export const encryptedStream = async(
media: WAMediaUpload, media: WAMediaUpload,
mediaType: MediaType, mediaType: MediaType,
saveOriginalFileIfRequired = true saveOriginalFileIfRequired = true,
logger?: Logger
) => { ) => {
const { stream, type } = await getStream(media) const { stream, type } = await getStream(media)
logger?.debug('fetched media stream')
const mediaKey = Crypto.randomBytes(32) const mediaKey = Crypto.randomBytes(32)
const {cipherKey, iv, macKey} = getMediaKeys(mediaKey, mediaType) const {cipherKey, iv, macKey} = getMediaKeys(mediaKey, mediaType)
// random name // random name
@@ -277,6 +280,8 @@ export const encryptedStream = async(
encWriteStream.push(null) encWriteStream.push(null)
writeStream && writeStream.end() writeStream && writeStream.end()
logger?.debug('encrypted data successfully')
return { return {
mediaKey, mediaKey,

View File

@@ -1,5 +1,5 @@
import { Boom } from '@hapi/boom' import { Boom } from '@hapi/boom'
import { createReadStream, promises as fs } from "fs" import { promises as fs } from "fs"
import { proto } from '../../WAProto' import { proto } from '../../WAProto'
import { MEDIA_KEYS, URL_REGEX, WA_DEFAULT_EPHEMERAL } from "../Defaults" import { MEDIA_KEYS, URL_REGEX, WA_DEFAULT_EPHEMERAL } from "../Defaults"
import { import {
@@ -56,6 +56,8 @@ export const prepareWAMessageMedia = async(
message: AnyMediaMessageContent, message: AnyMediaMessageContent,
options: MediaGenerationOptions options: MediaGenerationOptions
) => { ) => {
const logger = options.logger
let mediaType: typeof MEDIA_KEYS[number] let mediaType: typeof MEDIA_KEYS[number]
for(const key of MEDIA_KEYS) { for(const key of MEDIA_KEYS) {
if(key in message) { if(key in message) {
@@ -79,9 +81,11 @@ export const prepareWAMessageMedia = async(
if(cacheableKey) { if(cacheableKey) {
const mediaBuff: Buffer = options.mediaCache!.get(cacheableKey) const mediaBuff: Buffer = options.mediaCache!.get(cacheableKey)
if(mediaBuff) { if(mediaBuff) {
logger?.debug({ cacheableKey }, `got media cache hit`)
return WAProto.Message.decode(mediaBuff) return WAProto.Message.decode(mediaBuff)
} }
} }
if(mediaType === 'document' && !uploadData.fileName) { if(mediaType === 'document' && !uploadData.fileName) {
uploadData.fileName = 'file' uploadData.fileName = 'file'
} }
@@ -110,22 +114,26 @@ export const prepareWAMessageMedia = async(
) )
const [{ mediaUrl, directPath }] = await Promise.all([ const [{ mediaUrl, directPath }] = await Promise.all([
(() => { (async() => {
return options.upload( const result = await options.upload(
encWriteStream, encWriteStream,
{ fileEncSha256B64, mediaType, timeoutMs: options.mediaUploadTimeoutMs } { fileEncSha256B64, mediaType, timeoutMs: options.mediaUploadTimeoutMs }
) )
logger?.debug(`uploaded media`)
return result
})(), })(),
(async() => { (async() => {
try { try {
if(requiresThumbnailComputation) { if(requiresThumbnailComputation) {
uploadData.jpegThumbnail = await generateThumbnail(bodyPath, mediaType as any, options) uploadData.jpegThumbnail = await generateThumbnail(bodyPath, mediaType as any, options)
logger?.debug(`generated thumbnail`)
} }
if (requiresDurationComputation) { if (requiresDurationComputation) {
uploadData.seconds = await getAudioDuration(bodyPath) uploadData.seconds = await getAudioDuration(bodyPath)
logger?.debug(`computed audio duration`)
} }
} catch (error) { } catch (error) {
options.logger?.info({ trace: error.stack }, 'failed to obtain extra info') logger?.warn({ trace: error.stack }, 'failed to obtain extra info')
} }
})(), })(),
]) ])
@@ -133,7 +141,10 @@ export const prepareWAMessageMedia = async(
async() => { async() => {
encWriteStream.destroy() encWriteStream.destroy()
// remove tmp files // remove tmp files
didSaveToTmpPath && bodyPath && await fs.unlink(bodyPath) if(didSaveToTmpPath && bodyPath) {
await fs.unlink(bodyPath)
logger?.debug('removed tmp files')
}
} }
) )
@@ -155,6 +166,7 @@ export const prepareWAMessageMedia = async(
}) })
if(cacheableKey) { if(cacheableKey) {
logger.debug({ cacheableKey }, `set cache`)
options.mediaCache!.set(cacheableKey, WAProto.Message.encode(obj).finish()) options.mediaCache!.set(cacheableKey, WAProto.Message.encode(obj).finish())
} }
@@ -278,14 +290,14 @@ export const generateWAMessageContent = async(
} }
m = { buttonsMessage } m = { buttonsMessage }
} else if ('templateButtons' in message && !!message.templateButtons) { } else if('templateButtons' in message && !!message.templateButtons) {
const templateMessage: proto.ITemplateMessage = { const templateMessage: proto.ITemplateMessage = {
hydratedTemplate: { hydratedTemplate: {
hydratedButtons: message.templateButtons hydratedButtons: message.templateButtons
} }
} }
if ('text' in message) { if('text' in message) {
templateMessage.hydratedTemplate.hydratedContentText = message.text templateMessage.hydratedTemplate.hydratedContentText = message.text
} else { } else {
@@ -296,7 +308,7 @@ export const generateWAMessageContent = async(
Object.assign(templateMessage.hydratedTemplate, m) Object.assign(templateMessage.hydratedTemplate, m)
} }
if ('footer' in message && !!message.footer) { if('footer' in message && !!message.footer) {
templateMessage.hydratedTemplate.hydratedFooterText = message.footer templateMessage.hydratedTemplate.hydratedFooterText = message.footer
} }
@@ -377,8 +389,10 @@ export const generateWAMessage = async(
jid: string, jid: string,
content: AnyMessageContent, content: AnyMessageContent,
options: MessageGenerationOptions, options: MessageGenerationOptions,
) => ( ) => {
generateWAMessageFromContent( // ensure msg ID is with every log
options.logger = options?.logger?.child({ msgId: options.messageId })
return generateWAMessageFromContent(
jid, jid,
await generateWAMessageContent( await generateWAMessageContent(
content, content,
@@ -386,7 +400,7 @@ export const generateWAMessage = async(
), ),
options options
) )
) }
/** /**
* Extract the true message content from a message * Extract the true message content from a message