1
0
Эх сурвалжийг харах

Add friendly method for downloading profile picture

painor 4 жил өмнө
parent
commit
4ab78304ae

+ 14 - 0
gramjs/client/TelegramClient.ts

@@ -131,6 +131,20 @@ export class TelegramClient extends TelegramBaseClient {
         return downloadMethods.downloadFile(this, inputLocation, fileParams);
     }
 
+    //region download
+    downloadProfilePhoto(
+        entity: EntityLike,
+        downloadProfilePhotoParams: downloadMethods.DownloadProfilePhotoParams = {
+            isBig: false,
+        }
+    ) {
+        return downloadMethods.downloadProfilePhoto(
+            this,
+            entity,
+            downloadProfilePhotoParams
+        );
+    }
+
     _downloadPhoto(
         photo: Api.MessageMediaPhoto | Api.Photo,
         args: DownloadMediaInterface

+ 43 - 2
gramjs/client/downloads.ts

@@ -4,6 +4,8 @@ import { getAppropriatedPartSize, strippedPhotoToJpg } from "../Utils";
 import { sleep } from "../Helpers";
 import { MTProtoSender } from "../network";
 import type { Message } from "../tl/custom/message";
+import { EntityLike } from "../define";
+import { utils } from "../";
 
 export interface progressCallback {
     (
@@ -25,6 +27,10 @@ export interface DownloadFileParams {
     progressCallback?: progressCallback;
 }
 
+export interface DownloadProfilePhotoParams {
+    isBig?: boolean;
+}
+
 interface Deferred {
     promise: Promise<any>;
     resolve: (value?: any) => void;
@@ -55,10 +61,8 @@ export async function downloadFile(
             : DEFAULT_CHUNK_SIZE;
     }
 
-    // @ts-ignore
     const partSize = partSizeKb * 1024;
     const partsCount = end ? Math.ceil((end - start) / partSize) : 1;
-
     if (partSize % MIN_CHUNK_SIZE !== 0) {
         throw new Error(
             `The part size must be evenly divisible by ${MIN_CHUNK_SIZE}`
@@ -398,3 +402,40 @@ export async function _downloadPhoto(
         }
     );
 }
+
+export async function downloadProfilePhoto(
+    client: TelegramClient,
+    entity: EntityLike,
+    fileParams: DownloadProfilePhotoParams
+) {
+    entity = await client.getEntity(entity);
+
+    let photo;
+    if ("photo" in entity) {
+        photo = entity.photo;
+    } else {
+        throw new Error(
+            `Could not get photo from ${entity ? entity.className : undefined}`
+        );
+    }
+    let dcId;
+    let loc;
+    if (
+        photo instanceof Api.UserProfilePhoto ||
+        photo instanceof Api.ChatPhoto
+    ) {
+        dcId = photo.dcId;
+        loc = new Api.InputPeerPhotoFileLocation({
+            peer: utils.getInputPeer(entity),
+            photoId: photo.photoId,
+            big: fileParams.isBig,
+        });
+    } else {
+        return Buffer.alloc(0);
+    }
+    return client.downloadFile(loc, {
+        dcId,
+        fileSize: 2 * 1024 * 1024,
+        workers: 1,
+    });
+}

+ 4 - 4
gramjs/client/telegramBaseClient.ts

@@ -2,7 +2,7 @@ import { version } from "../";
 import { IS_NODE } from "../Helpers";
 import {
     ConnectionTCPFull,
-    ConnectionTCPObfuscated
+    ConnectionTCPObfuscated,
 } from "../network/connection";
 import { Session } from "../sessions";
 import { Logger } from "../extensions";
@@ -89,7 +89,7 @@ export class TelegramBaseClient {
             baseLogger = "gramjs",
             useWSS = typeof window !== "undefined"
                 ? window.location.protocol == "https:"
-                : false
+                : false,
         }: TelegramClientParams
     ) {
         if (!apiId || !apiHash) {
@@ -129,7 +129,7 @@ export class TelegramBaseClient {
             langCode: langCode,
             langPack: "", // this should be left empty.
             systemLangCode: systemLangCode,
-            proxy: undefined // no proxies yet.
+            proxy: undefined, // no proxies yet.
         });
         this._eventBuilders = [];
 
@@ -187,7 +187,7 @@ export class TelegramBaseClient {
                 (promise: any) => {
                     return promise.then((sender: any) => sender.disconnect());
                 }
-            )
+            ),
         ]);
 
         this._eventBuilders = [];