Bladeren bron

Export iterDownload and add documentation

Painor 3 jaren geleden
bovenliggende
commit
8cbab35c70
2 gewijzigde bestanden met toevoegingen van 33 en 4 verwijderingen
  1. 30 1
      gramjs/client/TelegramClient.ts
  2. 3 3
      gramjs/client/downloads.ts

+ 30 - 1
gramjs/client/TelegramClient.ts

@@ -20,7 +20,7 @@ import { MTProtoSender } from "../network";
 
 import { LAYER } from "../tl/AllTLObjects";
 import { betterConsoleLog } from "../Helpers";
-import { DownloadMediaInterface } from "./downloads";
+import { DownloadMediaInterface, IterDownloadFunction } from "./downloads";
 import { NewMessage, NewMessageEvent } from "../events";
 import { _handleUpdate, _updateLoop } from "./updates";
 import { Session } from "../sessions";
@@ -428,6 +428,35 @@ export class TelegramClient extends TelegramBaseClient {
         return downloadMethods.downloadFileV2(this, inputLocation, fileParams);
     }
 
+    /**
+     * Iterates over a file download, yielding chunks of the file.
+     * This method can be used to stream files in a more convenient way, since it offers more control (pausing, resuming, etc.)
+     * @param iterFileParams - {@link IterDownloadFunction}
+     * @return a Buffer downloaded from the inputFile.
+     * @example
+     * ```ts
+     * const photo = message.photo;
+     * for await (const chunk of client.iterDownload({
+     *       file: new Api.InputPhotoFileLocation({
+     *          id: photo.id,
+     *          accessHash: photo.accessHash,
+     *          fileReference: photo.fileReference,
+     *          thumbSize: size.type
+     *      }),
+     *      offset: start,
+     *      limit: end,
+     *      requestSize:2048*1024
+     * )){
+     *     console.log("Downloaded chunk of size",chunk.length);
+     * };
+     * ```
+     */
+    iterDownload(
+        iterFileParams: downloadMethods.IterDownloadFunction
+    ) {
+        return downloadMethods.iterDownload(this, iterFileParams);
+    }
+
     //region download
 
     /**

+ 3 - 3
gramjs/client/downloads.ts

@@ -128,7 +128,7 @@ export interface IterDownloadFunction {
     msgData?: [EntityLike, number];
 }
 
-class DirectDownloadIter extends RequestIter {
+export class DirectDownloadIter extends RequestIter {
     protected request?: Api.upload.GetFile;
     private _sender?: MTProtoSender;
     private _timedOut: boolean = false;
@@ -226,7 +226,7 @@ class DirectDownloadIter extends RequestIter {
     }
 }
 
-class GenericDownloadIter extends DirectDownloadIter {
+export class GenericDownloadIter extends DirectDownloadIter {
     async _loadNextChunk(): Promise<boolean | undefined> {
         // 1. Fetch enough for one chunk
         let data = Buffer.alloc(0);
@@ -282,7 +282,7 @@ class GenericDownloadIter extends DirectDownloadIter {
 }
 
 /** @hidden */
-function iterDownload(
+export function iterDownload(
     client: TelegramClient,
     {
         file,