fix: upload for product

This commit is contained in:
Adhiraj Singh
2022-09-02 20:09:54 +05:30
parent 9e581f59cb
commit 765dcc1d1f
5 changed files with 24 additions and 10 deletions

View File

@@ -67,6 +67,7 @@ export const MEDIA_PATH_MAP: { [T in MediaType]: string } = {
audio: '/mms/audio', audio: '/mms/audio',
sticker: '/mms/image', sticker: '/mms/image',
history: '', history: '',
'product-image': '/product/image',
'md-app-state': '' 'md-app-state': ''
} }

View File

@@ -75,7 +75,7 @@ type WithDimensions = {
width?: number width?: number
height?: 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 = ( export type AnyMediaMessageContent = (
({ ({
image: WAMediaUpload image: WAMediaUpload

View File

@@ -210,7 +210,11 @@ export async function uploadingNecessaryImagesOfProduct<T extends ProductUpdate
/** /**
* Uploads images not already uploaded to WA's servers * Uploads images not already uploaded to WA's servers
*/ */
export const uploadingNecessaryImages = async(images: WAMediaUpload[], waUploadToServer: WAMediaUploadFunction, timeoutMs = 30_000) => { export const uploadingNecessaryImages = async(
images: WAMediaUpload[],
waUploadToServer: WAMediaUploadFunction,
timeoutMs = 30_000
) => {
const results = await Promise.all( const results = await Promise.all(
images.map<Promise<{ url: string }>>( images.map<Promise<{ url: string }>>(
async img => { async img => {
@@ -234,7 +238,11 @@ export const uploadingNecessaryImages = async(images: WAMediaUpload[], waUploadT
const { mediaUrl } = await waUploadToServer( const { mediaUrl } = await waUploadToServer(
toReadable(Buffer.concat(contentBlocks)), toReadable(Buffer.concat(contentBlocks)),
{ mediaType: 'image', fileEncSha256B64: sha, timeoutMs } {
mediaType: 'product-image',
fileEncSha256B64: sha,
timeoutMs
}
) )
return { url: mediaUrl } return { url: mediaUrl }
} }

View File

@@ -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) => { export const generateProfilePicture = async(mediaUpload: WAMediaUpload) => {
let bufferOrFilePath: Buffer | string let bufferOrFilePath: Buffer | string
if(Buffer.isBuffer(mediaUpload)) { if(Buffer.isBuffer(mediaUpload)) {
@@ -501,6 +510,7 @@ export const getWAUploadToServer = ({ customUploadHosts, fetchAgent, logger }: C
} }
const reqBody = Buffer.concat(chunks) const reqBody = Buffer.concat(chunks)
fileEncSha256B64 = encodeBase64EncodedStringForUpload(fileEncSha256B64)
for(const { hostname, maxContentLengthBytes } of hosts) { for(const { hostname, maxContentLengthBytes } of hosts) {
logger.debug(`uploading to "${hostname}"`) logger.debug(`uploading to "${hostname}"`)

View File

@@ -44,6 +44,7 @@ const MIMETYPE_MAP: { [T in MediaType]: string } = {
audio: 'audio/ogg; codecs=opus', audio: 'audio/ogg; codecs=opus',
sticker: 'image/webp', sticker: 'image/webp',
history: 'application/x-protobuf', history: 'application/x-protobuf',
'product-image': 'image/jpeg',
'md-app-state': 'application/x-protobuf', 'md-app-state': 'application/x-protobuf',
} }
@@ -146,13 +147,7 @@ export const prepareWAMessageMedia = async(
didSaveToTmpPath didSaveToTmpPath
} = await encryptedStream(uploadData.media, mediaType, requiresOriginalForSomeProcessing) } = await encryptedStream(uploadData.media, mediaType, requiresOriginalForSomeProcessing)
// url safe Base64 encode the SHA256 hash of the body // url safe Base64 encode the SHA256 hash of the body
const fileEncSha256B64 = encodeURIComponent( const fileEncSha256B64 = fileEncSha256.toString('base64')
fileEncSha256.toString('base64')
.replace(/\+/g, '-')
.replace(/\//g, '_')
.replace(/\=+$/, '')
)
const [{ mediaUrl, directPath }] = await Promise.all([ const [{ mediaUrl, directPath }] = await Promise.all([
(async() => { (async() => {
const result = await options.upload( const result = await options.upload(