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 () {
|
||||
super ()
|
||||
|
||||
// new messages
|
||||
this.registerCallback(['action', 'add:relay', 'message'], json => {
|
||||
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
|
||||
const json = await this.refreshMediaConn ()
|
||||
const auth = json.auth // the auth token
|
||||
const hostname = `https://${json.hosts[0].hostname}${MediaPathMap[mediaType]}/${fileEncSha256B64}?auth=${auth}&token=${fileEncSha256B64}`
|
||||
|
||||
const urlFetch = await fetch(hostname, {
|
||||
method: 'POST',
|
||||
body: body,
|
||||
headers: { Origin: 'https://web.whatsapp.com' },
|
||||
})
|
||||
const responseJSON = await urlFetch.json()
|
||||
if (!responseJSON.url) {
|
||||
throw new Error('Upload failed got: ' + JSON.stringify(responseJSON))
|
||||
|
||||
let mediaUrl: string
|
||||
for (let host of json.hosts) {
|
||||
const hostname = `https://${host.hostname}${MediaPathMap[mediaType]}/${fileEncSha256B64}?auth=${auth}&token=${fileEncSha256B64}`
|
||||
try {
|
||||
const urlFetch = await fetch(hostname, {
|
||||
method: 'POST',
|
||||
body: body,
|
||||
headers: { Origin: 'https://web.whatsapp.com' },
|
||||
})
|
||||
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 = {}
|
||||
message[mediaType] = {
|
||||
url: responseJSON.url,
|
||||
url: mediaUrl,
|
||||
mediaKey: mediaKey.toString('base64'),
|
||||
mimetype: options.mimetype,
|
||||
fileEncSha256: fileEncSha256B64,
|
||||
|
||||
Reference in New Issue
Block a user