Program_DownloadSavedMedia: demo how to abort a transfer

This commit is contained in:
Wizou 2024-11-14 00:49:45 +01:00
parent f1c1d0a6a2
commit 18fc1b32af

View file

@ -1,5 +1,6 @@
using System; using System;
using System.IO; using System.IO;
using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using TL; using TL;
@ -11,10 +12,12 @@ namespace WTelegramClientTest
static async Task Main(string[] _) static async Task Main(string[] _)
{ {
Console.WriteLine("The program will download photos/medias from messages you send/forward to yourself (Saved Messages)"); Console.WriteLine("The program will download photos/medias from messages you send/forward to yourself (Saved Messages)");
var cts = new CancellationTokenSource();
await using var client = new WTelegram.Client(Environment.GetEnvironmentVariable); await using var client = new WTelegram.Client(Environment.GetEnvironmentVariable);
var user = await client.LoginUserIfNeeded(); var user = await client.LoginUserIfNeeded();
client.OnUpdates += Client_OnUpdates; client.OnUpdates += Client_OnUpdates;
Console.ReadKey(); Console.ReadKey();
cts.Cancel();
async Task Client_OnUpdates(UpdatesBase updates) async Task Client_OnUpdates(UpdatesBase updates)
{ {
@ -31,7 +34,7 @@ namespace WTelegramClientTest
filename ??= $"{document.id}.{document.mime_type[(document.mime_type.IndexOf('/') + 1)..]}"; filename ??= $"{document.id}.{document.mime_type[(document.mime_type.IndexOf('/') + 1)..]}";
Console.WriteLine("Downloading " + filename); Console.WriteLine("Downloading " + filename);
using var fileStream = File.Create(filename); using var fileStream = File.Create(filename);
await client.DownloadFileAsync(document, fileStream); await client.DownloadFileAsync(document, fileStream, progress: (p, t) => cts.Token.ThrowIfCancellationRequested());
Console.WriteLine("Download finished"); Console.WriteLine("Download finished");
} }
else if (message.media is MessageMediaPhoto { photo: Photo photo }) else if (message.media is MessageMediaPhoto { photo: Photo photo })