Get file and fire error LIMIT_INVALID
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
I got same problem downloading a message photo attachment
Same problem here. Tried everything and every size for limit. It just wont change. Please take a look at it
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();
}
edit: Here for more details: https://github.com/sochix/TLSharp/issues/840#issuecomment-470075295
I get the same problem as #851
I found the solution: https://stackoverflow.com/a/63756785/4414743