From 72b28e97bae90dd32ce88615bc3291582eb95822 Mon Sep 17 00:00:00 2001 From: Wizou Date: Tue, 28 Dec 2021 06:43:33 +0100 Subject: [PATCH] added DownloadProfilePhotoAsync helper --- FAQ.md | 8 ++++---- src/Client.cs | 41 +++++++++++++++++++++++++++++++++++++---- 2 files changed, 41 insertions(+), 8 deletions(-) diff --git a/FAQ.md b/FAQ.md index 9be9018..0e830a8 100644 --- a/FAQ.md +++ b/FAQ.md @@ -69,15 +69,15 @@ You can access these versions for testing in your program by going to our [priva After that, you should be able to see/install the pre-release versions in your Nuget package manager and install them in your application. *(make sure you enable the **pre-release** checkbox)* -#### 6. Telegram can't find any chats and asks me to signup (firstname, lastname) even for an existing account +#### 6. Telegram asks me to signup (firstname, lastname) even for an existing account This happens when you connect to Telegram Test servers instead of Production servers. On these separate test servers, all created accounts and chats are periodically deleted, so you shouldn't use them under normal circumstances. -You can verify this is your case by looking at [WTelegram logs](EXAMPLES.MD#logging) on the line `Connected to (Test) DC x...` +You can verify this is your issue by looking at [WTelegram logs](EXAMPLES.MD#logging) on the line `Connected to (Test) DC x...` This wrong-server problem typically happens when you use WTelegramClient Github source project in your application in DEBUG builds. It is **not recommended** to use WTelegramClient in source code form. -Instead, you should use the Nuget manager to **import the WTelegramClient Nuget package** into your application. +Instead, you should use the Nuget manager to **install package WTelegramClient** into your application. If you use the Github source project in an old .NET Framework 4.x or .NET Core x.x application, you may also experience the following error > System.TypeInitializationException (FileNotFoundException for "System.Text.Json Version=5.0.0.0 ...") @@ -140,7 +140,7 @@ However, note that those Channel-compatible methods will require an `InputChanne That object must be created with both fields `channel_id` and `access_hash` correctly filled. You can read more about this in [FAQ #4](#access-hash). -#### 10. `chats.chats[id]` throws KeyNotFoundException. My chats list is empty or does not contain the chat id. +#### 10. `chats.chats[id]` fails. My chats list is empty or does not contain the chat id. There can be several reasons why `chats.chats[id]` raise an error: - The user account you're currently logged-in as has not joined this particular chat. diff --git a/src/Client.cs b/src/Client.cs index 4c7d296..abb1501 100644 --- a/src/Client.cs +++ b/src/Client.cs @@ -1268,14 +1268,14 @@ namespace WTelegram /// Download a file from Telegram into the outputStream /// Telegram file identifier, typically obtained with a .ToFileLocation() call /// Stream to write file content to. This method does not close/dispose the stream - /// (optional) DC on which the file is stored + /// (optional) DC on which the file is stored /// (optional) Expected file size /// (optional) Callback for tracking the progression of the transfer /// The file type - public async Task DownloadFileAsync(InputFileLocationBase fileLocation, Stream outputStream, int fileDC = 0, int fileSize = 0, ProgressCallback progress = null) + public async Task DownloadFileAsync(InputFileLocationBase fileLocation, Stream outputStream, int dc_id = 0, int fileSize = 0, ProgressCallback progress = null) { Storage_FileType fileType = Storage_FileType.unknown; - var client = fileDC == 0 ? this : await GetClientForDC(fileDC, true); + var client = dc_id == 0 ? this : await GetClientForDC(dc_id, true); using var writeSem = new SemaphoreSlim(1); long streamStartPos = outputStream.Position; int fileOffset = 0, maxOffsetSeen = 0; @@ -1288,7 +1288,7 @@ namespace WTelegram await _parallelTransfers.WaitAsync(); var task = LoadPart(fileOffset); lock (tasks) tasks[fileOffset] = task; - if (fileDC == 0) { await task; fileDC = client._dcSession.DcID; } + if (dc_id == 0) { await task; dc_id = client._dcSession.DcID; } fileOffset += FilePartSize; if (fileSize != 0 && fileOffset >= fileSize) { @@ -1365,6 +1365,39 @@ namespace WTelegram return fileType; } + /// Download the profile photo for a given peer into the outputStream + /// User, Chat or Channel + /// Stream to write the file content to. This method does not close/dispose the stream + /// Whether to download the high-quality version of the picture + /// The file type of the photo, or 0 if no photo available + public async Task DownloadProfilePhotoAsync(IPeerInfo peer, Stream outputStream, bool big = false) + { + int dc_id; + var fileLocation = new InputPeerPhotoFileLocation { peer = peer.ToInputPeer() }; + if (big) fileLocation.flags = InputPeerPhotoFileLocation.Flags.big; + switch (peer) + { + case User user: + if (user.photo == null) return 0; + dc_id = user.photo.dc_id; + fileLocation.photo_id = user.photo.photo_id; + break; + case Chat chat: + if (chat.photo == null) return 0; + dc_id = chat.photo.dc_id; + fileLocation.photo_id = chat.photo.photo_id; + break; + case Channel channel: + if (channel.photo == null) return 0; + dc_id = channel.photo.dc_id; + fileLocation.photo_id = channel.photo.photo_id; + break; + default: + return 0; + } + return await DownloadFileAsync(fileLocation, outputStream, dc_id); + } + /// Helper method that tries to fetch all participants from a Channel (beyond Telegram server-side limitations) /// The channel to query /// Also fetch the kicked/banned members?