This commit is contained in:
Adhiraj
2020-09-23 18:39:32 +05:30
parent d86cde2805
commit f3ecbb11a0
2 changed files with 20 additions and 15 deletions

View File

@@ -360,11 +360,11 @@ export class WAConnection extends EventEmitter {
/** /**
* Does a fetch request with the configuration of the connection * Does a fetch request with the configuration of the connection
*/ */
protected fetchRequest = (endpoint: string, method: string = 'GET', body?: any, agent?: Agent) => ( protected fetchRequest = (endpoint: string, method: string = 'GET', body?: any, agent?: Agent, headers?: {[k: string]: string}) => (
fetch(endpoint, { fetch(endpoint, {
method, method,
body, body,
headers: { Origin: DEFAULT_ORIGIN }, headers: { Origin: DEFAULT_ORIGIN, ...(headers || {}) },
agent: agent || this.connectOptions.agent agent: agent || this.connectOptions.agent
}) })
) )

View File

@@ -102,13 +102,15 @@ export class WAConnection extends Base {
const mac = hmacSign(Buffer.concat([mediaKeys.iv, enc]), mediaKeys.macKey).slice(0, 10) const mac = hmacSign(Buffer.concat([mediaKeys.iv, enc]), mediaKeys.macKey).slice(0, 10)
const body = Buffer.concat([enc, mac]) // body is enc + mac const body = Buffer.concat([enc, mac]) // body is enc + mac
const fileSha256 = sha256(buffer) const fileSha256 = sha256(buffer)
// url safe Base64 encode the SHA256 hash of the body
const fileEncSha256 = sha256(body) const fileEncSha256 = sha256(body)
const fileEncSha256B64 = fileEncSha256 // url safe Base64 encode the SHA256 hash of the body
.toString('base64') const fileEncSha256B64 = encodeURIComponent(
.replace(/\+/g, '-') fileEncSha256
.replace(/\//g, '_') .toString('base64')
.replace(/\=+$/, '') .replace(/\+/g, '-')
.replace(/\//g, '_')
.replace(/\=+$/, '')
)
await generateThumbnail(buffer, mediaType, options) await generateThumbnail(buffer, mediaType, options)
@@ -117,16 +119,19 @@ export class WAConnection extends Base {
let mediaUrl: string let mediaUrl: string
for (let host of json.hosts) { for (let host of json.hosts) {
const auth = json.auth // the auth token const auth = encodeURIComponent(json.auth) // the auth token
const hostname = `https://${host.hostname}${MediaPathMap[mediaType]}/${fileEncSha256B64}?auth=${auth}&token=${fileEncSha256B64}` const url = `https://${host.hostname}${MediaPathMap[mediaType]}/${fileEncSha256B64}?auth=${auth}&token=${fileEncSha256B64}`
try { try {
const urlFetch = await this.fetchRequest(hostname, 'POST', body, options.uploadAgent) const urlFetch = await this.fetchRequest(url, 'POST', body, options.uploadAgent, { 'content-type': 'application/binary' })
mediaUrl = (await urlFetch.json())?.url const result = await urlFetch.json()
mediaUrl = result?.url
console.log (result)
if (mediaUrl) break if (mediaUrl) break
else { else {
json = await this.refreshMediaConn (true) json = await this.refreshMediaConn (true)
throw new Error (`upload failed`) throw new Error (`upload failed, reason: ${JSON.stringify(result)}`)
} }
} catch (error) { } catch (error) {
const isLast = host.hostname === json.hosts[json.hosts.length-1].hostname const isLast = host.hostname === json.hosts[json.hosts.length-1].hostname
@@ -279,7 +284,7 @@ export class WAConnection extends Base {
return this.mediaConn return this.mediaConn
} }
protected async getNewMediaConn () { protected async getNewMediaConn () {
const result = await this.query({json: ['query', 'mediaConn']}) const {media_conn} = await this.query({json: ['query', 'mediaConn']})
return result.media_conn return media_conn as MediaConnInfo
} }
} }