mirror of
https://github.com/FranP-code/Baileys.git
synced 2025-10-13 00:32:22 +00:00
fix: allow media upload retries
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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) }
|
||||
}
|
||||
)
|
||||
|
||||
@@ -606,7 +606,7 @@ export const getWAUploadToServer = (
|
||||
{ customUploadHosts, fetchAgent, logger, options }: SocketConfig,
|
||||
refreshMediaConn: (force: boolean) => Promise<MediaConnInfo>,
|
||||
): 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,
|
||||
|
||||
@@ -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')
|
||||
|
||||
Reference in New Issue
Block a user