mtcute/packages/client/src/methods/files/download-buffer.ts
2021-04-08 12:19:38 +03:00

31 lines
812 B
TypeScript

import { FileDownloadParameters, FileLocation } from '../../types'
import { TelegramClient } from '../../client'
/**
* Download a file and return its contents as a Buffer.
*
* > **Note**: This method _will_ download the entire file
* > into memory at once. This might cause an issue, so use wisely!
*
* @param params File download parameters
* @internal
*/
export async function downloadAsBuffer(
this: TelegramClient,
params: FileDownloadParameters
): Promise<Buffer> {
if (
params.location instanceof FileLocation &&
params.location.location instanceof Buffer
) {
return params.location.location
}
const chunks = []
for await (const chunk of this.downloadAsIterable(params)) {
chunks.push(chunk)
}
return Buffer.concat(chunks)
}