32 lines
812 B
TypeScript
32 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)
|
||
|
}
|