diff --git a/src/Types/Message.ts b/src/Types/Message.ts index b2f6f5f..f0623b6 100644 --- a/src/Types/Message.ts +++ b/src/Types/Message.ts @@ -236,7 +236,7 @@ export type MessageGenerationOptionsFromContent = MiscMessageGenerationOptions & userJid: string } -export type WAMediaUploadFunction = (readStream: Readable, opts: { fileEncSha256B64: string, mediaType: MediaType, timeoutMs?: number }) => Promise<{ mediaUrl: string, directPath: string }> +export type WAMediaUploadFunction = (encFilePath: string, opts: { fileEncSha256B64: string, mediaType: MediaType, timeoutMs?: number }) => Promise<{ mediaUrl: string, directPath: string }> export type MediaGenerationOptions = { logger?: ILogger diff --git a/src/Utils/business.ts b/src/Utils/business.ts index 57459a5..5c0bb2b 100644 --- a/src/Utils/business.ts +++ b/src/Utils/business.ts @@ -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) } } ) diff --git a/src/Utils/messages-media.ts b/src/Utils/messages-media.ts index ea04080..7dd30cb 100644 --- a/src/Utils/messages-media.ts +++ b/src/Utils/messages-media.ts @@ -606,7 +606,7 @@ export const getWAUploadToServer = ( { customUploadHosts, fetchAgent, logger, options }: SocketConfig, refreshMediaConn: (force: boolean) => Promise, ): WAMediaUploadFunction => { - return async(stream, { mediaType, fileEncSha256B64, timeoutMs }) => { + return async(filePath, { mediaType, fileEncSha256B64, timeoutMs }) => { // send a query JSON to obtain the url & auth token to upload our media let uploadInfo = await refreshMediaConn(false) @@ -626,7 +626,7 @@ export const getWAUploadToServer = ( const body = await axios.post( url, - stream, + createReadStream(filePath), { ...options, maxRedirects: 0, diff --git a/src/Utils/messages.ts b/src/Utils/messages.ts index 510d3dc..f305350 100644 --- a/src/Utils/messages.ts +++ b/src/Utils/messages.ts @@ -177,7 +177,7 @@ export const prepareWAMessageMedia = async( const [{ mediaUrl, directPath }] = await Promise.all([ (async() => { const result = await options.upload( - createReadStream(encFilePath), + encFilePath, { fileEncSha256B64, mediaType, timeoutMs: options.mediaUploadTimeoutMs } ) logger?.debug({ mediaType, cacheableKey }, 'uploaded media')