fix: merge conflict errors

This commit is contained in:
Adhiraj Singh
2021-12-17 19:30:10 +05:30
parent be0e2210d9
commit d8b415a075

View File

@@ -4,13 +4,13 @@ import type { Options, Response } from 'got'
import { Boom } from '@hapi/boom' import { Boom } from '@hapi/boom'
import * as Crypto from 'crypto' import * as Crypto from 'crypto'
import { Readable, Transform } from 'stream' import { Readable, Transform } from 'stream'
import { createReadStream, createWriteStream, promises as fs, ReadStream, WriteStream } from 'fs' import { createReadStream, createWriteStream, promises as fs, WriteStream } from 'fs'
import { exec } from 'child_process' import { exec } from 'child_process'
import { tmpdir } from 'os' import { tmpdir } from 'os'
import { URL } from 'url' import { URL } from 'url'
import { join } from 'path' import { join } from 'path'
import { once } from 'events' import { once } from 'events'
import { MessageType, WAMessageContent, WAProto, WAGenericMediaMessage, WAMediaUpload, MediaType, DownloadableMessage } from '../Types' import { MessageType, WAMessageContent, WAProto, WAGenericMediaMessage, WAMediaUpload, MediaType, DownloadableMessage, CommonSocketConfig, WAMediaUploadFunction, MediaConnInfo } from '../Types'
import { generateMessageID } from './generics' import { generateMessageID } from './generics'
import { hkdf } from './crypto' import { hkdf } from './crypto'
import { DEFAULT_ORIGIN, MEDIA_PATH_MAP } from '../Defaults' import { DEFAULT_ORIGIN, MEDIA_PATH_MAP } from '../Defaults'
@@ -463,49 +463,54 @@ export function extensionForMediaMessage(message: WAMessageContent) {
export const getWAUploadToServer = ({ customUploadHosts, agent, logger }: CommonSocketConfig<any>, refreshMediaConn: (force: boolean) => Promise<MediaConnInfo>): WAMediaUploadFunction => { export const getWAUploadToServer = ({ customUploadHosts, agent, logger }: CommonSocketConfig<any>, refreshMediaConn: (force: boolean) => Promise<MediaConnInfo>): WAMediaUploadFunction => {
return async(stream, { mediaType, fileEncSha256B64, timeoutMs }) => { return async(stream, { mediaType, fileEncSha256B64, timeoutMs }) => {
const { default: got } = await import('got')
// 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
let uploadInfo = await refreshMediaConn(false) let uploadInfo = await refreshMediaConn(false)
let mediaUrl: string let urls: { mediaUrl: string, directPath: string }
const hosts = [ ...customUploadHosts, ...uploadInfo.hosts.map(h => h.hostname) ] const hosts = [ ...customUploadHosts, ...uploadInfo.hosts.map(h => h.hostname) ]
for (let hostname of hosts) { for (let hostname of hosts) {
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}`
try { try {
const {body: responseText} = await got.post( const {body: responseText} = await got.post(
url, url,
{ {
headers: { headers: {
'Content-Type': 'application/octet-stream', 'Content-Type': 'application/octet-stream',
'Origin': DEFAULT_ORIGIN 'Origin': DEFAULT_ORIGIN
}, },
agent: { agent: {
https: agent https: agent
}, },
body: stream, body: stream,
timeout: timeoutMs timeout: timeoutMs
}
)
const result = JSON.parse(responseText)
if(result?.url || result?.directPath) {
urls = {
mediaUrl: result.url,
directPath: result.direct_path
} }
) break
const result = JSON.parse(responseText) } else {
mediaUrl = result?.url uploadInfo = await refreshMediaConn(true)
throw new Error(`upload failed, reason: ${JSON.stringify(result)}`)
if (mediaUrl) break }
else { } catch (error) {
uploadInfo = await refreshMediaConn(true) const isLast = hostname === hosts[uploadInfo.hosts.length-1]
throw new Error(`upload failed, reason: ${JSON.stringify(result)}`) logger.debug(`Error in uploading to ${hostname} (${error}) ${isLast ? '' : ', retrying...'}`)
} }
} catch (error) { }
const isLast = hostname === hosts[uploadInfo.hosts.length-1] if (!urls) {
logger.debug(`Error in uploading to ${hostname} (${error}) ${isLast ? '' : ', retrying...'}`) throw new Boom(
} 'Media upload failed on all hosts',
} { statusCode: 500 }
if (!mediaUrl) { )
throw new Boom( }
'Media upload failed on all hosts', return urls
{ statusCode: 500 } }
)
}
return { mediaUrl }
}
} }