TLSharp: get file

796 Views Asked by At

Have error: LIMIT_INVALID

Memory Exception? Anybody have worked examples?

TLMessageMediaDocument mediaFile = _msg.Media as TLMessageMediaDocument;
TLDocument doc = mediaFile.Document as TLDocument;
client.GetFile(new TLInputDocumentFileLocation() {
   AccessHash = doc.AccessHash,
   Id = doc.Id, Version = doc.Version },
doc.Size).Wait();
1

There are 1 best solutions below

0
On

There are few steps that are must-to-know when downloading files. Telegram documentation says:

1. If precise flag is not specified, then
    The parameter offset must be divisible by 4 KB.
    The parameter limit must be divisible by 4 KB.
    1048576 (1 MB) must be divisible by limit.

2. If precise is specified, then
    The parameter offset must be divisible by 1 KB.
    The parameter limit must be divisible by 1 KB.
    limit must not exceed 1048576 (1 MB).

I have no idea what a precise flag is that's why I used the 1st one (using limits and offsets that are divisible by 4 KB) because 4 is divisible by 1.

Here's the doc link: https://core.telegram.org/api/files

Here the code that I use using C# TLSharp

var mb = 1048576;
var upperLimit = (int)Math.Pow(2, Math.Ceiling(Math.Log(fileInfo.Size, 2))) * 4;
var limit = Math.Min(mb, upperLimit);

var currentOffset = 0;
using (var fs = File.OpenWrite(filename))
{
    while (currentOffset < fileInfo.Size)
    {
        file = m_telegramClient.GetFile(fileLocation, limit, currentOffset).ConfigureAwait(false).GetAwaiter().GetResult();
        fs.Write(file.Bytes, 0, file.Bytes.Length);
        currentOffset += file.Bytes.Length;
    }

    fs.Close();
}