fix: allow media upload retries

This commit is contained in:
canove
2025-05-06 08:06:05 -03:00
parent 8cc8b44724
commit f58a38fde9
4 changed files with 19 additions and 7 deletions

View File

@@ -1,7 +1,11 @@
import { Boom } from '@hapi/boom'
import { createHash } from 'crypto'
import { createWriteStream, promises as fs } from 'fs'
import { tmpdir } from 'os'
import { join } from 'path'
import { CatalogCollection, CatalogStatus, OrderDetails, OrderProduct, Product, ProductCreate, ProductUpdate, WAMediaUpload, WAMediaUploadFunction } from '../Types'
import { BinaryNode, getBinaryNodeChild, getBinaryNodeChildren, getBinaryNodeChildString } from '../WABinary'
import { generateMessageIDV2 } from './generics'
import { getStream, getUrlFromDirectPath, toReadable } from './messages-media'
export const parseCatalogNode = (node: BinaryNode) => {
@@ -235,22 +239,30 @@ export const uploadingNecessaryImages = async(
const { stream } = await getStream(img)
const hasher = createHash('sha256')
const contentBlocks: Buffer[] = []
const filePath = join(tmpdir(), 'img' + generateMessageIDV2())
const encFileWriteStream = createWriteStream(filePath)
for await (const block of stream) {
hasher.update(block)
contentBlocks.push(block)
encFileWriteStream.write(block)
}
const sha = hasher.digest('base64')
const { directPath } = await waUploadToServer(
toReadable(Buffer.concat(contentBlocks)),
filePath,
{
mediaType: 'product-catalog-image',
fileEncSha256B64: sha,
timeoutMs
}
)
await fs
.unlink(filePath)
.catch(err => console.log('Error deleting temp file ', err))
return { url: getUrlFromDirectPath(directPath) }
}
)