feat: add "direct_path" to media messages

This commit is contained in:
Adhiraj Singh
2021-12-13 17:31:20 +05:30
parent ea077e9fea
commit d15dd6e1d2
3 changed files with 14 additions and 9 deletions

View File

@@ -424,7 +424,7 @@ export const makeMessagesSocket = (config: SocketConfig) => {
// 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 = [ ...config.customUploadHosts, ...uploadInfo.hosts.map(h => h.hostname) ] const hosts = [ ...config.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
@@ -446,10 +446,14 @@ export const makeMessagesSocket = (config: SocketConfig) => {
} }
) )
const result = JSON.parse(responseText) const result = JSON.parse(responseText)
mediaUrl = result?.url
if (mediaUrl) break if(result?.url || result?.directPath) {
else { urls = {
mediaUrl: result.url,
directPath: result.direct_path
}
break
} else {
uploadInfo = await refreshMediaConn(true) uploadInfo = await refreshMediaConn(true)
throw new Error(`upload failed, reason: ${JSON.stringify(result)}`) throw new Error(`upload failed, reason: ${JSON.stringify(result)}`)
} }
@@ -458,13 +462,13 @@ export const makeMessagesSocket = (config: SocketConfig) => {
logger.debug(`Error in uploading to ${hostname} (${error}) ${isLast ? '' : ', retrying...'}`) logger.debug(`Error in uploading to ${hostname} (${error}) ${isLast ? '' : ', retrying...'}`)
} }
} }
if (!mediaUrl) { if (!urls) {
throw new Boom( throw new Boom(
'Media upload failed on all hosts', 'Media upload failed on all hosts',
{ statusCode: 500 } { statusCode: 500 }
) )
} }
return { mediaUrl } return urls
} }
return { return {

View File

@@ -140,7 +140,7 @@ export type MessageGenerationOptionsFromContent = MiscMessageGenerationOptions &
userJid: string userJid: string
} }
export type WAMediaUploadFunction = (readStream: ReadStream, opts: { fileEncSha256B64: string, mediaType: MediaType, timeoutMs?: number }) => Promise<{ mediaUrl: string }> export type WAMediaUploadFunction = (readStream: ReadStream, opts: { fileEncSha256B64: string, mediaType: MediaType, timeoutMs?: number }) => Promise<{ mediaUrl: string, directPath: string }>
export type MediaGenerationOptions = { export type MediaGenerationOptions = {
logger?: Logger logger?: Logger

View File

@@ -118,7 +118,7 @@ export const prepareWAMessageMedia = async(
} catch (error) { } catch (error) {
options.logger?.info({ trace: error.stack }, 'failed to obtain extra info') options.logger?.info({ trace: error.stack }, 'failed to obtain extra info')
} }
const {mediaUrl} = await options.upload( const {mediaUrl, directPath} = await options.upload(
createReadStream(encBodyPath), createReadStream(encBodyPath),
{ fileEncSha256B64, mediaType, timeoutMs: options.mediaUploadTimeoutMs } { fileEncSha256B64, mediaType, timeoutMs: options.mediaUploadTimeoutMs }
) )
@@ -136,11 +136,12 @@ export const prepareWAMessageMedia = async(
[`${mediaType}Message`]: MessageTypeProto[mediaType].fromObject( [`${mediaType}Message`]: MessageTypeProto[mediaType].fromObject(
{ {
url: mediaUrl, url: mediaUrl,
directPath,
mediaKey, mediaKey,
fileEncSha256, fileEncSha256,
fileSha256, fileSha256,
fileLength, fileLength,
mediaKeyTimestamp: unixTimestampSeconds(),
...uploadData ...uploadData
} }
) )