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:
@@ -467,7 +467,7 @@ export const getWAUploadToServer = ({ customUploadHosts, fetchAgent, logger }: C
|
||||
let uploadInfo = await refreshMediaConn(false)
|
||||
|
||||
let urls: { mediaUrl: string, directPath: string }
|
||||
const hosts = [ ...customUploadHosts, ...uploadInfo.hosts.map(h => h.hostname) ]
|
||||
const hosts = [ ...customUploadHosts, ...uploadInfo.hosts ]
|
||||
|
||||
let chunks: Buffer[] = []
|
||||
for await(const chunk of stream) {
|
||||
@@ -476,11 +476,17 @@ export const getWAUploadToServer = ({ customUploadHosts, fetchAgent, logger }: C
|
||||
|
||||
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 url = `https://${hostname}${MEDIA_PATH_MAP[mediaType]}/${fileEncSha256B64}?auth=${auth}&token=${fileEncSha256B64}`
|
||||
let result: any
|
||||
try {
|
||||
if(maxContentLengthBytes && reqBody.length > maxContentLengthBytes) {
|
||||
throw new Boom(`Body too large for "${hostname}"`, { statusCode: 413 })
|
||||
}
|
||||
|
||||
const body = await axios.post(
|
||||
url,
|
||||
reqBody,
|
||||
@@ -509,8 +515,12 @@ export const getWAUploadToServer = ({ customUploadHosts, fetchAgent, logger }: C
|
||||
throw new Error(`upload failed, reason: ${JSON.stringify(result)}`)
|
||||
}
|
||||
} catch (error) {
|
||||
const isLast = hostname === hosts[uploadInfo.hosts.length-1]
|
||||
logger.debug({ trace: error.stack, uploadResult: result }, `Error in uploading to ${hostname} ${isLast ? '' : ', retrying...'}`)
|
||||
if(axios.isAxiosError(error)) {
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user