mirror of
https://github.com/FranP-code/Baileys.git
synced 2025-10-13 00:32:22 +00:00
try on all hosts for media
This commit is contained in:
@@ -7,7 +7,6 @@ export class WAConnection extends Base {
|
|||||||
|
|
||||||
constructor () {
|
constructor () {
|
||||||
super ()
|
super ()
|
||||||
|
|
||||||
// new messages
|
// new messages
|
||||||
this.registerCallback(['action', 'add:relay', 'message'], json => {
|
this.registerCallback(['action', 'add:relay', 'message'], json => {
|
||||||
const message = json[2][0][2] as WAMessage
|
const message = json[2][0][2] as WAMessage
|
||||||
|
|||||||
@@ -109,20 +109,30 @@ export class WAConnection extends Base {
|
|||||||
// send a query JSON to obtain the url & auth token to upload our media
|
// send a query JSON to obtain the url & auth token to upload our media
|
||||||
const json = await this.refreshMediaConn ()
|
const json = await this.refreshMediaConn ()
|
||||||
const auth = json.auth // the auth token
|
const auth = json.auth // the auth token
|
||||||
const hostname = `https://${json.hosts[0].hostname}${MediaPathMap[mediaType]}/${fileEncSha256B64}?auth=${auth}&token=${fileEncSha256B64}`
|
|
||||||
|
let mediaUrl: string
|
||||||
const urlFetch = await fetch(hostname, {
|
for (let host of json.hosts) {
|
||||||
method: 'POST',
|
const hostname = `https://${host.hostname}${MediaPathMap[mediaType]}/${fileEncSha256B64}?auth=${auth}&token=${fileEncSha256B64}`
|
||||||
body: body,
|
try {
|
||||||
headers: { Origin: 'https://web.whatsapp.com' },
|
const urlFetch = await fetch(hostname, {
|
||||||
})
|
method: 'POST',
|
||||||
const responseJSON = await urlFetch.json()
|
body: body,
|
||||||
if (!responseJSON.url) {
|
headers: { Origin: 'https://web.whatsapp.com' },
|
||||||
throw new Error('Upload failed got: ' + JSON.stringify(responseJSON))
|
})
|
||||||
|
mediaUrl = (await urlFetch.json())?.url
|
||||||
|
|
||||||
|
if (mediaUrl) break
|
||||||
|
else throw new Error (`upload failed`)
|
||||||
|
} catch (error) {
|
||||||
|
const isLast = host.hostname === json.hosts[json.hosts.length-1].hostname
|
||||||
|
this.log (`Error in uploading to ${host}${isLast ? '' : ', retrying...'}`, MessageLogLevel.info)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
if (!mediaUrl) throw new Error('Media upload failed on all hosts')
|
||||||
|
|
||||||
const message = {}
|
const message = {}
|
||||||
message[mediaType] = {
|
message[mediaType] = {
|
||||||
url: responseJSON.url,
|
url: mediaUrl,
|
||||||
mediaKey: mediaKey.toString('base64'),
|
mediaKey: mediaKey.toString('base64'),
|
||||||
mimetype: options.mimetype,
|
mimetype: options.mimetype,
|
||||||
fileEncSha256: fileEncSha256B64,
|
fileEncSha256: fileEncSha256B64,
|
||||||
|
|||||||
Reference in New Issue
Block a user