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',
sticker: '/mms/image',
history: '',
'product-image': '/product/image',
'md-app-state': ''
}

View File

@@ -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

View File

@@ -210,7 +210,11 @@ export async function uploadingNecessaryImagesOfProduct<T extends ProductUpdate
/**
* 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(
images.map<Promise<{ url: string }>>(
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 }
}

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) => {
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}"`)

View File

@@ -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(