TLSharp icon indicating copy to clipboard operation
TLSharp copied to clipboard

Get file and fire error LIMIT_INVALID

Open Mostafasaffari opened this issue 8 years ago • 6 comments

hi guys. I have problem when i get file (mp4) from this code

 var inputPeer = new TLInputPeerChannel()
                    {
                        channel_id = channel.id,
                        access_hash = channel.access_hash.Value
                    };
                    var res = await client.SendRequestAsync<TLChannelMessages>(new TLRequestGetHistory()
                    {
                        peer = inputPeer,
                        add_offset = start,
                        limit = end
                    });
                    var document = res.messages.lists
                                .OfType<TLMessage>()
                                .Where(m => m.media != null)
                                .Select(m => m.media)
                                .OfType<TLMessageMediaDocument>()
                                .Select(md => md.document)
                                .OfType<TLDocument>()
                                .FirstOrDefault();
                    if (document != null)
                    {
                        var resFile = await client.GetFile(
                            new TLInputDocumentFileLocation()
                            {
                                access_hash = document.access_hash,
                                id = document.id,
                                version = document.version
                            }, document.size);
                    }

then rise LIMIT_INVALID

Mostafasaffari avatar Aug 23 '17 13:08 Mostafasaffari

I got same problem downloading a message photo attachment

darkusone avatar Aug 25 '17 06:08 darkusone

Same problem here. Tried everything and every size for limit. It just wont change. Please take a look at it

Mohfath avatar Oct 16 '17 10:10 Mohfath

Simply it seems they missed a part in document (https://core.telegram.org/api/files) about downloading files and limit. the limit should be a number in base 2 larger than document.size here is replacement code for it: (int)Math.Pow(2, Math.Ceiling(Math.Log(document.size, 2)))

well, it should be:

                     var resFile = await client.GetFile(
                            new TLInputDocumentFileLocation()
                            {
                                access_hash = document.access_hash,
                                id = document.id,
                                version = document.version
                            }, (int)Math.Pow(2, Math.Ceiling(Math.Log(document.size, 2))));

hope it helps

Update: 2017-11-14

I found out again that the code throws the same "LIMIT_INVALID" error if limit (or filePartSize) goes above 1MB (for files with size more than 1MB). it's undocumented also! so the solution is to get the files in smaller parts and then merge them. here is code: (I assumed each filePart as 512KB, works for smaller file sizes)

int filePart = 512 * 1024;
int offset = 0;

using (MemoryStream ms = new MemoryStream())
{
	while (offset < castedItem.size)
	{
		resFile = await _telegramClient.GetFile(
			new TLInputDocumentFileLocation()
			{
				access_hash = document.access_hash,
				id = document.id,
				version = document.version
			},
			filePart, offset);
		ms.Write(resFile.bytes, 0, resFile.bytes.Length);
		offset += filePart;
	}
	resFile.bytes = ms.ToArray();
}

mahyarff avatar Nov 07 '17 20:11 mahyarff

edit: Here for more details: https://github.com/sochix/TLSharp/issues/840#issuecomment-470075295

WhiteLionATX avatar Mar 05 '19 11:03 WhiteLionATX

I get the same problem as #851

emreordukaya avatar May 07 '19 10:05 emreordukaya

I found the solution: https://stackoverflow.com/a/63756785/4414743

martikyan avatar Sep 05 '20 17:09 martikyan