mirror of
https://github.com/FranP-code/Baileys.git
synced 2025-10-13 00:32:22 +00:00
feat: add option to specify limit for custom upload host
This commit is contained in:
@@ -28,7 +28,7 @@ export type DownloadableMessage = { mediaKey?: Uint8Array, directPath?: string,
|
|||||||
export type MediaConnInfo = {
|
export type MediaConnInfo = {
|
||||||
auth: string
|
auth: string
|
||||||
ttl: number
|
ttl: number
|
||||||
hosts: { hostname: string }[]
|
hosts: { hostname: string, maxContentLengthBytes: number }[]
|
||||||
fetchDate: Date
|
fetchDate: Date
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ import type { Agent } from "https"
|
|||||||
import type { Logger } from "pino"
|
import type { Logger } from "pino"
|
||||||
import type { URL } from "url"
|
import type { URL } from "url"
|
||||||
import type NodeCache from 'node-cache'
|
import type NodeCache from 'node-cache'
|
||||||
|
import { MediaConnInfo } from "./Message"
|
||||||
|
|
||||||
export type WAVersion = [number, number, number]
|
export type WAVersion = [number, number, number]
|
||||||
export type WABrowserDescription = [string, string, string]
|
export type WABrowserDescription = [string, string, string]
|
||||||
@@ -35,5 +36,5 @@ export type CommonSocketConfig<T> = {
|
|||||||
/** provide a cache to store media, so does not have to be re-uploaded */
|
/** provide a cache to store media, so does not have to be re-uploaded */
|
||||||
mediaCache?: NodeCache
|
mediaCache?: NodeCache
|
||||||
|
|
||||||
customUploadHosts: string[]
|
customUploadHosts: MediaConnInfo['hosts']
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,8 +19,6 @@ export type SocketConfig = CommonSocketConfig<AuthenticationState> & {
|
|||||||
userDevicesCache?: NodeCache
|
userDevicesCache?: NodeCache
|
||||||
/** map to store the retry counts for failed messages */
|
/** map to store the retry counts for failed messages */
|
||||||
msgRetryCounterMap?: { [msgId: string]: number }
|
msgRetryCounterMap?: { [msgId: string]: number }
|
||||||
/** custom domains to push media via */
|
|
||||||
customUploadHosts: string[]
|
|
||||||
/**
|
/**
|
||||||
* fetch a message from your store
|
* fetch a message from your store
|
||||||
* implement this so that messages failed to send (solves the "this message can take a while" issue) can be retried
|
* implement this so that messages failed to send (solves the "this message can take a while" issue) can be retried
|
||||||
|
|||||||
@@ -467,7 +467,7 @@ export const getWAUploadToServer = ({ customUploadHosts, fetchAgent, logger }: C
|
|||||||
let uploadInfo = await refreshMediaConn(false)
|
let uploadInfo = await refreshMediaConn(false)
|
||||||
|
|
||||||
let urls: { mediaUrl: string, directPath: string }
|
let urls: { mediaUrl: string, directPath: string }
|
||||||
const hosts = [ ...customUploadHosts, ...uploadInfo.hosts.map(h => h.hostname) ]
|
const hosts = [ ...customUploadHosts, ...uploadInfo.hosts ]
|
||||||
|
|
||||||
let chunks: Buffer[] = []
|
let chunks: Buffer[] = []
|
||||||
for await(const chunk of stream) {
|
for await(const chunk of stream) {
|
||||||
@@ -476,11 +476,17 @@ export const getWAUploadToServer = ({ customUploadHosts, fetchAgent, logger }: C
|
|||||||
|
|
||||||
let reqBody = Buffer.concat(chunks)
|
let reqBody = Buffer.concat(chunks)
|
||||||
|
|
||||||
for (let hostname of hosts) {
|
for (let { hostname, maxContentLengthBytes } of hosts) {
|
||||||
|
logger.debug(`uploading to "${hostname}"`)
|
||||||
|
|
||||||
const auth = encodeURIComponent(uploadInfo.auth) // the auth token
|
const auth = encodeURIComponent(uploadInfo.auth) // the auth token
|
||||||
const url = `https://${hostname}${MEDIA_PATH_MAP[mediaType]}/${fileEncSha256B64}?auth=${auth}&token=${fileEncSha256B64}`
|
const url = `https://${hostname}${MEDIA_PATH_MAP[mediaType]}/${fileEncSha256B64}?auth=${auth}&token=${fileEncSha256B64}`
|
||||||
let result: any
|
let result: any
|
||||||
try {
|
try {
|
||||||
|
if(maxContentLengthBytes && reqBody.length > maxContentLengthBytes) {
|
||||||
|
throw new Boom(`Body too large for "${hostname}"`, { statusCode: 413 })
|
||||||
|
}
|
||||||
|
|
||||||
const body = await axios.post(
|
const body = await axios.post(
|
||||||
url,
|
url,
|
||||||
reqBody,
|
reqBody,
|
||||||
@@ -509,8 +515,12 @@ export const getWAUploadToServer = ({ customUploadHosts, fetchAgent, logger }: C
|
|||||||
throw new Error(`upload failed, reason: ${JSON.stringify(result)}`)
|
throw new Error(`upload failed, reason: ${JSON.stringify(result)}`)
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
const isLast = hostname === hosts[uploadInfo.hosts.length-1]
|
if(axios.isAxiosError(error)) {
|
||||||
logger.debug({ trace: error.stack, uploadResult: result }, `Error in uploading to ${hostname} ${isLast ? '' : ', retrying...'}`)
|
result = error.response?.data
|
||||||
|
}
|
||||||
|
|
||||||
|
const isLast = hostname === hosts[uploadInfo.hosts.length-1]?.hostname
|
||||||
|
logger.warn({ trace: error.stack, uploadResult: result }, `Error in uploading to ${hostname} ${isLast ? '' : ', retrying...'}`)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// clear buffer just to be sure we're releasing the memory
|
// clear buffer just to be sure we're releasing the memory
|
||||||
|
|||||||
Reference in New Issue
Block a user