From 765dcc1d1f58619d1ce03547172fcb3bd91e4301 Mon Sep 17 00:00:00 2001 From: Adhiraj Singh Date: Fri, 2 Sep 2022 20:09:54 +0530 Subject: [PATCH] fix: upload for product --- src/Defaults/index.ts | 1 + src/Types/Message.ts | 2 +- src/Utils/business.ts | 12 ++++++++++-- src/Utils/messages-media.ts | 10 ++++++++++ src/Utils/messages.ts | 9 ++------- 5 files changed, 24 insertions(+), 10 deletions(-) diff --git a/src/Defaults/index.ts b/src/Defaults/index.ts index b1ce4ca..2c33f56 100644 --- a/src/Defaults/index.ts +++ b/src/Defaults/index.ts @@ -67,6 +67,7 @@ export const MEDIA_PATH_MAP: { [T in MediaType]: string } = { audio: '/mms/audio', sticker: '/mms/image', history: '', + 'product-image': '/product/image', 'md-app-state': '' } diff --git a/src/Types/Message.ts b/src/Types/Message.ts index c4e474d..9cd6dac 100644 --- a/src/Types/Message.ts +++ b/src/Types/Message.ts @@ -75,7 +75,7 @@ type WithDimensions = { width?: number height?: number } -export type MediaType = 'image' | 'video' | 'sticker' | 'audio' | 'document' | 'history' | 'md-app-state' +export type MediaType = 'image' | 'video' | 'sticker' | 'audio' | 'document' | 'history' | 'md-app-state' | 'product-image' export type AnyMediaMessageContent = ( ({ image: WAMediaUpload diff --git a/src/Utils/business.ts b/src/Utils/business.ts index a79bfb1..a5a5fb8 100644 --- a/src/Utils/business.ts +++ b/src/Utils/business.ts @@ -210,7 +210,11 @@ export async function uploadingNecessaryImagesOfProduct { +export const uploadingNecessaryImages = async( + images: WAMediaUpload[], + waUploadToServer: WAMediaUploadFunction, + timeoutMs = 30_000 +) => { const results = await Promise.all( images.map>( async img => { @@ -234,7 +238,11 @@ export const uploadingNecessaryImages = async(images: WAMediaUpload[], waUploadT const { mediaUrl } = await waUploadToServer( toReadable(Buffer.concat(contentBlocks)), - { mediaType: 'image', fileEncSha256B64: sha, timeoutMs } + { + mediaType: 'product-image', + fileEncSha256B64: sha, + timeoutMs + } ) return { url: mediaUrl } } diff --git a/src/Utils/messages-media.ts b/src/Utils/messages-media.ts index fb370e0..9e25e36 100644 --- a/src/Utils/messages-media.ts +++ b/src/Utils/messages-media.ts @@ -121,6 +121,15 @@ export const extractImageThumb = async(bufferOrFilePath: Readable | Buffer | str } } +export const encodeBase64EncodedStringForUpload = (b64: string) => ( + encodeURIComponent( + b64 + .replace(/\+/g, '-') + .replace(/\//g, '_') + .replace(/\=+$/, '') + ) +) + export const generateProfilePicture = async(mediaUpload: WAMediaUpload) => { let bufferOrFilePath: Buffer | string if(Buffer.isBuffer(mediaUpload)) { @@ -501,6 +510,7 @@ export const getWAUploadToServer = ({ customUploadHosts, fetchAgent, logger }: C } const reqBody = Buffer.concat(chunks) + fileEncSha256B64 = encodeBase64EncodedStringForUpload(fileEncSha256B64) for(const { hostname, maxContentLengthBytes } of hosts) { logger.debug(`uploading to "${hostname}"`) diff --git a/src/Utils/messages.ts b/src/Utils/messages.ts index ce6e533..a7f93b3 100644 --- a/src/Utils/messages.ts +++ b/src/Utils/messages.ts @@ -44,6 +44,7 @@ const MIMETYPE_MAP: { [T in MediaType]: string } = { audio: 'audio/ogg; codecs=opus', sticker: 'image/webp', history: 'application/x-protobuf', + 'product-image': 'image/jpeg', 'md-app-state': 'application/x-protobuf', } @@ -146,13 +147,7 @@ export const prepareWAMessageMedia = async( didSaveToTmpPath } = await encryptedStream(uploadData.media, mediaType, requiresOriginalForSomeProcessing) // url safe Base64 encode the SHA256 hash of the body - const fileEncSha256B64 = encodeURIComponent( - fileEncSha256.toString('base64') - .replace(/\+/g, '-') - .replace(/\//g, '_') - .replace(/\=+$/, '') - ) - + const fileEncSha256B64 = fileEncSha256.toString('base64') const [{ mediaUrl, directPath }] = await Promise.all([ (async() => { const result = await options.upload(