perf: experimental do not use fs for enc stream

This commit is contained in:
Adhiraj Singh
2021-12-17 18:27:03 +05:30
parent e51bbc4893
commit 38a44be006
3 changed files with 78 additions and 53 deletions

View File

@@ -94,7 +94,7 @@ export const prepareWAMessageMedia = async(
const requiresOriginalForSomeProcessing = requiresDurationComputation || requiresThumbnailComputation
const {
mediaKey,
encBodyPath,
encWriteStream,
bodyPath,
fileEncSha256,
fileSha256,
@@ -108,28 +108,35 @@ export const prepareWAMessageMedia = async(
.replace(/\//g, '_')
.replace(/\=+$/, '')
)
try {
if(requiresThumbnailComputation) {
uploadData.jpegThumbnail = await generateThumbnail(bodyPath, mediaType as any, options)
const [{ mediaUrl, directPath }] = await Promise.all([
(() => {
return options.upload(
encWriteStream,
{ fileEncSha256B64, mediaType, timeoutMs: options.mediaUploadTimeoutMs }
)
})(),
(async() => {
try {
if(requiresThumbnailComputation) {
uploadData.jpegThumbnail = await generateThumbnail(bodyPath, mediaType as any, options)
}
if (requiresDurationComputation) {
uploadData.seconds = await getAudioDuration(bodyPath)
}
} catch (error) {
options.logger?.info({ trace: error.stack }, 'failed to obtain extra info')
}
})(),
])
.finally(
async() => {
encWriteStream.destroy()
// remove tmp files
didSaveToTmpPath && bodyPath && await fs.unlink(bodyPath)
}
if (requiresDurationComputation) {
uploadData.seconds = await getAudioDuration(bodyPath)
}
} catch (error) {
options.logger?.info({ trace: error.stack }, 'failed to obtain extra info')
}
const {mediaUrl, directPath} = await options.upload(
createReadStream(encBodyPath),
{ fileEncSha256B64, mediaType, timeoutMs: options.mediaUploadTimeoutMs }
)
// remove tmp files
await Promise.all(
[
fs.unlink(encBodyPath),
didSaveToTmpPath && bodyPath && fs.unlink(bodyPath)
]
.filter(Boolean)
)
delete uploadData.media
const obj = WAProto.Message.fromObject({