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
let uploadInfo = await refreshMediaConn(false)
let mediaUrl: string
let urls: { mediaUrl: string, directPath: string }
const hosts = [ ...config.customUploadHosts, ...uploadInfo.hosts.map(h => h.hostname) ]
for (let hostname of hosts) {
const auth = encodeURIComponent(uploadInfo.auth) // the auth token
@@ -446,10 +446,14 @@ export const makeMessagesSocket = (config: SocketConfig) => {
}
)
const result = JSON.parse(responseText)
mediaUrl = result?.url
if (mediaUrl) break
else {
if(result?.url || result?.directPath) {
urls = {
mediaUrl: result.url,
directPath: result.direct_path
}
break
} else {
uploadInfo = await refreshMediaConn(true)
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...'}`)
}
}
if (!mediaUrl) {
if (!urls) {
throw new Boom(
'Media upload failed on all hosts',
{ statusCode: 500 }
)
}
return { mediaUrl }
return urls
}
return {

View File

@@ -140,7 +140,7 @@ export type MessageGenerationOptionsFromContent = MiscMessageGenerationOptions &
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 = {
logger?: Logger

View File

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