added DownloadFileAsync helpers

This commit is contained in:
Wizou 2021-09-23 13:13:36 +02:00
parent 0b3180917f
commit be4d1aca6b
3 changed files with 54 additions and 10 deletions

View file

@ -902,6 +902,48 @@ namespace WTelegram
return this.Messages_SendMedia(peer, media, text, Helpers.RandomLong(),
reply_to_msg_id: reply_to_msg_id, entities: entities, schedule_date: schedule_date);
}
/// <summary>Download given photo from Telegram into the outputStream</summary>
/// <param name="outputStream">stream to write to. This method does not close/dispose the stream</param>
/// <param name="photoSize">if unspecified, will download the largest version of the photo</param>
public async Task<Storage_FileType> DownloadFileAsync(Photo photo, Stream outputStream, PhotoSizeBase photoSize = null)
{
var fileLocation = photo.ToFileLocation(photoSize ?? photo.LargestPhotoSize);
await MigrateDCAsync(photo.dc_id);
return await DownloadFileAsync(fileLocation, outputStream);
}
/// <summary>Download given photo from Telegram into the outputStream</summary>
/// <param name="outputStream">stream to write to. This method does not close/dispose the stream</param>
/// <param name="thumbSize">if specified, will download the given thumbnail instead of the full document</param>
public async Task<Storage_FileType> DownloadFileAsync(Document document, Stream outputStream, PhotoSizeBase thumbSize = null)
{
var fileLocation = document.ToFileLocation(thumbSize);
await MigrateDCAsync(document.dc_id);
return await DownloadFileAsync(fileLocation, outputStream);
}
/// <summary>Download given file from Telegram into the outputStream</summary>
/// <param name="fileLocation">Telegram file identifier, typically obtained with a .ToFileLocation() call</param>
/// <param name="outputStream">stream to write to. This method does not close/dispose the stream</param>
public async Task<Storage_FileType> DownloadFileAsync(InputFileLocationBase fileLocation, Stream outputStream)
{
const int ChunkSize = 128 * 1024;
int fileSize = 0;
Upload_File fileData;
do
{
var fileBase = await this.Upload_GetFile(fileLocation, fileSize, ChunkSize);
fileData = fileBase as Upload_File;
if (fileData == null)
throw new ApplicationException("Upload_GetFile returned unsupported " + fileBase.GetType().Name);
await outputStream.WriteAsync(fileData.bytes, 0, fileData.bytes.Length);
fileSize += fileData.bytes.Length;
} while (fileData.bytes.Length == ChunkSize);
await MigrateDCAsync(); // migrate back to main DC
return fileData.type;
}
#endregion
/// <summary>Enable the collection of id/access_hash pairs (experimental)</summary>

View file

@ -337,6 +337,7 @@ namespace WTelegram
private void WriteTypeAsEnum(StreamWriter sw, TypeInfo typeInfo)
{
enumTypes.Add(typeInfo.ReturnName);
bool lowercase = typeInfo.ReturnName == "Storage_FileType";
sw.WriteLine($"{tabIndent}public enum {typeInfo.ReturnName} : uint");
sw.WriteLine($"{tabIndent}{{");
string prefix = "";
@ -348,6 +349,7 @@ namespace WTelegram
{
string className = CSharpName(ctor.predicate);
if (!allTypes.Add(className)) continue;
if (lowercase) className = className.ToLowerInvariant();
ctorToTypes.Remove(ctor.ID);
sw.WriteLine($"{tabIndent}\t///<summary>See <a href=\"https://core.telegram.org/constructor/{ctor.predicate}\"/></summary>");
sw.WriteLine($"{tabIndent}\t{className[prefixLen..]} = 0x{ctor.ID:X8},");

View file

@ -413,25 +413,25 @@ namespace TL
public enum Storage_FileType : uint
{
///<summary>See <a href="https://core.telegram.org/constructor/storage.fileUnknown"/></summary>
Unknown = 0xAA963B05,
unknown = 0xAA963B05,
///<summary>See <a href="https://core.telegram.org/constructor/storage.filePartial"/></summary>
Partial = 0x40BC6F52,
partial = 0x40BC6F52,
///<summary>See <a href="https://core.telegram.org/constructor/storage.fileJpeg"/></summary>
Jpeg = 0x007EFE0E,
jpeg = 0x007EFE0E,
///<summary>See <a href="https://core.telegram.org/constructor/storage.fileGif"/></summary>
Gif = 0xCAE1AADF,
gif = 0xCAE1AADF,
///<summary>See <a href="https://core.telegram.org/constructor/storage.filePng"/></summary>
Png = 0x0A4F63C0,
png = 0x0A4F63C0,
///<summary>See <a href="https://core.telegram.org/constructor/storage.filePdf"/></summary>
Pdf = 0xAE1E508D,
pdf = 0xAE1E508D,
///<summary>See <a href="https://core.telegram.org/constructor/storage.fileMp3"/></summary>
Mp3 = 0x528A0677,
mp3 = 0x528A0677,
///<summary>See <a href="https://core.telegram.org/constructor/storage.fileMov"/></summary>
Mov = 0x4B09EBBC,
mov = 0x4B09EBBC,
///<summary>See <a href="https://core.telegram.org/constructor/storage.fileMp4"/></summary>
Mp4 = 0xB3CEA0E4,
mp4 = 0xB3CEA0E4,
///<summary>See <a href="https://core.telegram.org/constructor/storage.fileWebp"/></summary>
Webp = 0x1081464C,
webp = 0x1081464C,
}
///<summary>See <a href="https://core.telegram.org/type/User"/></summary>